Contests 链接:Educational Codeforces Round 39
过题数:4
排名:454/10671
A. Partition
题意
将一个长度为 n n 的序列 中的每个数字,分到两个序列 B B 和 中,一个数字只能被分到一个序列,要求序列 B B 中所有数字的和减去序列 中所有数字的和的差最大,问最大的差是多少。
输入
第一行为一个整数 n (1≤n≤100) n ( 1 ≤ n ≤ 100 ) ,第二行为 n n 个整数 。
输出
输出序列 B B 中所有数字的和减去序列 中所有数字的和的差的最大值。
样例
输入 |
---|
3 1 -2 0 |
输出 |
3 |
提示 |
我们可以令 B={1,0},C={−2} B = { 1 , 0 } , C = { − 2 } 于是 sumB=1,sumC=−2,sumB−sumC=3 s u m B = 1 , s u m C = − 2 , s u m B − s u m C = 3 。 |
输入 |
---|
6 16 23 16 15 42 8 |
输出 |
120 |
提示 |
令 B={16,23,16,15,42,8},C={} B = { 16 , 23 , 16 , 15 , 42 , 8 } , C = { } ,那么 sumB=120,sumC=0,B−C=120 s u m B = 120 , s u m C = 0 , B − C = 120 。 |
题解
把所有正数都放到 B B 序列,所有负数都放到 序列,最后的结果等于 A A 序列中所有数字绝对值的和。
过题代码
#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 long
const int maxn = 200;
int n, num;
int main() {
#ifdef LOCAL
freopen("test.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif // LOCAL
ios::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;
}
B. Weird Subtraction Process
题意
给定两个正数 和 b b ,每次对这两个正数做下面的操作:
- 如果 或者 b=0 b = 0 ,结束操作。否则执行操作 2 2 。
- 如果 ,将 a a 变为 ,然后执行操作 1 1 。否则执行操作 。
- 如果 b≥2a b ≥ 2 a ,将 b b 变为 ,然后执行操作 1 1 。否则结束操作。
求操作结束后 和 b b 的值。
输入
输入包含两个整数 。
输出
输出操作结束后 a a 和 的值。
样例
输入 |
---|
12 5 |
输出 |
0 1 |
提示 |
a=12,b=5→a=2,b=5→a=2,b=1→a=0,b=1 a = 12 , b = 5 → a = 2 , b = 5 → a = 2 , b = 1 → a = 0 , b = 1 。 |
输入 |
---|
31 12 |
输出 |
7 12 |
提示 |
a=31,b=12→a=7,b=12 a = 31 , b = 12 → a = 7 , b = 12 。 |
题解
b≥2a b ≥ 2 a 时,多次执行 b−2a b − 2 a 的结果是 b%2a b % 2 a ,因此用取模运算代替减法运算,可以在 O(logn) O ( log n ) 的时间复杂度内求得答案。
过题代码
#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 long
LL a, b;
int main() {
#ifdef LOCAL
freopen("test.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif // LOCAL
ios::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;
}
C. String Transformation
题意
给定一个字符串 s s ,可以将这个字符串的任意一个字符变成它的下一个字符(按 顺序),问给定的字符串能否通过任意次这种操作成为一个含有子字符序列 abc⋯z a b c ⋯ z 的字符串。
输入
输出为一个只包含小写字符的字符串 s (1≤|s|≤105) s ( 1 ≤ | s | ≤ 10 5 ) 。
输出
如果无法成为满足题意的字符串,输出 −1 − 1 ,否则输出转化后的字符串,如果有多解输出任意一个。
样例
输入 |
---|
aacceeggiikkmmooqqssuuwwyy |
输出 |
abcdefghijklmnopqrstuvwxyz |
输入 |
---|
thereisnoanswer |
输出 |
-1 |
题解
用一个 ch c h 来记录当前位置的字符需要变成的子序列 abc⋯z a b c ⋯ z 中的字符,如果当前字符的 ASCII A S C I I 不大于 ch c h ,说明当前字符可以变成 ch c h ,然后将 ch++ c h + + 成为下一个字符,最后判断 ch c h 是否到达 ′z′+1 ′ z ′ + 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 long
const int maxn = 100000 + 100;
char str[maxn];
int main() {
#ifdef LOCAL
freopen("test.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif // LOCAL
ios::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;
}
D. Timetable
题意
Ivan I v a n 要上 n n 天的课,每天 节课,一天的课表用一个长度为 m m 的 字符串表示, 0 0 表示不用上课, 表示有课, Ivan I v a n 需要从他上的第一节课开始待在学校,直到他上的最后一节课,但是他总共可以翘 k k 节课,问他最少的待在学校的时间可以是多少。
输入
第一行包含三个整数 ,接下去 n n 行每行为一个长度为 的字符串,表示每天的课表。
输出
输出 Ivan I v a n 待在学校的最少时间。
样例
输入 |
---|
2 5 101001 10110 |
输出 |
5 |
提示 |
Ivan I v a n 可以翘掉第一天的任意一节课,这样他第一天就可以只待在学校 1 1 节课的时间,第二天待在学校 四节课的时间。 |
输入 |
---|
2 5 001001 10110 |
输出 |
8 |
提示 |
Ivan I v a n 无法翘掉任何一节课,所以他每天都需要待在学校 4 4 节课的时间。 |
题解
首先贪心 预处理出第 i i 天翘掉 节课能够待在学校的最少时间(每次翘课一定从第一节课往后面翘或者从最后一节课往前面翘) Min[i][j] M i n [ i ] [ j ] ,接着定义 dp[i][j] d p [ i ] [ j ] 表示从第 1 1 天到第 天总共翘 j j 节课 需要待在学校的最少时间,对于到第 i i 天共翘掉 节课,从 0 0 到 枚举前一天翘掉 l l 节课待在学校的最短时间 ,加上今天翘掉 j−l j − l 节课待在学校的最短时间 Min[i][j−l] M i n [ i ] [ j − l ] ,就是到第 i i 天翘掉 节课待在学校的最短时间,于是有递推式:
dp[i][j]=min(dp[i−1][l]+Min[i][j−l]) d p [ i ] [ j ] = min ( d p [ i − 1 ] [ l ] + M i n [ i ] [ j − l ] )最后注意一下递推时的边界条件,答案就是 dp[n][k] d p [ n ] [ k ] 。过题代码
#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 long const 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 LOCAL freopen("test.txt", "r", stdin); // freopen("out.txt", "w", stdout); #endif // LOCAL ios::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; }