@Dmaxiya
2018-08-17T02:17:10.000000Z
字数 4791
阅读 1223
Codeforces
Contests 链接:Educational Codeforces Round 39
过题数:4
排名:454/10671
将一个长度为 的序列 中的每个数字,分到两个序列 和 中,一个数字只能被分到一个序列,要求序列 中所有数字的和减去序列 中所有数字的和的差最大,问最大的差是多少。
第一行为一个整数 ,第二行为 个整数 。
输出序列 中所有数字的和减去序列 中所有数字的和的差的最大值。
| 输入 | 输出 | 提示 |
|---|---|---|
| 3 1 -2 0 |
3 | 我们可以令 于是 。 |
| 6 16 23 16 15 42 8 |
120 | 令 ,那么 。 |
把所有正数都放到 序列,所有负数都放到 序列,最后的结果等于 序列中所有数字绝对值的和。
#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 = 200;int n, num;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;for(int i = 0; i < n; ++i) {scanf("%d", &num);ans += abs(num);}printf("%d\n", ans);}return 0;}
给定两个正数 和 ,每次对这两个正数做下面的操作:
- 如果 或者 ,结束操作。否则执行操作 。
- 如果 ,将 变为 ,然后执行操作 。否则执行操作 。
- 如果 ,将 变为 ,然后执行操作 。否则结束操作。
求操作结束后 和 的值。
输入包含两个整数 。
输出操作结束后 和 的值。
| 输入 | 输出 | 提示 |
|---|---|---|
| 12 5 | 0 1 | 。 |
| 31 12 | 7 12 | 。 |
时,多次执行 的结果是 ,因此用取模运算代替减法运算,可以在 的时间复杂度内求得答案。
#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 longLL a, b;int main() {#ifdef LOCALfreopen("test.txt", "r", stdin);// freopen("out.txt", "w", stdout);#endif // LOCALios::sync_with_stdio(false);while(scanf("%I64d%I64d", &a, &b) != EOF) {while(a != 0 && b != 0) {if(a >= 2 * b) {a %= 2 * b;continue;}if(b >= 2 * a) {b %= 2 * a;continue;}break;}printf("%I64d %I64d\n", a, b);}return 0;}
给定一个字符串 ,可以将这个字符串的任意一个字符变成它的下一个字符(按 顺序),问给定的字符串能否通过任意次这种操作成为一个含有子字符序列 的字符串。
输出为一个只包含小写字符的字符串 。
如果无法成为满足题意的字符串,输出 ,否则输出转化后的字符串,如果有多解输出任意一个。
| 输入 | 输出 |
|---|---|
| aacceeggiikkmmooqqssuuwwyy | abcdefghijklmnopqrstuvwxyz |
| thereisnoanswer | -1 |
用一个 来记录当前位置的字符需要变成的子序列 中的字符,如果当前字符的 不大于 ,说明当前字符可以变成 ,然后将 成为下一个字符,最后判断 是否到达 。
#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 = 100000 + 100;char str[maxn];int main() {#ifdef LOCALfreopen("test.txt", "r", stdin);// freopen("out.txt", "w", stdout);#endif // LOCALios::sync_with_stdio(false);while(scanf("%s", str) != EOF) {char ch = 'a';for(int i = 0; str[i]; ++i) {if(str[i] <= ch) {if(ch <= 'z') {str[i] = ch;++ch;}}}if(ch == 'z' + 1) {printf("%s\n", str);} else {printf("-1\n");}}return 0;}
要上 天的课,每天 节课,一天的课表用一个长度为 的 字符串表示, 表示不用上课, 表示有课, 需要从他上的第一节课开始待在学校,直到他上的最后一节课,但是他总共可以翘 节课,问他最少的待在学校的时间可以是多少。
第一行包含三个整数 ,接下去 行每行为一个长度为 的字符串,表示每天的课表。
输出 待在学校的最少时间。
| 输入 | 输出 | 提示 |
|---|---|---|
2 5 10100110110 |
5 | 可以翘掉第一天的任意一节课,这样他第一天就可以只待在学校 节课的时间,第二天待在学校 四节课的时间。 |
2 5 00100110110 |
8 | 无法翘掉任何一节课,所以他每天都需要待在学校 节课的时间。 |
首先贪心 预处理出第 天翘掉 节课能够待在学校的最少时间(每次翘课一定从第一节课往后面翘或者从最后一节课往前面翘),接着定义 表示从第 天到第 天总共翘 节课 需要待在学校的最少时间,对于到第 天共翘掉 节课,从 到 枚举前一天翘掉 节课待在学校的最短时间 ,加上今天翘掉 节课待在学校的最短时间 ,就是到第 天翘掉 节课待在学校的最短时间,于是有递推式:
最后注意一下递推时的边界条件,答案就是 。
#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 = 600;const int INF = INT_MAX;int n, m, k;int Index[maxn];char str[maxn][maxn];int Min[maxn][maxn];int dp[maxn][maxn];int main() {#ifdef LOCALfreopen("test.txt", "r", stdin);// freopen("out.txt", "w", stdout);#endif // LOCALios::sync_with_stdio(false);while(scanf("%d%d%d", &n, &m, &k) != EOF) {memset(Min, 0, sizeof(Min));memset(dp, 0x3f, sizeof(dp));for(int i = 1; i <= n; ++i) {scanf("%s", str[i] + 1);int cnt = 0;for(int j = 1; j <= m; ++j) {if(str[i][j] == '1') {Index[cnt++] = j;}}for(int j = 0; j < cnt; ++j) {Min[i][j] = INF;for(int kk = 0; kk + (cnt - j) - 1 < cnt; ++kk) {Min[i][j] = min(Min[i][j], Index[kk + (cnt - j) - 1] - Index[kk] + 1);}}}for(int i = 0; i <= k; ++i) {dp[0][i] = 0;}for(int i = 1; i <= n; ++i) {for(int j = 0; j <= k; ++j) {for(int kk = 0; kk <= j; ++kk) {dp[i][j] = min(dp[i][j], dp[i - 1][kk] + Min[i][j - kk]);}}}printf("%d\n", dp[n][k]);}return 0;}