A. Sweet Problem
time limit per test1 second memory limit per test256 megabytes
You have three piles of candies: red, green and blue candies:
- the first pile contains only red candies and there are r candies in it,
- the second pile contains only green candies and there are g candies in it,
- the third pile contains only blue candies and there are b candies in it.
Each day Tanya eats exactly two candies of different colors. She is free to choose the colors of eaten candies: the only restriction that she can't eat two candies of the same color in a day.
Find the maximal number of days Tanya can eat candies? Each day she needs to eat exactly two candies.
Input
The first line contains integer t \((1 \le t \le 1000)\) — the number of test cases in the input. Then t test cases follow.
Each test case is given as a separate line of the input. It contains three integers r, g and b \((1 \le r, g, b \le 10^8)\) — the number of red, green and blue candies, respectively.
Output
Print t integers: the i-th printed integer is the answer on the i-th test case in the input.
Exampleinput
6 1 1 1 1 2 1 4 1 1 7 4 10 8 1 4 8 2 8
output
1 2 2 10 5 9
Note
In the first example, Tanya can eat candies for one day only. She can eat any pair of candies this day because all of them have different colors.
In the second example, Tanya can eat candies for two days. For example, she can eat red and green candies on the first day, and green and blue candies on the second day.
In the third example, Tanya can eat candies for two days. For example, she can eat red and green candies on the first day, and red and blue candies on the second day. Note, that two red candies will remain uneaten.
签到
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define pb push_back
typedef long long ll;
const int maxn=(int)2e5+100;
const int mod=(int)1e9+7;
void solve(){
ll r,g,b;scanf("%lld%lld%lld",&r,&g,&b);
ll x=max({r,g,b});
printf("%lld\n",r+g+b-x>x?(r+g+b)/2:r+g+b-x)
}
int main(){
int T;cin>>T;
while(T--) solve();
}
B. PIN Codes
time limit per test1 second memory limit per test256 megabytes
A PIN code is a string that consists of exactly 4 digits. Examples of possible PIN codes: 7013, 0000 and 0990. Please note that the PIN code can begin with any digit, even with 0.
Polycarp has n \((2 \le n \le 10)\) bank cards, the PIN code of the i-th card is \(p_i\).
Polycarp has recently read a recommendation that it is better to set different PIN codes on different cards. Thus he wants to change the minimal number of digits in the PIN codes of his cards so that all n codes would become different.
Formally, in one step, Polycarp picks i-th card \((1 \le i \le n)\), then in its PIN code \(p_i\) selects one position (from 1 to 4), and changes the digit in this position to any other. He needs to change the minimum number of digits so that all PIN codes become different.
Polycarp quickly solved this problem. Can you solve it?
Input
The first line contains integer t \((1 \le t \le 100)\) — the number of test cases in the input. Then test cases follow.
The first line of each of t test sets contains a single integer n \((2 \le n \le 10) \)— the number of Polycarp's bank cards. The next n lines contain the PIN codes \(p_1, p_2, \dots, p_n\) — one per line. The length of each of them is 4. All PIN codes consist of digits only.
Output
Print the answers to t test sets. The answer to each set should consist of a n + 1 lines
In the first line print k — the least number of changes to make all PIN codes different. In the next n lines output the changed PIN codes in the order corresponding to their appearance in the input. If there are several optimal answers, print any of them.
Exampleinput
3 2 1234 0600 2 1337 1337 4 3139 3139 3139 3139
output
0 1234 0600 1 1337 1237 3 3139 3138 3939 6139
n<10,暴力改最后一位就好了
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define pb push_back
typedef long long ll;
const int maxn=(int)2e5+100;
const int mod=(int)1e9+7;
int n,vis[20];
string s[20];
void solve(){
cin>>n;
rep(i,1,n) cin>>s[i];
memset(vis,0,sizeof(vis));
int ans=0;
rep(i,2,n){
int flag=0;
rep(j,1,n) if(i!=j&&s[i]==s[j]){flag=1;break;}
if(flag){
if(s[i][3]=='9') s[i][3]='0';
else s[i][3]++;
if(!vis[i]) ans++,vis[i]=1;
--i;
}
}
printf("%d\n",ans);
rep(i,1,n) cout<<s[i]<<"\n";
}
int main(){
int T;cin>>T;
while(T--) solve();
}
C. Everyone is a Winner!
time limit per test1 second memory limit per test256 megabytes
On the well-known testing system MathForces, a draw of n rating units is arranged. The rating will be distributed according to the following algorithm: if k participants take part in this event, then the n rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remain — it is not given to any of the participants.
For example, if n = 5 and k = 3, then each participant will recieve an 1 rating unit, and also 2 rating units will remain unused. If n = 5, and k = 6, then none of the participants will increase their rating.
Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.
For example, if n=5, then the answer is equal to the sequence 0, 1, 2, 5. Each of the sequence values (and only them) can be obtained as\( \lfloor n/k \rfloor\) for some positive integer k (where\( \lfloor x \rfloor\) is the value of x rounded down): \(0 = \lfloor 5/7 \rfloor, 1 = \lfloor 5/5 \rfloor, 2 = \lfloor 5/2 \rfloor, 5 = \lfloor 5/1 \rfloor\).
Write a program that, for a given n, finds a sequence of all possible rating increments.
Input
The first line contains integer number t \((1 \le t \le 10)\) — the number of test cases in the input. Then t test cases follow.
Each line contains an integer n \((1 \le n \le 10^9)\) — the total number of the rating units being drawn.
Output
Output the answers for each of t test cases. Each answer should be contained in two lines.
In the first line print a single integer m — the number of different rating increment values that Vasya can get.
In the following line print m integers in ascending order — the values of possible rating increments.
Exampleinput
4 5 11 1 3
output
4 0 1 2 5 6 0 1 2 3 5 11 2 0 1 3 0 1 3
签到题
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define pb push_back
typedef long long ll;
const int maxn=(int)2e5+100;
const int mod=(int)1e9+7;
ll n;
void solve(){
scanf("%lld",&n);
set<int> ans; ans.insert(0);
rep(i,1,sqrt(n)) ans.insert(n/i),ans.insert(i);
printf("%d\n",(int)ans.size());
for(auto u:ans) printf("%d ",u);
puts("");
}
int main(){
int T;cin>>T;
while(T--) solve();
}
D. Secret Passwords
time limit per test1 second memory limit per test256 megabytes
One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of n passwords — strings, consists of small Latin letters.
Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords a and b as follows:
- two passwords a and b are equivalent if there is a letter, that exists in both a and b;
- two passwords a and b are equivalent if there is a password c from the list, which is equivalent to both a and b.
If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.
For example, if the list contain passwords "a", "b", "ab", "d", then passwords "a", "b", "ab" are equivalent to each other, but the password "d" is not equivalent to any other password from list. In other words, if:
- admin's password is "b", then you can access to system by using any of this passwords: "a", "b", "ab";
- admin's password is "d", then you can access to system by using only "d".
Only one password from the list is the admin's password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.
Input
The first line contain integer n \((1 \le n \le 2 \cdot 10^5)\) — number of passwords in the list. Next n lines contains passwords from the list – non-empty strings \(s_i\), with length at most 50 letters. Some of the passwords may be equal.
It is guaranteed that the total length of all passwords does not exceed \(10^6\) letters. All of them consist only of lowercase Latin letters.
Output
In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.
Examplesinput
4 a b ab d
output
2
input
3 ab bc abc
output
1
input
1 codeforces
output
1
Note
In the second example hacker need to use any of the passwords to access the system.
并查集裸题
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define pb push_back
typedef long long ll;
const int maxn=(int)2e5+100;
const int mod=(int)1e9+7;
int n,pre[maxn],a[maxn][30];
char s[maxn][55];
int find(int x){return x==pre[x]?x:pre[x]=find(pre[x]);}
void join(int x,int y){
x=find(x);y=find(y);
if(x!=y) pre[x]=y;
}
int main(){
scanf("%d",&n);
rep(i,1,n) pre[i]=i;
rep(i,1,n) scanf("%s",s[i]+1);
rep(i,1,n){
int len=strlen(s[i]+1);
rep(j,1,len) a[i][s[i][j]-'a'+1]=1;
}
rep(i,1,26){
int flag=0;
rep(j,1,n) if(a[j][i]){flag=j;break;}
if(flag) rep(j,flag+1,n) if(a[j][i]) join(flag,j);
}
int ans=0;
rep(i,1,n) if(pre[i]==i) ans++;
printf("%d\n",ans);
}
E. Editor
time limit per test1 second memory limit per test256 megabytes
The development of a text editor is a hard problem. You need to implement an extra module for brackets coloring in text.
Your editor consists of a line with infinite length and cursor, which points to the current character. Please note that it points to only one of the characters (and not between a pair of characters). Thus, it points to an index character. The user can move the cursor left or right one position. If the cursor is already at the first (leftmost) position, then it does not move left.
Initially, the cursor is in the first (leftmost) character.
Also, the user can write a letter or brackets (either (, or )) to the position that the cursor is currently pointing at. A new character always overwrites the old value at that position.
Your editor must check, whether the current line is the correct text. Text is correct if the brackets in them form the correct bracket sequence.
Formally, correct text (CT) must satisfy the following rules:
- any line without brackets is CT (the line can contain whitespaces);
- If the first character of the string — is (, the last — is ), and all the rest form a CT, then the whole line is a CT;
- two consecutively written CT is also CT.
Examples of correct texts: hello(codeforces), round, ((i)(write))edi(tor)s, ( me). Examples of incorrect texts: hello)oops(, round), ((me).
The user uses special commands to work with your editor. Each command has its symbol, which must be written to execute this command.
The correspondence of commands and characters is as follows:
- L — move the cursor one character to the left (remains in place if it already points to the first character);
- R — move the cursor one character to the right;
- any lowercase Latin letter or bracket (( or )) — write the entered character to the position where the cursor is now.
For a complete understanding, take a look at the first example and its illustrations in the note below.
You are given a string containing the characters that the user entered. For the brackets coloring module's work, after each command you need to:
- check if the current text in the editor is a correct text;
- if it is, print the least number of colors that required, to color all brackets.
If two pairs of brackets are nested (the first in the second or vice versa), then these pairs of brackets should be painted in different colors. If two pairs of brackets are not nested, then they can be painted in different or the same colors. For example, for the bracket sequence ()(())()() the least number of colors is 2, and for the bracket sequence (()(()())())(()) — is 3.
Write a program that prints the minimal number of colors after processing each command.Input
The first line contains an integer n \((1 \le n \le 10^6)\) — the number of commands.
The second line contains s — a sequence of commands. The string s consists of n characters. It is guaranteed that all characters in a string are valid commands.
Output
In a single line print n integers, where the i-th number is:
- -1 if the line received after processing the first i commands is not valid text,
- the minimal number of colors in the case of the correct text.
Examplesinput
11 (RaRbR)L)L(
output
-1 -1 -1 -1 -1 -1 1 1 -1 -1 2
input
11 (R)R(R)Ra)c
output
-1 -1 1 1 -1 -1 1 1 1 -1 1
Note
In the first example, the text in the editor will take the following form:
- (
^ - (
^ - (a
^ - (a
^ - (ab
^ - (ab
^ - (ab)
^ - (ab)
^ - (a))
^ - (a))
^ - (())
^
线段树裸题,看懂题目之后就很好做了,线段树维护的是括号数量的后缀和的最大值和最小值,最大值就是答案(很显然),最小值判断是否合法(最小值不能小于0)
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define pb push_back
#define lson rt<<1
#define rson rt<<1|1
typedef long long ll;
const int maxn=(int)1e6+100;
const int mod=(int)1e9+7;
char s[maxn];
int b[maxn],maxx[maxn<<2],minx[maxn<<2],lazy[maxn<<2],n;
void Push_down(int rt){
if(lazy[rt]==0) return;
maxx[lson]+=lazy[rt];maxx[rson]+=lazy[rt];
minx[lson]+=lazy[rt];minx[rson]+=lazy[rt];
lazy[lson]+=lazy[rt];lazy[rson]+=lazy[rt];
lazy[rt]=0;
}
void pushup(int rt){
maxx[rt]=max(maxx[lson],maxx[rson]);
minx[rt]=min(minx[lson],minx[rson]);
}
void update(int l,int r,int rt,int L,int R,int val){
if(L<=l&&R>=r){
maxx[rt]+=val;minx[rt]+=val;lazy[rt]+=val;
return;
}
Push_down(rt);
int mid=(l+r)>>1;
if(L<=mid) update(l,mid,lson,L,R,val);
if(R>mid) update(mid+1,r,rson,L,R,val);
pushup(rt);
}
int main(){
scanf("%d%s",&n,s+1);
int pos=1,cnt=0;
rep(i,1,n){
if(s[i]=='R') ++pos;
else if(s[i]=='L'&&pos>1) --pos;
else if(s[i]==')'){
update(1,n,1,pos,n,-1-b[pos]);
if(b[pos]==0) cnt--;
else if(b[pos]==1) cnt-=2;
b[pos]=-1;
}
else if(s[i]=='('){
update(1,n,1,pos,n,1-b[pos]);
if(b[pos]==0) cnt++;
else if(b[pos]==-1) cnt+=2;
b[pos]=1;
}
else{
update(1,n,1,pos,n,-b[pos]);
if(b[pos]==1) cnt--;
if(b[pos]==-1) cnt++;
b[pos]=0;
}
if(minx[1]<0||cnt!=0) printf("-1 ");
else printf("%d ",maxx[1]);
}
}
哇大佬好强啊