Daily Practice 7th:Codeforces Round #754 (Div. 2)

这篇博客探讨了数学和算法在信息技术领域的几个典型问题,包括VP*7问题的解决策略、01字串的排序优化、寻找字符串中最短的特定子串以及树的特殊标记问题。通过具体代码实现,展示了如何运用数学思维和算法技巧解决实际问题。
摘要由CSDN通过智能技术生成

VP*7;

rating:800--1000--1400--2100

A. A.M. Deviation

给出三个数,这三个数的arithmetic是其中两个数的和与第三个数两倍的差的绝对值,给出一种操作,即随意在三个数中选择两个,其中一个-1,另一个+1,问给出的三个数经过若干次操作后arithmetic最小是多少。

思路: 我们可以看这种操作的实质:若是选择两个需要相加的数,则没有任何变化;若是选择一个需要相加的数,另一个是要乘2的数,则对arithmetic的贡献是+3或-3,那就和对3取模联系上了:最大数和最小数之和与第二个数的差的绝对值若是可以整除3,则可以通过若干次操作使得arithmetic=0,否则可以是余数1或2,但是当2的时候可以再进行一次操作使得-2或+2变成+1或-1,那代码就很好写了。

AC Code:

#include <bits/stdc++.h>
#pragma GCC optimize(2)

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)
		write(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;
ll a[4];

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 >> a[1] >> a[2] >> a[3];
		std::sort(a + 1, a + 4);
		if (a[1] + a[3] == 2 * a[2])
			std::cout << 0 << '\n';
		else if (abs(a[1] + a[3] - 2 * a[2]) % 3 == 0) {
			std::cout << 0 << '\n';
		} else
			std::cout << 1 << '\n';
	}
	return 0;
}

B. Reverse Sort

 给出一个01字串,目的是最后把它排序乘非递减序列,每次可以选择一个长度为k的子序列,把它替换为它的回文列,问最少需要几次操作使得达到目的,并输出选择的k和子序列。

思路:直接把原字符串排序,一位一位比较,不同的拿出来进行操作,这就是要选择的子序列,只要需要操作的,都是一次即可完成。这个是可以证明的:需要交换顺序的就是应该是1的0和应该是0的1,而应该是1的0都是在后面的,应该是0的1都是在前面的,这样交换一次顺序即可;注意原数组满足条件时,直接输出0。

AC Code:

#include <bits/stdc++.h>
#pragma GCC optimize(2)

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)
		write(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 = 1e3 + 5;
int t, n;
int cnt1[N], cnt0[N];
std::string s;

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 >> s;
		std::string c = s;
		std::vector<int>vec;
		bool flag = true;
		std::sort(c.begin(), c.end());
		for (int i = 0; i < n - 1; i++) {
			if (s[i] == '1' && s[i + 1] == '0') {
				flag = false;
				break;
			}
		}
		if (flag) {
			std::cout << 0 << '\n';
			continue;
		}
		for (int i = 0; i < n; i++) {
			if (c[i] != s[i]) {
				vec.push_back(i + 1);
			}
		}
		std::cout << 1 << '\n';
		std::cout << vec.size() << ' ';
		for (int i = 0; i < vec.size(); i++) {
			std::cout << vec[i] << " \n"[i == vec.size() - 1];
		}
	}
	return 0;
}

C. Dominant Character

给出一个仅有a,b,c的字符串s,找到一个最短的子串,使得其中a的数量严格大于b和c的数量。 

思路:看的别人的题解,真没想到这个可以枚举所有的可能情况,一共有七种可能的情况,最长的满足条件的子串长度为7,列举然后在原字符串内查找即可,可以很容易知道,其他满足条件的,大于7的子串,这七个中,一定有它的子串。

AC Code:

#include <bits/stdc++.h>
#pragma GCC optimize(2)

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)
		write(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 = 1e3 + 5;
int t, n;
std::string s;
std::string a = "aa";
std::string b = "aba";
std::string c = "aca";
std::string d = "abca";
std::string e = "acba";
std::string f = "abbacca";
std::string g = "accabba";

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 >> s;
		int m;
		m = s.find(a);
		if (m != -1) {
			std::cout << 2 << '\n';
			continue;
		}
		m = s.find(b);
		if (m != -1) {
			std::cout << 3 << '\n';
			continue;
		}
		m = s.find(c);
		if (m != -1) {
			std::cout << 3 << '\n';
			continue;
		}
		m = s.find(d);
		if (m != -1) {
			std::cout << 4 << '\n';
			continue;
		}
		m = s.find(e);
		if (m != -1) {
			std::cout << 4 << '\n';
			continue;
		}
		m = s.find(f);
		if (m != -1) {
			std::cout << 7 << '\n';
			continue;
		}
		m = s.find(g);
		if (m != -1) {
			std::cout << 7 << '\n';
			continue;
		}
		std::cout << -1 << '\n';
	}
	return 0;
}

D. Treelabeling

给出一棵树,每个节点都有一个数,先手后手轮流移动一个棋子,移动的条件:仅可以移动到相邻的节点;这个节点的权与原节点的权的异或小于等于两数的最小值;这个节点没被访问过。若谁无法操作了,那谁输。问怎样给n个节点从1~n赋值,使得先手胜利的概率更大。 

思路:根据异或的性质,若是两个数的异或要小于这两个数,那两个数的最高位一定相同,即最高位都是1。那我们考虑一种极端情况,即任何一个节点都不能移动,就是所有相邻的节点的异或都不满足移动的条件,这样先手必胜。那我们需要按照奇偶层节点分成两个集合,把最高位相同的分开放在两个集合中。

AC Code:

#include <bits/stdc++.h>
#pragma GCC optimize(2)

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)
		write(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 = 2e5 + 5;
int t, n, d[33], mp[N];
std::vector<int>G[N], V[2];
std::vector<int>D[33];

void dfs(int u, int fa, int oe) {//分离奇偶层
	V[oe].push_back(u);
	for (auto v : G[u]) {
		if (v != fa)
			dfs(v, u, oe ^ 1);//两个数交替赋值时考虑位运算可以使代码简洁
	}
}

int cal(int x) {
	int ans = 0;
	while (x) {
		x >>= 1;
		++ans;
	}
	return ans;
}

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;
		for (int i = 1; i <= n; i++) {
			G[i].clear();
		}
		for (int i = 1; i < n; i++) {
			int u, v;
			std::cin >> u >> v;
			G[u].push_back(v);
			G[v].push_back(u);
		}
		V[0].clear();
		V[1].clear();
		dfs(1, 0, 0);
		for (int i = 0; i < 33; i++) {
			D[i].clear();
			d[i] = i;
		}
		for (int i = 1; i <= n; i++) {
			D[cal(i)].push_back(i);
		}
		std::sort(d, d + 33, [](int na, int nb) {//新的排序方式!学习了
			return D[na].size() > D[nb].size();
		});
		for (int i = 0; i < 33; i++) {//给每个点赋值,并不严格考虑边界问题
			if (V[0].size() > V[1].size()) {
				while (!D[d[i]].empty()) {
					mp[V[0].back()] = D[d[i]].back();
					V[0].pop_back();
					D[d[i]].pop_back();
				}
			} else {
				while (!D[d[i]].empty()) {
					mp[V[1].back()] = D[d[i]].back();
					V[1].pop_back();
					D[d[i]].pop_back();
				}
			}
		}
		for (int i = 1; i <= n; i++) {
			std::cout << mp[i] << " \n"[i == n];
		}
	}
	return 0;
}

os:这种难度的题目就算有了想法也不太知道怎么写hhh,学习别人代码,练习码力qwq

若有错误请指教,谢谢!

orzorz

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值