山东第六届ACM省赛B题
超时代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<string.h> 4 #include<sstream> 5 #include<string> 6 #include<map> 7 #include<iterator> 8 9 using namespace std; 10 map<int,int>mp; 11 int main() 12 { 13 int T,n; 14 cin>>T; 15 16 while(T--) 17 { 18 mp.clear(); 19 cin>>n; 20 getchar(); 21 char f; 22 int v; 23 map<int,int>::iterator it; 24 for(int i = 0; i < n; ++i) 25 { 26 cin>>f; 27 if(f=='q') 28 { 29 for(it=mp.begin(); it!=mp.end(); it++) 30 if(it->second==1){ 31 cout<<it->first<<endl; 32 break; 33 } 34 if(it==mp.end())cout<<"none"<<endl; 35 } 36 else if(f=='b') 37 { 38 cin>>v; 39 mp[v]++; 40 } 41 else if(f=='c') 42 { 43 cin>>v; 44 mp[v]--; 45 } 46 } 47 } 48 return 0; 49 }
AC Code:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<string.h> 4 #include<sstream> 5 #include<string> 6 #include<map> 7 #include<iterator> 8 #include<set> 9 using namespace std; 10 set<int> si; ///始终存放的是出现次数为 1 的元素,内部的元素自动的由set排序 11 map<int,int>mp; 12 int main() 13 { 14 int T,n; 15 cin>>T; 16 while(T--) 17 { 18 mp.clear(); 19 si.clear(); 20 cin>>n; 21 getchar(); 22 char f; 23 int v; 24 map<int,int>::iterator it; 25 for(int i = 0; i < n; ++i) 26 { 27 cin>>f; 28 if(f=='q') 29 { 30 if(!si.empty())cout<<*si.begin()<<endl; 31 else cout<<"none"<<endl; 32 } 33 else if(f=='b') 34 { 35 cin>>v; 36 mp[v]++; 37 if(mp[v]==1) 38 si.insert(v); 39 else si.erase(v); 40 } 41 else if(f=='c') 42 { 43 cin>>v; 44 mp[v]--; 45 if(mp[v]==1) 46 si.insert(v); 47 else si.erase(v); 48 } 49 } 50 } 51 return 0; 52 }