首页 > 其他分享 >CodeTON Round 7 补题(C、D)

CodeTON Round 7 补题(C、D)

时间:2023-11-26 21:25:27浏览次数:27  
标签:int sum 个数 cin CodeTON 补题 数组 Round se

CodeTON Round 7

C. Matching Arrays

思路

开一个c数组来记录a的从小到大排序后的原来的下标,接着将b数组从小到大排序,先找出将a数组后x个数和b数组x的数比较,再将a的前n - x和b的后n-x个数比较。如果a数组后x个数都大于b数组前x的数,且a的前n - x都不大于b的后n-x个数,则输出YES

ac代码

#include <bits/stdc++.h>

using namespace std;
using i64  = long long;
const i64 inf = 8e18;
typedef pair<int, int> pii;

const int N = 3e5 + 10;
int a[N], b[N], c[N], ans[N];

void solve() {
    int n, x;
    cin >> n >> x;
    for (int i = 0; i < n; i ++) cin >> a[i];
    for (int i = 0; i < n; i ++) cin >> b[i];

    iota(c, c + n, 0);
    sort(c, c + n, [](int x, int y){
        return a[x] < a[y];
    });
    sort(b, b + n);

    bool ok = 1;
    for (int i = n - x; i < n; i++) {
        int idx = c[i];
        if (a[idx] <= b[i - (n - x)]) ok = 0;
        ans[idx] = b[i - (n - x)];
    }

    for (int i = 0; i < n - x; i++) {
        int idx = c[i];
        if (a[idx] > b[x + i]) ok = 0;
        ans[idx] = b[x + i];
    }

    if (ok) {
        cout << "YES\n";
        for (int i = 0; i < n; i++) cout << ans[i] << ' ';
        cout << endl;
    }else cout << "NO\n";
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);

    int t = 1;
    cin >> t;
    while (t --) solve();

    return 0;
}

D. Ones and Twos

思路

用一个set存下数组里为1的下标,用sum记录数组当前的和,用cnt的记录min(前缀2的个数,后缀2的个数)。
对于待查找数s,如果满足s <= t or (s % 2 == t % 2 && (s - t) / 2 <= cnt)则输出YES,否则输出NO

ac代码

#include <bits/stdc++.h>

using namespace std;
using i64  = long long;
const i64 inf = 8e18;
typedef pair<int, int> pii;
const int N = 3e5 + 10;

void solve() {
    int n, q;
    cin >> n >> q;
    set<int> se;
    vector<int> a(n + 1);
    int sum = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        if (a[i] == 1) se.insert(i);
        sum += a[i];
    }

    while (q --) {
        int op; cin >> op;

        if (op == 1) {
            int s; cin >> s;
            int cnt = (se.size() == 0 ? n : min(*se.begin() - 1, n - *se.rbegin()));
            
            int t = sum - 2 * cnt;
            if (s <= t || (s % 2 == t % 2 && (s - t) / 2 <= cnt)) cout << "YES\n";
            else cout << "NO\n";

        }else{
            int i, v;
            cin >> i >> v;
            if (v == 1) se.insert(i);
            else se.erase(i);
            sum += v - a[i];
            a[i] = v;
        }
    }

}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);

    int t = 1;
    cin >> t;
    while (t --) solve();

    return 0;
}

标签:int,sum,个数,cin,CodeTON,补题,数组,Round,se
From: https://www.cnblogs.com/kichuan/p/17857812.html

相关文章

  • CodeTON Round 7 (Div. 1 + Div. 2, Rated, Prizes!)
    CodeTONRound7(Div.1+Div.2,Rated,Prizes!)A-JaggedSwaps解题思路:若\(a[1]=1\),则可以。代码:#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;typedefpair<int,int>pii;#definefifirst#definesesecondvoidsolve(){......
  • CodeTON Round 7 (Div. 1 + Div. 2, Rated, Prizes!)
    CodeTONRound7(Div.1+Div.2,Rated,Prizes!)A-JaggedSwapsintmain(){IOS;for(cin>>_;_;--_){cin>>n;rep(i,1,n)cin>>a[i];while(true){boolf=0;rep(i,......
  • 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\)。证明:如......
  • 牛客周赛Round20. C 小红的01串构造 (纯构造
    packagenewCode.周赛Round20;importjava.util.Scanner;publicclassC{publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);intn=sc.nextInt(),k=sc.nextInt(),t=sc.nextInt();if(t>=k||2......
  • GroundingDINO安装报错解决
    title:GroundingDINO安装报错解决banner_img:https://drive.studyinglover.com/api/raw/?path=/photos/blog/background/1679397024795.jpegdate:2023-6-2117:25:00categories:-踩坑GroundingDINO安装报错解决在安装会遇到这个错误ERROR:Commanderroredoutwith......
  • 牛客周赛Round20. A 赝品
    packagenewCode.周赛Round20;importjava.util.Arrays;importjava.util.Scanner;publicclassA{publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);intn=sc.nextInt();int[]a=newint[n+10];......
  • 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我的思路其实也是找规律,根本没严谨地证明正确性。不应该一条路走到黑的......
  • Educational Codeforces Round 158 补题(A~D)
    A.思路找出最大耗油的路程即可ac代码#include<bits/stdc++.h>usingnamespacestd;usingi64=longlong;consti64inf=8e18;typedefpair<int,int>pii;voidsolve(){intn,x;cin>>n>>x;std::vector<int>v(n);f......
  • 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-......