UVA 540 and UVA 136 队列及优先队列
来源:互联网 发布:大牛晒密软件 编辑:程序博客网 时间:2024/06/06 03:41
题目连接:UVA 136
UVA 540
UVA 540 团体队列:
题目不难理解;但是在操作的过程中需要注意一些问题, 如:
1.关于如何按照team插入元素,怎么找到他原来的队伍,然后插进去,还有一个更棘手的问题就是,queue没法随便插入,他是一个顺序结构的东西,所以我觉得刘老师的做法真的太赞了,我在总队里给team排队,然后你们小队再自己玩去吧,注意::总队里排的是team号!!!
2.还有就是他输入不止一组数据,所以要清空所用的东西,map可以用clear这好办,但是queue没有啊,而且,queue还定义了队列数组,那你清空可就来费事了,所以在合适的地方建立这些容器,这是一个不错的方法。
代码:
//map 和 queue 用完都要clear一下,但是由于queue没有clear()这个功能,所以,要在合适的地方建queue#include<iostream>#include<cstdio>#include<string>#include<map>#include<queue>using namespace std;const int maxn = 1000 + 10;int main(){ int n; int x = 0; while(cin >> n && n != 0) { cout << "Scenario #" << x + 1 << endl; map<string, int> team; for(int i = 0; i < n; i++) { int m; cin >> m; while(m--) { string str; cin >> str; team[str] = i; } }// 人员全部输入进来了 queue<int> q; queue<string> q2[maxn]; string s; while(cin >> s) { if(s == "STOP") break; else if(s == "DEQUEUE") { int t = q.front(); cout << q2[t].front() <<endl;; q2[t].pop(); if(q2[t].empty()) q.pop(); } else if(s == "ENQUEUE") { string ss; cin >> ss; int t = team[ss]; if(q2[t].empty()) q.push(t); q2[t].push(ss); } } x++; printf("\n"); team.clear(); } return 0;}
UVA 136 丑数
这道题就更不难理解了,其实主要就是联系一下优先队列,但是这个对于自定义的优先队列好像也没练到,其中有一点我感觉比较好的就是刘老师为了防止重复,用了一个set来做监视器,这个想法很妙啊。
代码:
//用set来巧妙的判断了原来queue中是不是存在过这个元素。#include<iostream>#include<queue>#include<vector>#include<set>using namespace std;int main(){ priority_queue<long long int, vector<long long int>, greater<long long int> > pq; set<long long int> num; num.insert(1); pq.push(1); for(int i = 1; ; i++) { long long int x0 = pq.top(); // 取首元素要用top pq.pop(); if(i == 1500) {cout << "The 1500'th ugly number is " << x0 << "." << endl; break;} else { long long int x1, x2, x3; x1 = x0 * 2; x2 = x0 * 3; x3 = x0 * 5; if(!num.count(x1)) {num.insert(x1); pq.push(x1);} if(!num.count(x2)) {num.insert(x2); pq.push(x2);} if(!num.count(x3)) {num.insert(x3); pq.push(x3);} } } return 0;}
关于自定义类型Point类的queue:
#include<iostream>#include<cstdio>#include<queue>using namespace std;class Point{ public: int x_, y_; Point(int x, int y): x_(x), y_(y) {} int x() { return x_; } int y() { return y_; }}; void show(Point p) { cout << "( " << p.x_ << ", " << p.y_ << " )" << endl; }struct cmp { bool operator () (Point p1, Point p2) { if(p1.x_ < p2.x_) return true; else if(p1.x_ == p2.x_) { if(p1.y_ < p1.y_) return true; else return false; } else return false; }};int main(){ priority_queue<int, vector<Point>, cmp> pq; int x, y; while(1) { int x; int y; cin >> x >> y; if(x == 0 && y == 0) break; Point point(x, y); pq.push(point); } while(!pq.empty()) { show(pq.top()); pq.pop(); } return 0;}//pq.top() 取的是优先级最高的元素
0 0
- UVA 540 and UVA 136 队列及优先队列
- UVA - 11997(优先队列)
- UVa 136 Ugly Numbers(优先队列)
- UVA - 136 Ugly Numbers set+优先队列
- uva 136(Ugly Numbers, 优先队列基础)
- Uva 136 Ugly Numbers 优先队列
- Ugly Numbers(UVa 136)优先队列
- UVA.136 Ugly Numbers (优先队列)
- UVA 136 Ugly Number(优先队列)
- UVa 10050 Hartals(优先队列)
- UVA 1203 Argus(优先队列)
- uva 1203 - Argus(优先队列)
- UVA FILL(BFS + 优先队列)
- UVA 1203Argus【优先队列】
- UVa 10603 BFS+优先队列
- UVA 1203 Argus(优先队列)
- UVA-10954 贪心+优先队列
- UVA 12100 Printer Queue(队列,优先队列)
- 链表学习(3)
- error LNK1112: 模块计算机类型“x64”与目标计算机类型“X86”冲突 (VS2013配置opencv2.4.10的BUG)
- 二分贪心24
- javac 命令出现字符编码不支持
- 《hive编程指南》学习文档(二)
- UVA 540 and UVA 136 队列及优先队列
- Java Collection
- solr索引的概念merge
- 博主偷偷换了博客 请关注
- xv6启动源码阅读
- 一个网站完整详细的SEO优化方案
- Android属性动画--ViewPropertyAnimator
- 34. Search for a Range
- POI对Excel自定义日期格式的读取