A
暴力划分2/2,1/3判定
#include <bits/stdc++.h>
using namespace std;
int main()
{
//freopen("in.txt", "r", stdin);
int a[5], sum = 0;
for (int i = 0; i < 4; ++i) {
cin >> a[i];
sum += a[i];
}
if (sum & 1) {
cout << "NO" << endl;
return 0;
}
for (int i = 0; i < 4; ++i) {
for (int j = i + 1; j < 4; ++j) {
if (a[i] + a[j] == sum / 2) {
cout << "YES" << endl;
return 0;
}
}
}
for (int i = 0; i < 4; ++i)
if (a[i] == sum / 2) {
cout << "YES" << endl;
return 0;
}
cout << "NO" << endl;
}
B
特判n=1的情况
#include <bits/stdc++.h>
using namespace std;
int main()
{
//freopen("in.txt", "r", stdin);
int a[5], sum = 0;
for (int i = 0; i < 4; ++i) {
cin >> a[i];
sum += a[i];
}
if (sum & 1) {
cout << "NO" << endl;
return 0;
}
for (int i = 0; i < 4; ++i) {
for (int j = i + 1; j < 4; ++j) {
if (a[i] + a[j] == sum / 2) {
cout << "YES" << endl;
return 0;
}
}
}
for (int i = 0; i < 4; ++i)
if (a[i] == sum / 2) {
cout << "YES" << endl;
return 0;
}
cout << "NO" << endl;
}
C
考虑到n=7,点数最多6,直接暴力染色统计答案
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10;
pair<int, int> E[maxn * maxn];
int color[maxn], ans;
int n, m;
void calc() {
set< pair<int, int> > a;
for (int i = 0; i < m; ++i) {
int u = color[E[i].first], v = color[E[i].second];
if (u > v) swap(u, v);
if (a.find(make_pair(u, v)) == a.end()) {
a.insert(make_pair(u, v));
}
}
ans = max(ans, (int)a.size());
}
void dfs(int u) {
if (u == n + 1) {
calc();
return;
}
for (int i = 1; i <= 6; ++i) {
color[u] = i;
dfs(u + 1);
}
}
int main()
{
//freopen("in.txt", "r", stdin);
cin >> n >> m;
for (int i = 0; i < m; ++i) {
cin >> E[i].first >> E[i].second;
}
dfs(1);
cout << ans << endl;
}
D
审题后发现要形成答案,队内必须出现值相同的情况,考虑出现次数大于1的数和等于1的数
对每个次数大于1的数作为全队最强去找其他数是否存在认为比他更聪明的情况,存在即加入进答案
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 7000 + 5;
ll a[maxn];
ll b[maxn];
map<ll, int> pre;
int fa[maxn];
int find(int x) {
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
int vis[maxn], block[maxn], one[maxn];
int main()
{
//freopen("in.txt", "r", stdin);
int n; cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
fa[i] = i;
}
for (int i = 1; i <= n; ++i)
cin >> b[i];
int tot = 0, totone = 0;
for (int i = 1; i <= n; ++i) {
if (pre.count(a[i])) {
int fu = pre[a[i]], fv = i;
fa[fv] = fu;
b[fu] += b[fv];
if (!vis[fu]) vis[fu] = 1, block[tot++] = fu;
}
else pre[a[i]] = i;
}
for (int i = 1; i <= n; ++i) {
if (!vis[find(i)]) one[totone++] = i;
}
if (!tot) {
cout << 0 << endl;
return 0;
}
ll ans = 0;
memset(vis, 0, sizeof(vis));
for (int i = 0; i < tot; ++i) {
if (vis[block[i]]) continue;
vis[block[i]] = 1;
ans += b[block[i]];
for (int j = 0; j < tot; ++j) {
if (vis[block[j]]) continue;
if ((a[block[j]] | a[block[i]]) == a[block[i]]) {
ans += b[block[j]];
vis[block[j]] = 1;
}
}
for (int j = 0; j < totone; ++j) {
if (vis[one[j]]) continue;
if((a[one[j]] | a[block[i]]) == a[block[i]]) {
ans += b[one[j]];
vis[one[j]] = 1;
}
}
}
cout << ans << endl;
}