记2025/6/14飞机场惨案
Folden_xiaoming · · 个人记录
在差不多两个月后,我才想起来写这篇文章。
那天学校举办了一场
| t1 | t2 | t3 | t4 | t5 | t6 |
|---|---|---|---|---|---|
| 100 | 175 | 137 | 160 | 364 | 0 |
抛开别的低级错误不谈,注意到 t4 只得了
(ps:考场上削弱了一下数据,std 在洛谷上无法 AC)
std:
#include<bits/stdc++.h>
using namespace std;
int g,p,cnt,pos,proot,a[100005];
int f[100005];
void init(int n){
for(int i=1;i<=n;i++)f[i]=i;
}
int getroot(int u){
if(f[u]==u)return u;
return f[u]=getroot(f[u]);
}
void conn(int u,int v){
f[getroot(u)]=getroot(v); //u所在的树,挂在v所在的树下
}
int main(){
cin>>g>>p;
init(g);
for(int i=1;i<=p;i++){
cin>>pos;
//使用pos所在的树根
proot=getroot(pos);
if(proot==0)break; //无停机坪可用
cnt++;
//并将pos所在的树合并至前方一棵树上
conn(proot,getroot(proot-1));
}
cout<<cnt;
return 0;
}
/*
# P9812 2015 S3 贪心 并查集
贪心:每次都用可使用的最大编号
优化:如何快速跳过已经用掉的大编号 ?
并查集:已经用过的大编号合并至前方小编号
每棵树的树根是可用的停机坪,子节点是不可用的停机坪
可使用停机坪 1-pos
尽量用大的编号,即pos的根
树根使用完毕后,将这棵树挂在前方的一棵树下
*/
my code:
#include<bits/stdc++.h>//AC
using namespace std;
#define int long long
int ga,p,g,f[110000],ans;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin>>ga>>p;
for(int i=1;i<=p;i++){
cin>>g;
while(f[g]){
g--;
if(g==0){
cout<<ans;
return 0;
}
}
f[g]=1;
ans++;
}
return 0;
}
可以发现,我在赛时一眼看出了贪心的结论,还自信地挂上了 AC 的注释。
后来,我又看了题面,才注意到有可能有特殊情况,需要在循环结束后判断。
这是我头一次见到让我痛失
飞机场惨案