标签:
例题1 uva11995 http://acm.hust.edu.cn/vjudge/problem/18700
猜测符合哪种数据结构 , 用stl模拟判断.
1 //#define txtout 2 //#define debug 3 #include<bits/stdc++.h> 4 #define mt(a,b) memset(a,b,sizeof(a)) 5 using namespace std; 6 typedef long long LL; 7 const double pi=acos(-1.0); 8 const double eps=1e-8; 9 const int inf=0x3f3f3f3f; 10 const int M=1e5+10; 11 int n; 12 int a[M]; 13 int b[M]; 14 char answer[8][32]={"stack","queue","priority queue","impossible","not sure"}; 15 stack<int> s; 16 queue<int> q; 17 priority_queue<int> p; 18 bool checkStack(){ 19 while(!s.empty()) s.pop(); 20 for(int i=0;i<n;i++){ 21 if(a[i]==1){ 22 s.push(b[i]); 23 continue; 24 } 25 if(s.empty()) return false; 26 if(s.top()!=b[i]) return false; 27 s.pop(); 28 } 29 return true; 30 } 31 bool checkQueue(){ 32 while(!q.empty()) q.pop(); 33 for(int i=0;i<n;i++){ 34 if(a[i]==1){ 35 q.push(b[i]); 36 continue; 37 } 38 if(q.empty()) return false; 39 if(q.front()!=b[i]) return false; 40 q.pop(); 41 } 42 return true; 43 } 44 bool checkPriority(){ 45 while(!p.empty()) p.pop(); 46 for(int i=0;i<n;i++){ 47 if(a[i]==1){ 48 p.push(b[i]); 49 continue; 50 } 51 if(p.empty()) return false; 52 if(p.top()!=b[i]) return false; 53 p.pop(); 54 } 55 return true; 56 } 57 int solve(){ 58 bool isStack=checkStack(); 59 bool isQueue=checkQueue(); 60 bool isPriority=checkPriority(); 61 if(isStack&&!isQueue&&!isPriority) return 0; 62 if(!isStack&&isQueue&&!isPriority) return 1; 63 if(!isStack&&!isQueue&&isPriority) return 2; 64 if(!isStack&&!isQueue&&!isPriority) return 3; 65 return 4; 66 } 67 int main(){ 68 #ifdef txtout 69 freopen("in.txt","r",stdin); 70 freopen("out.txt","w",stdout); 71 #endif // txtout 72 while(~scanf("%d",&n)){ 73 for(int i=0;i<n;i++){ 74 scanf("%d%d",&a[i],&b[i]); 75 } 76 puts(answer[solve()]); 77 } 78 return 0; 79 }
例题2 uva 11991 http://acm.hust.edu.cn/vjudge/problem/18696
n个正整数, m 次查询 都是10^5, 每次查询第k 个 值为v的下标. 开10的6次方vector,每个值的下表到对应vector, o1查询. on预处理
1 //#define txtout 2 //#define debug 3 #include<bits/stdc++.h> 4 #define mt(a,b) memset(a,b,sizeof(a)) 5 using namespace std; 6 typedef long long LL; 7 const double pi=acos(-1.0); 8 const double eps=1e-8; 9 const int inf=0x3f3f3f3f; 10 const int M=1e5+10; 11 int n,m; 12 int a[M]; 13 int k[M]; 14 int v[M]; 15 int answer[M]; 16 vector<int> id[M*10]; 17 void solve(){ 18 for(int i=0;i<n;i++){ 19 id[a[i]].clear(); 20 } 21 for(int i=0;i<n;i++){ 22 id[a[i]].push_back(i+1); 23 } 24 for(int i=0;i<m;i++){ 25 int value=v[i]; 26 if(id[value].size()<k[i]){ 27 answer[i]=0; 28 } 29 else{ 30 answer[i]=id[value][k[i]-1]; 31 } 32 } 33 } 34 int main(){ 35 #ifdef txtout 36 freopen("in.txt","r",stdin); 37 freopen("out.txt","w",stdout); 38 #endif // txtout 39 while(~scanf("%d%d",&n,&m)){ 40 for(int i=0;i<n;i++){ 41 scanf("%d",&a[i]); 42 } 43 for(int i=0;i<m;i++){ 44 scanf("%d%d",&k[i],&v[i]); 45 } 46 solve(); 47 for(int i=0;i<m;i++){ 48 printf("%d\n",answer[i]); 49 } 50 } 51 return 0; 52 }
白书用 map <int, vector<int> > ,也是一种写法 nlogn
1 //#define txtout 2 //#define debug 3 #include<bits/stdc++.h> 4 #define mt(a,b) memset(a,b,sizeof(a)) 5 using namespace std; 6 typedef long long LL; 7 const double pi=acos(-1.0); 8 const double eps=1e-8; 9 const int inf=0x3f3f3f3f; 10 const int M=1e5+10; 11 int n,m; 12 map<int, vector<int> > a; 13 int main(){ 14 #ifdef txtout 15 freopen("in.txt","r",stdin); 16 freopen("out.txt","w",stdout); 17 #endif // txtout 18 while(~scanf("%d%d",&n,&m)){ 19 a.clear(); 20 int x,y; 21 for(int i=0;i<n;i++){ 22 scanf("%d",&x); 23 if(!a.count(x)) a[x]=vector<int>(); 24 a[x].push_back(i+1); 25 } 26 for(int i=0;i<m;i++){ 27 scanf("%d%d",&x,&y); 28 if(!a.count(y)||a[y].size()<x){ 29 puts("0"); 30 continue; 31 } 32 printf("%d\n",a[y][x-1]); 33 } 34 } 35 return 0; 36 }
例题3 la3135 http://acm.hust.edu.cn/vjudge/problem/18684
输入 A B, 表示每B秒产生A编号, 输出前k个编号, 同时产生的优先选编号小的。
优先队列,规则时间小的靠前,时间相同编号小的考前。优先队列大的在堆顶。
1 //#define txtout 2 //#define debug 3 #include<bits/stdc++.h> 4 #define mt(a,b) memset(a,b,sizeof(a)) 5 using namespace std; 6 typedef long long LL; 7 const double pi=acos(-1.0); 8 const double eps=1e-8; 9 const int inf=0x3f3f3f3f; 10 const int M=1e5+10; 11 int n; 12 char a[M]; 13 struct Q{ 14 int id,now,add; 15 bool operator <(const Q &b) const { 16 return now>b.now||(now==b.now&&id>b.id); 17 } 18 }now,pre; 19 priority_queue<Q> q; 20 int main(){ 21 #ifdef txtout 22 freopen("in.txt","r",stdin); 23 freopen("out.txt","w",stdout); 24 #endif // txtout 25 while(!q.empty()) q.pop(); 26 while(~scanf("%s",a)){ 27 if(!strcmp(a,"#")){ 28 scanf("%d",&n); 29 while(n--){ 30 pre=q.top(); 31 q.pop(); 32 printf("%d\n",pre.id); 33 now.id=pre.id; 34 now.add=pre.add; 35 now.now=pre.now+pre.add; 36 q.push(now); 37 } 38 while(!q.empty()) q.pop(); 39 continue; 40 } 41 int id,t; 42 scanf("%d%d",&id,&t); 43 now.id=id; 44 now.now=now.add=t; 45 q.push(now); 46 } 47 return 0; 48 }
end
标签:
原文地址:http://www.cnblogs.com/gaolzzxin/p/5750977.html