[Codeforces Educational Round 71]Div. 2

總結

手速場...像我這種沒手速的就直接炸了...html

辣雞 E 題交互,少打了個 ? 調了半個小時...node

到最後沒時間 G 題題都沒看就結束了...結果早上起來被告知是阿狸的打字機...看了看題一毛同樣...c++

提供翻譯造福人類...數組


A. There Are Two Types Of Burgers

Description

題庫連接spa

你有 \(b\) 片面包,\(p\) 塊牛肉,\(f\) 塊雞肉。兩片面包和一塊肉能夠組成一個漢堡,兩種漢堡的價錢分別是 \(h,c\)。求最大收益。\(t\) 組詢問。翻譯

\(1\leq t,b,p,f,h,c\leq 100\)code

Solution

不用貪心,直接枚舉多少個雞肉漢堡多少個牛肉漢堡,判斷面包是否夠用便可。htm

Code

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

int t, b, p, f, h, c, ans; 

int main() {
    scanf("%d", &t);
    while (t--) {
        ans = 0;
        scanf("%d%d%d%d%d", &b, &p, &f, &h, &c);
        for (int i = 0; i <= p; i++)
            for (int j = 0; j <= f; j++)
                if (i+j <= b/2)
                    ans = max(ans, i*h+c*j);
        printf("%d\n", ans);
    }
    return 0;
}

B. Square Filling

Description

題庫連接blog

給你一個 \(n\times m\) 的空白棋盤,每次你能夠選擇一個 \(2\times 2\) 的矩陣染色。問你是否能染成目標狀態,輸出方案(不用求最優解)。排序

\(1\leq n,m\leq 50\)

Solution

對於目標棋盤中的某一塊黑色,必定只能由四種方法使他變黑。

那麼對於每一個黑塊,直接枚舉染它的矩陣的左上角,看這四種方案中是否有可行的。即這個 \(2\times 2\) 的矩陣在目標棋盤中都爲黑色,若可行,標記輸出;若都不可行,顯然無解。

Code

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

int n, m, a[55][55], tmp; 
int ans[55][55], tot;

void noans() {
    puts("-1"); exit(0);
}
bool color(int x, int y) {
    if (x == n || y == m || x == 0 || y == 0) return false;
    if (!a[x][y] || !a[x+1][y] || !a[x][y+1] || !a[x+1][y+1]) return false;
    ans[x][y] = 1;
    return true;
}
int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            scanf("%d", &a[i][j]);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (a[i][j])
                if (color(i-1, j-1) || color(i, j-1) || color(i-1, j) || color(i, j));
                else noans();
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            tot += ans[i][j];
    printf("%d\n", tot);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (ans[i][j])
                printf("%d %d\n", i, j);
    return 0;
}

C. Gas Pipeline

Description

題庫連接

一條長度爲 \(n\) 的路,讓你在路上架管道,管道高度 \(>\) 馬路高度,馬路高度只爲 \(0\)\(1\),管道高度只能爲 \(1\)\(2\)。而且管道開始和結束高度爲 \(1\)

若是在 \(x\) 處要修改管道高度,那麼花費管道長度爲 \(2\),不然爲 \(1\)。同時管道須要柱子支撐,長度爲 \(n\) 的管道須要 \(n+1\) 根柱子,柱子長度取決於相鄰的部分的最大高度。

單位長度管道花費 \(a\),單位長度柱子花費爲 \(b\)。求最小花費。

\(2\leq n\leq 2\cdot 10^5,1\leq a,b\leq 10^8\)

Solution

\(f_{i,0/1}\) 表示修管道 \(1\sim i\),且 \(i\) 處管道高度爲 \(1/2\) 的最小花費。

轉移只需考慮第 \(i-1\) 個位置管道高度便可。注意,若是 \(i\) 處馬路高度爲 \(1\),那麼直接將 \(f_{i,0}\) 賦值爲 \(\infty\)

答案爲 \(f_{n,0}\)

Code

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5+5;
const ll inf = 2e15;

int t, n, a, b; 
char ch[N];
ll f[N][2];

int main() {
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d%d", &n, &a, &b);
        scanf("%s", ch+1);
        f[1][0] = a+2ll*b;
        f[1][1] = 2ll*a+2ll*b;
        for (int i = 2; i <= n; i++) {
            f[i][0] = min(f[i-1][0]+a+b, f[i-1][1]+2ll*a+2ll*b);
            f[i][1] = min(f[i-1][1]+a+2ll*b, f[i-1][0]+2ll*a+2ll*b);
            if (ch[i] == '1') f[i][0] = inf;
        }
        printf("%I64d\n", f[n][0]);
    }
    return 0;
}

D. Number Of Permutations

Description

題庫連接

給你 \(n\) 個有序二元組。定義一個「壞的」二元組序列指,存在一個維度單調不降。

將這 \(n\) 個二元組全排列,問有多少種方法使它變得「不壞」,對 \(998244353\) 取模。

\(1\leq n\leq 3\cdot 10^5\)

Solution

考慮間接法。

對於單一維度,咱們將其排序,若連續 \(x\) 個數字相同,那麼這 \(x\) 個位置能夠任意排,貢獻 \(x!\) 的方案數。

兩個維度處理完以後,咱們來加上多減去的部分。不過前提是原二元組序列可以排成兩個維度均單調不降的序列。

那麼若是排序後連續 \(x\) 個二元組相同,那麼這 \(x\) 個位置能夠任意排,貢獻 \(x!\) 的方案數。

Code

#include <bits/stdc++.h>
using namespace std;
const int yzh = 998244353, N = 3e5+5; 

int n, fac[N], ca[N], cb[N], ans, tmp, cnt = 1;
struct node {
    int a, b;
    bool operator < (const node &t) const {
        return a == t.a ? b < t.b : a < t.a;    
    }
} x[N];

int main() {
    scanf("%d", &n); fac[0] = 1;
    for (int i = 1; i <= n; i++) {
        scanf("%d%d", &x[i].a, &x[i].b);
        fac[i] = 1ll*i*fac[i-1]%yzh;
        ca[x[i].a]++, cb[x[i].b]++;
    }
    ans = fac[n], tmp = 1;
    for (int i = 1; i <= n; i++)
        tmp = 1ll*tmp*fac[ca[i]]%yzh;
    (ans -= tmp) %= yzh;
    tmp = 1;
    for (int i = 1; i <= n; i++)
        tmp = 1ll*tmp*fac[cb[i]]%yzh;
    (ans -= tmp) %= yzh;
    sort(x+1, x+n+1);
    for (int i = 1; i <= n; i++)
        if (x[i].b < x[i-1].b) {printf("%d\n", (ans+yzh)%yzh); return 0; }
    tmp = 1;
    for (int i = 1; i <= n+1; i++)
        if (x[i].a == x[i-1].a && x[i].b == x[i-1].b) ++tmp;
        else {
            cnt = 1ll*cnt*fac[tmp]%yzh;
            tmp = 1;
        }
    (ans += cnt) %= yzh;
    printf("%d\n", (ans+yzh)%yzh);
    return 0;
}

E. XOR Guessing

Description

這是一道交互題。

題庫連接

交互庫先生成一個數 \(x\),你須要猜出 \(x\) 是多少。

你能夠詢問兩次,你每次給出 \(100\) 個整數,這些整數範圍和 \(x\) 相同,交互庫會隨機選出 \(100\) 個數中的一個,與 \(x\) 異或後返回。注意,全部詢問的數(共 \(200\) 個)不能重複。詢問兩次後輸出結果。

\(x\in[0,2^{14}-1]\)

Solution

可知 \(x\) 是位數爲 \(14\) 的二進制數字(高位 \(0\) 補齊)。那麼兩次詢問分別肯定低的 \(7\) 位和高 \(7\) 位便可。

肯定低的 \(7\) 位時,只需讓給出的 \(100\) 個數低的 \(7\) 位均爲 \(0\) 便可,由於 \(2^7=128>100\),必定能選出 \(100\) 個數。只需取返回值的低的 \(7\) 位,這和真實值的低的 \(7\) 位時相等的。

高位同理。

Code

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

int main() {
    int a = (1<<7)-1, tmp, ans = 0;
    int b = (1<<14)-1-a;
    printf("? ");
    for (int i = 1; i <= 100; i++)
        printf("%d%c", (i<<7), " \n"[i == 100]);
    fflush(stdout);
    scanf("%d", &tmp);
    ans += (tmp&a);
    printf("? ");
    for (int i = 1; i <= 100; i++)
        printf("%d%c", i, " \n"[i == 100]);
    fflush(stdout);
    scanf("%d", &tmp);
    ans += (tmp&b);
    printf("! %d\n", ans);
    fflush(stdout);
    return 0;
}

F. Remainder Problem

Description

題庫連接

讓你維護長度爲 \(500000\) 的序列,支持 \(q\) 次操做:

  • 單點加;
  • 詢問:\(500000\) 個位置模 \(x\)\(y\) 的位置的值的和。

\(1\leq q\leq 500000\)

Solution

發現 \(500000^{1.5}\) 大概等於三億多...發現時限 \(4s\),就喜聞樂見的分塊了。

維護數組 \(cal[x][y]\),表示模 \(x\)\(y\) 的位置的值的和。只需開到 \(\sqrt{500000}\) 的大小就好了。

較大的值直接在原數組上枚舉。

修改和詢問複雜度是 \(O(\sqrt{500000})\) 的,查表的話是 \(O(1)\) 的。

Code

#include <bits/stdc++.h>
using namespace std;
const int N = 500000+5;

int cal[710][710], a[N], q, t, x, y;

int main() {
    scanf("%d", &q);
    while (q--) {
        scanf("%d%d%d", &t, &x, &y);
        if (t == 1) {
            a[x] += y;
            for (int i = 1; i <= 709; i++)
                cal[i][x%i] += y; 
        } else {
            if (x <= 709) printf("%d\n", cal[x][y]);
            else {
                int ans = 0;
                for (int i = 0; i*x+y <= 500000; i++)
                    ans += a[i*x+y];
                printf("%d\n", ans); 
            }
        }
    }
    return 0;
}

G. Indie Album

題庫連接

生成 \(n\) 個字符串。生成方式爲

  • 新取一個字符;
  • 將前面某個字符串複製後,在末尾新添一個字符。

字符集爲小寫字母。\(q\) 組詢問,問串 \(t\) 在某個串中出現了多少次。

\(1\leq n,q,\sum|t|\leq 4\cdot 10^5\)

Solution

[NOI 2011]阿狸的打字機

Code

[NOI 2011]阿狸的打字機

我太懶了...

相關文章
相關標籤/搜索