2025.5.19赛后总结
__DragonFly · · 个人记录
P10901 [蓝桥杯 2024 省 C] 封闭图形个数
错误代码
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int a[N];
bool vis[N];
int n;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
int maxx = -1;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
maxx = max(maxx, a[i]);
}
int sum = 0;
for (int i = 1; i <= n; ++i)
{
int ans = 0, cnt = 1, j = i;
for (int k = 1; k <= n; ++k) vis[k] = 0;
while (1)
{
if (j > n) j = 1;
if (cnt > maxx) break;
if (a[j] == cnt)
{
if (vis[j] != 1)
{
ans += a[j];
cnt = 1;
vis[j] = 1;
}
}
else
{
cnt++;
}
j++;
}
sum = max(ans, sum);
}
cout << sum;
return 0;
}
错误原因
遗漏条件:用一个变量存储收入囊中的卡片数量的数量,如果数量等于 continue;j++。
P2712 摄像头
错误代码
#include <bits/stdc++.h>
using namespace std;
const int N = 505;
int cnt[N];
bool vis[N];
queue <int> q;
vector <int> v[N];
int n, sum, w;
void topo()
{
for (int i = 1; i <= 500; ++i)
{
if (cnt[i] == 0 && vis[i] == 1)
{
q.push(i);
}
}
while (!q.empty())
{
int H = q.front();
q.pop();
sum++;
int len = v[H].size();
for (int i = 0; i < len; ++i)
{
int u = v[H][i];
cnt[u]--;
if (cnt[u] == 0) q.push(u);
}
}
return ;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i)
{
int x, m, y;
cin >> x >> m;
if (vis[x] == 0) w++;
vis[x] = 1;
for (int j = 1; j <= m; ++j)
{
cin >> y;
v[x].push_back(y);
if (vis[y] == 0) w++;
vis[y] = 1;
cnt[y]++;
}
}
topo();
if (sum == n) cout << "YES";
else cout << w - sum;
return 0;
}
错误原因
因为我们拓扑的是位置,所以sum不能随便加
#include <bits/stdc++.h>
using namespace std;
const int N = 505;
vector <int> v[N];
queue <int> q;
int num[505];
bool G[505];
int n, cnt;
void topo()
{
for (int i = 0; i <= 500; ++i)
{
if (num[i] == 0)
{
q.push(i);
}
}
while (!q.empty())
{
int H = q.front();
q.pop();
if (G[H] == 1) cnt--;
int len = v[H].size();
for (int i = 0; i < len; ++i)
{
num[v[H][i]]--;
if (num[v[H][i]] == 0) q.push(v[H][i]);
}
}
return ;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
cnt = n;
for (int i = 1; i <= n; ++i)
{
int u, m;
cin >> u >> m;
G[u] = 1;
for (int j = 1; j <= m; ++j)
{
int y;
cin >> y;
v[u].push_back(y);
num[y]++;
}
}
topo();
if (cnt <= 0) cout << "YES";
else cout << cnt;
return 0;
}
P1518 [USACO2.4] 两只塔姆沃斯牛 The Tamworth Two
错误代码
#include <bits/stdc++.h>
using namespace std;
const int N = 15;
char a[N][N];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int n = 10, m = 10, fc = 2, ff = 2;
int cx, cy, fx, fy;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
cin >> a[i][j];
if (a[i][j] == 'C') cx = i, cy = j;
if (a[i][j] == 'F') fx = i, fy = j;
}
}
int cnt = 0;
while (!(cx == fx && cy == fy))
{
cnt++;
if (cnt == 100000000)
{
cout << 0;
return 0;
}
int ncx = cx + dx[fc], ncy = cy + dy[fc];
if (ncx < 1 || ncx > n || ncy < 1 || ncy > m)
{
fc++;
if (fc == 4) fc = 0;
}
else if (a[ncx][ncy] == '*')
{
fc++;
if (fc == 4) fc = 0;
if (a[cx + dx[fc]][cy + dy[fc]] == '*') continue;
}
else
{
cx += dx[fc], cy += dy[fc];
}
int nfx = fx + dx[ff], nfy = fy + dy[ff];
if (nfx < 1 || nfx > n || nfy < 1 || nfy > m)
{
ff++;
if (ff == 4) ff = 0;
}
else if (a[nfx][nfy] == '*')
{
ff++;
if (ff == 4) ff = 0;
if (a[fx + dx[ff]][fy + dy[ff]] == '*') continue;
}
else
{
fx += dx[ff], fy += dy[ff];
}
}
cout << cnt;
return 0;
}
错误原因
第
#include <bits/stdc++.h>
using namespace std;
const int N = 15;
char a[N][N];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int n = 10, m = 10, fc = 2, ff = 2;
int cx, cy, fx, fy;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
cin >> a[i][j];
if (a[i][j] == 'C') cx = i, cy = j;
if (a[i][j] == 'F') fx = i, fy = j;
}
}
int cnt = 0;
while (!(cx == fx && cy == fy))
{
cnt++;
if (cnt == 100000000)
{
cout << 0;
return 0;
}
int ncx = cx + dx[fc], ncy = cy + dy[fc];
if (ncx < 1 || ncx > n || ncy < 1 || ncy > m)
{
fc++;
if (fc == 4) fc = 0;
}
else if (a[ncx][ncy] == '*')
{
fc++;
if (fc == 4) fc = 0;
// if (a[cx + dx[fc]][cy + dy[fc]] == '*') continue;
}
else
{
cx += dx[fc], cy += dy[fc];
}
int nfx = fx + dx[ff], nfy = fy + dy[ff];
if (nfx < 1 || nfx > n || nfy < 1 || nfy > m)
{
ff++;
if (ff == 4) ff = 0;
}
else if (a[nfx][nfy] == '*')
{
ff++;
if (ff == 4) ff = 0;
if (a[fx + dx[ff]][fy + dy[ff]] == '*') continue;
}
else
{
fx += dx[ff], fy += dy[ff];
}
}
cout << cnt;
return 0;
}
P3910 纪念邮票
正确思路
我们可以枚举
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int len = 100000; len >= 1; len--)
{
int l = 1 - 1, r = n + 1;
while (l + 1 < r)
{
int mid = (l + r) / 2;
int le = mid, re = len + mid - 1;
if ((le + re) * len / 2 > m) r = mid;
else if ((le + re) * len / 2 == m)
{
l = mid;
break;
}
else l = mid;
}
if (l == 0) continue; // 不能为0
if ((l + len + l - 1) * len / 2 == m && l + len - 1 <= n) cout << "[" << l << "," << len + l - 1 << "]" << "\n";
}
return 0;
}
P4047 [JSOI2010] 部落划分
正确思路
题目要求最近的两个部落的距离尽可能的远,所以我们就要把最近的两个部落相连,但如果这二个部落已经相连过了,那么就不连。也就是最小生生成树。答案:如果有了
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
const double eps = 1e-4;
int fa[N];
int n, k, m;
struct node
{
int x, y;
}a[N];
struct Node
{
int u, v;
double val;
}e[N * N];
void init()
{
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]);
}
bool cmp(Node x, Node y)
{
return x.val < y.val;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> k;
init();
for (int i = 1; i <= n; ++i)
{
cin >> a[i].x >> a[i].y;
}
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
m++;
e[m].u = i;
e[m].v = j;
e[m].val = sqrt((a[i].x - a[j].x) * (a[i].x - a[j].x) + (a[i].y - a[j].y) * (a[i].y - a[j].y) * 1.0);
}
}
sort(e + 1, e + 1 + m, cmp);
int cnt = n;
for (int i = 1; i <= m; ++i)
{
int t1 = find(e[i].u), t2 = find(e[i].v);
if (t1 != t2)
{
if (cnt == k)
{
printf("%.2lf", e[i].val);
break;
}
cnt--;
fa[t1] = t2;
}
}
return 0;
}