zoukankan      html  css  js  c++  java
  • Codeforces Round #662 (Div. 2)

    Codeforces Round #662 (Div. 2)

    A. Rainbow Dash, Fluttershy and Chess Coloring

    题意: 给一个正方形区域上色,类似于国际象棋:给两种颜色,要求相邻块的颜色不能相同。上色顺序是必须在一个已上色的相邻块上或者边界上上色。求最少上色几次可以完成全部上色?
    题解: 画了画,猜的结论
    代码:

    #include <bits/stdc++.h>
    
    using namespace std;
    
    int n, t;
    
    int main() {
        cin >> t;
        while (t--) {
            cin >> n;
            cout << n / 2 + 1 << endl;
        }
        return 0;
    }
    

    B. Applejack and Storages

    题意: 题意为是我们一开始用n根各种长度的木棒,经过q次增加一根或减少某根长度的木棒,问在每次增减操作后能否用这些木棒构造出一个长方形和正方形,构造的时候只需要用这些木棒构成长方形和正方形的周长部分即可,并且每条边只能放一根木棒.
    题解: 维护长方形和正方形的数目,x维护正方形的数目,y维护减去正方形的数目后长条可以两两配对的数目,然后模拟维护即可
    代码:

    #include <bits/stdc++.h>
    
    using namespace std;
    
    int const N = 1e5 + 10;
    int n, a[N], x, y, m;
    
    int main() {
        cin >> n;
        for (int i = 1, t; i <= n; ++i) {
            scanf("%d", &t);
            a[t]++;
        }
        for (int i = 1; i <= N - 10; ++i) {
            x += a[i] / 4;
            y += a[i] % 4 / 2;
        }
        
        char op[2];
        cin >> m;
        for (int i = 1, t; i <= m; ++i) {
            scanf("%s", op);
            scanf("%d", &t);
            if (op[0] == '+') {
                ++a[t];
                if (a[t] % 4 == 0) x++, --y;
                else if (a[t] % 4 == 2) ++y;
            }
            else {
                --a[t];
                if (a[t] % 4 == 1) --y;
                else if (a[t] % 4 == 3) --x, ++y;
            }
            if (x && (x > 1 || y >= 2)) cout << "YES
    ";
            else cout << "NO
    ";
        }
        return 0;
    }
    

    C. Pinkie Pie Eats Patty-cakes

    题意: 给你n个数字,现在你需要将这些数字按照一定顺序排列后,使得数值相等的数字之间的间距的最小值最大。
    题解: 比赛的时候写了个假二分,卡了一个小时,现在回头想想,二分里面的check函数没法写。但是实际上这道题直接贪心结论O(n)即可。
    以这组数据为例:
    14
    1 1 1 2 2 2 3 3 3 4 4 6 7 8
    把出现最多的数字分别尽可能摊开,然后中间的空隙填补其他的数字,具体见: https://blog.csdn.net/StandNotAlone/article/details/107873256
    最终结果为:1,2,3, 4,6,8 1,2,3, 4,7, 1,2,3
    代码:

    #include <bits/stdc++.h>
    
    using namespace std;
    
    int const N = 2e5 + 10;
    int T, n, a[N], cnt[N];
    
    int main() {
        cin >> T;
        while (T--) {
            memset(cnt, 0, sizeof cnt);
            cin >> n;
            for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), cnt[a[i]]++;
            int maxv_cnt = -1;
            for (int i = 1; i <= n; ++i) maxv_cnt = max(maxv_cnt, cnt[i]);
            vector<int> num;
            for (int i = 1; i <= n; ++i) if (cnt[i] == maxv_cnt) num.push_back(i);
            // int maxv_num_cnt = num.size();
            cout << num.size() - 1 + (n - maxv_cnt * num.size()) / (maxv_cnt - 1) << endl;
        }
        return 0;
    }
    
    // 3
    // 2
    // 0
    // 4
    
    

    参考:

    1. https://www.cnblogs.com/2aptx4869/p/13456091.html
    2. https://blog.csdn.net/StandNotAlone/article/details/107873256
    3. https://www.cnblogs.com/sszz/p/13456556.html
  • 相关阅读:
    四套读写方案
    如何保证ArrayList线程安全
    异常总结<经典例题>
    java.移位运算符
    java反射机制
    面试题:return和finally执行
    Spring_通过注解配置 Bean(1)
    Spring_通过 FactoryBean 配置 Bean
    Spring_通过工厂方法配置 Bean
    Spring_管理 Bean 的生命周期
  • 原文地址:https://www.cnblogs.com/spciay/p/13495881.html
Copyright © 2011-2022 走看看