[关闭]
@Dmaxiya 2018-08-17T10:17:10.000000Z 字数 4791 阅读 995

Educational Codeforces Round 39

Codeforces


Contests 链接:Educational Codeforces Round 39
过题数:4
排名:454/10671

A. Partition

题意

将一个长度为 的序列 中的每个数字,分到两个序列 中,一个数字只能被分到一个序列,要求序列 中所有数字的和减去序列 中所有数字的和的差最大,问最大的差是多少。

输入

第一行为一个整数 ,第二行为 个整数

输出

输出序列 中所有数字的和减去序列 中所有数字的和的差的最大值。

样例

输入 输出 提示
3
1 -2 0
3 我们可以令 于是
6
16 23 16 15 42 8
120 ,那么

题解

把所有正数都放到 序列,所有负数都放到 序列,最后的结果等于 序列中所有数字绝对值的和。

过题代码

  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 = 200;
  21. int n, num;
  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("%d", &n) != EOF) {
  29. int ans = 0;
  30. for(int i = 0; i < n; ++i) {
  31. scanf("%d", &num);
  32. ans += abs(num);
  33. }
  34. printf("%d\n", ans);
  35. }
  36. return 0;
  37. }

B. Weird Subtraction Process

题意

给定两个正数 ,每次对这两个正数做下面的操作:

  1. 如果 或者 ,结束操作。否则执行操作
  2. 如果 ,将 变为 ,然后执行操作 。否则执行操作
  3. 如果 ,将 变为 ,然后执行操作 。否则结束操作。

求操作结束后 的值。

输入

输入包含两个整数

输出

输出操作结束后 的值。

样例

输入 输出 提示
12 5 0 1
31 12 7 12

题解

时,多次执行 的结果是 ,因此用取模运算代替减法运算,可以在 的时间复杂度内求得答案。

过题代码

  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. LL a, b;
  21. int main() {
  22. #ifdef LOCAL
  23. freopen("test.txt", "r", stdin);
  24. // freopen("out.txt", "w", stdout);
  25. #endif // LOCAL
  26. ios::sync_with_stdio(false);
  27. while(scanf("%I64d%I64d", &a, &b) != EOF) {
  28. while(a != 0 && b != 0) {
  29. if(a >= 2 * b) {
  30. a %= 2 * b;
  31. continue;
  32. }
  33. if(b >= 2 * a) {
  34. b %= 2 * a;
  35. continue;
  36. }
  37. break;
  38. }
  39. printf("%I64d %I64d\n", a, b);
  40. }
  41. return 0;
  42. }

C. String Transformation

题意

给定一个字符串 ,可以将这个字符串的任意一个字符变成它的下一个字符(按 顺序),问给定的字符串能否通过任意次这种操作成为一个含有子字符序列 的字符串。

输入

输出为一个只包含小写字符的字符串

输出

如果无法成为满足题意的字符串,输出 ,否则输出转化后的字符串,如果有多解输出任意一个。

样例

输入 输出
aacceeggiikkmmooqqssuuwwyy abcdefghijklmnopqrstuvwxyz
thereisnoanswer -1

题解

用一个 来记录当前位置的字符需要变成的子序列 中的字符,如果当前字符的 不大于 ,说明当前字符可以变成 ,然后将 成为下一个字符,最后判断 是否到达

过题代码

  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 = 100000 + 100;
  21. char str[maxn];
  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", str) != EOF) {
  29. char ch = 'a';
  30. for(int i = 0; str[i]; ++i) {
  31. if(str[i] <= ch) {
  32. if(ch <= 'z') {
  33. str[i] = ch;
  34. ++ch;
  35. }
  36. }
  37. }
  38. if(ch == 'z' + 1) {
  39. printf("%s\n", str);
  40. } else {
  41. printf("-1\n");
  42. }
  43. }
  44. return 0;
  45. }

D. Timetable

题意

要上 天的课,每天 节课,一天的课表用一个长度为 字符串表示, 表示不用上课, 表示有课, 需要从他上的第一节课开始待在学校,直到他上的最后一节课,但是他总共可以翘 节课,问他最少的待在学校的时间可以是多少。

输入

第一行包含三个整数 ,接下去 行每行为一个长度为 的字符串,表示每天的课表。

输出

输出 待在学校的最少时间。

样例

输入 输出 提示
2 5 1
01001
10110
5 可以翘掉第一天的任意一节课,这样他第一天就可以只待在学校 节课的时间,第二天待在学校 四节课的时间。
2 5 0
01001
10110
8 无法翘掉任何一节课,所以他每天都需要待在学校 节课的时间。

题解

首先贪心 预处理出第 天翘掉 节课能够待在学校的最少时间(每次翘课一定从第一节课往后面翘或者从最后一节课往前面翘),接着定义 表示从第 天到第 天总共翘 节课 需要待在学校的最少时间,对于到第 天共翘掉 节课,从 枚举前一天翘掉 节课待在学校的最短时间 ,加上今天翘掉 节课待在学校的最短时间 ,就是到第 天翘掉 节课待在学校的最短时间,于是有递推式:

最后注意一下递推时的边界条件,答案就是

过题代码

  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 = 600;
  21. const int INF = INT_MAX;
  22. int n, m, k;
  23. int Index[maxn];
  24. char str[maxn][maxn];
  25. int Min[maxn][maxn];
  26. int dp[maxn][maxn];
  27. int main() {
  28. #ifdef LOCAL
  29. freopen("test.txt", "r", stdin);
  30. // freopen("out.txt", "w", stdout);
  31. #endif // LOCAL
  32. ios::sync_with_stdio(false);
  33. while(scanf("%d%d%d", &n, &m, &k) != EOF) {
  34. memset(Min, 0, sizeof(Min));
  35. memset(dp, 0x3f, sizeof(dp));
  36. for(int i = 1; i <= n; ++i) {
  37. scanf("%s", str[i] + 1);
  38. int cnt = 0;
  39. for(int j = 1; j <= m; ++j) {
  40. if(str[i][j] == '1') {
  41. Index[cnt++] = j;
  42. }
  43. }
  44. for(int j = 0; j < cnt; ++j) {
  45. Min[i][j] = INF;
  46. for(int kk = 0; kk + (cnt - j) - 1 < cnt; ++kk) {
  47. Min[i][j] = min(Min[i][j], Index[kk + (cnt - j) - 1] - Index[kk] + 1);
  48. }
  49. }
  50. }
  51. for(int i = 0; i <= k; ++i) {
  52. dp[0][i] = 0;
  53. }
  54. for(int i = 1; i <= n; ++i) {
  55. for(int j = 0; j <= k; ++j) {
  56. for(int kk = 0; kk <= j; ++kk) {
  57. dp[i][j] = min(dp[i][j], dp[i - 1][kk] + Min[i][j - kk]);
  58. }
  59. }
  60. }
  61. printf("%d\n", dp[n][k]);
  62. }
  63. return 0;
  64. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注