本文主要是介绍CodeForce #429 DIV2 A B C题解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
A:http://codeforces.com/contest/841/problem/A
题意:n个气球分给k个人,是否有这样的解:每个人手里的气球都颜色不重复
思路:个数最多的颜色个球的个数 >k, 就必然有人手里两个球
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>using namespace std;int main(int argc, char *argv[]) {
// freopen("A.txt", "r", stdin);int SZ = 26;int n,k;int num[SZ];while(~scanf("%d%d", &n, &k)) {memset(num, 0, sizeof(num));string s;cin >> s;for (int i = 0; i < s.size(); i++) {num[s[i] - 'a'] += 1;}int mx = 0;for (int i = 0; i < SZ; i++) {mx = max(mx, num[i]);
// cout << num[i] << " " << mx << endl;}
// cout << mx << endl;if (mx > (int)(k))printf("NO\n");elseprintf("YES\n");}return 0;
}
B:http://codeforces.com/contest/841/problem/B
题意:一个数组,A可以删除和为奇数的连续子串,B可以删除和为偶数的连续子串,不能操作的输,A是先手,问谁赢
思路:
1、如果和为奇数,A一定赢
2、如果和为偶数,那么有两种情况,一是没有奇数,那么A一定输,如果有奇数,那么奇数必然是偶数个。A作为先手,只能取和为奇数的序列,那么剩下的和必然是奇数,而接下来B只能取和为偶数的序列,B取剩下的就和一定是奇数,那么第二轮A就能取完剩下的所有数字,于是A就赢了。
综合来看,只要有奇数出现,B一定会熟
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>using namespace std;
int main(int argc, char *argv[]) {int x, n, r;while(~scanf("%d", &n)) {r = 2;for (int i = 0; i < n; i++) {scanf("%d", &x);if(x%2) {r = 1;} }if (r == 1) {printf("First\n");} else {printf("Second\n");}}return 0;
}
C:http://codeforces.com/contest/841/problem/C
解很简单,看下example应该都能找到规律。
证明:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>using namespace std;const int SIZE = 1e5 *2 +1;//int A[SIZE];
struct Node {int v;int idx;Node(){}Node(int vv, int i) {v = vv;idx = i;}
};Node A[SIZE];
Node B[SIZE];bool cmp(const Node a, const Node b) {return a.v > b.v;
}bool cmp2(const Node a, const Node b) {return a.v < b.v;
}bool cmp3(const Node a, const Node b) {return a.idx < b.idx;
}int main() {
// freopen("A.txt", "r", stdin);int m;while(~scanf("%d", &m)) {
// cout << "m:" << m << endl;for (int i =0; i < m; i++) {scanf("%d", &(A[i].v));A[i].idx = i;}for (int i = 0; i < m; i++) {scanf("%d", &(B[i].v));B[i].idx = i;}sort(A, A+m, cmp);sort(B, B+m, cmp2);for (int i = 0; i < m; i++) {
// cout << "i:" << i << " A:" << A[i].v << "," << A[i].idx << " B:"<< B[i].v << "," << B[i].idx << endl;A[i].idx = B[i].idx;}sort(A, A+m, cmp3);printf("%d", A[0].v);for (int i = 1; i < m; i++) {printf(" %d", A[i].v);}putchar('\n');}return 0;
}
这篇关于CodeForce #429 DIV2 A B C题解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!