AtCoder Beginner Contest 067
A - Sharing Cookies
Time limit : 2sec / Memory limit : 256MB
Score : 100 points
Problem Statement
Snuke is giving cookies to his three goats.
He has two cookie tins. One contains A cookies, and the other contains B cookies. He can thus give A cookies, B cookies or A+Bcookies to his goats (he cannot open the tins).
Your task is to determine whether Snuke can give cookies to his three goats so that each of them can have the same number of cookies.
Constraints
- 1≤A,B≤100
- Both A and B are integers.
Input
Input is given from Standard Input in the following format:
A B
Output
If it is possible to give cookies so that each of the three goats can have the same number of cookies, print Possible
; otherwise, print Impossible
.
Sample Input 1
4 5
Sample Output 1
Possible
If Snuke gives nine cookies, each of the three goats can have three cookies.
Sample Input 2
1 1
Sample Output 2
Impossible
Since there are only two cookies, the three goats cannot have the same number of cookies no matter what Snuke gives to them.
题意:给定两个饼干罐子,其中有不定数量的饼干。然后求给出任意罐子是否能使三只猫吃到一样多?
题解:给出A,或者B或者A和B都给出,判断给出饼干数量能否整除3
#include<bits/stdc++.h> using namespace std; int main() { int a,b; scanf("%d%d",&a,&b); if(a%3==0||b%3==0||(a+b)%3==0) printf("Possible\n"); else printf("Impossible\n"); return 0; }
B - Snake Toy
Time limit : 2sec / Memory limit : 256MB
Score : 200 points
Problem Statement
Snuke has N sticks. The length of the i-th stick is li.
Snuke is making a snake toy by joining K of the sticks together.
The length of the toy is represented by the sum of the individual sticks that compose it. Find the maximum possible length of the toy.
Constraints
- 1≤K≤N≤50
- 1≤li≤50
- li is an integer.
Input
Input is given from Standard Input in the following format:
N K l1 l2 l3 … lN
Output
Print the answer.
Sample Input 1
5 3 1 2 3 4 5
Sample Output 1
12
You can make a toy of length 12 by joining the sticks of lengths 3, 4 and 5, which is the maximum possible length.
Sample Input 2
15 14 50 26 27 21 41 7 42 35 7 5 5 36 39 1 45
Sample Output 2
386
题意:有N个数取K个数使得总和最大
题解:对于序列sort排序,倒置输出K个值的和
#include<bits/stdc++.h> using namespace std; int main() { int n,k; int a[50+7]; scanf("%d%d",&n,&k); int sum=0; for(int i=0;i<n;i++) scanf("%d",&a[i]); sort(a,a+n); for(int i=n-1;n-i<=k;i--) sum+=a[i]; printf("%d",sum); return 0; }
C - Splitting Pile
Time limit : 2sec / Memory limit : 256MB
Score : 300 points
Problem Statement
Snuke and Raccoon have a heap of N cards. The i-th card from the top has the integer ai written on it.
They will share these cards. First, Snuke will take some number of cards from the top of the heap, then Raccoon will take all the remaining cards. Here, both Snuke and Raccoon have to take at least one card.
Let the sum of the integers on Snuke's cards and Raccoon's cards be x and y, respectively. They would like to minimize |x−y|. Find the minimum possible value of |x−y|.
Constraints
- 2≤N≤2×105
- −109≤ai≤109
- ai is an integer.
Input
Input is given from Standard Input in the following format:
N a1 a2 … aN
Output
Print the answer.
Sample Input 1
6 1 2 3 4 5 6
Sample Output 1
1
If Snuke takes four cards from the top, and Raccoon takes the remaining two cards, x=10, y=11, and thus |x−y|=1. This is the minimum possible value.
Sample Input 2
2 10 -10
Sample Output 2
20
Snuke can only take one card from the top, and Raccoon can only take the remaining one card. In this case, x=10, y=−10, and thus |x−y|=20.
题意:对于一个序列分成连续的两部分,求两部分相减的最小值
题解:求每个状态的前缀和,每次用总和-前缀和为后半部分,两部分相减和minn比较最小值(注意minn初始化时要大于1e15)
#include<bits/stdc++.h> using namespace std; const int N=2e5+7; int main() { int n; cin>>n; long long sum=0; long long count=0; long long a[N]; long long minn=1e15; for(int i=1;i<=n;i++) { cin>>a[i]; sum+=a[i]; } count+=a[1]; for(int i=2;i<=n;i++) { minn=min(minn,abs(sum-count*2)); count+=a[i]; } cout<<minn<<endl; return 0; }
D - Fennec VS. Snuke
Time limit : 2sec / Memory limit : 256MB
Score : 400 points
Problem Statement
Fennec and Snuke are playing a board game.
On the board, there are N cells numbered 1 through N, and N−1 roads, each connecting two cells. Cell ai is adjacent to Cell bithrough the i-th road. Every cell can be reached from every other cell by repeatedly traveling to an adjacent cell. In terms of graph theory, the graph formed by the cells and the roads is a tree.
Initially, Cell 1 is painted black, and Cell N is painted white. The other cells are not yet colored. Fennec (who goes first) and Snuke (who goes second) alternately paint an uncolored cell. More specifically, each player performs the following action in her/his turn:
- Fennec: selects an uncolored cell that is adjacent to a black cell, and paints it black.
- Snuke: selects an uncolored cell that is adjacent to a white cell, and paints it white.
A player loses when she/he cannot paint a cell. Determine the winner of the game when Fennec and Snuke play optimally.
Constraints
- 2≤N≤105
- 1≤ai,bi≤N
- The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N a1 b1 : aN−1 bN−1
Output
If Fennec wins, print Fennec
; if Snuke wins, print Snuke
.
Sample Input 1
7 3 6 1 2 3 1 7 4 5 7 1 4
Sample Output 1
Fennec
For example, if Fennec first paints Cell 2 black, she will win regardless of Snuke's moves.
Sample Input 2
4 1 4 4 2 2 3
Sample Output 2
Snuke
题意:每个细胞连接成一个树,第一个涂成black,第n个涂成white,每次去涂相邻且相同颜色,且每次Frence先涂问不能涂得人算输,则最后谁赢了。
题解:注意用容器存入树
- ss[u].emplace_back(v);
- ss[v].emplace_back(u);
然后用一个队列去判断,首先放入1,n,一旦有联系的数字则继续放入,用vis去判断联系情况,如果联系为1则x++,联系n则y++。最后比较x,y就可以确定谁能赢。
#include<bits/stdc++.h> using namespace std; const int N=1e5+7; vector<int>ss[N]; queue<int>qe; int vis[N]; int main() { int n; cin>>n; int u,v,x,y; memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) ss[i].clear(); for(int i=1;i<n;i++) { cin>>u>>v; ss[u].emplace_back(v); ss[v].emplace_back(u); } qe.push(1); qe.push(n); vis[1]=1; vis[n]=2; x=0,y=0; while(!qe.empty()) { u=qe.front(); qe.pop(); for(int i=0;i<ss[u].size();i++) { v=ss[u][i]; if(!vis[v]) { vis[v]=vis[u]; qe.push(v); if(vis[v]==1) x++; else y++; } } } if(x<=y) cout<<"Snuke"<<endl; else cout<<"Fennec"<<endl; return 0; }