Codeforces Round #713 (Div. 3)

Codeforces Round #713 (Div. 3)

A - Spy Detected!

int main() {
    IOS;
    for (cin >> _; _; --_) {
        int a, b, c; cin >> n >> a >> b >> c;
        if (a == b && a != c) k = 3;
        else if (a == c && a != b) k = 2;
        else if (b == c) k = 1, a = b;
        rep (i, 4, n) {
            cin >> b;
            if (b != a) k = i;
        }
        cout << k << '\n';
    }
    return 0;
}

B - Almost Rectangle

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n; PII a(0, 0), b(0, 0);
        rep (i, 1, n) rep (j, 1, n) {
            cin >> s[i][j];
            if (s[i][j] == '*') {
                if (a == PII{0, 0}) a = { i, j };
                else b = { i, j };
            }
        }
        if (a.fi == b.fi && a.fi + 1 <= n) s[a.fi + 1][a.se] = s[b.fi + 1][b.se] = '*';
        else if (a.fi == b.fi) s[a.fi - 1][a.se] = s[b.fi - 1][b.se] = '*';
        else if (a.se == b.se && a.se + 1 <= n) s[a.fi][a.se + 1] = s[b.fi][b.se + 1] = '*';
        else if (a.se == b.se) s[a.fi][a.se - 1] = s[b.fi][b.se - 1] = '*'; 
        else s[a.fi][b.se] = s[b.fi][a.se] = '*';
        rep (i, 1, n) { rep (j, 1, n) cout << s[i][j]; cout << '\n'; }
    }
    return 0;
}

C - A-B Palindrome

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> m >> n >> s; bool f = n + m == s.size();
        rep (i, 0, s.size() - 1 >> 1) {
            if (s[i] == '1' && s[s.size() - i - 1] == '1') n -= 2;
            else if (s[i] == '1' && s[s.size() - i - 1] == '?') n -= 2, s[s.size() - i - 1] = '1';
            else if (s[i] == '?' && s[s.size() - i - 1] == '1') n -= 2, s[i] = '1';
            else if (s[i] == '0' && s[s.size() - i - 1] == '0') m -= 2;
            else if (s[i] == '0' && s[s.size() - i - 1] == '?') m -= 2, s[s.size() - i - 1] = '0';
            else if (s[i] == '?' && s[s.size() - i - 1] == '0') m -= 2, s[i] = '0';
            else if (s[i] != s[s.size() - i - 1]) f = 0;
            if (i == s.size() - i - 1 && s[i] == '1') ++n;
            else if (i == s.size() - i - 1 && s[i] == '0') ++m;
        }
        if (!f || (n & m & 1) || min(n, m) < 0) { cout << "-1\n"; continue; }
        rep (i, 0, s.size() - 1 >> 1) if (s[i] == '?') {
            if (n >= 2) s[i] = s[s.size() - i - 1] = '1', n -= 2;
            else if (m >= 2) s[i] = s[s.size() - i - 1] = '0', m -= 2;
            else if (n == 1) s[i] = '1';
            else s[i] = '0';
        }
        cout << s << '\n';
    }
    return 0;
}

D - Corrupted Array

排序, 判断是否存在就行

注意会爆 int

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n; set<ll> st; ll s = 0;
        rep (i, 1, n + 2) cin >> b[i]; sort(b + 1, b + 3 + n);
        rep (i, 1, n) s += b[i], st.insert(b[i]);
        if (s == b[n + 1] || s == b[n + 2]) rep (i, 1, n) cout << b[i] << char(" \n"[i == n]);
        else if (st.count(s + b[n + 1] - b[n + 2])) {
            rep (i, 1, n + 1) if (b[i] == s + b[n + 1] - b[n + 2]) { b[i] = b[n + 1]; break; }
            rep (i, 1, n) cout << b[i] << char(" \n"[i == n]);
        }
        else cout << "-1\n";
    }
    return 0;
}

E - Permutation by Sum

贪心即可, 先判断存不存在

再把\([l, r]\)区间设为\([n - r + l - 1, n]\)

贪心从高到底减小数字即可, 用set维护

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> k >> n >> m >> t; set<int> st; int s = (k * 2 - m + n) * (m - n + 1) >> 1;
        if (t > s || t < (m - n + 2) * (m - n + 1) >> 1) { cout << "-1\n"; continue; }
        rep(i, 1, k - m + n - 1) st.insert(i);
        rep(i, n, m) a[i] = k - i + n;
        rep(i, n, m) {
            if (s == t) break;
            auto it = st.lower_bound(a[i] - s + t);
            if (it == st.end()) continue;
            st.insert(a[i]); s -= a[i] - *it; a[i] = *it; st.erase(it);
        }
        rep(i, 1, n - 1) a[i] = *st.begin(), st.erase(st.begin());
        rep(i, m + 1, k) a[i] = *st.begin(), st.erase(st.begin());
        rep(i, 1, k) cout << a[i] << char(" \n"[i == k]);
    }
    return 0;
}

F - Education

每升一级, 去一次最小值

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n >> m; k = 0; ll ans = 1e9, d = 0;
        rep (i, 1, n) cin >> a[i];
        rep (i, 1, n - 1) cin >> b[i];
        rep (i, 1, n) {
            umin(ans, (m <= k ? 0 : (m - k - 1) / a[i] + 1) + d);
            int need = b[i] <= k ? 0 : (b[i] - k - 1) / a[i] + 1;
            k += a[i] * need - b[i]; d += need + 1;
        }
        cout << ans << '\n';
    }
    return 0;
}

G - Short Task

\(f(i)\)表示\(i\)的因子和,

发现\(f(i \times j) = f(i) \times f(j), gcd(i, j) = 1\) 故, \(f(i)\)为积性函数

那么显然能用线性筛来筛

\(gcd(i, j) != 1\)时, 先把\(gcd\)抽出来就好了

即设\(g(i)\)表示\(i\)的最小因子的次方前缀和\(g(i) = p^0 + p^1 + p^2 + ... + p^k, p^k \leqslant i\)

int pri[N], tot, st[N];
ll f[N], g[N];
 
void init(int n) {
    f[1] = g[1] = 1; st[1] = 1;
    rep(i, 2, n) {
        if (!f[i]) {
            pri[++tot] = i, f[i] = g[i] = 1 + i;
            umin(st[f[i]], i);
        }
        for (int j = 1; j <= tot && pri[j] <= n / i; ++j) {
            g[i * pri[j]] = i % pri[j] ? 1 + pri[j] : g[i] * pri[j] + 1;
            f[i * pri[j]] = i % pri[j] ? f[i] * f[pri[j]] : f[i] / g[i] * g[i * pri[j]];
            if (f[i * pri[j]] <= 1e7) umin(st[f[i * pri[j]]], i * pri[j]);
            if (i % pri[j] == 0) break;
        }
    }
}
 
int main() {
    IOS; memset(st, 0x3f, sizeof st); init(1e7);
    for (cin >> _; _; --_) {
        cin >> n;
        cout << (st[0] == st[n] ? -1 : st[n]) << '\n';
    }
    return 0;
}
posted @ 2021-04-12 16:45  洛绫璃  阅读(83)  评论(0编辑  收藏  举报