社区讨论
标题
P3419[POI 2005] SAM-Toy Cars参与者 2已保存回复 4
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 4 条
- 当前快照
- 1 份
- 快照标识符
- @mi6hbtri
- 此快照首次捕获于
- 2025/11/20 04:53 4 个月前
- 此快照最后确认于
- 2025/11/20 04:53 4 个月前
CPP
#include<bits/stdc++.h>
#define N 600011
using namespace std;
int hea[N],nex[N];
struct cmp
{
bool operator() (const int &a,const int &b){
return hea[a]<hea[b];
}
};
priority_queue <int,vector<int>,cmp> q;
int main(void)
{
int n,k,p;
cin>>n>>k>>p;
int a[p+1];
for(int i=1;i<=p;i++) {
scanf("%d",&a[i]);
}
for(int i=1;i<N;i++) hea[i]=p+1,nex[i]=p+1;
for(int i=p;i>=1;i--){
nex[i]=hea[a[i]],hea[a[i]]=i;
/* cout<<"nex["<<i<<"]="<<nex[i]<<endl;
cout<<"hea["<<a[i]<<"]="<<hea[a[i]]<<endl;*/
}
// cout<<hea[3]<<" "<<nex[hea[3]]<<endl;
int ans=0;
int have[n+1];
memset(have,0,sizeof have);
int vis=1;
int num=0;
/* for(int i=1;i<=n;i++){
cout<<i<<":";
for(int j=hea[i];j;j=nex[j]){
cout<<j<<" ";
if(j>p) break;
}
cout<<endl;
}*/
for(int i=1;i<=p;i++){
if(have[a[i]]==0){
// cout<<"have:"<<a[i]<<endl;
ans++;
vis++;
if(vis>k+1){
vis--;
while(have[q.top()]==0) q.pop();
have[q.top()]=0;
// cout<<"no:"<<q.top()<<endl;
q.pop();
}
}
have[a[i]]=1;
hea[a[i]]=nex[hea[a[i]]];
/*cout<<i<<":";
for(int j=1;j<=n;j++) cout<<"hea["<<j<<"]="<<hea[j]<<endl;*/
q.push(a[i]);
}
cout<<ans;
}
看题解思路和我几乎一样,为什么只有二十分/泪奔
回复
共 4 条回复,欢迎继续交流。
正在加载回复...