[关闭]
@Dmaxiya 2018-08-17T10:28:08.000000Z 字数 4441 阅读 897

Codeforces Round #402 (Div. 2)

Codeforces


Contests 链接:Codeforces Round #402 (Div. 2)
过题数:4
排名:802/10360

A. Pupils Redistribution

题意

有两个班级的同学,每个班 名同学,所有同学被分为 个学术等级,但是这 个等级的人数在两个班并不平均,在 班中第 个同学的等级为 班中第 个同学的等级为 ,校长想要通过交换使得同一个等级的同学在两个班的人数相等,如果可以,则输出最少的交换次数,否则输出

数据范围

题解

统计 ~ 的数字个数,如果为存在一个数字个数为奇数,则输出 ,否则输出交换次数。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. #define LL long long
  20. const int maxn = 220;
  21. int n;
  22. bool flag;
  23. int a[maxn], b[maxn];
  24. int num[maxn], numa[maxn];
  25. int main() {
  26. #ifdef LOCAL
  27. freopen("test.txt", "r", stdin);
  28. // freopen("out.txt", "w", stdout);
  29. #endif // LOCAL
  30. ios::sync_with_stdio(false);
  31. while(scanf("%d", &n) != EOF) {
  32. int ans = 0;
  33. flag = true;
  34. memset(num, 0, sizeof(num));
  35. memset(numa, 0, sizeof(numa));
  36. for(int i = 0; i < n; ++i) {
  37. scanf("%d", &a[i]);
  38. ++num[a[i]];
  39. ++numa[a[i]];
  40. }
  41. for(int i = 0; i < n; ++i) {
  42. scanf("%d", &b[i]);
  43. ++num[b[i]];
  44. }
  45. for(int i = 1; i <= 5; ++i) {
  46. if(num[i] % 2 != 0) {
  47. flag = false;
  48. break;
  49. }
  50. ans += abs(num[i] / 2 - numa[i]);
  51. }
  52. if(!flag) {
  53. printf("-1\n");
  54. continue;
  55. }
  56. printf("%d\n", ans / 2);
  57. }
  58. return 0;
  59. }

B. Weird Rounding

题意

给定两个整数 ,判断能否通过删掉 中的某些位上的数字,使得 没有前导零且能够被 整除,题目保证数据有解。

数据范围

题解

从低位到高位统计在 之前存在多少个非零数字,如果数字中 的个数少于 个,则需要删掉 个数字,使得 变为

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. #define LL long long
  20. int k, ans;
  21. char str[100];
  22. int main() {
  23. #ifdef LOCAL
  24. freopen("test.txt", "r", stdin);
  25. // freopen("out.txt", "w", stdout);
  26. #endif // LOCAL
  27. ios::sync_with_stdio(false);
  28. while(scanf("%s%d", str, &k) != EOF) {
  29. ans = 0;
  30. int cnt = 0;
  31. int len = strlen(str);
  32. for(int i = len - 1; i >= 0; --i) {
  33. if(str[i] == '0') {
  34. ++cnt;
  35. } else {
  36. ++ans;
  37. }
  38. if(cnt == k) {
  39. break;
  40. }
  41. }
  42. if(cnt == k) {
  43. printf("%d\n", ans);
  44. } else {
  45. printf("%d\n", len - 1);
  46. }
  47. }
  48. return 0;
  49. }

C. Dishonest Sellers

题意

商家在一周内做打折活动,但是这一周内的价格 不一定都比原价 便宜, 想要买其中的 件商品,但是他必须在一周内购买其中的至少 件商品,输出 需要付的最少的费用。

数据范围

题解

假设 所有商品都在一周后买,然后再挑选 件商品换到一周内购买,设 ,则只要在 中选最小的 个数字加到 即可,因为至少买 件商品,所以如果有大于 都小于 ,则也可以将这些商品在一周内购买。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. #define LL long long
  20. const int maxn = 200000 + 100;
  21. int n, k;
  22. LL a[maxn], b[maxn], d[maxn];
  23. int main() {
  24. #ifdef LOCAL
  25. freopen("test.txt", "r", stdin);
  26. // freopen("out.txt", "w", stdout);
  27. #endif // LOCAL
  28. ios::sync_with_stdio(false);
  29. while(scanf("%d%d", &n, &k) != EOF) {
  30. LL ans = 0;
  31. for(int i = 0; i < n; ++i) {
  32. scanf("%I64d", &a[i]);
  33. }
  34. for(int i = 0; i < n; ++i) {
  35. scanf("%I64d", &b[i]);
  36. d[i] = a[i] - b[i];
  37. ans += b[i];
  38. }
  39. sort(d, d + n);
  40. for(int i = 0; i < n; ++i) {
  41. if(d[i] <= 0 || i < k) {
  42. ans += d[i];
  43. } else {
  44. break;
  45. }
  46. }
  47. printf("%I64d\n", ans);
  48. }
  49. return 0;
  50. }

D. String Game

题意

有一个字符串 要通过消去其中的一些字符,使得这个字符串变为 字符串, 只会按序列 的顺序消除字符,但是并不知道这样能不能让 字符串变为 字符串,为了达到这个目的,他的哥哥 将会在某一步阻止她,然后自己消除字符。问 最多可以消除到第几个字符。题目保证 字符串能够消除成 字符串,且每消除一个字符,其他字符的位置不变。

数据范围

题解

二分答案,对于每次二分, 判断字符串 是否是剩下的字符串的子序列。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. #define LL long long
  20. const int maxn = 200000 + 100;
  21. int n;
  22. char str1[maxn], str2[maxn];
  23. int tmp[maxn];
  24. int num[maxn];
  25. char stmp[maxn];
  26. bool is(char *s1, char *s2) {
  27. int Index = 1;
  28. for(int i = 1; s1[i]; ++i) {
  29. if(s1[i] == s2[Index]) {
  30. ++Index;
  31. }
  32. }
  33. return s2[Index] == '\0';
  34. }
  35. bool judge(int x) {
  36. int cnt = 1;
  37. for(int i = x + 1; i <= n; ++i) {
  38. tmp[cnt++] = num[i];
  39. }
  40. sort(tmp + 1, tmp + cnt);
  41. for(int i = 1; i < cnt; ++i) {
  42. stmp[i] = str1[tmp[i]];
  43. }
  44. stmp[cnt] = '\0';
  45. return is(stmp, str2);
  46. }
  47. int main() {
  48. #ifdef LOCAL
  49. freopen("test.txt", "r", stdin);
  50. // freopen("out.txt", "w", stdout);
  51. #endif // LOCAL
  52. ios::sync_with_stdio(false);
  53. while(scanf("%s%s", str1 + 1, str2 + 1) != EOF) {
  54. n = strlen(str1 + 1);
  55. for(int i = 1; i <= n; ++i) {
  56. scanf("%d", &num[i]);
  57. }
  58. int low = 0;
  59. int high = n + 1;
  60. int mid;
  61. while(high - low > 1) {
  62. mid = (low + high) / 2;
  63. if(judge(mid)) {
  64. low = mid;
  65. } else {
  66. high = mid;
  67. }
  68. }
  69. printf("%d\n", low);
  70. }
  71. return 0;
  72. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注