AtCoder Beginner Contest 067 (A,B,C,D)
A - Sharing Cookies
有A个饼干和B个饼干,可以给羊A、B或A+B个饼干,要求三个羊可以平分。
一开始没有读懂题,以为给A+B就行了,错了一次。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #define ll long long 6 using namespace std; 7 8 int main(){ 9 int a,b; 10 cin >> a>>b; 11 if((a+b)%3==0 || a%3==0 || b%3==0){ 12 cout << "Possible" << endl; 13 } 14 else cout << "Impossible" << endl; 15 return 0; 16 }
B - Snake Toy
有N个棒,求其中K个棒的最大和。排下序就行了。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #define ll long long 6 using namespace std; 7 int a[55]; 8 bool cmp(int x, int y){ 9 return x > y; 10 } 11 int main(){ 12 int n,k; 13 cin>>n>>k; 14 for(int i = 0; i < n; i ++)scanf("%d",&a[i]); 15 sort(a,a+n,cmp); 16 int ans = 0; 17 for(int i = 0; i < min(n,k); i ++){ 18 ans += a[i]; 19 } 20 cout << ans << endl; 21 return 0; 22 }
C - Splitting Pile
N个扑克, 都有一个值,求从哪里切开使的两个和的差值最小。
前缀和。
1 #include <iostream> 2 #include <cmath> 3 #define lowbit(x) x&(-x) 4 #define ll long long 5 using namespace std; 6 const int MAX = 2e5+10; 7 ll a[MAX], sum[MAX]; 8 9 int main(){ 10 int n; 11 cin>>n; 12 for(int i = 1; i <= n; i ++){ 13 cin>>a[i]; 14 sum[i] = sum[i-1]+a[i]; 15 } 16 ll Min = 1LL*50000000000000000; 17 for(int i = 1; i < n; i ++){ 18 ll cnt = sum[n]-2LL*sum[i]; 19 if(Min > abs(cnt))Min = abs(cnt); 20 } 21 cout << Min << endl; 22 }
D - Fennec VS. Snuke
有n个点,n-1个线,1是白色,n是黑色,Fennec可以在白色旁边没画的点上画白色,Snuke可以在黑色旁边没画的点上画黑色,最后不能画的人输。
比赛时没做出来,赛后看了别人的代码懂了。求每个点到1和n的最小距离。
1 #include <iostream> 2 #include <stdio.h> 3 #include <queue> 4 #include <vector> 5 #include <string.h> 6 #include <algorithm> 7 using namespace std; 8 const int MAX = 1e5+10; 9 vector<int> vs[MAX]; 10 int df[MAX], ds[MAX],n; 11 int pastf[MAX], pasts[MAX]; 12 void bfs(int s, int dare){ 13 memset(pastf,0,sizeof(pastf)); 14 memset(pasts,0,sizeof(pasts)); 15 pastf[0] = 1;pasts[n-1] = 1; 16 queue<pair<int, int> > que; 17 que.push(make_pair(s,0)); 18 while(!que.empty()){ 19 int now = que.front().first; 20 int cost = que.front().second; 21 que.pop(); 22 if(dare == 1)df[now] = cost; 23 else ds[now] = cost; 24 int nowsize = (int)vs[now].size(); 25 for(int i = 0; i < nowsize; i ++){ 26 int nex = vs[now][i]; 27 if(dare == 1 && !pastf[nex]){ 28 pastf[nex] = 1; 29 que.push(make_pair(nex,cost+1)); 30 }else if(dare != 1 && !pasts[nex]){ 31 pasts[nex] = 1; 32 que.push(make_pair(nex,cost+1)); 33 } 34 } 35 } 36 } 37 int main(){ 38 int l, r; 39 scanf("%d",&n); 40 for(int i = 0; i < n-1; i ++){ 41 scanf("%d %d",&l,&r); 42 l--;r--; 43 vs[l].push_back(r); 44 vs[r].push_back(l); 45 } 46 bfs(0,1); 47 bfs(n-1,-1); 48 int countf = 0, counts = 0; 49 for(int i = 0; i < n; i ++){ 50 if(df[i] > ds[i]) counts++; 51 else countf++; 52 } 53 if(countf > counts)cout << "Fennec\n"; 54 else cout << "Snuke\n"; 55 return 0; 56 }