zoukankan      html  css  js  c++  java
  • Codeforces Round #684 (Div. 2)【ABC1C2】

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

    A. Buy the String

    题解

    枚举字符串中 (0)(1) 的个数即可。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int t;
        cin >> t;
        while (t--) {
            int n, c0, c1, h;
            cin >> n >> c0 >> c1 >> h;
            string s;
            cin >> s;
            int cnt[2] = {};
            for (char c : s) ++cnt[c - '0'];
            int ans = INT_MAX;
            for (int i = 0; i <= n; i++) {
                ans = min(ans, i * c0 + (n - i) * c1 + h * abs(i - cnt[0]));
            }
            cout << ans << "
    ";
        }
        return 0;
    }
    

    B. Sum of Medians

    题解

    贪心,先把较小的数填入每个数组的前 (lceil frac{n}{2} ceil - 1) 个元素,然后依次填完每个数组即可。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int t;
        cin >> t;
        while (t--) {
            int n, k;
            cin >> n >> k;
            vector<vector<int>> a(k);
            for (int i = 0, j = 0; i < n * k; i++) {
                int x;
                cin >> x;
                a[j].push_back(x);
                if (a[j].size() + 1 == (n + 1) / 2) ++j;
                if (a[j].size() == n) ++j;
                if (j == k) j = 0;
            }
            long long ans = 0;
            for (int i = 0; i < k; i++) {
                ans += a[i][(n + 1) / 2 - 1];
            }
            cout << ans << "
    ";
        }
        return 0;
    }
    

    C1. Binary Table (Easy Version)

    题解

    依次操作每个 (2 imes 2) 方阵即可,方阵中 (1) 的个数的规律为:4 -> 1 -> 2 -> 3 -> 0 。

    代码

    见C2。

    C2. Binary Table (Hard Version)

    题解

    依次把所有 (1) 都挤到右下角的 (2 imes 2) 的方阵中,然后操作一下该方阵即可。

    证明

    除右下角的方阵外最多操作 (n imes m - 4) 次,右下角的方阵最多操作 (4) 次,所以最多操作 (n imes m) 次。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int t;
        cin >> t;
        while (t--) {
            int n, m;
            cin >> n >> m;
            vector<string> MP(n);
            for (auto &x : MP) cin >> x;
            vector<pair<int, int>> v;
            auto op = [&](int x, int y) {
                v.emplace_back(x, y);
                MP[x][y] = MP[x][y] == '0' ? '1' : '0';
            };
            for (int i = 0; i < n - 2; i++) {
                for (int j = 0; j < m; j++) {
                    if (MP[i][j] == '1') {
                        op(i, j);
                        op(i + 1, j);
                        if (j == m - 1) op(i + 1, j - 1);
                        else op(i + 1, j + 1);
                    }
                }
            }
            for (int j = 0; j + 2 < m; j++) {
                for (int i = n - 2; i < n; i++) {
                    if (MP[i][j] == '1') {
                        op(i, j);
                        op(i, j + 1);
                        if (i == n - 2) op(i + 1, j + 1);
                        if (i == n - 1) op(i - 1, j + 1);
                    }
                }
            }
            auto cal = [&](int x, int y) {
                string s;
                s += MP[x][y];
                s += MP[x][y + 1];
                s += MP[x + 1][y];
                s += MP[x + 1][y + 1];
                for (int tot_1 = count(s.begin(), s.end(), '1'); tot_1 != 0; ) {
                    if (tot_1 == 1) {
                        int cnt_0 = 0;
                        for (int i = 0; i < 4; i++) {
                            if (s[i] == '1') {
                                v.emplace_back(x + (i >= 2), y + (i == 1 or i == 3));
                                s[i] = '0';
                            } else if (cnt_0 < 2) {
                                v.emplace_back(x + (i >= 2), y + (i == 1 or i == 3));
                                s[i] = '1';
                                ++cnt_0;
                            }
                        }
                    } else if (tot_1 == 2) {
                        int cnt_1 = 0;
                        for (int i = 0; i < 4; i++) {
                            if (s[i] == '1') {
                                if (cnt_1 < 1) {
                                    v.emplace_back(x + (i >= 2), y + (i == 1 or i == 3));
                                    s[i] = '0';
                                    ++cnt_1;
                                }
                            } else {
                                v.emplace_back(x + (i >= 2), y + (i == 1 or i == 3));
                                s[i] = '1';
                            }
                        }
                    } else if (tot_1 == 3) {
                        for (int i = 0; i < 4; i++) {
                            if (s[i] == '1') {
                                v.emplace_back(x + (i >= 2), y + (i == 1 or i == 3));
                                s[i] = '0';
                            }
                        }
                    } else if (tot_1 == 4) {
                        int cnt_1 = 0;
                        for (int i = 0; i < 4; i++) {
                            if (s[i] == '1') {
                                v.emplace_back(x + (i >= 2), y + (i == 1 or i == 3));
                                s[i] = '0';
                                if (++cnt_1 == 3) break;
                            }
                        }
                    }
                    tot_1 = count(s.begin(), s.end(), '1');
                }
                for (int i = 0; i < 4; i++) {
                    int nx = x + (i >= 2);
                    int ny = y + (i == 1 or i == 3);
                    MP[nx][ny] = s[i];
                }
            };
            cal(n - 2, m - 2);
            cout << v.size() / 3 << "
    ";
            int cnt = 0;
            for (auto [x, y] : v) {
                cout << x + 1 << ' ' << y + 1 << ' ';
                if (++cnt % 3 == 0) cout << "
    ";
            }
        }
        return 0;
    }
    
  • 相关阅读:
    多个类定义attr属性重复的问题:Attribute "xxx" has already been defined
    好用的批量改名工具——文件批量改名工具V2.0 绿色版
    得到ImageView中drawable显示的区域的计算方法
    得到view坐标的各种方法
    实现类似于QQ空间相册的点击图片放大,再点后缩小回原来位置
    Material Designer的低版本兼容实现(五)—— ActivityOptionsCompat
    Android 自带图标库 android.R.drawable
    解决 Attempting to destroy the window while drawing!
    解决Using 1.7 requires compiling with Android 4.4 (KitKat); currently using API 4
    Material Designer的低版本兼容实现(四)—— ToolBar
  • 原文地址:https://www.cnblogs.com/Kanoon/p/13997583.html
Copyright © 2011-2022 走看看