Codeforces Round #631 (Div. 2)

A

用桶维护即可

#include <bits/stdc++.h>
using namespace std;

const int N = 205;

int t,n,x,a[N],b[N];

signed main() {
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--) {
        cin>>n>>x;
        for(int i=1;i<=n;i++) cin>>a[i];
        memset(b,0,sizeof b);
        for(int i=1;i<=n;i++) b[a[i]]=1;
        int pos=1;
        while(x) {
            while(b[pos]) ++pos;
            b[pos]=1;
            --x;
        }
        pos=1;
        while(b[pos]) ++pos;
        cout<<pos-1<<endl;
    }
}

B

显然分出的两段是合法的,当且仅当

  • 段的最大值等于段的长度
  • 段内没有重复元素

于是先扫一遍得到正序倒序第一个重复元素的位置,再正反各扫一遍得到前缀后缀最大值即可

#include <bits/stdc++.h>
using namespace std;

const int N = 205;

int t,n,x,a[N],b[N];

signed main() {
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--) {
        cin>>n>>x;
        for(int i=1;i<=n;i++) cin>>a[i];
        memset(b,0,sizeof b);
        for(int i=1;i<=n;i++) b[a[i]]=1;
        int pos=1;
        while(x) {
            while(b[pos]) ++pos;
            b[pos]=1;
            --x;
        }
        pos=1;
        while(b[pos]) ++pos;
        cout<<pos-1<<endl;
    }
}

C

不合法当且仅当总长度不够,或者存在 \(i\) 使得 l[i]+i-1>n

如果合法,我们先令 \(p_i = i\),然后 \(i\) 从大到小遍历将它放到最右端即可

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 100005;

int n,m,l[N],p[N];

signed main() {
    ios::sync_with_stdio(false);
    cin>>n>>m;
    int sum=0;
    for(int i=1;i<=m;i++) cin>>l[i], sum+=l[i];
    for(int i=1;i<=m;i++) if(l[i]+i-1>n) {
        cout<<-1; return 0;
    }
    if(sum<n) {
        cout<<-1; return 0;
    }
    for(int i=1;i<=m;i++) p[i]=i;
    int len=n;
    for(int i=m;i>=1;--i) {
        if(len-l[i]>i-1) p[i]=len-l[i]+1,len-=l[i];
        else break;
    }
    for(int i=1;i<=m;i++) cout<<p[i]<<" ";
}

D

待补

猜你喜欢

转载自www.cnblogs.com/mollnn/p/12630252.html