Codeforces Round #626 (Div. 2)(部分)

鏈接:https://codeforces.com/contest/1323
來源:Codeforces

A. Even Subset Sum Problem(簽到)

  思路:讓從數組中選擇一些數字使得他們的和爲偶數。如果沒有這樣的組合,則輸出 “-1”。直接判斷數組中是否含有偶數,如果有就直接輸出其下標,否則就判斷數組中是否只有一個數字,如果是輸出 “-1”,否則輸出前兩個數的下標。

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 1e2 + 10;
int a[maxn], ans[maxn];

int main() {
    int T; scanf("%d", &T);
    while (T--) {
        int n; scanf("%d", &n);
        for (int i = 0; i < n; i ++) scanf("%d", &a[i]);
        bool flag = false;
        for (int i = 0; i < n; i ++) {
            if (a[i] % 2 == 0 ) {
                puts("1");
                printf("%d\n", i + 1);
                flag = true; break;
            }
        }
        if (flag) continue;
        if (n == 1) puts("-1");
        else printf("2\n1 2\n");
    }
    return 0;
}

B. Count Subrectangles(思維)

  思路:兩個數組可以構成一個矩陣,讓找到這個矩陣中面積爲 kk 的子矩陣有多少個(子矩陣中的元素必須都是一)。剛開始一直在處理連續 xx11 的個數。今天突然想到可以直接求一個前綴和(這裏是指每一塊連續的 11 的前綴)。然後預處理出來 kk 的所有因子,判斷 aa 中有多少個 sumsum 是大於當前因子 xx的,就表示連續 xx11總共有多少個,對於 bb 也是如此。

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 4e4 + 10;
int a[maxn], b[maxn], suma[maxn], sumb[maxn], ans[maxn];

int cal(int k) {
    int len = 0;
    for (int i = 1; i * i <= k; i ++) {
        if (k % i == 0) {
            ans[len ++] = i;
            if (i * i != k) ans[len ++] = k / i;
        }
    }
    return len;
}

int main() {
    int n, m, k; scanf("%d%d%d", &n, &m, &k);
    for (int i = 1; i <= n; i ++) {
        scanf("%d", &a[i]);
        if (a[i] == 0) suma[i] = 0;
        else suma[i] = suma[i - 1] + 1;
    }
    for (int i = 1; i <= m; i ++) {
        scanf("%d", &b[i]);
        if (b[i] == 0) sumb[i] = 0;
        else sumb[i] = sumb[i - 1] + 1;
    }
    int len = cal(k);
    ll res = 0;
    //for (int i = 0; i < len; i ++) cout << ans[i] << " "; cout << endl;
    for (int i = 0; i < len; i ++) {
        int a1 = ans[i], b1 = k / ans[i], cnta = 0, cntb = 0;
        for (int j = 1; j <= n; j ++) {
            if (suma[j] >= a1) cnta ++;
        }
        for (int j = 1; j <= m; j ++) {
            if (sumb[j] >= b1) cntb ++;
        }
        res += 1ll * cnta * cntb;
    }
    printf("%lld", res);
    return 0;
}

C. Unusual Competitions(貪心)

  思路:計算把給出的字符串變成合法的字符串需要的最小花費。我們用一個棧先進行括號匹配,並且記錄棧中左括號和右括號的數量。如果括號匹配就彈出棧,否則就放進棧中。然後判斷棧內兩種括號的數量是否相等,如果相等表示可以變成和法的序列,花費就是 endPosstartPosendPos - startPos。重複這樣的操作,如果最後棧中還有剩餘說明不可能變成合法的序列,否則就可以變成合法的序列。

#include<bits/stdc++.h>
using namespace std;

const int maxn = 1e6 + 10;
char str[maxn];

struct Node {
    char a;
    int pos;
}node[maxn];

stack<Node> s;

int main() {
    int n; scanf("%d", &n);
    scanf("%s", str);
    int ans_z = 0, ans_y = 0, ans = 0;
    for (int i = 0; i < n; i ++) {
        if (str[i] == '(') {
            ans_z ++;
            s.push({str[i], i});
        } else {
            ans_y ++;
            if (s.size()) {
                if (s.top().a == '(') {
                    ans_z --; ans_y --;
                    s.pop();
                }
                else s.push({str[i], i});
            } else {
                s.push({str[i], i});
            }
        }
        if (ans_z && ans_z == ans_y) {
            ans_z = ans_y = 0;
            while (s.size() > 1) s.pop();
            int pos = s.top().pos; s.pop();
            ans += i - pos + 1;
        }
    }
    if (!s.empty()) puts("-1");
    else printf("%d\n", ans);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章