Codeforces Round 995 (Div. 3)

A. Preparing for the Olympiad

暴力贪心即可,只要当前是优的就取

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <bits/stdc++.h>
using namespace std;
#define ls now<<1
#define rs now<<1|1
#define endl "\n"
#define lowbit(x) ((x)&(-x))
typedef long long ll;
const int N=1e5+7, mod=1e9+7;

int n,a[N],b[N],m,s;

void solve(){
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=n;i++)cin>>b[i];
    m=s=0;
    for(int i=1;i<n;i++){
        if(a[i]>=b[i+1])m+=a[i],s+=b[i+1];
    }
    m+=a[n];
    cout<<m-s<<endl;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t=1;
    cin>>t;
    while(t--)solve();
    return 0;
}

B. Journey

整除每个循环的长度,再判断需要额外走几天即可

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <bits/stdc++.h>
using namespace std;
#define ls now<<1
#define rs now<<1|1
#define endl "\n"
#define lowbit(x) ((x)&(-x))
typedef long long ll;
const int N=1e5+7, mod=1e9+7;

int n,a,b,c;

void solve(){
    cin>>n>>a>>b>>c;
    int ans=0;
    ans+=3*(n/(a+b+c));
    n%=(a+b+c);
    if(!n){
        cout<<ans<<endl;
        return;
    }
    if(n<=a)ans++;
    else if(n<=(a+b))ans+=2;
    else ans+=3;
    cout<<ans<<endl;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t=1;
    cin>>t;
    while(t--)solve();
    return 0;
}

C. Preparing for the Exam

标记好 Monocarp 会的题,对每场考试的题判断即可,如果他会的题数小于 n-1,那么一定无法通过

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <bits/stdc++.h>
using namespace std;
#define ls now<<1
#define rs now<<1|1
#define endl "\n"
#define lowbit(x) ((x)&(-x))
typedef long long ll;
const int N=3e5+7, mod=1e9+7;

int n,m,k;
int a[N],q[N],vis[N];

void solve(){
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++)vis[i]=0;
    for(int i=1;i<=m;i++)cin>>a[i];
    for(int i=1;i<=k;i++)cin>>q[i];
    if(k<=n-2){
        for(int i=1;i<=m;i++)cout<<"0";
        cout<<endl;
        return;
    }
    if(k==n){
        for(int i=1;i<=m;i++)cout<<"1";
        cout<<endl;
        return;
    }
    for(int i=1;i<=k;i++){
        vis[q[i]]=1;
    }
    for(int i=1;i<=m;i++){
        if(!vis[a[i]])cout<<"1";
        else cout<<"0";
    }
    cout<<endl;
    return;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t=1;
    cin>>t;
    while(t--)solve();
    return 0;
}

D. Counting Pairs

排序后二分,删除两个元素后判断总和是否处于某区间中,找符合条件的区间即可

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <bits/stdc++.h>
using namespace std;
#define ls now<<1
#define rs now<<1|1
#define endl "\n"
#define lowbit(x) ((x)&(-x))
typedef long long ll;
const int N=2e5+7, mod=1e9+7;

ll n,x,y;
int a[N];

void solve(){
    cin>>n>>x>>y;
    ll s=0;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        s+=a[i];
    }
    sort(a+1,a+1+n);
    ll ans=0;
    for(int i=2;i<=n;i++){
        int pos1=lower_bound(a+1,a+i,s-a[i]-y)-(a+1);
        int pos2=upper_bound(a+1,a+i,s-a[i]-x)-(a+1);
        ans+=(pos2-pos1);
    }
    cout<<ans<<endl;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t=1;
    cin>>t;
    while(t--)solve();
    return 0;
}

E. Best Price

首先易想明,最终价格一定是 a 或 b 数组中的某个值。

然后用二分在两个数组中分别找到,不留差评买的人数,和可能留差评买的人数。

拿可能留的人数减去不留的即为留差评的人数,只要小于 k,当前答案就可取

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <bits/stdc++.h>
using namespace std;
#define ls now<<1
#define rs now<<1|1
#define endl "\n"
#define lowbit(x) ((x)&(-x))
typedef long long ll;
const int N=2e5+7, mod=1e9+7;

int n,k;
int a[N],b[N];

ll fc(int x){
    int len=n-(lower_bound(b+1,b+1+n,x)-(b+1));
    int tmp=n-(lower_bound(a+1,a+1+n,x)-(a+1));
    if(len-tmp>k)return 0;
    return 1ll*len*x;
}

void solve(){
    cin>>n>>k;
    ll ans=0;
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=n;i++)cin>>b[i];
    sort(a+1,a+1+n);
    sort(b+1,b+1+n);
    for(int i=1;i<=n;i++){
        ans=max({ans,fc(a[i]),fc(b[i])});
    }
    cout<<ans<<endl;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t=1;
    cin>>t;
    while(t--)solve();
    return 0;
}

F. Joker

易发现,鬼牌的位置只会在现有位置的附近变动,不会跳动,所以转化为区间操作。

假设当前鬼牌区间为 $[l,r]$

如果当前抽的卡在鬼牌区间左侧,鬼牌区间只会变为 $[l-1,r]$

反之会变为 $[l,r+1]$

如果在鬼牌区间中,则会拆分为 $[1,1],[n,n],[l,r]$ 如果区间长度为 $1$ ,则没有 $[l,r]$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <bits/stdc++.h>
using namespace std;
#define ls now<<1
#define rs now<<1|1
#define endl "\n"
#define lowbit(x) ((x)&(-x))
typedef long long ll;
const int N=1e5+7, mod=1e9+7;

int n,m,q;

void solve(){
    cin>>n>>m>>q;
    set<pair<int,int>>s;
    s.insert({m,m});
    while(q--){
        int x;
        cin>>x;
        set<pair<int,int>>tmp;
        for(auto [l,r]:s){
            if(x<l)tmp.insert({max(1,l-1),r});
            else if(x>r)tmp.insert({l,min(n,r+1)});
            else {
                tmp.insert({1,1});
                tmp.insert({n,n});
                if(l!=r)tmp.insert({l,r});
            }
        }
        set<pair<int,int>>t;
        int nl=1,nr=0;
        for(auto [l,r]:tmp){
            if(l<=nr){
                nr=max(nr,r);
            }
            else {
                t.insert({nl,nr});
                nl=l;
                nr=r;
            }
        }
        t.insert({nl,nr});
        s=t;
        s.erase(s.begin());
        ll ans=0;
        for(auto [l,r]:s){
            ans+=r-l+1;
        }
        cout<<ans<<" ";
    }
    cout<<endl;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t=1;
    cin>>t;
    while(t--)solve();
    return 0;
}
Licensed under CC BY-NC-SA 4.0
最后更新于 Sep 11, 2025 16:27 +0800
发表了95篇文章 · 总计15万2千字
永远相信美好的事情即将发生。
使用 Hugo 构建
主题 StackJimmy 设计