Codeforces Round #738 (Div. 2)(A~D)题解

传送门
在这里插入图片描述
在这里插入图片描述
题目大意
给出n个数字,这n个数字可以选取一个[l,r]区间,按照a[l]&a[r],a[l+1]&a[r-1]…对于数字进行操作。问可以使得区间的最大值的最小情况是多少。
解题思路
由于这个题,对于区间的选取没有要求而且&操作只会降低元素的值。
我们不妨假设所有数字都进行了一次与操作。
解题代码

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

#define debug(a) cout << #a << ": " << a << endl;
#define LL long long 
#define IO ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)

bool si1[50],si2[50];

void solve()
{
    int n;
    cin >> n;
    memset(si1,false,sizeof(si1));
    int x,cnt = 0;
    cin >> x;
    while (x > 0)
    {
        int tmp = x%2;
        if(tmp == 1) si1[cnt] = true;
        cnt++;
        x /= 2;
    }
    memset(si2,false,sizeof(si2));
    for(int i = 1;i < n;++i) {
        cin >> x;
        cnt = 0;
        while (x > 0)
        {
            int tmp = x%2;
            if(tmp == 1) si2[cnt] = true;
            cnt++;
            x /= 2;
        }
        for(int j = 0;j <= 35;++j) {
            if(si1[j] && si2[j]) {
                si1[j] = true;
            }
            else si1[j] = false;
            si2[j] = false;
        }
    }
    int ans = 0;
    for(int i = 0;i < 35;++i) {
        if(si1[i])
            ans += (1<<i);
    }
    cout << ans << endl;
}

int main()
{
    IO;
    int t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

题目总结
与操作降低数值的思维水题。

在这里插入图片描述
在这里插入图片描述
题目大意
给定一个字符串,包含’B’,‘R’,’?‘三种字符。
我们可以将’?‘转换成’B’或者’R’,为了使改动过的字符串,相邻两个字符相同的情况尽可能少,求改动过的字符串。
解题思路
字符串可以分为两种情况:
一种是字符串全为’?’,那么我们就可以采用"BRBR…"的方式构造。
另一种是字符串不全为’?’,我们只要找到第一个非’?‘的位置,从该位置左右遍历,如果找到’?'就将它改成与相邻处不同的元素。
解题代码

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

#define debug(a) cout << #a << ": " << a << endl;
#define LL long long 
#define IO ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)

void solve()
{
    int n;
	cin >> n;
	string s;
	cin >> s;
	int cnt = -1;
	for(int i = 0;i < s.length();++i) {
		if(s[i] != '?') cnt = i;
	}
	if(cnt == -1) {
		for(int i = 0;i < s.length();++i) {
			if(i%2== 0) s[i] = 'B';
			else s[i] = 'R';
		}
	}
	else {
		for(int i = cnt-1;i >= 0;--i) {
			if(s[i] == '?') {
				if(s[i+1] == 'B') s[i] = 'R';
				else s[i] = 'B'; 
			}
			else continue;
		}
		for(int i = cnt+1;i < s.length();++i) {
			if(s[i] == '?') {
				if(s[i-1] == 'B') s[i] = 'R';
				else s[i] = 'B';
			}
		}
	}
	cout << s << endl;
}

int main()
{
    IO;
    int t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

题目总结
一种典型的构造题,值得深思,这类构造题较难的多种形式。

在这里插入图片描述
在这里插入图片描述
题目大意
给出n个点和2n-1条有向边,边分为两种。
一种是从i到i+1的n-1条边
另一种是i和n之间的边
求所有点遍历一次的一条路径


输入
给出n和n+1个数字,n表示有n+1个点,n+1个数字代表第i个数字与第n+1个点之间的边0代表从i到第n+1个点,1代表从n+1到i的有向边。
输出
一条可行路径
解题思路
由于我们可以从第一个点直接到达第n个点,那么我们可以将所有边假设成两种情况。
第一种,我们可以从第n个点到达第n+1个点。那么存在可行路径123…n+1
第二种,我们不能从第n个点到达第n+1个点。那么我们找到第一个点n+1能到达的位置,将其插在序列1…n中,注意存在插在队首的情况。
解题代码

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

#define debug(a) cout << #a << ": " << a << endl;
#define LL long long 
#define IO ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)

void solve()
{
    int n;
    cin >> n;
    int cnt = -1;
    for(int i = 1,x;i <= n;++i) {
        cin >> x;
        if(x == 1 && cnt == -1) cnt = i;
    }
    if(cnt == -1) {
        for(int i = 1;i <= n+1;++i) {
            cout << i << ' ';
        }
    }
    else if(cnt == 1) {
        cout << n+1 << ' ';
        for(int i = 1;i <= n;++i) {
            cout << i << ' ';
        }
    }
    else {
        for(int i = 1;i <= n+1;++i) {
            if(i < cnt) cout << i << ' ';
            else if(i == cnt) cout <<  n+1 << ' ';
            else cout << i-1 << ' ';
        }
    }
    cout << endl;
}

int main()
{
    IO;
    int t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

题目总结
经典的序列构造题,值得深入研究序列之美。

在这里插入图片描述
在这里插入图片描述
题目大意
存在两颗大树,各有m1和m2条边。
当我们需要建边时,需要同时在两棵树上建边。
问我们最多可以建多少条边使得两棵树上都不存在回路。
解题大意
首先我们可以对于第一个点进行建边,如果两棵树上存在同一个点对于第一个点都没有路径,那么我们可以对第一个点建边。然后,找到第一颗树上与第1个点未连接的边和第二颗树上与第一个点未连接的边。将这两个点相连。
证明过程较为复杂(我不太会),可自行证明。
解题代码

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

const int maxn = 1e5+10;

struct  stu{
    int n;
    int fa[maxn];
    stu (int n){
        for(int i = 1;i <= n;++i) {
            fa[i] = i;
        }
    }
    int find(int x) {
        return fa[x] == x ? x : fa[x] = find(fa[x]);
    }
    void merge(int x,int y)
    {
        x = find(x);
        y = find(y);
        fa[x] = y;
    }
};


int main()
{
    ios::sync_with_stdio(0),cin.tie(0);
    int n;
    cin >> n;
    stu A(n),B(n);
    int m1,m2;
    cin >> m1 >> m2;
    for(int i = 1;i <= m1;++i) {
        int u,v;
        cin >> u >> v;
        A.merge(u,v);
    }
    for(int i = 1;i <= m2;++i) {
        int u,v;
        cin >> u >> v;
        B.merge(u,v);
    }

    vector<pair<int,int> > ans;
    ans.clear();
    for(int i = 1;i <= n;++i) {
        if(A.find(1) != A.find(i) && B.find(1) != B.find(i)) {
            ans.push_back({1,i});
            A.merge(1,i);
            B.merge(1,i);
        }
    }
    vector<int> a,b;
    a.clear(),b.clear();
    for(int i = 1;i <= n;++i) {
        if(A.find(1) != A.find(i)) a.push_back(i);
        if(B.find(1) != B.find(i)) b.push_back(i);
    }


    
    m1 = a.size(),m2 = b.size();
    for(int i = 0,j = 0;i < m1 && j < m2;){
        while(i < m1 && A.find(a[i]) == A.find(1)) i++;
        while (j < m2 && B.find(b[j]) == B.find(1)) j++;
        if(i < m1 && j < m2) {
            ans.push_back({a[i],b[j]});
            A.merge(a[i],b[j]);
            B.merge(a[i],b[j]);
        }
        
    }
    cout << ans.size() << endl;
    for(auto s : ans) cout << s.first << ' ' << s.second << endl;
}

题目总结
图论方面的一些总结值得探究与总结

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值