Codeforces Round #445 (Div. 2, based on Technocup 2018 Elimination Round 3)

本文解析了四道经典的算法题目,包括使用二进制枚举解决等分问题、通过图论方法寻找最优解等,提供了详细的代码实现及思路说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A

直接二进制枚举所有情况

#include<bits/stdc++.h>

using namespace std;
#define LL long long
#define mst(a, b)	memset(a, b, sizeof a)
#define pb push_back
#define mk makr_pair
#define pill pair<int, int>
#define fi first
#define se second

const int qq = 1e5 + 10;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 10;
int a[10];

int main() {
	for(int i = 0; i < 6; ++i) {
		scanf("%d", a + i);
	}
	bool f = false;
	for(int i = 0; i < (1 << 6); ++i) {
		int x = 0, y = 0;
		int cnt = 0;
		for(int j = 0; j < 6; ++j) {
			if(i & (1 << j))	x += a[j], cnt++;
			else	y += a[j];
		}
		if(cnt == 3 && x == y)	f = true;
	}
	printf("%s\n", f ? "YES" : "NO");
	return 0;
}


B

说实话这题是猜的题意

#include<bits/stdc++.h>

using namespace std;
#define LL long long
#define mst(a, b)	memset(a, b, sizeof a)
#define pb push_back
#define mk makr_pair
#define pill pair<int, int>
#define fi first
#define se second

const int qq = 2e5 + 10;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 10;
bool vis[qq];
int num[qq];
set<int> st;

int main() {
	int n;	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) {
		scanf("%d", num + i);
		vis[num[i]] = true;
		st.insert(num[i]);
	}
	int cnt = st.size(), ans;
	for(int i = n; i >= 1; --i) {
		if(vis[num[i]]) {
			vis[num[i]] = false;
			cnt--;
			if(cnt == 0) {
				ans = num[i];
				break;
			}
		}
	}
	printf("%d\n", ans);
	return 0;
}


C

#include<bits/stdc++.h>

using namespace std;
#define LL long long
#define mst(a, b)	memset(a, b, sizeof a)
#define pb push_back
#define mk makr_pair
#define pill pair<int, int>
#define fi first
#define se second

const int qq = 2e5 + 10;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 10;
vector<int> G[qq];
bool vis[qq];
void Dfs(int u) {
	vis[u] = true;
	int sz = (int)G[u].size();
	for(int i = 0; i < sz; ++i) {
		int v = G[u][i];
		if(vis[v])	continue;
		Dfs(v);
	}
}

int main() {
	int n;	scanf("%d", &n);
	for(int x, i = 1; i <= n; ++i) {
		scanf("%d", &x);
		G[i].pb(x);
	}
	int cnt = 0;
	for(int i = n; i >= 1; --i) {
		if(!vis[i]) {
			cnt++;
			Dfs(i);
		}
 	}
 	printf("%d\n", cnt);
	return 0;
}


D

这题我真的想了好久、看了题解才发现是转化成图论题

参考大牛博客:黑猫

我这里简单说一点吧,首先题意要求所给字符串在答案串的子串中出现的次数是最多的,那么我们这里不得不考虑一个问题了,就是在答案串中单个字符组成的子串,所以我们可以肯定所给的n个字符串在答案串中作为子串出现的次数肯定是1,进而我们可以肯定答案串最长只有26并且没有重复字符。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>

using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define lson (rt << 1)
#define rson ((rt << 1) | 1)
#define pill pair<int, int>
#define mst(a, b)	memset(a, b, sizeof a)
#define REP(i, x, n)	for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 1e5 + 10;
const int INF = 1e9 + 10;
char ind[30], outd[30], jest[30], edge[30][30];
vector<int> G[30];
string ans;
void no() {
	cout << "NO\n";
	exit(0);
}
void getSt(int u) {
	ans += (u + 'a');
	jest[u] = 0;
	for(int i = 0; i < G[u].size(); ++i) {
		getSt(G[u][i]);
	}
}

int main(){
	std::ios::sync_with_stdio(false);
	int n;	cin >> n;
	string s;
	for(int i = 0; i < n; ++i) {
		cin >> s;
		for(int j = 0; j + 1 < s.size(); ++j) {
			int a = s[j] - 'a', b = s[j + 1] - 'a';
			if(!edge[a][b]) {
				jest[a] = jest[b] = 1;
				ind[b]++, outd[a]++;
				edge[a][b] = 1;
				G[a].pb(b);
			}
		}
		jest[s[0] - 'a'] = 1;
	}
	for(int i = 0; i < 26; ++i) {
		if(ind[i] > 1 || outd[i] > 1)	no();
	}
	for(int i = 0; i < 26; ++i) {
		if(!ind[i] && jest[i])	getSt(i);
	}
	for(int i = 0; i < 26; ++i) {
		if(jest[i])	no();
	}
	cout << ans << endl;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值