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

比赛链接:点击这里传送

官方题解链接:点击这里传送


1559A Mocha and Math 题目链接:点击这里传送

在这里插入图片描述
题意:
有一串长度为n的序列 a 1 , a 2 , a 3 , a 4 , a 5 a_1,a_2,a_3,a_4,a_5 a1,a2,a3,a4,a5……。选定一段区间 [ l , r ] \left[l,r\right] [l,r],使得里面的元素对称的替换为 a l + i a_{l+i} al+i& a r − i a_{r-i} ari,i>=0。
要求使这个新的序列的最大值尽可能小,输出这个最小值。
思路:
思维题。
一个数与另一个数这两个数只可能变小不可能变大。所以l和r直接取1和n即可。

#include<bits/stdc++.h>
using namespace std;
int a[105];
int ans = 0;
int t, n;
int main()
{
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> t;
	while (t--)
	{
		cin >> n;
		for (int i = 1; i <= n; i++) cin >> a[i];
		ans = a[1];
		for (int i = 2; i <= n; i++) ans = ans & a[i];
		cout << ans << endl;
	}

	return 0;
}

1559B Mocha and Red and Blue 题目链接:点击这里传送

在这里插入图片描述
题意:
给出一个只包含B和R的字符串。给出部分字符串确定的内容,要求你构造出一个字符串使得BB,RR这种重复字符出现的情况尽可能少。
思路:
如果开头是确定的,那么只需要保证与前面一位不同(或与后面一位不同)就行。这两者产生的贡献是一样的。如
B???B
BRBRBB BBRBRB
如果开头是问号,那么就假设开头是B或者R,按照相同的规则构造字符串,将两个字符串的贡献比较一下即可。

#include<bits/stdc++.h>
using namespace std;
int t, n;
string s, ans,res;

int main()
{
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> t;
	while (t--)
	{
		ans = "";
		res = "";
		int cnt1 = 0; int cnt2 = 0;
		cin >> n;
		cin >> s;
		if (s[0] == '?')
		{
			if (s[1] == 'B')
			{
				ans = 'R';
				res = 'R';
			}
			else if(s[1]=='R')
			{
				ans = 'B';
				res = 'B';
			}
			else
			{
				ans = 'B';
				res = 'R';
			}
		}
		else
		{
			ans = s[0];
			res = s[0];
		}
		for (int i = 1; i < n; i++)
		{
			if (s[i] != '?')
			{
				ans += s[i];
				res += s[i];
				if (ans[i] == ans[i - 1]) cnt1++;
				if (res[i] == res[i - 1]) cnt2++;
			}
			else
			{
				if (ans[i-1] == 'B') ans+= 'R';
				else ans+= 'B';
				if (res[i - 1] == 'B') res += 'R';
				else res += 'B';
			}
		}
		if (cnt1 < cnt2)
			cout << ans << endl;
		else cout << res << endl;
	}
	return 0;
}

1559C Mocha and Hiking 题目链接:点击这里传送

在这里插入图片描述
题意:
一共有n+1个节点。1->2->3->4->……->n有单向图。 [ 1 , n ] \left[1,n\right] [1,n]这些点到n+1都有一条路,方向由题目输入数据给出。现要求不重复的完整遍历完这n+1个点(起点任意),如果可行输出路线如果不行输出-1。
思路:
如果n+1指向1,那么路线就是n+1->1->2->3->……->n
如果n指向n+1,那么路线就是1->2->3->……->n->n+1
如果i指向n+1,n+1又能指向i+1,那么路线就是1->2->……->i->n+1->i+1->……->n

#include<bits/stdc++.h>
using namespace std;
int t, n;
#define MAXN 200005
int a[MAXN];//如果为0,i~n+1;如果为1,n+1~i
void solve()
{
	if (a[1] == 1)
	{
		cout << n + 1 << " ";
		for (int i = 1; i <= n; i++) cout << i << " ";
		cout << endl;
		return;
	}
	if (a[n] == 0)
	{
		for (int i = 1; i <= n + 1; i++) cout << i << " ";
		cout << endl;
		return;
	}
	for (int i = 1; i <= n - 1; i++)
	{
		if (a[i] == 0 && a[i + 1] == 1)
		{
			for (int j = 1; j <= i; j++) cout << j << " ";
			cout << n + 1 << " ";
			for (int j = i + 1; j <= n; j++) cout << j << " ";
			cout << endl;
			return;
		}
	}
	cout << -1 << endl;
}
int main()
{
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> t;
	while (t--)
	{
		cin >> n;
		for (int i = 1; i <= n; i++) cin >> a[i];
		solve();
	}

	return 0;
}

1559D1 Mocha and Diana (Easy Version) 题目链接:点击这里传送

在这里插入图片描述
题意:
志明和春娇各自有张图,有n个节点和m1,m2条边。现在他们将一起玩游戏,将各自地图上的两个节点连接起来,要求最后两个人的图没有环。问最多能连几条边。
思路:
各自建一个图。然后二重循环暴力遍历每对节点,通过并查集的方法连边和判断自环。

#include<bits/stdc++.h>
using namespace std;
#define MAXN 1005
int f1[MAXN],f2[MAXN];
int n, m1, m2;
void pre()
{
	for (int i = 1; i <= n; i++) f1[i] = i;
	for (int i = 1; i <= n; i++) f2[i] = i;
}
int findx1(int x)
{
	if (f1[x] == x) return x;
	else return f1[x] = findx1(f1[x]);
}
int findx2(int x)
{
	if (f2[x] == x) return x;
	else return f2[x] = findx2(f2[x]);
}
void merge1(int x, int y)
{
	int fx = findx1(x);
	int fy = findx1(y);
	f1[fy] = fx;
}
void merge2(int x, int y)
{
	int fx = findx2(x);
	int fy = findx2(y);
	f2[fy] = fx;
}
struct node
{
	int to; int from;
};
int main()
{
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	
		int cnt = 0;
		cin >> n >> m1 >> m2;
		pre();
		for (int i = 1; i <= m1; i++)
		{
			int to, from;
			cin >> to >> from;
			merge1(to, from);
		}
		for (int i = 1; i <= m2; i++)
		{
			int to, from;
			cin >> to >> from;
			merge2(to, from);
		}
		vector <node> v;
		for (int i = 1; i < n; i++)
		{
			for (int j = i + 1; j <= n; j++)
			{
				if (findx1(i) != findx1(j)&&findx2(i)!=findx2(j))
				{
					cnt++;
					merge1(i, j);
					merge2(i, j);
					v.push_back(node{ i,j });
				}
			}
		}
		cout << cnt << endl;
		for (int i = 0; i < v.size(); i++) cout << v[i].to << " " << v[i].from << endl;
	

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值