STL(pair map set vector priority_queue) poj 3297

Posted blues

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STL(pair map set vector priority_queue) poj 3297相关的知识,希望对你有一定的参考价值。

POJ 3297

算法竞赛初级杂烩包

题意:学生选课,没个学生只能选一门课。大写字符是课的名字,小写是人名。如果课程后面有多个相同名字算一个,如果一个人选多门课,则他选不上课,输出课和每门课选课人数

思路:

map<string,set<int> > stu:一个学生名对应他选了哪几门课

map<string,ser<int> > pro:课程名对应有几个学生选了他,set存对应的学生

vector<pair<int,string> > ans:最后结果,课程名对应所选学生人数

a[]:判断一个学生是否只选了一门课

 1 //#pragma comment(linker, "/STACK:167772160")//手动扩栈~~~~hdu 用c++交
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <iostream>
 6 #include <queue>
 7 #include <stack>
 8 #include <cmath>
 9 #include <set>
10 #include <utility>
11 #include <algorithm>
12 #include <vector>
13 #include <map>
14 // #include<malloc.h>
15 using namespace std;
16 #define clc(a,b) memset(a,b,sizeof(a))
17 #define LL long long
18 const int inf = 0x3f3f3f3f;
19 const double eps = 1e-5;
20 const double pi = acos(-1);
21 const LL MOD = 1e9+7;
22 // const LL p = 1e9+7;
23 // inline int r(){
24 //     int x=0,f=1;char ch=getchar();
25 //     while(ch>‘9‘||ch<‘0‘){if(ch==‘-‘) f=-1;ch=getchar();}
26 //     while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
27 //     return x*f;
28 // }
29 map<string,int> stu;
30 map<string,set<int> > pro;
31 vector<pair<int,string> > ans;
32 
33 int a[10010],n,m;
34 string str,book;
35 void init(){
36     stu.clear();
37     pro.clear();
38     ans.clear();
39     clc(a,0);
40     n=m=0;
41 }
42 
43 bool cmp(const pair<int,string> &a,const pair<int,string> &b){
44    if(a.first==b.first) return a.second<b.second;
45    return a.first>b.first;
46 }
47 
48 void work(){
49    std::map<string,set<int> >::iterator it;
50    std::set<int>::iterator q;
51    for(it=pro.begin();it!=pro.end();it++){
52       int cnt=0;
53       for(q=it->second.begin();q!=it->second.end();q++){
54          if(a[*q]==1) cnt++;
55       }
56       ans.push_back(make_pair(cnt,it->first)); 
57    }
58    sort(ans.begin(),ans.end(),cmp);
59    std::vector<pair<int,string> >::iterator p;
60    for(p=ans.begin();p!=ans.end();p++){
61       cout<<p->second<<" "<<p->first<<endl;
62    }
63 }
64 
65 int main(){
66     // freopen("in.txt","r",stdin);
67     init();
68     while(getline(cin,str)){
69        if(str[0]==0) break;
70        else if(str[0]==1) {
71           work();
72           init();
73           continue;
74        } 
75        else{
76           if(str[0]>=A&&str[0]<=Z){
77              book=str;
78              pro[book].insert(-1);
79           }
80           else{
81              std::map<string,int>::iterator it = stu.find(str);
82              if(it==stu.end())
83                 stu[str]=m++;
84              std::set<int>::iterator q=pro[book].find(stu[str]);
85              if(q==pro[book].end()){
86                  a[stu[str]]++;
87                  pro[book].insert(stu[str]); 
88              }
89           }
90        }
91     }
92     return 0;
93 }

 

以上是关于STL(pair map set vector priority_queue) poj 3297的主要内容,如果未能解决你的问题,请参考以下文章

stl+数论——1247D

STL初步不定长数组:vector + 集合:set + 映射:map

stl容器区别: vector list deque set map及底层实现

STL容器用法速查表:list,vector,stack,queue,deque,priority_queue,set,map

NO.6章 STL

求STL中List,Vector,Map,Set区别?