Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn‘t exceed x in the absolute value.
Natasha doesn‘t like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?
You can assume that initially Vanya had infinitely many cards with each integer number from ?-?x to x.
The first line contains two integers: n (1?≤?n?≤?1000) — the number of found cards and x (1?≤?x?≤?1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
Print a single number — the answer to the problem.
3 2 -1 1 2
1
2 3 -2 -2
2
In the first sample, Vanya needs to find a single card with number -2.
In the second sample, Vanya needs to find two cards with number 2. He can‘t find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
//15 ms 0 KB #include<stdio.h> int main() { int n,x; while(scanf("%d%d",&n,&x)!=EOF) { int sum=0,a; for(int i=1;i<=n;i++) { scanf("%d",&a); sum+=a; } if(sum<0)sum=-sum; int ans=sum/x; if(sum%x)ans++; printf("%d\n",ans); } return 0; }
Sereja is a coder and he likes to take part in Codesorfes rounds. However, Uzhland doesn‘t have good internet connection, so Sereja sometimes skips rounds.
Codesorfes has rounds of two types: Div1 (for advanced coders) and Div2 (for beginner coders). Two rounds, Div1 and Div2, can go simultaneously, (Div1 round cannot be held without Div2) in all other cases the rounds don‘t overlap in time. Each round has a unique identifier — a positive integer. The rounds are sequentially (without gaps) numbered with identifiers by the starting time of the round. The identifiers of rounds that are run simultaneously are different by one, also the identifier of the Div1 round is always greater.
Sereja is a beginner coder, so he can take part only in rounds of Div2 type. At the moment he is taking part in a Div2 round, its identifier equals to x. Sereja remembers very well that he has taken part in exactly k rounds before this round. Also, he remembers all identifiers of the rounds he has taken part in and all identifiers of the rounds that went simultaneously with them. Sereja doesn‘t remember anything about the rounds he missed.
Sereja is wondering: what minimum and what maximum number of Div2 rounds could he have missed? Help him find these two numbers.
The first line contains two integers: x (1?≤?x?≤?4000) — the round Sereja is taking part in today, and k (0?≤?k?<?4000) — the number of rounds he took part in.
Next k lines contain the descriptions of the rounds that Sereja took part in before. If Sereja took part in one of two simultaneous rounds, the corresponding line looks like: "1 num2 num1" (where num2 is the identifier of this Div2 round, num1 is the identifier of the Div1round). It is guaranteed that num1?-?num2?=?1. If Sereja took part in a usual Div2 round, then the corresponding line looks like: "2num" (where num is the identifier of this Div2 round). It is guaranteed that the identifiers of all given rounds are less than x.
Print in a single line two integers — the minimum and the maximum number of rounds that Sereja could have missed.
3 2 2 1 2 2
0 0
9 3 1 2 3 2 8 1 4 5
2 3
10 0
5 9
In the second sample we have unused identifiers of rounds 1, 6, 7. The minimum number of rounds Sereja could have missed equals to 2. In this case, the round with the identifier 1 will be a usual Div2 round and the round with identifier 6 will be synchronous with the Div1round.
The maximum number of rounds equals 3. In this case all unused identifiers belong to usual Div2 rounds.
//15 ms 0 KB #include<stdio.h> #include<string.h> int vis[4007]; int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF) { memset(vis,0,sizeof(vis)); for(int i=1;i<=m;i++) { int num,a,b; scanf("%d",&num); if(num==1) { scanf("%d%d",&a,&b); vis[a]=1; vis[b]=1; } else if(num==2) { scanf("%d",&a); vis[a]=1; } } int count1=0,count2=0,flag=0; for(int i=1;i<n;i++) if(!vis[i])count2++; for(int i=1;i<n;i++) { if(!vis[i]&&vis[i+1]){count1++;} else if(!vis[i]&&!vis[i+1]){count1++;i++;} } printf("%d %d\n",count1,count2); } return 0; }
Now it‘s time of Olympiads. Vanya and Egor decided to make his own team to take part in a programming Olympiad. They‘ve been best friends ever since primary school and hopefully, that can somehow help them in teamwork.
For each team Olympiad, Vanya takes his play cards with numbers. He takes only the cards containing numbers 1 and 0. The boys are very superstitious. They think that they can do well at the Olympiad if they begin with laying all the cards in a row so that:
Today Vanya brought n cards with zeroes and m cards with numbers one. The number of cards was so much that the friends do not know how to put all those cards in the described way. Help them find the required arrangement of the cards or else tell the guys that it is impossible to arrange cards in such a way.
The first line contains two integers: n (1?≤?n?≤?106) — the number of cards containing number 0; m (1?≤?m?≤?106) — the number of cards containing number 1.
In a single line print the required sequence of zeroes and ones without any spaces. If such sequence is impossible to obtain, print -1.
1 2
101
4 8
110110110101
4 10
11011011011011
1 5
-1
//109 ms 0 KB #include<stdio.h> using namespace std; int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF) { if(n>m+1||(n+1)*2<m){printf("-1\n");continue;}//不符合条件 int s=0,t=0,x=0,y=0;//s代表前导0的个数,x代表110的个数,y代表10的个数,t代表后导1的个数 for(s=0;s<=1;s++)//前导0只能有0个或者1个 for(t=0;t<=2;t++)//后导1只能有0个或者1个或者2个 { x=m+s-n-t;//110的个数 y=n-x-s;//10的个数 if(x>=0&&y>=0)goto there;//如果满足条件直接跳出 } there: for(int i=0;i<s;i++)printf("0"); for(int i=0;i<x;i++)printf("110"); for(int i=0;i<y;i++)printf("10"); for(int i=0;i<t;i++)printf("1"); printf("\n"); } return 0; }
Codeforces Round #235 (Div. 2),布布扣,bubuko.com
Codeforces Round #235 (Div. 2)
原文:http://blog.csdn.net/crescent__moon/article/details/21033509