1. 程式人生 > >Codeforces 1138B(列方程枚舉)

Codeforces 1138B(列方程枚舉)

getch tmp ORC ons const lse class for 感覺

構造模擬要分情況討論感覺不是夠本質,然後官解是因為只有四個量所以可以根據限制條件列兩個方程,再枚舉一下解就可以了。

const int maxn = 5000 + 5;
int n, c[maxn], a[maxn];
vector<int> zero, two, cl, ac, ans1, ans2;

int main() {
    cin >> n;
    getchar();
    rep(i, 1, n) {
        char ch = getchar();
        c[i] = ch - '0';
    }
    getchar();
    rep(i, 1, n) {
        char ch = getchar();
        a[i] = ch - '0';
    }
    rep(i, 1, n) {
        if (c[i] && a[i])   two.push_back(i);
        else if (c[i])  cl.push_back(i);
        else if (a[i])  ac.push_back(i);
        else    zero.push_back(i);
    }
    
    for (int a = 0; a <= zero.size(); a++) {
        int d = a - n / 2 + ac.size() + two.size();
        if (d < 0 || d > two.size() || a + d > n / 2)   continue;
        int tmp = n / 2 - a - d;
        for (int c = 0; c <= cl.size(); c++) {
            if (tmp - c <= ac.size()) {
                for (int i = 0; i < a; i++) cout << zero[i] << " ";
                for (int i = 0; i < tmp - c; i++)   cout << ac[i] << " ";
                for (int i = 0; i < c; i++) cout << cl[i] << " ";
                for (int i = 0; i < d; i++) cout << two[i] << " ";
                return 0;
            }
        }
    }
    printf("-1\n");
    return 0;
}

Codeforces 1138B(列方程枚舉)