您的位置:首页 > 其它

hdu 5071 Chat (模拟题)

2015-11-08 21:31 267 查看
题目很长,但理解清楚就很好做了。

有两个值得注意的地方:

* 最后输出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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  模拟题