这两天不知道怎么了啊....CF一直跌啊.....老错在一些小的问题上啊.....写一下算是一个教训....
#217 A
这道题目之前在CF上见到过类似的了,一开始思路是对的,但是无奈,考虑的太少了啊,把象的走法弄少了啊,结果就悲剧的锁上了代码,还改不了了啊....
通过坐标进行判断就行了啊,尽然有同学谢了搜索,真是膜拜啊.....
Little Petya is learning to play chess. He has already learned how to move a king, a rook and a bishop. Let us remind you the rules of moving chess pieces. A chessboard is 64 square fields organized into an 8 × 8 table. A field is represented by a pair of integers (r, c) — the number of the row and the number of the column (in a classical game the columns are traditionally indexed by letters). Each chess piece takes up exactly one field. To make a move is to move a chess piece, the pieces move by the following rules:
- A rook moves any number of fields horizontally or vertically.
- A bishop moves any number of fields diagonally.
- A king moves one field in any direction — horizontally, vertically or diagonally.
Petya is thinking about the following problem: what minimum number of moves is needed for each of these pieces to move from field(r1, c1) to field (r2, c2)? At that, we assume that there are no more pieces besides this one on the board. Help him solve this problem.
The input contains four integers r1, c1, r2, c2 (1 ≤ r1, c1, r2, c2 ≤ 8) — the coordinates of the starting and the final field. The starting field doesn't coincide with the final one.
You can assume that the chessboard rows are numbered from top to bottom 1 through 8, and the columns are numbered from left to right 1 through 8.
Print three space-separated integers: the minimum number of moves the rook, the bishop and the king (in this order) is needed to move from field (r1, c1) to field (r2, c2). If a piece cannot make such a move, print a 0 instead of the corresponding number.
4 3 1 6
2 1 3
5 5 5 6
1 0 1
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#define M 10010
#define INF 1 << 30;
using namespace std;
int main()
{
int x1, y1, x2, y2;
int x, y;
int s1, s2, s3;
cin >>x1>>y1>>x2>>y2;
x = abs(x1-x2);
y = abs(y1-y2);
if(x1 == x2 || y1 == y2)
s1 = 1;
else
s1 = 2;
s3 = max(x, y);
if((x1+y1)%2 != (x2+y2)%2)
s2 = 0;
else if(x == y)
s2 = 1;
else
s2 = 2;
cout <<s1<<' '<<s2<<' '<<s3<<endl;
return 0;
}
B题,读懂题意就很简单了啊,就是暴力求解就行了啊.....
但是老手残啊....
Lately, a national version of a bingo game has become very popular in Berland. There are n players playing the game, each player has a card with numbers. The numbers on each card are distinct, but distinct cards can have equal numbers. The card of the i-th player contains mi numbers.
During the game the host takes numbered balls one by one from a bag. He reads the number aloud in a high and clear voice and then puts the ball away. All participants cross out the number if it occurs on their cards. The person who crosses out all numbers from his card first, wins. If multiple people cross out all numbers from their cards at the same time, there are no winners in the game. At the beginning of the game the bag contains 100 balls numbered 1 through 100, the numbers of all balls are distinct.
You are given the cards for each player. Write a program that determines whether a player can win the game at the most favorable for him scenario or not.
The first line of the input contains integer n (1 ≤ n ≤ 100) — the number of the players. Then follow n lines, each line describes a player's card. The line that describes a card starts from integer mi (1 ≤ mi ≤ 100) that shows how many numbers the i-th player's card has. Then follows a sequence of integers ai, 1, ai, 2, ..., ai, mi (1 ≤ ai, k ≤ 100) — the numbers on the i-th player's card. The numbers in the lines are separated by single spaces.
It is guaranteed that all the numbers on each card are distinct.
Print n lines, the i-th line must contain word "YES" (without the quotes), if the i-th player can win, and "NO" (without the quotes) otherwise.
3 1 1 3 2 4 1 2 10 11
YES NO YES
2 1 1 1 1
NO NO
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#define M 10010
#define INF 1 << 30;
using namespace std;
int f[1010][1010];
int main()
{
int n, i, j, m[M], k, t;
int dp[1010];
memset(dp , 0 , sizeof(dp));
cin >>n;
for(i = 0; i < n; i++)
{
cin >>m[i];
for(j = 0; j < m[i]; j++)
cin >>f[i][j];
sort(f[i], f[i]+m[i]);
}
for(i = 0; i < n-1; i++)
{
for(j = i+1; j < n; j++)
{
if(m[i] >= m[j])
{
t = 0;
for(k = 0; k < m[i]; k++)
{
if(f[i][k] == f[j][t])
t++;
}
if(t >= m[j])
dp[i] = 1;
if(m[j] == m[i] && t >= m[j])
dp[j] = 1;
}
else
{
t = 0;
for(k = 0; k < m[j]; k++)
if(f[i][t] == f[j][k])
t++;
if(t >= m[i])
dp[j] = 1;
}
}
}
for(i = 0; i < n; i++)
{
if(!dp[i])
cout <<"YES"<<endl;
else
cout <<"NO"<<endl;
}
return 0;
}
#218 A
依旧是水题:
找到每一数列最多有多少个一样的,把不一样的改一下就行了啊......
This task will exclusively concentrate only on the arrays where all elements equal 1 and/or 2.
Array a is k-period if its length is divisible by k and there is such array b of length k, that a is represented by array b written exactly times consecutively. In other words, array a is k-periodic, if it has period of length k.
For example, any array is n-periodic, where n is the array length. Array [2, 1, 2, 1, 2, 1] is at the same time 2-periodic and 6-periodic and array [1, 2, 1, 1, 2, 1, 1, 2, 1] is at the same time 3-periodic and 9-periodic.
For the given array a, consisting only of numbers one and two, find the minimum number of elements to change to make the array k-periodic. If the array already is k-periodic, then the required value equals 0.
The first line of the input contains a pair of integers n, k (1 ≤ k ≤ n ≤ 100), where n is the length of the array and the value n is divisible by k. The second line contains the sequence of elements of the given array a1, a2, ..., an (1 ≤ ai ≤ 2), ai is the i-th element of the array.
Print the minimum number of array elements we need to change to make the array k-periodic. If the array already is k-periodic, then print0.
6 2 2 1 2 2 2 1
1
8 4 1 1 2 1 1 1 2 1
0
9 3 2 1 1 1 2 1 1 1 2
3
In the first sample it is enough to change the fourth element from 2 to 1, then the array changes to [2, 1, 2, 1, 2, 1].
In the second sample, the given array already is 4-periodic.
In the third sample it is enough to replace each occurrence of number two by number one. In this case the array will look as [1, 1, 1, 1, 1, 1, 1, 1, 1] — this array is simultaneously 1-, 3- and 9-periodic.
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#define M 10010
#define INF 1 << 30;
using namespace std;
int f[1010][1010];
int main()
{
int n, k;
int i, j;
int sum = 0;
cin >>n>>k;
for(i = 1; i <= n/k; i++)
for(j = 1; j <= k; j++)
cin >>f[i][j];
int sum1, sum2;
for(j = 1; j <= k; j++)
{
sum1 = sum2 = 0;
for(i = 1; i <= n/k; i++)
{
if(f[i][j] == 1)
sum1 ++;
else
sum2 ++;
}
int kk = max(sum1, sum2);
sum += n/k - kk;
}
cout <<sum<<endl;
return 0;
}
B题,也很水啊,求出他们之间的最大公约数,然后看看a,b除以gcd的数,是不是2,3,5的倍数,是的话就算出来次数,不是输出-1。
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#define M 10010
#define LL __int64
#define INF 1 << 30;
using namespace std;
int gcd(int n,int m)
{
if(n>m)swap(n,m);
return m%n==0?n:gcd(m%n,n);
}
int main()
{
LL a, b;
cin >>a>>b;
if(a > b)
swap(a,b);
if(a == b)
{
cout <<'0'<<endl;
return 0;
}
LL t = gcd(a,b);
LL aa = a/t;
LL bb = b/t;
if(aa==1)aa=aa;
else if(((aa%2!=0 && aa%3!=0 && aa%5!= 0) || (bb%2!= 0 && bb%3!= 0 && bb%5!=0)))
{
cout<<"-1"<<endl;
return 0;
}
LL cnt = 0;
while(aa != 1)
{
if(aa%5 == 0)
{
aa /= 5;
cnt ++;
}
else if(aa%3 == 0)
{
aa /= 3;
cnt ++;
}
else if(aa%2 == 0)
{
aa/=2;
cnt ++;
}
else break;
}
while(bb != 1)
{
if(bb%5 == 0)
{
bb /= 5;
cnt ++;
}
else if(bb%3 == 0)
{
bb /= 3;
cnt ++;
}
else if(bb%2 == 0)
{
bb/=2;
cnt ++;
}
else break;
}
if(aa!=1||bb!=1)
{
cout<<-1<<endl;
return 0;
}
cout <<cnt<<endl;
return 0;
}
C题,其实也挺简单的就是得想明白过程,注意要充分用到所有给的材料,然后贪心就可以了啊。。。。
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#define M 10010
#define LL long long
#define INF 1 << 30;
using namespace std;
int main()
{
char str[1010];
int i;
LL need[10], p[10], h[10], vis[10];
LL f[10];
LL r, sum = 0;
LL cnt = 0;
memset(need , 0 , sizeof(need));
memset(vis , 0 , sizeof(vis));
cin >>str;
for(i = 1; i <= 3; i++)
cin >>h[i];
for(i = 1; i <= 3; i++)
cin >>p[i];
cin >>r;
int k = strlen(str);
for(i = 0; i < k; i++)
{
if(str[i] == 'B')
need[1] ++;
if(str[i] == 'S')
need[2] ++;
if(str[i] == 'C')
need[3] ++;
}
for(i = 1; i <= 3; i++)
cnt += need[i]*p[i];
for(i = 1; i <= 3; i++)
{
if(!need[i])
vis[i] = 1;
}
LL _max = -1;
//cout <<need[1]<<' '<<need[2]<<' '<<need[3]<<endl;
for(i = 1; i <= 3; i++)
{
if(!vis[i])
{
f[i] = h[i]/need[i];
if(f[i] > _max)
_max = f[i];
}
}
//cout <<_max<<endl;
for(i = 1; i <= _max+1; i++)
{
for(int j = 1; j <= 3; j++)
{
if(!vis[j])
{
if(h[j] >= need[j])
{
h[j] -= need[j];
}
else
{
LL pp = need[j] - h[j];
h[j] = 0;
if(r >= pp*p[j])
r -= pp*p[j];
else
{
cout <<i-1<<endl;
return 0;
}
}
}
}
}
sum = _max+1;
LL t = r/cnt;
sum += t;
cout <<sum<<endl;
return 0;
}