首页 > 其他分享 >CodeTON Round 7 (Div. 1 + Div. 2, Rated, Prizes!)

CodeTON Round 7 (Div. 1 + Div. 2, Rated, Prizes!)

时间:2023-11-26 16:00:53浏览次数:40  
标签:cnt Rated int rep pp cin Prizes Div

CodeTON Round 7 (Div. 1 + Div. 2, Rated, Prizes!)

A - Jagged Swaps

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n;
        rep (i, 1, n) cin >> a[i];
        while (true) {
            bool f = 0;
            rep (i, 2, n - 1) if (a[i] > a[i - 1] && a[i] > a[i + 1]) {
                swap(a[i], a[i + 1]);
                f = 1;
            }
            if (!f) break;
        }
        bool f = 1;
        rep (i, 1, n) if (i != a[i]) {
            f = 0;
            break;
        }
        cout << (f ? "YES\n" : "NO\n");
    }
    return 0;
}

B - AB Flipping

正反各来一次

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n >> s + 1;
        int ans = 0;
        rep (i, 1, n) f[i] = 1;
        per (i, n - 1, 1) {
            if (s[i] == 'A' && s[i + 1] == 'B') {
                ++ans;
                swap(s[i], s[i + 1]);
                f[i] = 0;
            }
        }
        rep (i, 1, n - 1) {
            if (s[i] == 'A' && s[i + 1] == 'B' && f[i]) {
                ++ans;
                swap(s[i], s[i + 1]);
                f[i] = 0;
            }
        }
        cout << ans << '\n';
    }
    return 0;
}

C - Matching Arrays

每次以贪心地增加/减少匹配项

const int N = 2e5 + 5;
 
int n, m, _, k, cas;
int a[N], b[N], c[N], d[N];
 
int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n >> m;
        rep (i, 1, n) cin >> a[i], c[i] = i;
        rep (i, 1, n) cin >> b[i];
        sort(c + 1, c + 1 + n, [&](int x, int y) { return a[x] < a[y]; });
        sort(b + 1, b + 1 + n);
        int cnt = 0;
        rep (i, 1, n) cnt += a[c[i]] > b[i];
        if (cnt < m) {
            set<PII> q;
            rep (i, 1, n) if (a[c[i]] <= b[i]) {
                if (!q.empty() && q.begin()->fi < a[c[i]]) {
                    int pp = q.begin()->se;
                    swap(b[i], b[pp]);
                    ++cnt;
                    if (cnt == m) break;
                    q.erase(q.begin());
                    q.emplace(b[pp], pp);
                } else {
                    q.emplace(b[i], i);
                }
            }
        } else if (cnt > m) {
            set<PII> q;
            rep (i, 1, n) if (a[c[i]] > b[i]) {
                if (!q.empty() && b[i] >= q.begin()->fi) {
                    int pp = q.begin()->se;
                    swap(b[i], b[pp]);
                    --cnt;
                    if (cnt == m) break;
                    q.erase(q.begin());
                }
                q.emplace(a[c[i]], i);
            }
        }
        if (cnt == m) {
            rep (i, 1, n) d[c[i]] = b[i];
            cout << "YES\n";
            rep (i, 1, n) cout << d[i] << ' ';
            cout << '\n';
        } else {
            cout << "NO\n";
        }
    }
    return 0;
}

D - Ones and Twos

考察奇偶性,考虑从头/尾丢弃 item 来使得剩余的子数组和等于所求

  1. 当头尾丢弃的 item 和是偶数时,必定有解,每次也偶数的从头/尾丢弃偶数即可(或者头尾一起各丢一个 1)

  2. 当头尾丢弃的 item 和是奇数是,先从数组头 or 尾贪心的选一个方向丢一个 1, 就可以转成情况 1 了(前提剩余 sum 可以凑出所求和)

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n >> m;
        int s = 0;
        set<int> st;
        rep (i, 1, n) {
            cin >> a[i], s += a[i];
            if (a[i] == 1) st.emplace(i);
        }
        rep (i, 1, m) {
            int op; cin >> op;
            if (op == 2) {
                int x, y; cin >> x >> y;
                if (a[x] == 1) st.erase(x);
                s -= a[x]; a[x] = y;
                if (a[x] == 1) st.emplace(x);
                s += a[x];
            } else {
                int x; cin >> x;
                if (s < x) cout << "NO\n";
                else if (s == x) cout << "YES\n";
                else {
                    int z = s - x;
                    if (!(z & 1)) cout << "YES\n";
                    else if (!st.empty() && z >= (min(*st.begin() - 1, n - *st.rbegin()) << 1) + 1) cout << "YES\n";
                    else cout << "NO\n";
                } 
            }
        }
    }
    return 0;
}

E - Permutation Sorting

每个数字移动的距离不会超过 n, 为了方便计算,选择拆环,让 a[n + 1 ~ n * 2] = a[1 ~ n]

每个位置移动的绝对距离是 \(s_i\), 即 \((i + h_i − 1) % n =a_i - 1\)

然后就是考虑路径上哪些点 j 可以跳过,即 \(i < j < i + h_i \ and \ (i < a_j < i + h_i \ or \ i < a_j + n < i + h_i)\)

或者说对于点 i 的移动路径是 [l, r] (l = i, (r - 1) mod n + 1 = \(a_i\)), 他路径上可以跳过的点就是路径被 [l, r] 包含的路径

然后就是找个数据结构维护单点修改,区间查询路径上可以跳过的点数就好了

const int N = 1e6 + 5;
 
int n, m, _, cas;
int a[N], c[N << 1], ans[N];
 
void add(int x, int k) { for (; x <= n << 1; x += -x & x) c[x] += k; }
 
int ask(int x) { int ans = 0; for (; x; x -= x & -x) ans += c[x]; return ans; }
 
int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n;
        memset(c, 0, sizeof(int) * n << 1);
        vector<PII> e;
        rep (i, 1, n) {
            cin >> a[i];
            if (i <= a[i]) {
                e.pb(i, a[i]);
                e.pb(i + n, a[i] + n);
            }
            else e.pb(i, a[i] + n);
        }
        sort(e.begin(), e.end(), greater<PII>());
        for (auto [l, r]: e) {
            if (l <= n) {
                ans[a[l]] = r - l - ask(r) + ask(l - 1);
            }
            add(r, 1);
        }
        rep (i, 1, n) cout << ans[i] << ' ';
        cout << '\n';
    }
    return 0;
}

标签:cnt,Rated,int,rep,pp,cin,Prizes,Div
From: https://www.cnblogs.com/2aptx4869/p/17857380.html

相关文章

  • CodeTON Round 7 (Div. 1 + Div. 2) 解题报告
    CodeTONRound7(Div.1+Div.2)ContestLink广告:本场比赛博主使用了CCH完成,体验很好,推荐高rating用户使用(低rating受cloudflare影响很大)。A.JaggedSwaps\(\text{Status:\color{green}+\color{black}00:03}\)结论:输出YES当且仅当\(a_1=1\)。证明:如......
  • CF 158 (Rated for Div
    CF-158这次比赛较上次也是有进步,成功地多AC了一道题。但第4题也是很遗憾只差一点了。A.LineTrip题意:车在数轴上从$0$点到达$x$点又返回$0$点,有$k$点的油,可以走$k$个单位,在数轴上$a_1,a_2,a_3...a_n$处可以加油到$k$点,$0$点处和$x$点处无法加油,问$k$的最小值。思路:那么根据题......
  • How Can South Asia Adapt Integrated River Basin Management to Its Soil Erosion
    Duetotheinstabilityofthemonsoon,floodsanddroughtsarefrequentinSouthAsia,resultinginseveresoilerosion.Everyyear,SouthAsiasuffershugelossesduetosoilerosion,includingpropertydamage,humancasualties,andenvironmentaldamage.......
  • CodeTON Round 7 (Div. 1 + Div. 2, Rated, Prizes!)
    CodeTONRound7(Div.1+Div.2,Rated,Prizes!)基本情况A题花了快半小时,做出来了但是不如正解。B题又是老毛病,一条路走到黑,爆搜打出来超时就死命想剪枝和记忆化,没想过换方法(觉得贪心不可行)。A-JaggedSwaps我的解法没啥好说的,纯模拟。看到\(n\leq10\)知道能过。......
  • Educational Codeforces Round 158 (Rated for Div. 2)
    EducationalCodeforcesRound158(RatedforDiv.2)基本情况A题很水,几分钟秒了。B题想到一个解,被自己hack掉之后没有重新想,一直想在自己一开始的基础上改好,结果最后B都没拿下。B.ChipandRibbon我的思路其实也是找规律,根本没严谨地证明正确性。不应该一条路走到黑的......
  • HTML <div> 和<span>
    HTML <div>和<span>HTML可以通过<div>和<span>将元素组合起来。HTML区块元素大多数HTML元素被定义为块级元素或内联元素。块级元素在浏览器显示时,通常会以新行来开始(和结束)。实例:<h1>,<p>,<ul>,<table>HTML内联元素内联元素在显示时通常不会以新行开始。......
  • Educational Codeforces Round 158 (Rated for Div. 2)
    A.LineTrip题意是:有n个加油点,人要来回两趟,问你最少要多少油?usingnamespacestd;inta[100];voidsolve(){ intn,m; cin>>n>>m; for(inti=1;i<=n;i++)cin>>a[i]; intans=a[1]; for(inti=2;i<=n;i++){ ans=max(ans,a[i]-a[i-1]); } ans=max(ans,2*(m-......
  • Educational Codeforces Round 158 (Rated for Div. 2) A-C
    A大致题意:有一条长度为x的直线公路,最开始你位于0号点并且此时你的油箱装满了油,公路有n个加油站,当你经过加油站的时候你可以在加油站加满油,每走一个单位需要花费1升油量,起始位置和终点没有加油站,请问你的油箱容量至少为多少升才可以够你跑一个来回。解题思路:我们的路径大致是......
  • Codeforces Round 905 (Div. 3)
    CodeforcesRound905(Div.3)A.Morning题意:操作:显示,向前走都为一次操作;目标:显示这四个数思路:0->10,然后依次作差就行#include<bits/stdc++.h>usingnamespacestd;voidsolve(){chara[4];intmi=100,sum=4,b[4];for(inti=0;i<4;i++){cin>>a[i]......
  • Codeforces Round 903 (Div. 3)
    CodeforcesRound903(Div.3)A.Don'tTrytoCount大概题意给你两个字符串a,b。a串可进行的操作为将整个a串复制到之前的a串后面(直接用a+a即可),然后看操作多少次可以让b串变为a串的子串如果不能就输出-1。#include<iostream>usingnamespacestd;stringa,b;voidsolve()......