http://codeforces.com/contest/228/problem/A
Valera the Horse is going to the party with friends. He has been following the fashion trends for a while, and he knows that it is very popular to wear all horseshoes of different color. Valera has got four horseshoes left from the last year, but maybe some of them have the same color. In this case he needs to go to the store and buy some few more horseshoes, not to lose face in front of his stylish comrades.
Fortunately, the store sells horseshoes of all colors under the sun and Valera has enough money to buy any four of them. However, in order to save the money, he would like to spend as little money as possible, so you need to help Valera and determine what is the minimum number of horseshoes he needs to buy to wear four horseshoes of different colors to a party.
The first line contains four space-separated integers s1, s2, s3, s4 (1 ≤ s1, s2, s3, s4 ≤ 109) — the colors of horseshoes Valera has.
Consider all possible colors indexed with integers.
Print a single integer — the minimum number of horseshoes Valera needs to buy.
1 7 3 3
1
7 7 7 7
3
第一题是个大水题,当时没想到用unique()来做直接暴力做的。。哦忘了就4组数据,怎么做都行,如果数据量大的话用unique()会好很多。。。
#include #include #include #include int a[10]; using namespace std; int main() { int b,c,d; while(scanf("%d",&a[0])!=EOF) { scanf("%d%d%d",&a[1],&a[2],&a[3]); sort(a,a+4); int i; int count=0; for(i=0;i<</SPAN>4;i++) { if(a[i]==a[i+1]) { count++; } } printf("%d\n",count); } return 0; }
解法2:
#include
#include
#include
using namespace std;
int main()
{
int a[10];
//int i;
while(scanf("%d%d%d%d",&a[0],&a[1],&a[2],&a[3])!=EOF)
{
sort(a,a+4);
printf("%d\n",4-(unique(a,a+4)-a));
}
return 0;
}
http://codeforces.com/contest/228/problem/B
You've got two rectangular tables with sizes na × ma and nb × mb cells. The tables consist of zeroes and ones. We will consider the rows and columns of both tables indexed starting from 1. Then we will define the element of the first table, located at the intersection of the i-th row and the j-th column, as ai, j; we will define the element of the second table, located at the intersection of the i-th row and the j-th column, as bi, j.
We will call the pair of integers (x, y) a shift of the second table relative to the first one. We'll call the overlap factor of the shift (x, y) value:
where the variables i, j take only such values, in which the expression ai, j·bi + x, j + y makes sense. More formally, inequalities 1 ≤ i ≤ na, 1 ≤ j ≤ ma, 1 ≤ i + x ≤ nb, 1 ≤ j + y ≤ mb must hold. If there are no values of variables i, j, that satisfy the given inequalities, the value of the sum is considered equal to 0.
Your task is to find the shift with the maximum overlap factor among all possible shifts.
The first line contains two space-separated integers na, ma (1 ≤ na, ma ≤ 50) — the number of rows and columns in the first table. Then na lines contain ma characters each — the elements of the first table. Each character is either a "0", or a "1".
The next line contains two space-separated integers nb, mb (1 ≤ nb, mb ≤ 50) — the number of rows and columns in the second table. Then follow the elements of the second table in the format, similar to the first table.
It is guaranteed that the first table has at least one number "1". It is guaranteed that the second table has at least one number "1".
Print two space-separated integers x, y (|x|, |y| ≤ 109) — a shift with maximum overlap factor. If there are multiple solutions, print any of them.
3 2
01
10
00
2 3
001
111
0 1
3 3
000
010
000
1 1
1
-1 -1
表示题目没读懂。。下次题意没读懂的情况下可以直接按照题目要求直接做。。。
#include
#include
#include
using namespace std;
char a[100][100],b[100][100];
int main()
{
int na,nb,ma,mb;
scanf("%d%d",&na,&ma);
int i,j;
for(i=1;i<=na;i++)
{
getchar();
for(j=1;j<=ma;j++)
{
scanf("%c",&a[i][j]);
}
}
scanf("%d%d",&nb,&mb);
for(i=1;i<=nb;i++)
{
getchar();
for(j=1;j<=mb;j++)
{
scanf("%c",&b[i][j]);
}
}
int x,y,tx,ty;
int sum=0;
int max=-1000;
for(x=-50;x<=50;x++)
{
for(y=-50;y<=50;y++)
{
sum=0;
for(i=1;i<=na;i++)
{
for(j=1;j<=ma;j++)
{
if(i+x<=nb&&i+x>=1&&j+y<=mb&&j+y>=1)
{
// if(i+x<=nb&&i+x>0&&j+y>0&&j+y<=mb)
sum+=(a[i][j]-48)*(b[i+x][j+y]-48);
}
}
}
if(sum>max)
{
max=sum;
tx=x;
ty=y;
}
}
}
printf("%d %d\n",tx,ty);
return 0;
}