Educational Codeforces Round 39

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

A. Partition

题意

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

输入

第一行为一个整数 n   ( 1 n 100 ) ,第二行为 n 个整数 a 1 , a 2 , , a n   ( 100 a i 100 )

输出

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

样例

输入
3
1 -2 0
输出
3
提示
我们可以令 B = { 1 , 0 } , C = { 2 } 于是 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 = { } ,那么 s u m B = 120 , s u m C = 0 , B C = 120

题解

把所有正数都放到 B 序列,所有负数都放到 C 序列,最后的结果等于 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

题意

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

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

求操作结束后 a b 的值。

输入

输入包含两个整数 a , b   ( 1 a , b 10 18 )

输出

输出操作结束后 a b 的值。

样例

输入
12 5
输出
0 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

题解

b 2 a 时,多次执行 b 2 a 的结果是 b % 2 a ,因此用取模运算代替减法运算,可以在 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 ,可以将这个字符串的任意一个字符变成它的下一个字符(按 A S C I I 顺序),问给定的字符串能否通过任意次这种操作成为一个含有子字符序列 a b c z 的字符串。

输入

输出为一个只包含小写字符的字符串 s   ( 1 | s | 10 5 )

输出

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

样例

输入
aacceeggiikkmmooqqssuuwwyy
输出
abcdefghijklmnopqrstuvwxyz
输入
thereisnoanswer
输出
-1

题解

用一个 c h 来记录当前位置的字符需要变成的子序列 a b c z 中的字符,如果当前字符的 A S C I I 不大于 c h ,说明当前字符可以变成 c h ,然后将 c h + + 成为下一个字符,最后判断 c h 是否到达 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

题意

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

输入

第一行包含三个整数 n , m , k   ( 1 n , m 500 , 0 k 500 ) ,接下去 n 行每行为一个长度为 m 的字符串,表示每天的课表。

输出

输出 I v a n 待在学校的最少时间。

样例

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

题解

首先贪心 O ( n m ) 预处理出第 i 天翘掉 j 节课能够待在学校的最少时间(每次翘课一定从第一节课往后面翘或者从最后一节课往前面翘) M i n [ i ] [ j ] ,接着定义 d p [ i ] [ j ] 表示从第 1 天到第 i 天总共翘 j 节课 I v a n 需要待在学校的最少时间,对于到第 i 天共翘掉 j 节课,从 0 k 枚举前一天翘掉 l 节课待在学校的最短时间 d p [ i 1 ] [ l ] ,加上今天翘掉 j l 节课待在学校的最短时间 M i n [ i ] [ j l ] ,就是到第 i 天翘掉 j 节课待在学校的最短时间,于是有递推式:

d p [ i ] [ j ] = min ( d p [ i 1 ] [ l ] + M i n [ i ] [ j l ] )
最后注意一下递推时的边界条件,答案就是 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;
}

猜你喜欢

转载自blog.csdn.net/CSDNjiangshan/article/details/81365405