Codeforces #205前三题
来源:互联网 发布:我的世界手机版女仆js 编辑:程序博客网 时间:2024/04/30 10:37
Valera has got n domino pieces in a row. Each piece consists of two halves — the upper one and the lower one. Each of the halves contains a number from 1 to 6. Valera loves even integers very much, so he wants the sum of the numbers on the upper halves and the sum of the numbers on the lower halves to be even.
To do that, Valera can rotate the dominoes by 180 degrees. After the rotation the upper and the lower halves swap places. This action takes one second. Help Valera find out the minimum time he must spend rotating dominoes to make his wish come true.
The first line contains integer n (1 ≤ n ≤ 100), denoting the number of dominoes Valera has. Next n lines contain two space-separated integers xi, yi (1 ≤ xi, yi ≤ 6). Number xi is initially written on the upper half of the i-th domino, yi is initially written on the lower half.
Print a single number — the minimum required number of seconds. If Valera can't do the task in any time, print - 1.
24 26 4
0
12 3
-1
31 42 34 4
1
In the first test case the sum of the numbers on the upper halves equals 10 and the sum of the numbers on the lower halves equals 6. Both numbers are even, so Valera doesn't required to do anything.
In the second sample Valera has only one piece of domino. It is written 3 on the one of its halves, therefore one of the sums will always be odd.
In the third case Valera can rotate the first piece, and after that the sum on the upper halves will be equal to 10, and the sum on the lower halves will be equal to 8.
#include<iostream>#include<cstring>#include<string>#include<cmath>#include<cstdio>#include<algorithm>using namespace std;int main(){ int n,i; int cnt1,cnt2,cnt3,cnt4; int a,b; while(~scanf("%d",&n)) { cnt1=cnt2=cnt3=cnt4=0; for(i=0;i<n;i++) { scanf("%d%d",&a,&b); if(a%2==0&&b%2==0) cnt1++; if(a%2==1&&b%2==0) cnt2++; if(a%2==0&&b%2==1) cnt3++; if(a%2==1&&b%2==1) cnt4++; } if(cnt2+cnt3==0&&cnt4%2==1) puts("-1"); else if(cnt4%2==0) { if((cnt2+cnt3)%2==1) puts("-1"); else if(cnt2%2==0) puts("0"); else puts("1"); } else { if((cnt2+cnt3)%2==1) puts("-1"); else if(cnt2%2==0) puts("1"); else puts("0"); } } return 0;}
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
110 99
12 1
213 24 13 45
41 2 2 1
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
#include<iostream>#include<cstring>#include<string>#include<cmath>#include<cstdio>#include<algorithm>using namespace std;int a[205];int p[205];int ans[205];int main(){ int n,i,j,res,sum; while(~scanf("%d",&n)) { sum=0; memset(p,0,sizeof(p)); //把个数都归为0 memset(ans,0,sizeof(ans)); for(i=0;i<2*n;i++) { scanf("%d",&a[i]); p[a[i]]++; if(p[a[i]]<=2) sum++; //上面下面各分配一个为2,否则为1 } if(sum%2) res=(sum/2)*(sum/2+1); else res=(sum/2)*(sum/2); int k=1; for(i=10;i<100;i++) { if(p[i]) { for(j=0;j<2*n;j++) { if(a[j]==i) { ans[j]=k; //第一次为1,则下一次为2 k=3-k; } } } } cout<<res<<endl; cout<<ans[0]; for(i=1;i<2*n;i++) cout<<" "<<ans[i]; cout<<endl; } return 0;}
在群里面问了一下@Joy,他说可能是因为统计个数为1的数目的时候使得个数为>=2的数目不能够平分。自己琢磨了一下。自己sum/2*sum/2或者sum/2*(sum/2+1)是建立在个数平分的基础上,比如1 1 1 2 3 3 3 4这样的数据,把个数大于等于2的先统计,这样结果是9,但是按照自己的写法却是8. 感谢cxlove的数据!!自己那样并没有达到真正所谓的平分。
#include<iostream>#include<cstring>#include<string>#include<cmath>#include<cstdio>#include<algorithm>using namespace std;int a[205];int p[205];int ans[205];int main(){ int n,i,j,res,sum; while(~scanf("%d",&n)) { sum=0; memset(p,0,sizeof(p)); //把个数都归为0 memset(ans,0,sizeof(ans)); for(i=0;i<2*n;i++) { scanf("%d",&a[i]); p[a[i]]++; if(p[a[i]]<=2) sum++; //上面下面各分配一个为2,否则为1 } if(sum%2) res=(sum/2)*(sum/2+1); else res=(sum/2)*(sum/2); int k=1; for(i=10;i<100;i++) { if(p[i]>=2) //先统计个数大于1的 { for(j=0;j<2*n;j++) { if(a[j]==i) { ans[j]=k; //第一次为1,则下一次为2 k=3-k; } } } } for(i=0;i<2*n;i++) { if(!ans[i]) //个数为1的 { ans[i]=k; k=3-k; } } cout<<res<<endl; cout<<ans[0]; for(i=1;i<2*n;i++) cout<<" "<<ans[i]; cout<<endl; } return 0;}/*213 24 13 45412 12 12 34 34 45 56 67612 34 34 34 34 45 45 45 67 67 67 67*/
Valera has array a, consisting of n integers a0, a1, ..., an - 1, and function f(x), taking an integer from 0 to 2n - 1 as its single argument. Value f(x) is calculated by formula , where value bit(i) equals one if the binary representation of number xcontains a 1 on the i-th position, and zero otherwise.
For example, if n = 4 and x = 11 (11 = 20 + 21 + 23), then f(x) = a0 + a1 + a3.
Help Valera find the maximum of function f(x) among all x, for which an inequality holds: 0 ≤ x ≤ m.
The first line contains integer n (1 ≤ n ≤ 105) — the number of array elements. The next line contains n space-separated integersa0, a1, ..., an - 1 (0 ≤ ai ≤ 104) — elements of array a.
The third line contains a sequence of digits zero and one without spaces s0s1... sn - 1 — the binary representation of number m. Numberm equals .
Print a single integer — the maximum value of function f(x) for all .
23 810
3
517 0 10 2 111010
27
In the first test case m = 20 = 1, f(0) = 0, f(1) = a0 = 3.
In the second sample m = 20 + 21 + 23 = 11, the maximum value of function equals f(5) = a0 + a2 = 17 + 10 = 27.
#include<iostream>#include<cstring>#include<string>#include<cmath>#include<cstdio>#include<algorithm>using namespace std;int a[100005];char b[100005];int p[100005];int dp[100005]; //dp[i]记录的是a[i]之前的所有和int dp2[100005]; //dp2[i]记录的是a[i]*(b[i]=='1')之前的所有和int main(){ int n,i; int sum; while(~scanf("%d",&n)) { sum=0; for(i=0;i<n;i++) scanf("%d",&a[i]); dp[0]=a[0]; for(i=1;i<n;i++) dp[i]=dp[i-1]+a[i]; scanf("%s",b); if(b[0]=='1') dp2[0]=a[0]; else dp2[0]=0; for(i=1;i<n;i++) { if(b[i]=='1') dp2[i]=dp2[i-1]+a[i]; else dp2[i]=dp2[i-1]; } int t=0; for(i=n-1;i>=1;i--) if(b[i]=='1') { p[t++]=i; } for(i=0;i<n;i++) if(b[i]=='1') sum+=a[i]; //cout<<sum<<endl; for(i=0;i<t;i++) //每次碰到1把当前改为0,前面全为1,后面不变 { //cout<<p[i]<<endl; int tmp=0; int x=p[i]; tmp=dp[x-1]; tmp=tmp-dp2[x]+dp2[n-1]; if(tmp>sum) sum=tmp; } printf("%d\n",sum); } return 0;}/*23 810517 0 10 2 1110101217 0 10 2 1 17 0 10 2 1 4 5101000111101*/
- Codeforces #205前三题
- Codeforces Round#196 前三题
- Codeforces Round#197 前三题
- Codeforces #199前三题
- Codeforces #213 div2前三题
- codeforces #216前三题
- Codeforces Round #217前三题
- codeforces #220 div2前三题
- codeforces#357 前三题题解
- codeforces #359 前三题题解
- Codeforces Round #205
- CodeForces 205B
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- 磁盘分区助手操作失败引起的D盘文件损坏错误
- do { }while(0)的妙用
- linux系统下,使用dd命令制作U盘安装系统教程
- GDB 多线程调试
- YUV主要采样格式理解
- Codeforces #205前三题
- Error[Pe147]: declaration is incompatible with "__nounwind __interwork __softfp unsigned long __get_
- hive学习笔记
- MPEG系列与H.26x系列标准
- (C语言)队列的链式实现(数据结构十一)
- MyEclipse不编译问题的解决
- 排序算法
- Hive集成Mysql作为元数据
- Android系统Recovery工作原理之使用update.zip升级过程分析