sgu546 Ternary Password

July 18, 2013
Sgu

题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=546 这题还好,1Y,考虑情况周全,就没问题了,还好提交之前把想到的情况都测试了一遍..


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
char s[202];
int n, a, b, a0[202], b0[202];
int main(void) {
    //freopen("in.txt", "r", stdin);
    while (~scanf("%d%d%d%s", &n, &a, &b, s)) {
        int a1 = 0, b1 = 0;
        for (int i = 0;i < n; ++i) {
            if (s[i] == '0') a0[a1] = i, a1++; else if (s[i] == '1') b0[b1] = i, b1++;
        }
        if (a1 == a && b1 == b) printf("0\n%s\n", s);
        else if (a+b>n) printf("-1\n");
        else {
            int cnt = 0, a2 = 0, b2 = 0;
            a2 = a1-a, b2 = b1-b;
            if (a1 < a) {
                for (int f = 0; f < b2 && a1 < a; ++f) s[b0[f]] = '0', cnt++, a1++, b1--;
                if (a1 < a) {
                    for (int e = 0; e < n && a1 < a; ++e) if (s[e] == '2') s[e] = '0', cnt++, a1++;
                }
            }
            if (b1 < b) {
                for (int f = 0; f < a2 && b1 < b; ++f) s[a0[f]] = '1', cnt++, b1++, a1--;
                if (b1 < b) {
                    for (int e = 0; e < n && b1<b; ++e) if (s[e] == '2') s[e] = '1', cnt++, b1++;
                }
            }
            if (a1 > a) {
                for (int f = 0; f < n && a1 > a; ++f) if (s[f] == '0') a1--, s[f] = '2', cnt++;
            }
            if (b1 > b) {
                for (int f = 0; f < n && b1 > b; ++f) if (s[f] == '1') b1--, s[f] = '2', cnt++;
            }
            printf("%d\n%s\n", cnt, s);
        }
    }

    return 0;
}

只是我的方法貌似比较笨==

comments powered by Disqus