Educational Codeforces Round 94 (Rated for Div. 2)【ABCD】

比赛链接:https://codeforces.com/contest/1400

A. String Similarity

题意

给出一个长 $2n-1$ 的二进制串 $s$,构造一个长 $n$ 的字符串,使其与 $s$ 的每个长 $n$ 的子串至少有一处字母相等。

题解

长 $n$ 的子串恰有 $n$ 个,每个子串取一位即可。

代码

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

void solve() {
    int n;
    string s;
    cin >> n >> s;
    for (int i = 0; i < int(s.size()); i += 2)
        cout << s[i];
    cout << "\n";
}

int main() {
    int t;
    cin >> t;
    while (t--) solve();
}

B. RPG Protagonist

题意

有两个容积分别为 $p,f$ 的背包和两种物品:

  • 一种有 $cnt_s$ 个,每个体积为 $s$
  • 一种有 $cnt_w$ 个,每个体积为 $w$

问最多能装多少个物品。

题解

优先装体积较小的一种物品,然后枚举第一个背包所装的数量。

代码

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

void solve() {
    int p, f;
    cin >> p >> f;
    int cnt_s, cnt_w;
    cin >> cnt_s >> cnt_w;
    int s, w;
    cin >> s >> w;
    if (s > w) {
        swap(cnt_s, cnt_w);
        swap(s, w);
    }
    auto func = [](int& cap, int wt, int &cnt) {
        int mi = min(cnt, cap / wt);
        cap -= mi * wt;
        cnt -= mi;
        return mi;
    };
    int ans = 0;
    const int N = min(cnt_s, p / s);
    for (int i = 0; i <= N; i++) {
        int res = i;
        int caps = p - i * s, capw = f;
        int ns = cnt_s - i, nw = cnt_w;
        res += func(caps, w, nw);
        res += func(capw, s, ns);
        res += func(capw, w, nw);
        ans = max(ans, res);
    }
    cout << ans << "\n";
}

int main() {
    int t;
    cin >> t;
    while (t--) solve();
}

C. Binary String Reconstruction

题意

一个二进制串 $s$ 的衍生串 $t$ 按如下规则构造:

  • 如果 $s_{i-x}=1$ 或 $s_{i+x}=1$,$t_i=1$
  • 否则 $t_i=0$

给出 $t$ 和 $x$,试还原 $s$ 。

题解

根据 $t_i=0$ 确定 $s$ 中的 $0$,其余位置均为 $1$ 。

然后判断构造出的 $s$ 是否与 $t$ 矛盾即可。

代码

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

void solve() {
    string s;
    cin >> s;
    int x;
    cin >> x;
    const int N = s.size();
    string ans(N, '1');
    for (int i = 0; i < N; i++) {
        if (s[i] == '0') {
            if (i - x >= 0) ans[i - x] = '0';
            if (i + x < N) ans[i + x] = '0';
        }
    }
    bool ok = true;
    for (int i = 0; i < N; i++) {
        if (s[i] == '1') {
            if (i - x >= 0 and ans[i - x] == '1') continue; 
            if (i + x < N and ans[i + x] == '1') continue;
            ok = false;
        }
    }
    cout << (ok ? ans : "-1") << "\n";
}

int main() {
    int t;
    cin >> t;
    while (t--) solve();
}

D. Zigzags

题意

给出一个大小为 $n$ 的数组 $a$,找出满足:

  • $1 \le i < j < k < l \le n$
  • $a_i=a_k$ and $a_j=a_l$

的四元组 $(i,j,k,l)$ 的数目。

题解

移项得:$(a_i,a_j)=(a_k,a_l)$ 。

即计算数组中相同的二元组的数目。

Tips

因为 $n^2$ 将近 $10^7$,所以使用 map 的 $O(n^2log_n)$ 会超时,可以用二维散列代替。

代码

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

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        --a[i];
    }
    long long ans = 0;
    vector<int> cnt(n * n);
    for (int j = 0; j < n; j++) {
        for (int i = j - 1; i >= 0; i--) {
            ++cnt[a[i] * n + a[j]];
        }
        int k = j + 1;
        for (int l = k + 1; l < n; l++)
            ans += cnt[a[k] * n + a[l]];
    }
    cout << ans << "\n";
}

int main() {
    int t;
    cin >> t;
    while (t--) solve();
}

 

posted @ 2020-09-03 00:15  Kanoon  阅读(250)  评论(0编辑  收藏  举报