Codeforces Round #708 (Div. 2)

Codeforces Round #708 (Div. 2)

A - Meximization

贪心, 重复的元素最后输出

int main() {
    IOS;
    for (cin >> _; _; --_) {
        set<int> st; cin >> n; VI a;
        rep (i, 1, n) {
            cin >> m;
            if (st.count(m)) a.pb(m);
            else st.insert(m);
        }
        for (auto &i : st) cout << i << ' ';
        for (auto &i : a) cout << i << ' '; cout << '\n';
    }
    return 0;
}

B - M-arrays

注意到只和余数有关, a[i] = a[i] % m

a[i] == 0 || a[i] + a[i] == m 的直接可以随便拼

对于 a[i] 和 a[m - i] 的, 贪心拼, 一方可以比另一方多一个

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n >> m; k = 0;
        rep (i, 0, m - 1) c[i] = 0;
        rep(i, 1, n) {
            int a; cin >> a;
            ++c[a % m];
        }
        rep (i, 0, m / 2) {
            if (i == 0 || i + i == m) k += !!c[i];
            else {
                int a = abs(c[m-i] - c[i]), mi = min(c[m - i], c[i]);
		if(!(c[m - i] || c[i]))continue;
		if(a <= 1) ++k;
		else k += a;
            }
        }
        cout << k << '\n';
    }
    return 0;
}

C - k-LCM

\(C_2\) 直接 while (k > 3) cout << "1 ", --n, --k; 变成 \(C_1\)

n % 4 == 0的 n/2, n/4, n/4

n % 2 == 0的 2, n - 2 >> 1, n - 2 >> 1

n % 2 == 1的 1, n >> 1, n >> 1

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n >> k;
        while (k > 3) cout << "1 ", --n, --k;
        if (n % 2) cout << "1 " << n / 2 << ' ' << n / 2 << '\n';
        else if (n % 4) cout << "2 " << (n - 2 >> 1) << ' ' << (n - 2 >> 1) << '\n';
        else cout << n / 4 << ' ' << n / 4 << ' ' << n / 2 << '\n';
    }
    return 0;
}

D - Genius

来回条, 每次都比上次跳的远(默认tag[i] != tag[j])

d[i][j] 表示从 i 跳到 j 得分最大值(j <= i), s[i][j] 从 1~i 跳到 j 的分数最大值

则 per (j, i - 1, 1) if (tag[i] ^ tag[j]) umax(d[i][j], d[i][i] + abs(s[i] - s[j])), umax(d[i][i], s[i - 1][j] + abs(s[i] - s[j]))

s[i][j] = max(s[i - 1][j], d[i][j])

空间优化发现循环空间就够了, s数组也能省掉

ll a[N], s[N], d[2][N];
 
int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n;
        rep (i, 1, n) cin >> a[i], d[0][i] = -1e18;
        rep (i, 1, n) cin >> s[i];
        rep (i, 1, n) {
            rep (j, 1, n) d[i & 1][j] = d[i & 1 ^ 1][j]; d[i & 1][i] = 0;
            per (j, i - 1, 1) if (a[i] ^ a[j])
                umax(d[i & 1][j], d[i & 1][i] + abs(s[i] - s[j])),
                umax(d[i & 1][i], d[i & 1 ^ 1][j] + abs(s[i] - s[j]));
            umax(d[i & 1][i], d[i & 1 ^ 1][i]);
        }
        cout << *max_element(d[n & 1], d[n & 1] + n + 1) << '\n';
    }
    return 0;
}
posted @ 2021-03-22 14:40  洛绫璃  阅读(92)  评论(0编辑  收藏  举报