Codeforces Round #670 (Div. 2) Solved: 3 out of 5

Codeforces Round #670 (Div. 2)


A. Subset Mex

题意: 有一个长度为n的数组,要求将数组拆分成两个集合A、B,求最大的mex(A) + mex(B)。mex为集合中没有出现过的最小非负整数。

题解: 优先把第一次出现的数分配到A中,出现第二次则分配到B中。即可求出最大的mex和。

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
 
using namespace std;
typedef long long ll;
int _T, n, x;
void run(){
	cin >> n;
	int ansa = 0, ansb = 0;
	int vis[2][110] = {0};
	for(int i = 0; i < n; i++){
		cin >> x;
		if(!vis[0][x])	vis[0][x] = 1;
		else vis[1][x] = 1;
	}
	while(vis[0][ansa]) ++ansa;
	while(vis[1][ansb]) ++ansb;
	cout << ansa + ansb << endl;
}
 
int main(){
	IOS;
	cin >> _T;
	while(_T--)	run();
	return 0;
}

B. Maximum Product

题意: 有一个长度为n的数组,要求选取其中的五个数,求出最大的积。

题解: 贪心+枚举。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)

const int inf = 0x3f3f3f3f;
const int N = 1e5 + 10;
inline int nextInt(){int x; cin >> x; return x;}
void run(){
	int n = nextInt();
	ll a[N], maxa = -inf;;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		if(maxa < a[i])	maxa = a[i];
	}
	sort(a + 1, a + n + 1, [](ll a, ll b){return abs(a) > abs(b);});
	if(maxa < 0){
		cout << a[n] * a[n - 1] * a[n - 2] * a[n - 3] * a[n - 4] << endl;
		return;
	}
	ll ans = 1;
	for(int i = 1; i <= 5; i++)	ans *= a[i];
	for(int i = 6; i <= n; i++){
		for(int j = 1; j <= 5; j++){
			ll tmp = a[i];
			for(int k = 1; k <= 5; k++){
				if(j != k)	tmp *= a[k];
			}
			ans = max(ans, tmp);
		}
	}
	cout << ans << endl;
}
int main(){
	IOS;
	int _T = nextInt();
	while(_T--)	run();
	return 0;
}

C. Link Cut Centroids

题意: 让你删去一条边,再添加一条边,使得这个树的重心只有一个。
题解: 预备知识参考树的重心。如果树本身就只有一个重心,那么就随便删除一条边即可。如果有两个重心,那么在其中一个重心上找到一个直连点不是另一个重心,删除连另外一个就好了。

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
inline int nextInt(){int x; cin >> x;return x;}

const int N = 1e5 + 10;
int n, cnt, r1, r2;
int head[N], dp[N];
struct edge{	int v, next;} e[N << 1];

void addedge(int u, int v){
	e[cnt].v = v, e[cnt].next = head[u]; head[u] = cnt++;
	e[cnt].v = u, e[cnt].next = head[v]; head[v] = cnt++;
}
void dfs(int u, int fa){
	dp[u] = 1;
	int maxs = 0;
	for(int i = head[u]; ~i; i = e[i].next){
		int v = e[i].v;
		if(v != fa){
			dfs(v, u);
			dp[u] += dp[v];
			maxs = max(maxs, dp[v]);
		}
	}
	maxs = max(maxs, n - dp[u]);
	if((maxs << 1) <= n)	
		r2 = r1, r1 = u;
}
void run(){
	cin >> n;
	for(int i = 0; i < N; i++)	head[i] = -1;
	for(int i = 1; i < n; i++){
		int u = nextInt(), v = nextInt();
		addedge(u, v);
	}
	r1 = r2 = 0;
	dfs(1, -1);
	if(r2 == 0){
		cout << "1 " << e[head[1]].v << endl << "1 " << e[head[1]].v << endl;
	}else{
		int tmp = r1;
		for(int i = head[r2]; ~i; i = e[i].next){
			tmp = e[i].v;
			if(tmp != r1)	break;
		}
		cout << tmp << " " << r2 << endl << tmp << " " << r1 << endl;
	}
}
int main(){
	IOS;
	int _T = nextInt();
	while(_T--) run();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值