【解題報告】Codeforces Round #390 (Div. 2)

題目鏈接


A. Lesha and array splitting(Codeforces 754A)

思路

首先當且僅當數組裏所有的元素都是0時數組無法被劃分爲非零子數組。剩下的情況都是可以的,只要構造出一種合法情況就行了。最簡單的是不對數組做劃分,這種“劃分”方法是合法的當且僅當數組的所有元素的和不爲0。那麼,當數組中所有元素的和爲0時只要將數組劃分成兩段,保證第一段元素之和不爲0即可。

代碼

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

const int maxn = 110;
int flag, n, pos, a[maxn];

int main() {
//  freopen("data.txt", "r", stdin);
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        flag |= a[i];
        // 處理前綴和
        a[i] += a[i-1];
        // 記錄前綴和非0的位置
        if(a[i] != 0) {
            pos = i;
        }
    }
    // 當所有元素都爲0時
    if(flag == 0) {
        puts("NO");
        return 0;
    }
    puts("YES");
    // 當所有元素的和爲0時
    if(a[n] != 0) {
        printf("1\n1 %d\n", n);
        return 0;
    }
    printf("2\n1 %d\n%d %d\n", pos, pos + 1, n);
    return 0;
}

B. Ilya and tic-tac-toe game(Codeforces 754B)

思路

枚舉矩陣內所有的“三連塊”,當其中至少有兩個x且沒有o的時候就輸出YES否則輸出NO。

代碼

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

const int dx[] = {-1, -1, 0, 1};
const int dy[] = {0, 1, 1, 1};
char ch, G[10][10];

bool ok() {
    for(int i = 1; i <= 4; i++) {
        for(int j = 1; j <= 4; j++) {
            // 在橫,縱,主副對角線上找三連子
            for(int k = 0; k < 4; k++) {
                int x1 = i + dx[k];
                int y1 = j + dy[k];
                int x2 = x1 + dx[k];
                int y2 = y1 + dy[k];
                // 若存在兩個以上的x且不存在o
                if(G[i][j] + G[x1][y1] + G[x2][y2] >= 2) {
                    return true;
                }
            }
        }
    }
    return false;
}

int main() {
//  freopen("data.txt", "r", stdin);
    memset(G, -1, sizeof(G));
    for(int i = 1; i <= 4; i++) {
        for(int j = 1; j <= 4; j++) {
            cin >> ch;
            // 方便判斷而做的預處理
            if(ch == 'x') {
                G[i][j] = 1;
            }
            if(ch == '.') {
                G[i][j] = 0;
            }
        }
    }
    puts(ok() ? "YES" : "NO");
    return 0;
}

D. Fedor and coupons(Codeforces 754D)

思路

這種 nk 的題目給人一種揹包的感覺,但由於要維護相交區間,因此不可能用搜索,動態規劃等思想設計出能承受 3×105 數據量的算法。所以想到是不是會存在貪心算法。將區間 [l,r] 按照 l 從小到大的順序排序。遍歷區間,假設當前處理到區間 i ,那麼選擇區間 i 之前的 r 最大的 k 個區間一定是最好的選擇(證明略)。動態地暴力找出 r 最大的區間是相當費時的,此時我們可以用堆(優先隊列)來維護已經處理過的區間的 r ,這樣就可以通過彈出比較小的 r 來維護已經處理過的 r 最大的 k 的區間了。

代碼

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

typedef tuple <int, int, int> t;
const int maxn = 3e5 + 10;
priority_queue <int> pq;
int n, k, l, r, idx, w, ans, L, R, cnt;
t ta[maxn];

int main() {
//  freopen("data.txt", "r", stdin);
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++) {
        scanf("%d%d", &l, &r);
        ta[i] = t(l, r, i);
    }
    // 按照l從小到大排序
    sort(ta + 1, ta + n + 1);
    for(int i = 1; i <= n; i++) {
        tie(l, r, idx) = ta[i];
        // 優先隊列默認爲大頂堆
        pq.push(-r);
        while(pq.size() > k) {
            pq.pop();
        }
        w = -pq.top() - l + 1;
        if(pq.size() == k && ans < w) {
            ans = w;
            L = l;
        }
    }
    R = L + ans - 1;
    printf("%d\n", ans);
    // 若不存在相交的k個區間,則隨便輸出k個區間
    if(ans == 0) {
        for(int i = 1; i <= k; i++) {
            printf("%d ", i);
        }
    } else {
        for(int i = 1; i <= n; i++) {
            tie(l, r, idx) = ta[i];
            // 包括相交區間的區間都要被選擇
            if(l > L || R > r) {
                continue;
            }
            printf("%d ", idx);
            if(++cnt == k) {
                break;
            }
        }
    }
    puts("");
    return 0;
}

(其它題目略)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章