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 }
时间: 2024-07-31 03:46:26

STL(pair map set vector priority_queue) poj 3297的相关文章

HLG 2113 Count (C++ --- map容器 基础题)

链接: http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2113 Description Given a number of strings, can you find how many strings that appears T times? Input The input contains multiple test cases. Each case begins with a inte

支持向量机(SVM:support vector machine)

传统机器学习分类任务中,我认为支持向量机是最难.最复杂.最有效的一种模型.可能是由于其是一种特殊的神经网络的缘故吧! 1.支持向量机简介 支持向量机(support vector machines,SVM)是一种二类分类模型.它的基本模型是定义在特征空间上的间隔最大的线性分类器,间隔最大使它有别于感知机:支持向量机还包括核技巧,这使它成为实质上的非线性分类器.支持向量机的学习策略就是间隔最大化,可形式化为一个求解凸二次规划(convex quadratic programming,不怕,附录有解

Tangled in Cables(Kruskal+map容器处理字符串)

/** 题意: 给你两个城市之间的道路(无向图),求出需要的 电缆.如果大于所提供的,就输出Not enough ... 否则输出所需要的电缆长度. 输入:N (给定的电缆总长度) m1 (有多少个城市—) str1 str2 str3 str4 : :(城市的名字) m2(相当于给出m2条边) a  b  c (a城市到b城市的距离为c) : : : : 输出: 所需的最短长度   若大于给定的长度  输出 Not enough cable 分析: 简单的最小生成树+字符串处理 用map容器映

C++ Primer 学习笔记_55_STL剖析(十):容器适配器(stack、 queue 、priority_queue)源码浅析与使用示例

七种基本容器:vector.deque.list.set.multiset.map.multimap 一.容器适配器 stack queue priority_queue stack.queue.priority_queue 都不支持任一种迭代器,它们都是容器适配器类型,stack是用vector/deque/list对象创建了一个先进后出容器:queue是用deque或list对象创建了一个先进先出容器:priority_queue是用vector/deque创建了一个排序队列,内部用二叉堆实

poj 3159 Candies(dijstra优化非vector写法)

题目链接:http://poj.org/problem?id=3159 题意:给n个人派糖果,给出m组数据,每组数据包含A,B,c 三个数,意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c . 最后求n比1最多多多少糖果. 可以从条件中的 B-A<=c及B<=A+c最后要达成这个条件就是要当B>A+c时B=A+c即可 所以差不多就是求最短路.这题中还有一些优化比如 if(vis[u]) continue;这个避免了u点的重复查找 #include <

java自定义类型 作为HashMap中的Key值 (Pair&lt;V,K&gt;为例)

由于是自定义类型,所以HashMap中的equals()函数和hashCode()函数都需要自定义覆盖. 不然内容相同的对象对应的hashCode会不同,无法发挥算法的正常功能,覆盖equals函数,应该就相当于c++重载==运算符来保证能判断是否相等.只不过java没有自定义重载运算符这个功能的,需要进行函数覆盖. equals的函数原型是 boolean equals(Object o);注意括号内.hashCode的函数原型就是int hashCode(); 先看一段代码: import

js五种不同的遍历 (filter, map,foreach,every, some,)

var arr=[1,2,"a",2,4,1,4,"a",5,6,7,8,"aa","bb","c","a"] // foreach console.log(arr.forEach((item) => { return item === "a" // undefined. foreach无返回值,只是遍历执行 })) // map console.log(arr

stl(GCC4.9.3 in MinGW)中rbtree lower_bound/upper_bound 的实现

STL内部实现的rbtree,实现 lower_bound/upper_bound 过程,是从 begin() 开始向 end() 进行遍历,将元素的 key 与目标 key 进行比较,直至找到的第一个符合要求的 iterator 为止!具体看代码,如下 位于bits/stl_tree.h 1 template<typename _Key, typename _Val, typename _KeyOfValue, 2 typename _Compare, typename _Alloc> 3

(最大k度限制生成树)POJ 1639 - Picnic Planning

题意: 给一个无向带权图,图上有不超过20个人和1个公园,现在这些人想到公园去集合,他们可以直接去公园也可以,和其他人一起坐车去公园(假设他们的车容量无限),但是这个公园停车场只有k个位置,现在要求他们到达公园所需要的总花费. 分析: 乍一看是最小生成树,但是停车场只有k个位置,所以就限定了公园节点只能最多连k个人,也就是说有一个点的度数是给定了的. 想了很久,第一感觉就是想其他生成树那样,肯定要把边删去,从环中选择更优解, 按套路来说. 但是想了很久不知道怎么处理. 不过,看到题目只有20个人