0
点赞
收藏
分享

微信扫一扫

Codeforces Round #779 (Div. 2)补题

天天天蓝loveyou 2022-03-30 阅读 80
c++算法

A. Marin and Photoshoot

给出一个01子串,0代表男演员,1代表女演员,要求满足每连续至少为2长度的子串中,男演员数量不多于女演员,问最少需要在子串中添加多少人。

思路: 从样例找规律发现,若要满足条件,需要每两个0之间至少有两个1,模拟即可。

AC Code:

/*********************************************************************
    Project:work.cpp
    Author:_dawn
    Time: 2022-03-27 22:22
    Mail:z1322647656@163.com
*********************************************************************/
#include <bits/stdc++.h>

template <typename T>
inline void read(T &x) {
	x = 0;
	int f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0', ch = getchar();
	}
	x *= f;
}

template <typename T>
void write(T x) {
	if (x < 0)
		putchar('-'), x = -x;
	if (x > 9)
		print(x / 10);
	putchar(x % 10 + '0');
}

#define INF 0x3f3f3f3f
typedef long long ll;
const double PI = acos(-1);
const double eps = 1e-6;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
int t, n;
std::string s;

signed main() {
//	freopen("test.in","r",stdin);
//  freopen("output.in", "w", stdout);
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cin >> t;
	while (t--) {
		std::cin >> n;
		std::cin >> s;
		int pos = -1, cnt0 = 0, ans=0;
		for (ll i = 0; i < n; i++) {
			if (s[i] == '0') {
				if (pos != -1) {
					int cnt = i - pos - 1;
					ans += std::max(2 - cnt, 0);
				}
				pos = i;
			}
		}
		std::cout << ans << '\n';
	}
}

os:A没过,,,我也不敢相信,主要是觉得要考虑n==1的情况,结果根本不用考虑www

B. Marin and Anti-coprime Permutation

给出一个从1~n的含有n个数的数组,有多少种构造方式使得其gcd>1。

 思路:使得gcd大于1,那么就考虑2,只要所有相乘的数均为偶数即可。偶数乘什么都是偶数,所以要令n为偶数,才可以满足条件;所以n为奇数时直接输出;n为偶数时,有n/2个偶数,有n/2个空位去填这些偶数,剩下的n/2个奇数也有n/2个空位,排列组合可知计算阶乘的平方即可。虽然有模数,但是注意要开long long。

AC Code:

/*********************************************************************
    Project:work.cpp
    Author:_dawn
    Time: 2022-03-27 23:33
    Mail:z1322647656@163.com
*********************************************************************/
#include <bits/stdc++.h>

template <typename T>
inline void read(T &x) {
	x = 0;
	int f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0', ch = getchar();
	}
	x *= f;
}

template <typename T>
void write(T x) {
	if (x < 0)
		putchar('-'), x = -x;
	if (x > 9)
		print(x / 10);
	putchar(x % 10 + '0');
}

#define INF 0x3f3f3f3f
typedef long long ll;
const double PI = acos(-1);
const double eps = 1e-6;
const int mod = 998244353;
const int N = 1e3 + 5;
int t, n;
ll a[N], ans[N];

void init() {
	ans[0] = 1;
	for (int i = 1; i <= 1001; i++) {
		ans[i] = (ans[i - 1] % mod * i % mod) % mod;
	}
}

int main() {
//	freopen("test.in","r",stdin);
//  freopen("output.in", "w", stdout);
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	init();
	std::cin >> t;
	while (t--) {
		std::cin >> n;
		if (n & 1)
			std::cout << 0 << '\n';
		else
			std::cout << (ans[n / 2] % mod * ans[n / 2] % mod) % mod << '\n';
	}
	return 0;
}

os:B题倒是出的很快。。。

C. Shinju and the Lost Permutation

给出一个有1~n n个数字的数组,问是否存在这样一个构造数列,使得其通过i次变化可以使得数列的power值为给定的数。

思路: 1、因为每次只移动一个数,power值的增量不会大于1,而减小量我们无法确定;2、因为一个数列里从1~n都存在,所以要存在一个状态使得power值等于1,且仅有一个值为1。满足这个条件即可。

AC Code:

/*********************************************************************
    Project:work.cpp
    Author:_dawn
    Time: 2022-03-28 18:11
    Mail:z1322647656@163.com
*********************************************************************/
#include <bits/stdc++.h>

template <typename T>
inline void read(T &x) {
	x = 0;
	int f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0', ch = getchar();
	}
	x *= f;
}

template <typename T>
void write(T x) {
	if (x < 0)
		putchar('-'), x = -x;
	if (x > 9)
		print(x / 10);
	putchar(x % 10 + '0');
}

#define INF 0x3f3f3f3f
typedef long long ll;
const double PI = acos(-1);
const double eps = 1e-6;
const int mod = 1e9 + 7;
const int N = 1e5 + 5;
int t, n;
int a[N];

int main() {
//	freopen("test.in","r",stdin);
//  freopen("output.in", "w", stdout);
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cin >> t;
	while (t--) {
		std::cin >> n;
		bool flag = true;
		int cnt = 0;
		for (int i = 1; i <= n; i++) {
			std::cin >> a[i];
			if (a[i] == 1)
				cnt++;
		}
		if (cnt > 1 || cnt == 0) {
			std::cout << "NO" << '\n';
			continue;
		}
		a[0] = a[n];
		for (int i = 1; i <= n; i++) {
			if (a[i] > a[i - 1] && a[i] - a[i - 1] > 1) {
				flag = false;
				break;
			}
		}
		std::cout << (flag ? "YES" : "NO") << '\n';
	}
	return 0;
}

os:一开始觉得数列满足条件或不满足条件与数列增长或者某个数有关,,,没搞出来

D1. 388535 (Easy Version)

给出l和r,然后得到一个r-l+1长度的数列,找出一个x,使得一个permutation数列以任意顺序构造得到的数列每个数异或x得到给出的序列。

思路: 计算给出的序列中所有数每一位1的个数,若1的个数不等于原序列该位1的个数和,则x该位一定为1,因为若是1的个数不同,则需要x的这一位1来改变其1的个数;若相等则0或者1都可以,因为0和1的选择仅影响序列顺序,并不影响结果。

AC Code:

/*********************************************************************
    Project:work.cpp
    Author:_dawn
    Time: 2022-03-28 18:11
    Mail:z1322647656@163.com
*********************************************************************/
#include <bits/stdc++.h>

template <typename T>
inline void read(T &x) {
	x = 0;
	int f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0', ch = getchar();
	}
	x *= f;
}

template <typename T>
void write(T x) {
	if (x < 0)
		putchar('-'), x = -x;
	if (x > 9)
		print(x / 10);
	putchar(x % 10 + '0');
}

#define INF 0x3f3f3f3f
typedef long long ll;
const double PI = acos(-1);
const double eps = 1e-6;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
int t, l, r;
int a[N];

int main() {
//	freopen("test.in","r",stdin);
//  freopen("output.in", "w", stdout);
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cin >> t;
	while (t--) {
		std::cin >> l >> r;
		int n = r - l + 1;
		for (int i = l; i <= r; i++) {
			std::cin >> a[i];
		}
		int x = 0;
		for (int i = 16; i >= 0; i--) {
			int cnt0 = 0, cnt1 = 0;
			for (int j = l; j <= r; j++) {
				if (a[j] >> i & 1)
					cnt0++;
				if (j >> i & 1)
					cnt1++;
			}
			if (cnt0 != cnt1) {
				x += (1 << i);
			}
		}
		std::cout << x << '\n';
	}
	return 0;
}

os:位运算还需要加强,有时候见到异或啊什么的就慌了QAQ

若有错误请指教orzorz

举报

相关推荐

0 条评论