2025.4.22 赛后总结
__DragonFly · · 个人记录
P4826 [USACO15FEB] Superbull S
错误代码
#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
int a[N], fa[N];
int n, m;
struct node
{
int u, v;
int val;
}e[N * N];
void inti()
{
for (int i = 1; i <= n; ++i) fa[i] = i;
return ;
}
int find(int u)
{
if (fa[u] == u) return u;
return fa[u] = find(fa[u]);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
inti();
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i)
{
for (int j = i + 1; j <= n; ++j)
{
m++;
e[m].u = i;
e[m].v = j;
e[m].val = a[i] ^ a[j];
}
}
sort(e + 1, e + 1 + m, [](const node&x, const node&y)
{
return x.val > y.val;
});
int sum = 0, cnt = 0;
for (int i = 1; i <= m; ++i)
{
int t1 = find(e[i].u), t2 = find(e[i].v);
if (t1 == t2) continue;
sum += e[i].val;
fa[t1] = t2;
cnt++;
if (cnt == n - 1)
{
cout << sum;
break;
}
}
return 0;
}
错误原因
注意题目范围队伍ID在
正确代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2005;
int a[N], fa[N];
int n, m;
struct node
{
int u, v;
int val;
}e[N * N];
void inti()
{
for (int i = 1; i <= n; ++i) fa[i] = i;
return ;
}
int find(int u)
{
if (fa[u] == u) return u;
return fa[u] = find(fa[u]);
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
inti();
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i)
{
for (int j = i + 1; j <= n; ++j)
{
m++;
e[m].u = i;
e[m].v = j;
e[m].val = a[i] ^ a[j];
}
}
sort(e + 1, e + 1 + m, [](const node&x, const node&y)
{
return x.val > y.val;
});
int sum = 0, cnt = 0;
for (int i = 1; i <= m; ++i)
{
int t1 = find(e[i].u), t2 = find(e[i].v);
if (t1 == t2) continue;
sum += e[i].val;
fa[t1] = t2;
cnt++;
if (cnt == n - 1)
{
cout << sum;
break;
}
}
return 0;
}
P1582 倒水
题目思路
如果有
那么我们把
code
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, k;
int get_1(int x)
{
int cnt = 0;
for (int i = 32; i >= 0; i--)
{
if ((x >> i) & 1)
{
cnt++;
}
}
return cnt;
}
int get_di1(int x)
{
for (int i = 0; i <= 32; i++)
{
if ((x >> i) & 1)
{
return i;
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> k;
int ans = 0;
while (get_1(n) > k)
{
int tmp = get_di1(n);
ans += (1ll << tmp);
n += (1ll << tmp);
}
cout << ans;
return 0;
}
P10577 [蓝桥杯 2024 国 A] 兔子集结
题目思路
每只兔子会追离自己最近的那只兔子,那么每只兔子有以下几种情况。
- 1. 第
a 只兔子追着第b 只兔子,第b 只兔子追着第a 只兔子,那么它们会在第a 只兔子和第b 只兔子的位置的中点相遇,因为每只兔子的速度一致(每次只能移动一个位置)。 - 2. 第
a 只兔子追着第b 只兔子,但第c 只兔子追着第d 只兔子。因为每只兔子的速度一致(每次只能移动一个位置),所以第a ,b ,c ,d 兔子无论如何都无法相遇,除非第d 只兔子准备去追第c 只兔子(第1 种情况),那么a ,b ,c ,d 都会相遇。
那么根据上面的情况,我们可以用并查集来维护兔子的追逐情况和一
code
#include <bits/stdc++.h>
using namespace std;const int N = 1e5 + 5;int fa[N];int a[N];int n;
struct node
{
int pos;
int id;
}s[N];
int find(int u)
{
if (fa[u] == u) return u;
return fa[u] = find(fa[u]);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> s[i].pos;
s[i].id = i;
}
// 按位置从小到大排序,方便后面判断追逐哪只兔子
sort(s + 1, s + 1 + n, [](const node&x, const node& y)
{
return x.pos < y.pos;
});
fa[1] = 2; // 第1只兔子只可能会追逐第2只兔子
fa[n] = n - 1;// 第n只兔子只可能会追逐第n-1只兔子
for (int i = 2; i <= n - 1; ++i)
{
// 看离哪只兔子近就追那只
int l = s[i - 1].pos, r = s[i + 1].pos;
if (s[i].pos - l <= r - s[i].pos) fa[i] = i - 1;
else fa[i] = i + 1;
}
for (int i = 1; i < n; ++i)
{
if (fa[i] == i + 1 && fa[i + 1] == i)
{
// 第1种情况
fa[i] = i;
fa[i + 1] = i + 1;
s[i + 1].pos = s[i].pos = (s[i].pos + s[i + 1].pos) / 2;
}
}
for (int i = 1; i <= n; ++i)
{
if (fa[i] != i)
{
// 第i只兔子最终会停留在族长那里
s[i].pos = s[find(i)].pos;
}
}
// 按照输入顺序排序,方便输出
sort(s + 1, s + 1 + n, [](const node&x, const node& y)
{
return x.id < y.id;
});
for (int i = 1; i <= n; ++i) cout << s[i].pos << ' ';
return 0;
}