思路
暴力能过,只不过新加入文档编号cnt不能每次都从1数起,而是要从min(cnt++,x)数起(x为删除文档的最小编号)
我最终的做法用了优先队列,如果前面的编号被清除了,就往里面加(空间是够用的)。
虽然有点多此一举,但事实证明确实会快一点,下面贴两份代码。
优先队列代码
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n,cnt=0,x,a[100050];
string str;
priority_queue<int,vector<int> ,greater<int> >can;
cin>>n;
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++){
cin>>str;
if(str=="New"){
if(!can.empty()){
x=can.top();
a[x]=1;
cout<<x<<endl;
can.pop();
}else{
a[++cnt]=1;
cout<<cnt<<endl;
}
}
if(str=="Delete"){
cin>>x;
if(a[x]){
cout<<"Successful"<<endl;
a[x]=0;
can.push(x);
}else{
cout<<"Failed"<<endl;
}
}
}
return 0;
} 暴力代码
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n,cnt=1,x,a[100005];
string str;
cin>>n;
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++){
cin>>str;
if(str=="New"){
while(a[cnt]) cnt++;
a[cnt]=1;
cout<<cnt<<endl;
}
if(str=="Delete"){
cin>>x;
if(a[x]){
cout<<"Successful"<<endl;
a[x]=0;
cnt=min(cnt,x);
}else{
cout<<"Failed"<<endl;
}
}
}
return 0;
} 
京公网安备 11010502036488号