Daily Practice 9th:Codeforces Round #781 (Div. 2)

本文分享了三道算法竞赛题目及其解决方案。第一题通过构造找到四个数,使得它们的和为给定值且满足特定条件。第二题讨论如何通过复制和交换操作使数组元素全部相等。第三题涉及树的感染问题,寻找最小感染时间。每道题均提供AC代码和解题思路。
摘要由CSDN通过智能技术生成

VP*9;

rating:800--900--1600--2000

A. GCD vs LCM

给出一个数n,找到四个数,使得四个数和为n,且均分为两组,其中一组的最大公因数等于另一组的最小公倍数。

思路:构造,GCD(1,N-3)=LCM(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, 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;
		std::cout << 1 << ' ' << n - 3 << ' ' << 1 << ' ' << 1 << '\n';
	}
	return 0;
}

os:笑死,一开始只想到一边是1,1,没想到另一边可以这样构造

B. Array Cloning Technique

给出一个数组,有两种操作,1:把任意一个数组复制一遍;2:把现有的数组中任意两个数互换,问最少进行几次操作,使得数组中所有数都相等。

思路: 最优选择肯定是令所有数都等于数组中一开始出现次数最多的那个,通过两个操作,模拟即可。

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 = 1e5 + 5;
ll t, n, 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;
		std::map<int, int>mp;
		ll maxn = -1, pos;
		for (int i = 1; i <= n; i++) {
			std::cin >> a[i];
			mp[a[i]]++;
			if (mp[a[i]] > maxn) {
				maxn = mp[a[i]], pos = a[i];
			}
		}
		if (maxn == n) {
			std::cout << 0 << '\n';
			continue;
		}
		int cnt = 0, res = n - maxn, tt = maxn ;
		while (res > 0) {
			if (res > tt) {
				res -= tt;
				cnt++, cnt += tt;
				tt *= 2;
			} else {
				cnt++, cnt += res;
				res = 0;
				tt *= 2;
			}
		}
		std::cout << cnt << '\n';
	}
	return 0;
}

C. Tree Infection

对于一棵树,我们可以选择一个点感染它,而与该点有相同父节点的节点也会被传染,每一秒可以感染一个节点,也可以同时传染一个节点,问整棵树被感染最少的时间。

思路: 首先感染的时候我们挑选子节点多的先感染,这样可以最大化利用传染的时间;对于答案,我们二分值域,就是mid时间是否可以感染所有节点。对于每个mid时间,我们去感染的时间点最多只能感染mid-1个兄弟节点,根据时间和被感染节点的关系写check函数。

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, a[N], x, cnt;
std::vector<int>vec[N];

bool check(int mid) {
	int res = 0;
	for (int i = 1, j = mid - 1; i <= cnt; i++, j--) {
		res += std::max(0, a[i] - j);
	}
	return mid - cnt >= res;
}

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++) {
			vec[i].clear();
		}
		for (int i = 2; i <= n; i++) {
			std::cin >> x;
			vec[x].push_back(i);
		}
		cnt = 1;
		a[1] = 0;
		for (int i = 1; i <= n; i++) {
			if (vec[i].size())
				a[++cnt] = (vec[i] .size() - 1);
		}
		std::sort(a + 1, a + 1 + cnt, std::greater<int>());
		int l = cnt, r = n;
		while (l < r) {
			int mid = (l + r) >> 1;
			if (check(mid))
				r = mid;
			else
				l = mid + 1;
		}
		std::cout << r << '\n';
	}
	return 0;
}

D. GCD Guess

最多可以询问30次,每次询问选择a和b,两个不同的正整数,回答的是GCD(x+a,x+b),求x的值。

思路:根据GCD性质,我们知道GCD(a,b)=GCD(a,b-a),所以询问的就是GCD(x+a,b-a),就是询问x+a与任意一个数的GCD,30次询问,我们可以想到(我没想到)二分和按位计算,我们按照数据范围可以知道,1e9的数字是可以在30次询问以内得到二进制每一位的数字的,怎样求每一位是什么呢,我们可以假设已经求出来的数位是x_{t},令a=2^{t+1}-x_{t},这样x的后t位就都可以确定了,也就是都为0,且还向前进了一位,则如果第t+1位是0,则x+a位是1,若为1,则因为进位就是0,方法是使b-a=2^{t+2},若x+a第t+1位为1,则得到的GCD是2^{t+1},否则为2^{t+2}

代码鸽了。。。 参考

若有错误请指教,谢谢!

orzorz 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值