@Dmaxiya
2018-08-17T02:28:08.000000Z
字数 4441
阅读 1119
Codeforces
Contests 链接:Codeforces Round #402 (Div. 2)
过题数:4
排名:802/10360
有两个班级的同学,每个班 名同学,所有同学被分为 个学术等级,但是这 个等级的人数在两个班并不平均,在 班中第 个同学的等级为 , 班中第 个同学的等级为 ,校长想要通过交换使得同一个等级的同学在两个班的人数相等,如果可以,则输出最少的交换次数,否则输出 。
统计 和 中 ~ 的数字个数,如果为存在一个数字个数为奇数,则输出 ,否则输出交换次数。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <bitset>#include <algorithm>#include <functional>#include <iomanip>using namespace std;#define LL long longconst int maxn = 220;int n;bool flag;int a[maxn], b[maxn];int num[maxn], numa[maxn];int main() {#ifdef LOCALfreopen("test.txt", "r", stdin);// freopen("out.txt", "w", stdout);#endif // LOCALios::sync_with_stdio(false);while(scanf("%d", &n) != EOF) {int ans = 0;flag = true;memset(num, 0, sizeof(num));memset(numa, 0, sizeof(numa));for(int i = 0; i < n; ++i) {scanf("%d", &a[i]);++num[a[i]];++numa[a[i]];}for(int i = 0; i < n; ++i) {scanf("%d", &b[i]);++num[b[i]];}for(int i = 1; i <= 5; ++i) {if(num[i] % 2 != 0) {flag = false;break;}ans += abs(num[i] / 2 - numa[i]);}if(!flag) {printf("-1\n");continue;}printf("%d\n", ans / 2);}return 0;}
给定两个整数 和 ,判断能否通过删掉 中的某些位上的数字,使得 没有前导零且能够被 整除,题目保证数据有解。
从低位到高位统计在 个 之前存在多少个非零数字,如果数字中 的个数少于 个,则需要删掉 个数字,使得 变为 。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <bitset>#include <algorithm>#include <functional>#include <iomanip>using namespace std;#define LL long longint k, ans;char str[100];int main() {#ifdef LOCALfreopen("test.txt", "r", stdin);// freopen("out.txt", "w", stdout);#endif // LOCALios::sync_with_stdio(false);while(scanf("%s%d", str, &k) != EOF) {ans = 0;int cnt = 0;int len = strlen(str);for(int i = len - 1; i >= 0; --i) {if(str[i] == '0') {++cnt;} else {++ans;}if(cnt == k) {break;}}if(cnt == k) {printf("%d\n", ans);} else {printf("%d\n", len - 1);}}return 0;}
商家在一周内做打折活动,但是这一周内的价格 不一定都比原价 便宜, 想要买其中的 件商品,但是他必须在一周内购买其中的至少 件商品,输出 需要付的最少的费用。
假设 所有商品都在一周后买,然后再挑选 件商品换到一周内购买,设 ,则只要在 中选最小的 个数字加到 即可,因为至少买 件商品,所以如果有大于 个 都小于 ,则也可以将这些商品在一周内购买。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <bitset>#include <algorithm>#include <functional>#include <iomanip>using namespace std;#define LL long longconst int maxn = 200000 + 100;int n, k;LL a[maxn], b[maxn], d[maxn];int main() {#ifdef LOCALfreopen("test.txt", "r", stdin);// freopen("out.txt", "w", stdout);#endif // LOCALios::sync_with_stdio(false);while(scanf("%d%d", &n, &k) != EOF) {LL ans = 0;for(int i = 0; i < n; ++i) {scanf("%I64d", &a[i]);}for(int i = 0; i < n; ++i) {scanf("%I64d", &b[i]);d[i] = a[i] - b[i];ans += b[i];}sort(d, d + n);for(int i = 0; i < n; ++i) {if(d[i] <= 0 || i < k) {ans += d[i];} else {break;}}printf("%I64d\n", ans);}return 0;}
有一个字符串 要通过消去其中的一些字符,使得这个字符串变为 字符串, 只会按序列 的顺序消除字符,但是并不知道这样能不能让 字符串变为 字符串,为了达到这个目的,他的哥哥 将会在某一步阻止她,然后自己消除字符。问 最多可以消除到第几个字符。题目保证 字符串能够消除成 字符串,且每消除一个字符,其他字符的位置不变。
二分答案,对于每次二分, 判断字符串 是否是剩下的字符串的子序列。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <bitset>#include <algorithm>#include <functional>#include <iomanip>using namespace std;#define LL long longconst int maxn = 200000 + 100;int n;char str1[maxn], str2[maxn];int tmp[maxn];int num[maxn];char stmp[maxn];bool is(char *s1, char *s2) {int Index = 1;for(int i = 1; s1[i]; ++i) {if(s1[i] == s2[Index]) {++Index;}}return s2[Index] == '\0';}bool judge(int x) {int cnt = 1;for(int i = x + 1; i <= n; ++i) {tmp[cnt++] = num[i];}sort(tmp + 1, tmp + cnt);for(int i = 1; i < cnt; ++i) {stmp[i] = str1[tmp[i]];}stmp[cnt] = '\0';return is(stmp, str2);}int main() {#ifdef LOCALfreopen("test.txt", "r", stdin);// freopen("out.txt", "w", stdout);#endif // LOCALios::sync_with_stdio(false);while(scanf("%s%s", str1 + 1, str2 + 1) != EOF) {n = strlen(str1 + 1);for(int i = 1; i <= n; ++i) {scanf("%d", &num[i]);}int low = 0;int high = n + 1;int mid;while(high - low > 1) {mid = (low + high) / 2;if(judge(mid)) {low = mid;} else {high = mid;}}printf("%d\n", low);}return 0;}