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.
2 4 2 6 4
0
1 2 3
-1
3 1 4 2 3 4 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;
}
#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;
}
/*
2
13 24 13 45
4
12 12 12 34 34 45 56 67
6
12 34 34 34 34 45 45 45 67 67 67 67
*/
#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;
}
/*
2
3 8
10
5
17 0 10 2 1
11010
12
17 0 10 2 1 17 0 10 2 1 4 5
101000111101
*/