Codeforces Round #853 (Div. 2)题解(A-D)

A - Serval and Mocha's Array

题意可以简化为,数组经过排列后前两个数的最大公因数小于2即为美丽的数组,因此可以O(n^2)判断是否存在最大公因数小于2的两个数。

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

void solve() {
	int n, a[1000];
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	int flag = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (i == j)
				continue;
			if (__gcd(a[i], a[j]) <= 2)
				flag = 1;
		}
	}
	if (flag != 1)
		cout << "No";
	else
		cout << "Yes";
	cout << endl;
	return;
}

int main() {
	int n;
	cin >> n;
	while (n--)
		solve();
}

B - Serval and Inversion Magic

可以从字符串的两头扫向中间,判断每一组的两个字符是相同还是不同。不同的区间必须是连续的,才可以实现题中变换。因此只要判断有没有出现两段不同的区间即可。

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

void solve() {
	int n, a[1000];
	string s;
	cin >> n;
	cin >> s;
	int flag = 0;
	for (int i = 0; i < n / 2; i++) {
		if (s[i] != s[n - i - 1]) {
			if (flag == 2)
				flag = 3;
			else if (flag == 0)
				flag = 1;
		} else {
			if (flag == 1)
				flag = 2;
		}
	}
	if (flag == 3)
		cout << "No";
	else
		cout << "Yes";
	cout << endl;
	return;
}

int main() {
	int n;
	cin >> n;
	while (n--)
		solve();
}

C - Serval and Toxel's Arrays

答案可以通过枚举每个数字出现的次数得到。

每个数字出现的次数可以通过每个数字在多少个数列中出现得到。

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

void solve() {
	ll n, m;
	cin >> n >> m;
	ll tt[n];
	ll a[n + m + 1], cnt[n + m + 1];
	memset(a, -1, sizeof(a));
	memset(cnt, 0, sizeof(cnt));
	for (ll i = 0; i < n; i++) {
		cin >> tt[i];
		a[tt[i]] = 0;
	}
	for (ll j = 0; j < m; j++) {
		ll t1, t2;
		cin >> t1 >> t2;
		t1--;
		cnt[tt[t1]] += j + 1 - a[tt[t1]];
		a[tt[t1]] = -1;
		tt[t1] = t2;
		a[t2] = j + 1;
	}
	for (ll i = 1; i <= n + m; i++) {
		if (a[i] != -1) {
			cnt[i] += m - a[i] + 1;
		}
	}
	ll res = 0;
	for (ll i = 1; i <= n + m; i++) {
//		cout << cnt[i] << " ";
		res += m * (m + 1) / 2 - (m - cnt[i]) * (m - cnt[i] + 1) / 2;
	}
//	cout << endl;
	cout << res << endl;
	return;
}

int main() {
	int n;
	cin >> n;
	while (n--)
		solve();
}

D - Serval and Shift-Shift-Shift

首先无解的情况是有且仅有一个串为0。 

有解的情况下,我们发现进行异或操作,每一位如果与1异或才会发生改变,与0异或则不变。因此我们想到一位一位地去调整。

我们只要先把a和b最左边的位置对齐,然后对于右边剩下的位,如果a和b不同,我们就可以很容易地一位一位地调整。

因此问题的关键在于如何把最左边的位对齐。

第一种情况初始a最左侧的位在b最左侧的位右侧,这种情况我们只需要把把a左移使b最左侧的位与a最左侧的位对齐即可。

第二种情况初始a最左侧的位在在b最左侧的位左侧,这种情况稍微麻烦一点。

我们先把a中从目标位开始往左的所有位置都调整为1(可以利用a中第一个和最后一个1来完成),然后找到让目标位往后的第一个0,使用左移操作目标位对齐就能把左侧所有没用的1消除。

可以发现每位最多只调整一次,因此调整次数不会超过n。

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
queue<int> ans;
string a, b;
int n;

void update(int x) {
	ans.push(x);
	if (x > 0) {
		for (int i = x; i < n; i++) {
			if (a[i] == '1')
				a[i - x] ^= 1;
		}
	} else {
		for (int i = n - 1 + x; i >= 0; i--) {
			if (a[i] == '1')
				a[i - x] ^= 1;
		}
	}
	return;
}

void solve() {
	int x, y;
	cin >> n >> a >> b;
	if (a == b) {
		cout << 0 << endl;
		return;
	}
	if (a == string(n, '0') || b == string(n, '0')) {
		cout << -1 << endl;
		return;
	}
	x = a.find('1');
	y = b.rfind('1');
	if (a[y] == '0')
		update(x - y);
	x = a.find('1');
	for (int i = y + 1; i < n; i++)
		if (a[i] == '1')
			update(x - i);
	for (int i = y - 1; i >= 0; i--)
		if (a[i] != b[i])
			update(y - i);
	cout << ans.size() << endl;
	while (!ans.empty()) {
		cout << ans.front() << " ";
		ans.pop();
	}
	cout << endl;
	return;
}

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五百场cf灰名

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值