Codeforces Round #267 (Div. 2) 水了一發 真.記錄

來源:互聯網
上載者:User

標籤:io   os   for   div   sp   log   on   c   amp   

閑著沒事就水了一發DIV2,本來想新註冊一個小小號來著。結果驗證碼一直顯示不出來。於是就用小號做。

結果rank44,但是沒有rating.


以下均不解釋題意了。


A:O(n)腦殘類比。

Code:

#include <cstdio>int main() {    int n, a, b;    scanf("%d", &n);    int res = 0;    while(n--) {        scanf("%d%d", &a, &b);        if (a + 2 <= b)            ++res;    }    printf("%d", res);    return 0;}

B:注意到兩個數相異或後得出的結果的二進位表達中1的位元就是不同的個數。反正怎麼做都行,O(nlogn)水。

Code:

#include <cstdio>#include <cstring>#include <cctype>#include <iostream>#include <algorithm>using namespace std;#define M 1010int a[M];int count(int x) {    int res = 0;    for(; x; x -= x & -x)        ++res;    return res;}int main() {    int n, m, k;    scanf("%d%d%d", &n, &m, &k);    register int i, j;    int S = (1 << n) - 1;    for(i = 1; i <= m + 1; ++i) {        scanf("%d", &a[i]);        a[i] &= S;    }        int ans = 0;    for(i = 1; i <= m; ++i)        if (count(a[i] ^ a[m + 1]) <= k)            ++ans;        printf("%d", ans);        return 0;}

C:顯然是dp,設f[i][j]表示第i段的結尾為j時的最優值,顯然f[i][j]=max{f[i-1][k]+sum[j]-sum[j-m]}(0<=k<=j-m)

不過這樣是O(k*n^2),可能逾時。

我們發現每一階段的轉移能用到的最優狀態都是上一階段的首碼最優值,於是dp時直接記錄下來用來下面的轉移,這樣就不用枚舉了,變為O(k*n)水過。

貌似卡記憶體,用了滾動數組。

#include <cstdio>#include <cstring>#include <cctype>#include <iostream>#include <algorithm>using namespace std;#define N 5010long long a[N];long long dp[2][N], sav[2][N];int main() {    int n, m, num;    scanf("%d%d%d", &n, &m, &num);    register int i, j, k;    for(i = 1; i <= n; ++i) {        scanf("%I64d", &a[i]);        a[i] += a[i - 1];    }        int now = 0, last = 1;    for(i = 1; i <= num; ++i) {        now ^= 1;        last ^= 1;        memset(dp[now], 0, sizeof(dp[now]));        memset(sav[now], 0, sizeof(sav[now]));        for(j = i * m; j <= n; ++j) {            dp[now][j] = sav[last][j - m] + a[j] - a[j - m];            sav[now][j] = max(sav[now][j - 1], dp[now][j]);        }    }        long long res = 0;    for(i = num * m; i <= n; ++i)        res = max(res, dp[now][i]);        printf("%I64d", res);        return 0;}

D:各種坑爹。我們事實上想要找出一個單詞能夠通過一系列變換得到的最小答案是多少,不過可能存在環形轉移,不能直接做。

於是求出SCC後縮點,每個節點的初始答案等於節點所有內部節點的最優值。然後在DAG上dp即可。

反正就是很噁心。

注意答案可能爆int.

#include <cstdio>#include <cstring>#include <cctype>#include <iostream>#include <string>#include <algorithm>#include <map>using namespace std;#define N 100010#define E 100010int sav[N];    map<string, int> M;inline void low(string &s) {    int len = s.length();    for(int i = 0; i < len; ++i)        if (s[i] >= 'A' && s[i] <= 'Z')            s[i] = s[i] - 'A' + 'a';}struct Ans {    int num, len;    Ans(int _num = 0, int _len = 0):num(_num),len(_len){}    bool operator < (const Ans &B) const {        return (num < B.num) || (num == B.num && len < B.len);    }    void get(string &s) {        int res = 0, l = s.length();        for(int i = 0; i < l; ++i)            if (s[i] == 'r')                ++res;        num = res;        len = s.length();    }}S[N * 3], dp[N * 3];int head[N * 3], next[E], end[E];void addedge(int a, int b) {    static int q = 1;    end[q] = b;    next[q] = head[a];    head[a] = q++;}int dfn[N * 3], lowd[N * 3], tclock, stack[N * 3], belong[N * 3], top, num;bool instack[N * 3];void dfs(int x) {    dfn[x] = lowd[x] = ++tclock;    stack[++top] = x;    instack[x] = 1;    for(int j = head[x]; j; j = next[j]) {        if (!dfn[end[j]]) {            dfs(end[j]);            lowd[x] = min(lowd[x], lowd[end[j]]);        }        else if (instack[end[j]])            lowd[x] = min(lowd[x], dfn[end[j]]);    }    if (dfn[x] == lowd[x]) {        dp[++num] = Ans(0x3f3f3f3f, 0);        int i;        while(1) {            i = stack[top--];            belong[i] = num;            instack[i] = 0;            if (S[i] < dp[num])                dp[num] = S[i];            if (dfn[i] == lowd[i])                break;        }    }}    struct Graph {    int head[N * 3], next[E], end[E], ind;    void reset() {        ind = 0;        memset(head, -1, sizeof(head));    }    void addedge(int a, int b) {        int q = ind++;        end[q] = b;        next[q] = head[a];        head[a] = q;    }}NewG;bool Calc[N * 3];void work(int x) {    Calc[x] = 1;    for(int j = NewG.head[x]; j != -1; j = NewG.next[j]) {        if (!Calc[NewG.end[j]])            work(NewG.end[j]);        if (dp[NewG.end[j]] < dp[x])            dp[x] = dp[NewG.end[j]];    }}int main() {    int n;    scanf("%d", &n);        string s, a, b;    register int i, j;    int id = 0;    for(i = 1; i <= n; ++i) {        cin >> s;        low(s);        if (!M[s]) {            M[s] = ++id;            S[id].get(s);        }        sav[i] = M[s];    }        int m;    scanf("%d", &m);    int x, y;    for(i = 1; i <= m; ++i) {        cin >> a >> b;        low(a), low(b);        if (!M[a]) {            M[a] = ++id;            S[id].get(a);        }        x = M[a];        if (!M[b]) {            M[b] = ++id;            S[id].get(b);        }        y = M[b];        if (x != y)            addedge(x, y);    }        for(i = 1; i <= id; ++i)        if (!dfn[i])            dfs(i);        NewG.reset();    for(i = 1; i <= id; ++i)        for(j = head[i]; j; j = next[j])            if (belong[i] != belong[end[j]])                NewG.addedge(belong[i], belong[end[j]]);        for(i = 1; i <= num; ++i)        if (!Calc[i])            work(i);        long long ansnum = 0, anslen = 0;    for(i = 1; i <= n; ++i)        ansnum += dp[belong[sav[i]]].num, anslen += dp[belong[sav[i]]].len;        printf("%I64d %I64d", ansnum, anslen);        return 0;}


E:待填坑。

Codeforces Round #267 (Div. 2) 水了一發 真.記錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.