Multiset的使用 TOJ 2196.Nuanran's Idol II 与 UVA11136 Hoax or what

 1 /*
 2 ID: neverchanje
 3 PROG: TOJ2195
 4 LANG: C++11
 5 */
 6 #include<iostream>
 7 #include<set>
 8 using namespace std;
 9
10 char cmd;
11 int n,val;
12 int main(){
13 //    freopen("a.txt","r",stdin);
14 //    freopen(".out","w",stdout);
15     while(cin>>n){
16         if(!n) break;
17         multiset<int> s;
18         while(n--){
19             cin>>cmd;
20             if(cmd==‘B‘){
21                 cin>>val;
22                 s.insert(val);
23             }
24             else{//cmd==‘G‘
25                 int y=*s.begin();
26                 cout<<y<<endl;
27                 s.erase(s.begin());
28             }
29         }
30     }
31      return 0;
32 }
33
34 /*
35 DESCRIPTION:
36 multiset可以支持重复元素,使得删除操作有两种情况
37 s.erase(s.begin())是只删除一个最小元素(即最小元素有多个时,仍只删除一个)
38 s.erase(*s.begin())会删除最小元素及相同项
39 */

uva11136 因为uva的数据貌似有问题,所以没过(其他人的Ac代码也wa了),但这份代码没问题

 1 /*
 2 ID: neverchanje
 3 PROG:
 4 LANG: C++11
 5 */
 6 #include<vector>
 7 #include<iostream>
 8 #include<cstring>
 9 #include<string>
10 #include<algorithm>
11 #include<cmath>
12 #include<cstdio>
13 #include<set>
14 #include<queue>
15 #include<map>
16 #define INF 0Xfffffffff
17 #define st_size (1<<18)-1
18 #define maxn
19 typedef  long long ll;
20 using namespace std;
21
22 int n,m,x;
23 multiset<int> s;
24 int main(){
25 //    freopen("a.txt","r",stdin);
26 //    freopen(".out","w",stdout);
27     while(cin>>n)
28     {
29         if(!n) break;
30         s.clear();
31         int sum=0;
32
33         for(int j=0;j<n;j++){
34             cin>>m;
35             for(int i=0;i<m;i++){
36                 cin>>x;
37                 s.insert(x);
38             }
39             sum+=*--s.end()-*s.begin();
40             s.erase(--s.end());
41             s.erase(s.begin());
42         }
43         cout<<sum<<endl;
44     }
45     return 0;
46 }
47
48 /*
49 DESCRIPTION:
50
51 */

Multiset的使用 TOJ 2196.Nuanran's Idol II 与 UVA11136 Hoax or what

时间: 2024-08-29 01:15:14

Multiset的使用 TOJ 2196.Nuanran's Idol II 与 UVA11136 Hoax or what的相关文章

toj 2196 优先队列和堆的用法

很简单的优先队列或者堆的使用. 1 #include <iostream> 2 #include <queue> 3 using namespace std; 4 5 //greater对int来说表示值越小优先级越高,也可以自己定义比较函数 6 priority_queue< int, vector<int>, greater<int> > q; 7 char op[2]; 8 9 int main () 10 { 11 int n; 12 w

leetcode&amp;编程之美——博文目录

leetcode刷题整理: 1——Two Sum(哈希表hashtable,map) 2——Add Two Numbers(链表) 3——Longest Substring Without Repeating Characters(set,哈希表,两个指针) 9——Palindrome Number (数学问题) 11——Container With Most Water(两个指针) 12——Integer to Roman(string,数学问题) 13——Roman to Integer(s

zoj Median (multiset)

Median Time Limit: 5 Seconds      Memory Limit: 65536 KB The median of m numbers is after sorting them in order, the middle one number of them ifm is even or the average number of the middle 2 numbers if m is odd. You have an empty number list at fir

hdu 4268 Alice and Bob(multiset)

# include<stdio.h> # include<algorithm> # include<iostream> # include<string.h> # include<map> # include<set> # include<vector> using namespace std; struct node { int h; int w; }; struct node a[100010],b[100010];

第十三篇:multimap容器和multiset容器中的find操作

前言 multimap容器是map容器的“ 增强版 ”,它允许一个键对应多个值.对于map容器来说,find函数将会返回第一个键值匹配元素所在处的迭代器.那么对于multimap容器来说,find函数将如何运作呢?如果要实现和map容器的find函数同样的功能,则它将返回多个迭代器,这样太复杂了.本文将讲解C++中multimap容器的“ find实现 ”. 解决思路一 摒弃find函数,使用另外两个新函数,它们是专家们为了解决multimap中的“ find操作 ”问题专门设计的: 1. lo

hdu 5578 Friendship of Frog(multiset的应用)

Problem Description N frogs from different countries are standing in a line. Each country is represented by a lowercase letter. The distance between adjacent frogs (e.g. the 1st and the2nd frog, the N−1th and the Nth frog, etc) are exactly 1. Two fro

【multiset】hdu 5349 MZL&#39;s simple problem

[multiset]hdu 5349 MZL's simple problem 题目链接:hdu 5349 MZL's simple problem 题目大意 n次操作,插入元素.删除最小元素.查询最大元素并输出. C++STL的multiset的使用 set--多元集合(元素不可重复),multiset--可重复元素的多元集合 多元集合(MultiSets)和集合(Sets)相像,只不过支持重复对象.(具体用法请参照set容器) set和multiset内部是以平衡二叉树实现的: 从内部数据结

实例讲解,set,multiset,map,multimap关联容器

测试环境:windows 7 vs2010 内部元素有序排列,新元素插入的位置取决于它的值,查找速度快. 除了各容器都有的函数外,还支持以下成员函数: find: 查找等于某个值的元素(x小于y和y小于x同时不成立即为相等) lower_bound: 查找某个下界 upper_bound: 查找某个上界 equal_range: 同时查找上界和下界 count:计算等于某个值的元素个数(x小于y和y小于x同时不成立即为相等) insert: 用以插入一个元素或一个区间 在学习关联容器之前,我们先

HDU 4268 Alice and Bob(贪心+multiset)

HDU 4268 题意:Alice与Bob在玩卡片游戏,他们每人有n张卡片,若Alice的一张卡片长与宽都不小于Bob的一张卡片,则Bob的卡片就会被盖住,一张卡片只可以使用一次,且不可旋转求Alice最多可以盖住多少张Bob的卡片. 思路:记录两人卡片情况,并按照长度将两人卡片分别降序排序.遍历两人的卡片,将长度小于Alice的卡片长度的Bob卡片的宽度插入multiset中,在multiset中找到小于等于Alice卡片宽度的第一个数,将这个数给消去且答案+1.//贪心法自行发挥即可. co