hdu 5071 Chat (模拟题)
2015-11-08 21:31
267 查看
题目很长,但理解清楚就很好做了。
有两个值得注意的地方:
* 最后输出Bye的时候每次只输出当前的top。所以一开始若有Always on top,就要首先输出。
* 再输出Bye的时候,若是一句话也没讲,就不用输出
代码
有两个值得注意的地方:
* 最后输出Bye的时候每次只输出当前的top。所以一开始若有Always on top,就要首先输出。
* 再输出Bye的时候,若是一句话也没讲,就不用输出
代码
#include <iostream> #include <string> #include <map> #include <vector> using namespace std; vector<long long> V; map<long long,long long> mp; long long firFlag; void init() { firFlag = 0; mp.clear(); V.clear(); } void Add(long long cnt) { long long x; cin >> x; cout << "Operation #" << cnt << ": "; if(mp.count(x)) cout << "same priority." << endl; else { V.push_back(x); mp[x] = 0; cout << "success." << endl; } } void Close(long long cnt) { long long x; cin >> x; cout << "Operation #" << cnt << ": "; if(mp.count(x)) { cout << "close " << x << " with " << mp[x] << "." << endl; //mp.erase(x); map<long long,long long>::iterator iter = mp.begin(); for(; iter != mp.end(); iter++) if(iter->first == x) break; mp.erase(iter); vector<long long>::iterator it; for(it = V.begin(); it != V.end(); it++) if(*it == x) break; V.erase(it); if(firFlag == x) firFlag = 0; } else cout << "invalid priority." << endl; } void Chat(long long cnt) { long long x; cin >> x; cout << "Operation #" << cnt << ": "; if(V.empty()) cout << "empty." << endl; else { cout << "success." << endl; if(firFlag) mp[firFlag] += x; else { long long tmp = V.front(); mp[tmp] += x; } } } void Rotate(long long cnt) { long long x; cin >> x; cout << "Operation #" << cnt << ": "; long long m = V.size(); if(x < 1 || x > m) cout << "out of range." << endl; else { cout << "success." << endl; long long tmp = V[x-1]; vector<long long>::iterator it = V.begin(); for(long long k = 1; k < x; k++) it++; V.erase(it); V.insert(V.begin(),tmp); } } void Prior(long long cnt) { cout << "Operation #" << cnt << ": "; if(V.empty()) cout << "empty." << endl; else { cout << "success." << endl; map<long long,long long>::iterator iter = mp.end(); --iter; long long x = iter->first; vector<long long>::iterator it; for(it = V.begin(); it != V.end(); it++) if(*it == x) break; V.erase(it); V.insert(V.begin(), x); } } void Choose(long long cnt) { long long x; cin >> x; cout << "Operation #" << cnt << ": "; if(mp.count(x)) { cout << "success." << endl; vector<long long>::iterator it; for(it = V.begin(); it != V.end(); it++) if(*it == x) break; V.erase(it); V.insert(V.begin(), x); } else cout << "invalid priority." << endl; } void Top(long long cnt) { long long x; cin >> x; cout << "Operation #" << cnt << ": "; if(mp.count(x)) { firFlag = x; cout << "success." << endl; } else cout << "invalid priority." << endl; } void Untop(long long cnt) { cout << "Operation #" << cnt << ": "; if(firFlag) { firFlag = 0; cout << "success." << endl; } else cout << "no such person." << endl; } int main() { long long t , n; cin >> t; while(t--) { init(); cin >> n; for(long long i = 1; i <= n; i++) { string s; cin >> s; if(s == "Add") Add(i); else if(s == "Close") Close(i); else if(s == "Chat") Chat(i); else if(s == "Rotate") Rotate(i); else if(s == "Prior") Prior(i); else if(s == "Choose") Choose(i); else if(s == "Top") Top(i); else if(s == "Untop") Untop(i); } if(!V.empty()) { if(firFlag && mp[firFlag]) cout << "Bye " << firFlag << ": " << mp[firFlag] << endl; for(unsigned i = 0; i < V.size(); i++) { if(V[i] != firFlag && mp[V[i]]) { cout << "Bye " << V[i] << ": " << mp[V[i]] << endl; } } } } return 0; }
相关文章推荐
- 网页设计与制作模拟题
- 敬请关注,全国软考有奖自测专题
- 2011ACM福州网络预选赛B题 HDU4062 Abalone
- TOJ 4453
- CSU 1469 Change The Handles acm 模拟题
- CodeForces 405B Playing Dominoes acm 模拟题
- HDU 1200
- CodeForces 131A
- ZOJ 3311
- POJ 2183
- sicily 1003之模拟题
- CodeForces 402A
- CodeForces 404A
- NOIp 前N天做题
- Automatic Correction of Misspellings
- HDU 4671——模拟题
- zoj-1005倒水问题
- poj 1029 确定唯一假币
- NYOJ 364 田忌赛马
- codeingtriping 1003 位图像素的颜色