zoukankan      html  css  js  c++  java
  • 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 << '
    ';
        }
        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 << '
    ';
        }
        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 << '
    ';
            else if (n % 4) cout << "2 " << (n - 2 >> 1) << ' ' << (n - 2 >> 1) << '
    ';
            else cout << n / 4 << ' ' << n / 4 << ' ' << n / 2 << '
    ';
        }
        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) << '
    ';
        }
        return 0;
    }
    
  • 相关阅读:
    天梯赛5-12 愿天下有情人都是失散多年的兄妹 【dfs】
    poj2718 Smallest Difference【贪心】
    HDU problem 5635 LCP Array【思维】
    codeforces 782C Andryusha and Colored Balloons【构造】
    HDU 4278 Faulty Odometer【进制转换】
    codeforces B. The Meeting Place Cannot Be Changed【二分】
    POJ 3264 Balanced Lineup 【线段树】
    HDU 1850
    CodeForces-714C
    HDU Problem 1247 Hat's Words 【字典树】
  • 原文地址:https://www.cnblogs.com/2aptx4869/p/14565922.html
Copyright © 2011-2022 走看看