Codeforces Round 983 (Div. 2)

牛魔的渣机 ChromeBook

A. Circuit

每盏灯对应的开关有一个为 on 时是亮的,其他时候都为熄灭

 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
#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;
int off,on;

void solve(){
    cin>>n;
    on=off=0;
    for(int i=1;i<=2*n;i++){
        int x;
        cin>>x;
        if(x==1)on++;
        else off++;
    }
    int ans1=0,ans2=0;
    if(on<=n)ans2=on;
    else{
        ans2=n-(on-n);
    }
    ans1=n-on/2-off/2;
    cout<<ans1<<" "<<ans2<<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. Medians

开始题没看完,分的区间长度也必须全为奇数,能分的都分成三个即可,再考虑如果左右的个数为偶数应该往哪个区间加一即可

 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
#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,k;

void solve(){
    cin>>n>>k;
    if(n==1&&k==1){
        cout<<"1\n1\n";
        return;
    }
    if(k==1||k==n){
        cout<<"-1\n";
        return;
    }
    cout<<3<<endl;
    int l=k-1,r=n-k,ans=k,ans2=k+1;
    if(l%2==0)ans--;
    if(r%2==0)ans2++;
    cout<<1<<" "<<ans<<" "<<ans2<<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. Trinity

开始暴力两端,显然不全面

应该考虑每个位置作为中值的可能,再对这个值进行二分查找左右有多少个需要改的即可

 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
#include <bits/stdc++.h>
using namespace std;
#define ls now<<1
#define rs now<<1|1
#define endl "\n"
#define lowbit(x) ((x)&(-x))
#define int long long
const int N=2e5+8, mod=1e9+7;

int n;
int a[N];

int bs1(int x,int y){
    int l=1,r=n,ans=0;
    if(x+y>a[n])return n+1;
    while(l<=r){
        int mid=(l+r)>>1;
        if(x+y<=a[mid])r=mid-1,ans=mid;
        else l=mid+1;
    }
    return ans;
}

int bs2(int x){
    int l=1,r=n,ans=0;
    while(l<=r){
        int mid=(l+r)>>1;
        if(a[mid]+a[mid+1]<=x)l=mid+1,ans=mid;
        else r=mid-1;
    }
    return ans;
}

void solve(){
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    sort(a+1,a+1+n);
    int ans=1e9;
    a[0]=a[1];
    a[n+1]=a[n];
    for(int i=1;i<=n;i++){
        int l=bs1(a[i],a[i-1]);
        int r=bs2(a[l-1]);
        r=min(i-1,r);
        l=n-l+1;
        ans=min(ans,l+r);
    }
    cout<<ans<<endl;
}

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

D. Genokraken

注意到,删边之后的森林都是链,且这条链的根节点不能改变

同时,$p_i$ 是单调递增的

可以发现,开一个队列,记录当前的叶节点,如果最小的叶节点和 $i$ 的询问是 $0$,那么将 $i$ 接到这个叶节点下即可

如果这个询问为 $1$,那么删掉最小的这个叶节点,向后继续询问,直到有询问为 $0$ 的,将其接上即可

实际上只对每个节点访问了一次 $O(n)$,同时询问量只有 $O(n)$ 次,因为我们的询问是靠队列中的元素来进行的,每次询问会删掉一个,实际上最多每个节点被询问了一次

 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
#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;
int ans[N];

void solve(){
    cin>>n;
    for(int i=1;i<=n;i++){
        ans[i]=0;
    }
    deque<int>q;
    q.push_back(1);
    for(int i=2;i<n;i++){
        printf("? %d %d\n",q.front(),i);
        int x;
        cin>>x;
        if(x&&!ans[i-1]){
            ans[i]=0;
            q.push_back(i);
            continue;
        }
        while(x){
            q.pop_front();
            printf("? %d %d\n",q.front(),i);
            cin>>x;
        }
        ans[i]=q.front();
        q.pop_front();
        q.push_back(i);
    }
    cout<<"! ";
    for(int i=1;i<n;i++)cout<<ans[i]<<" ";
    cout<<endl;
    fflush(stdout);
}

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 设计