首页 > 其他分享 >Codeforces Round 878 (Div. 3)

Codeforces Round 878 (Div. 3)

时间:2023-12-02 22:33:05浏览次数:61  
标签:cnt 878 int ++ Codeforces -- y1 Div y2

Codeforces Round 878 (Div. 3)

A:ABC

A. Cipher Shifer

题意:在自身后面添加一个字母,但是不能添加自身

思路:找到第二个与自身相符的就再找

#include <bits/stdc++.h>

using namespace std;
const int MAX = 110;
char a[MAX];

void solve() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    int num = 0;
    for (int i = 1; i < n; i++) {
        if (a[num] == a[i]) {
            cout << a[num];
            num = ++i;
        }
    }
    cout << "\n";
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

B. Binary Cafe

题意:求一个数n用<2^k的二进制数的表示方法

思路:每个数都有一个独立的二进制来表示,每个二进制表示都可记作一种方案(空集是一种单独的方案)int>1e9所以k>32直接输出n+1就行。其他的再判断就行

#include <bits/stdc++.h>

using namespace std;
#define int long long

int qmi(int a, int b) {
    int res = 1;
    while (b) {
        if (b & 1) {
            res = res * a;
        }
        a *= a;
        b >>= 1;
    }
    return res;
}

void solve() {
    int n, k;
    cin >> n >> k;
    if (k >= 32) {
        cout << n + 1 << "\n";
    } else {
        int res = qmi(2, k) - 1;
        if (res > n) cout << n + 1 << "\n";
        else {
            cout << res + 1 << "\n";
        }
    }
}

signed main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

C. Ski Resort

题意:一共n天,要连续去k天,这k天中a[i]<=q

思路:用一个记录有多少天连续满足这个条件,然后如果cnt>=k那么急可以在这几天去

#include <bits/stdc++.h>

using namespace std;
#define int long long
const int MAX = 2e5 + 10;
bool t[MAX];
int dp[MAX];

void solve() {
    int n, k, q;
    cin >> n >> k >> q;
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        if (x <= q) {
            t[i] = true;
        } else t[i] = false;
    }
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        if (t[i]) cnt++;
        else cnt = 0;
        dp[i] = dp[i - 1];
        if (cnt >= k) {
            dp[i] += cnt - k + 1;
        }
    }
    cout << dp[n] << "\n";

}

signed main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

D. Wooden Toy Festival

题意:有x,y,z三个数,要让abs(a[i]-x)+abs(a[i]-y)+abs(a[i]-z)最小

思路:二分答案,是否有满足大于当前二分答案的两倍的个数大于3(大致分成三份),如果有就在大于当前二分的值的区间,否则就左边找

#include <bits/stdc++.h>

using namespace std;
#define int long long
const int MAX = 2e5 + 10;
int a[MAX];
int n;

bool check(int k) {
    int num = 0;
    int x = a[1];
    for (int i = 1; i <= n; i++)
        if (abs(a[i] - x) > k * 2) {
            x = a[i];
            num++;
        }
    if (num >= 3) return false;
    return true;
}

void solve() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    if (n <= 3) {
        cout << "0" << endl;
        return;
    }
    sort(a + 1, a + n + 1);
    int l = 0, r = a[n];
    int mid;
    while (l <= r) {
        mid = (l + r) >> 1;
        if (check(mid)) r = mid - 1;
        else l = mid + 1;
    }
    cout << l << "\n";
}

signed main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

E. Character Blocking

总结:大致思路有,但是没想到用map来存麻痹时间,题刷少了

题意:有三种操作:1.在 t 秒内屏蔽两个字符串中位于 pos位置的字符

2.交换两个未屏蔽的字符(注:不一定是不同字符串的)

3.确定两个字符在查询是是否相等

思路:找一个cnt确定有几个数是坏(没被屏蔽并且不相等),用map来储存屏蔽时间

#include <bits/stdc++.h>

using namespace std;

void solve() {
    string a, b;
    cin >> a >> b;
    a = " " + a;
    b = " " + b;
    int t, m;
    cin >> t >> m;
    int cnt = 0;
    map<int, vector<int>> mp;
    for (int i = 1; i < a.size(); i++) {
        cnt += a[i] != b[i];
    }
    for (int i = 1; i <= m; i++) {//屏蔽时间问题
        for (auto &x: mp[i]) {
            if (a[x] != b[x]) cnt++;
        }
        int op;
        cin >> op;
        if (op == 1) {
            int x;
            cin >> x;
            cnt -= (a[x] != b[x]);
            mp[i + t].push_back(x);//i+t是解除屏蔽的时间
        } else if (op == 2) {
            int x1, y1, x2, y2;
            cin >> x1 >> y1 >> x2 >> y2;
            if (x1 == 1 && x2 == 1) {
                if (a[y1] != b[y1]) cnt--;
                if (a[y2] != b[y2]) cnt--;
                swap(a[y1], a[y2]);
                if (a[y1] != b[y1]) cnt++;
                if (a[y2] != b[y2]) cnt++;
            } else if (x1 == 1 && x2 == 2) {
                if (a[y1] != b[y1]) cnt--;
                if (a[y2] != b[y2]) cnt--;
                swap(a[y1], b[y2]);
                if (a[y1] != b[y1]) cnt++;
                if (a[y2] != b[y2]) cnt++;
            } else if (x1 == 2 && x2 == 1) {
                if (b[y1] != a[y1]) cnt--;
                if (b[y2] != a[y2]) cnt--;
                swap(b[y1], a[y2]);
                if (b[y1] != a[y1]) cnt++;
                if (b[y2] != a[y2]) cnt++;
            } else if (x1 == 2 && x2 == 2) {
                if (b[y1] != a[y1]) cnt--;
                if (b[y2] != a[y2]) cnt--;
                swap(b[y1], b[y2]);
                if (b[y1] != a[y1]) cnt++;
                if (b[y2] != a[y2]) cnt++;
            }
        } else {
            if (cnt) cout << "NO\n";
            else cout << "YES\n";
        }
    }
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

标签:cnt,878,int,++,Codeforces,--,y1,Div,y2
From: https://www.cnblogs.com/bbbbear/p/17872372.html

相关文章

  • [Codeforces] CF1627B Not Sitting
    题意Rahul和Tina在玩一个游戏。游戏在一个\(n\timesm\)的网格图上进行,记第\(r\)行第\(c\)列上的格子为\((r,c)\)。定义\((a,b)\)与\((c,d)\)之间的距离为\(\left|a-c\right|+\left|b-d\right|\)。游戏开始后,Tina会选择恰好\(k\)个格子,并将其涂成粉红色。涂......
  • [Codeforces] CF1659B Bit Flipping
    题面给定一个长为\(n\)的01串,你可以进行\(k\)次操作。每次操作中,你可以选择任意一位,并将除了这一位以外的其它位翻转(\(1\)变\(0\),\(0\)变\(1\)),输出\(k\)次操作后能获得的字典序最大的字符串,并输出每一位在操作中被选择的次数。若有多解输出任意一解。思路可以发现......
  • [Codeforces] CF1675D Vertical Paths
    题目描述给定一棵由\(n\)个顶点组成的有根树。顶点由\(1\)到\(n\)编号。任何顶点都可以是树的根。请在树上找出这样一组路径:每个顶点恰好属于一条路径,每条路径可以包含一个或多个顶点;在每条路径中,每个节点的下一个节点是当前节点的子节点(即路径总是向下——从父节点......
  • Codeforces Round 912 (Div. 2) E - Geo Game
    考虑什么时候会改变答案的奇偶,显然可以根据\(x\oplusy\)的奇偶性分组,在组内进行跳跃不会改变,只有当组间跳跃的时候才会改变。打表观察先手什么时候必胜,其中:\(u\)是当前获胜目标为奇/偶(1/0),\(v\)是位于哪一组,\(a,b\)代表两组还剩多少,\(st\)代表当前答案的奇偶性。intdfs(intu,......
  • 【ErikTse】2023-Codeforces新手训练营 第六期题解
    A.Wrath题目大意给你一个\(L\)数组和\(n\)个人,第\(i\)个人可以使用威力为\(L_i\)的闪电旋风劈击杀前面\(L_i\)人,问你最后能存活多少人?思路差分。开一个数组来标记当前威力的闪电旋风劈能击杀到的最远的人和使用技能的人,最远击杀的人所在的位置+1,自己的位置-1,这样算前缀和时所......
  • Codeforces Round 912 (Div. 2)
    CodeforcesRound912(Div.2)基本概述最难受的一集。A题秒了。B题幸苦推了两个小时,最后也通过了pretest了,结果赛后被HACK。C题知道是DP,但觉得不好推状态转移方程,所以全心全意去做B题了。爆掉\(150\)分B.StORageroom我的思路其实就几乎是答案。之前几乎没怎......
  • RuCode 2020 Division A+B. I ✖ [PR #5] 和平共处
    前言认认真真学习了一下这道题相关的做法以及有关的二分图网络流理论,感觉自己又刷新了一些东西的理解。所以说我们就从普通的二分图匹配开始吧!二分图匹配众所周知,二分图最大匹配可以用网络流Dinic算法做到\(O(m\sqrtn)\)的复杂度。在某些特定的图下,我们有一种“贪心流”......
  • [Codeforces] CF1591C Minimize Distance
    CF1591CMinimizeDistance题目一条线上有\(n\)(\(1\len\le2\cdot10^5\))个仓库,第\(i\)个仓库的位置是\(x_i\)(\(1\lei\len\))。你有\(n\)箱货物,要分别运到这\(n\)个仓库里。你的初始位置在点\(0\),一次可以携带\(k\)(\(1\lek\len\))箱货物。在送完携带......
  • [Codeforces] CF1603A Di-visible Confusion
    CF1603ADi-visibleConfusion题目给一个长度为\(n\)的序列\(a_1,a_2,\dots,a_n\),对于每个位置\(i\),如果\(a_i\%\left(i+1\right)\not=0\),就可以将\(a_i\)删掉。删掉之后,后面的数都会往前面移动一位。问能否将序列删成空。数据范围\(1\let\le10^4,1\len\le10^5,1\le......
  • [AGC052C] Nondivisible Prefix Sums 题解
    题目链接点击打开链接题目解法好题!一个序列是不合法的,必定满足某些结论,我们不妨猜测一下首先如果和为\(P\)的倍数,必定不合法然后手玩几个可以发现,最极限的情况是\(P-1\)个\(1\;+\;\)\(b_i\;+\;\)\(P-b_i\)如果在这个情况下再加一个\(1\),就爆了其中\(1\)可以替......