首页 > 其他分享 >Codeforces 821 Div2

Codeforces 821 Div2

时间:2022-09-21 08:46:23浏览次数:64  
标签:std int tt Codeforces long cin ++ 821 Div2

T1:大小为n的数组,最多进行k次操作:下标模k意义下相等则可进行交换。求操作后连续k个元素的最大值
固定最大值的k个连续因素小标为[0,k),现在只需使得它为最大即可,将可交换位置中的最大值交换[0,k)的相应位置即可获得最大值

点击查看代码
#include<bits/stdc++.h>

using i64 = long long;
constexpr int P = 998244353;

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int tt = 1; std::cin >> tt;

    while(tt--){
        int n, k; std::cin >> n >> k;

        std::vector<i64> a(n);
        for(int i = 0; i < n; ++i){
            std::cin >> a[i]; 
        }

        for(int i = k; i < n; ++i){
            if(a[i] > a[i % k]){
                a[i % k] = a[i];
            }
        }
        std::cout << accumulate(a.begin(), a.begin() + k, 0ll) << '\n';
    }

    return 0;
}

T2:n个人比赛,比赛规则,1,2比赛,胜者与3比赛,再胜者与4比赛,一次类推,最后得到冠军。故必定进行n - 1次比赛,游戏结束。
现在给定x, y,表示对于其中任何一个人,此人赢了x场或输了y场,问是否有满足该情况的比赛进程成立。
根据比赛特点,对于某个特定的人,要么连胜,要么直接输,这就意味着必定有(x > 0 && y == 0) || (x == 0 && y == 1)
现在我们假设存在满足情况的比赛进程,从1开始构造,取1在第一场比赛中胜,进行模拟,故[2, 2 + x - 1]均输,2 + x必输,于是再次从2 + x开始,使其为胜,重复1的过程,直到比赛次数大于等于n - 1,若合理,则比赛次数为n - 1,否则大于n
tip:对于构造题,几乎可以认为,只要寻找到一个非常特殊的情况,看其在满足限制条件得情况下是否满足题意来确定是否有满足题意得解,所以确定特殊情况(其特殊在哪里,优势),并构造出该特殊情况就成为重中之重。

点击查看代码
#include<bits/stdc++.h>

using i64 = long long;
constexpr int P = 998244353;

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int tt = 1; std::cin >> tt;

    while(tt--){
        int n, x, y; std::cin >> n >> x >> y;

        if(x < y){
            std::swap(x, y);
        }
        if(x == 0){
            std::cout << -1 << '\n';
            continue;
        }
        if(y){
            std::cout << -1 << '\n';
            continue;
        }
        int now = 1, key = 0; std::vector<int> r;
        while(now <= n){
            r.push_back(now);
            if(key == 0){
                now += x + 1;
                key = 1;
            }else{
                now += x;
            }
        }

        if(now > n + 1){
            std::cout << -1 << '\n';
            continue;
        }else{
            std::vector<int> x(n + 1);
            for(auto xx : r){
                if(xx <= n){
                    x[xx] = xx;
                }
            }
            for(int i = 2; i <= n; ++i){
                if(x[i] == 0){
                    x[i] = x[i - 1];
                }
                std::cout << x[i] << ' ';
            }
            std::cout << '\n';
        }

    }

    return 0;
}

T3:给定大小为n的数组,最多进行n次操作:选择下标为l和r的点,

\[(a[l] + a[r]) \% 2 == 0, a[l] = a[r] \]

\[(a[l] + a[r]) \% 2 == 1, a[r] = a[l] \]

是否存在某种操作序列,使得数组非递减。
构造题,寻找特殊情况(从终点出发,特殊在非递减,我们特殊至数组所有元素相等),现在考虑在该操作下使得其所有元素相等。
先使a[1] == a[n],然后对于(1, n)内点一定存在其中某种操作使得其等于a[1],故该特殊情况符合构造要求

点击查看代码
#include<bits/stdc++.h>

using i64 = long long;
constexpr int P = 998244353;

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int tt = 1; std::cin >> tt;

    while(tt--){
        int n; std::cin >> n;

        std::vector<int> a(n);
        for(int i = 0; i < n; ++i){
            std::cin >> a[i];
        }

        if(n == 1){
            std::cout << 0 << '\n';
            continue;
        }

        std::vector<std::pair<int, int>> q{{0, n - 1}};
        if((a[0] + a[n - 1]) & 1){
            a[n - 1] = a[0];
        }else{
            a[0] = a[n - 1];
        }

        for(int i = 1; i < n - 1; ++i){
            if((a[i] + a[0]) & 1){
                q.push_back({0, i});
            } else{
                q.push_back({i, n - 1});
            }
        }

        std::cout << q.size() << '\n';
        for(auto x : q){
            std::cout << x.first + 1 << ' ' << x.second + 1 << '\n';
        }
    }

    return 0;
}

T4:给定两个二进制字符串a,b,存在操作:在a选择两个位置,同时取反。若位置相邻,代价为x;若位置不相邻,代价为y;问操作后使得a=b的最小代价
有(x >= y)
预处理出不同位置序列,个数为res,若个数为奇数,则不可能使得a = b;
为偶数时:
0.0个最小代价为0
1.2个,若不相邻最小代价为y,否则为min(x, 2 * y)
2.不为两个,则必定存在两两不同位置,使得其不相邻,最小代价为(res / 2 * y)

点击查看代码
#include<bits/stdc++.h>

using i64 = long long;
constexpr int P = 998244353;

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int tt = 1; std::cin >> tt;

    while(tt--){
        i64 n, x, y; std::cin >> n >> x >> y;

        std::string aa, bb; std::cin >> aa >> bb;

        std::vector<int> a(n), b(n);
        for(int i = 0; i < n; ++i){
            a[i] = aa[i] - '0';
            b[i] = bb[i] - '0';
        }

        std::vector<int> p(n); i64 res = 0;
        for(int i = 0; i < n; ++i){
            if(a[i] != b[i]){
                p[i] = 1;
                ++res;
            }
        }
        if(res & 1){
            std::cout << -1 << '\n';
            continue;
        }

        if(res == 2){
            int key = 0;
            for(int i = 1; i < n; ++i){
                if(p[i] && p[i - 1]){
                    key = 1;
                }
            }
            std::cout << (key ? (n == 2 ? x : std::min(x, y * 2)) : y) << '\n';
        }else{
            std::cout << (res / 2 * y) << '\n';
        }

    }

    return 0;
}

T5:题意同T4,但不保证x >= y
分情况讨论:
1.x >= y时同T4
2.x < y时进行考虑,待思考。。。

标签:std,int,tt,Codeforces,long,cin,++,821,Div2
From: https://www.cnblogs.com/iqer/p/16714176.html

相关文章

  • CF 821
    B:n个人比赛,比赛规则,1,2比赛,胜者与3比赛,再胜者与4比赛,一次类推,最后得到冠军。故必定进行n-1次比赛,游戏结束。现在给定x,y,表示对于其中任何一个人,此人赢了x场或输了y场,问......
  • Codeforces Round #821 (Div. 2) - D2. Zero-One (Hard Version)
    区间DPProblem-D2-Codeforces题意给一个长度为\(n(5<=n<=5000)\)的01串,每次操作可选择一个\(l,r(l<r)\),把\(s[l],s[r]\)反转。如果\(l+1==r\),花费为x,否......
  • Codeforces Round #807 (Div. 2) - D. Mark and Lightbulbs
    思维Problem-D-Codeforces题意给两个长度为\(n(3<=n<=2*10^5)\)的01串s与t,求最小操作次数,使s变成t;不存在则输出-1操作为:对于2<=i<=n-1,若\(s_......
  • Codeforces Round #820 (Div. 3) G. Cut Substrings
    DPProblem-G-Codeforces题意给一个长度为\(n(1<=n<=500)\)的主串s,一个长度为\(m(1<=m<=500)\)的模式串t,每次可以将当前的s中与t相同的子串变成一串"."(如......
  • Codeforces Round #821 (Div. 2) ABCD1
    A.ConsecutiveSumhttps://codeforces.com/contest/1733/problem/A题目大意:给定一个长度为n的数组a。最多可以执行k次以下操作:选择两个指数i和j,其中imodk=jmodk......
  • Codeforces Round #821(Div.2) (A-C) 题解
    CodeforcesRound#821(Div.2)(A-C)  A.ConsecutiveSum大致题意给定一组共n个数据,如果俩个数的下标在modk意义下同余,则可以交换a[I]和a[j] ,求操作后一段......
  • Codeforces Round #821 (Div. 2)(持续更新)
    Preface唉LOL打到一半被迫营业开打,感觉这算是第一次自己认真的在线打CF,以我的老年人手速感觉要罚时飞起了10:35开始打到12:10顶不住了睡觉去了,E大概看了个题感觉挺有意思的......
  • Codeforces Round #819 (Div. 1 + Div. 2) and Grimoire of Code Annual Contest 2022
    Preface明天的CF一定打啊(绝对不咕),今天白天补现代作业,英语作业到哭,而且还要准备四六级,每天逼着自己背单词A.MainakandArray不难发现换中间的数不会影响答案,因此操作......
  • Codeforces Round #821 D2
    D2.Zero-One(HardVersion)我们由D1可知当我们的y小于x/2时我们可以用2y来减小相邻的cost那我们考虑要是y比较大的时候我们也可以用多个x来减小cost我们可以轻松的......
  • Codeforces Round #821 (Div. 2)
    CodeforcesRound#821(Div.2)C.ParityShuffleSorting题目大意每次操作可以选择l,r,如果\(a_l+a_r\)是奇数可以让\(a_l=a_r\),否则可以让\(a_l=a_r\),要求使用不超过n......