UVA 540 Team Queue
2014-08-01 19:00
330 查看
思路:使用优先队列,按队伍出现的时刻和自身出现的时刻定义优先级,同时记录此时刻队列里是否有自己队伍的人,一开始没注意,wa了两发。
#include<map> #include<queue> #include<cstdio> #include<string> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int MAXN = 1010; map<int, int>mp; int teamPos[MAXN],existElem[MAXN], T, n; struct Elem{ int idx, pos, self; Elem(int idx, int pos, int self){ this->pos = pos; this->idx = idx; this->self = self; } bool operator < (const Elem &A) const { if(idx == A.idx) return pos > A.pos; return idx > A.idx; } }; int main(){ string str; int elemNum, CASE(0); freopen("in.cpp", "r", stdin); while(~scanf("%d", &T) && T){ mp.clear(); priority_queue<Elem>Q; while(!Q.empty()) Q.pop(); for(int i = 1;i <= T;i ++){ scanf("%d", &n); for(int j = 0;j < n;j ++){ scanf("%d", &elemNum); mp.insert(pair<int, int>(elemNum, i)); } } int num(0), cnt(0); memset(teamPos, 0, sizeof teamPos); memset(existElem, 0, sizeof existElem); printf("Scenario #%d\n", ++CASE); while(cin >> str && str[0] != 'S'){ if(str[0] == 'D'){ int tmp = Q.top().self; printf("%d\n",tmp); map<int, int>::iterator it = mp.find(tmp); existElem[it->second] --; Q.pop(); continue; } scanf("%d", &n); map<int, int>::iterator it = mp.find(n); int idx = it->second; if(teamPos[idx] && existElem[idx]) Q.push(Elem(teamPos[idx], cnt++, n)); else{ Q.push(Elem(++num, cnt++, n)); teamPos[idx] = num; } existElem[idx]++; } puts(""); } return 0; }
相关文章推荐
- UVA 540-Team Queue
- uva 540(Team Queue)
- UVA - 540 - Team Queue
- UVA 540 Team Queue(团体队列)
- Team Queue UVA 540 (团体队列)
- UVa 540: Team Queue
- UVA540:Team Queue(团体队列)
- UVA 540 Team Queue
- Uva 540 - Team Queue
- Uva540 Team Queue 【队列】【例题5-6】
- UVa540 Team Queue
- UVA - 540 Team Queue
- UVa 540 Team Queue(团队队列)
- UVA 540 —— Team Queue
- UVA540 Team Queue(队列简单用法)
- uva 540 Team Queue---比较好的数据结构题!!!!!思路不全面啊!
- uva 540 Team Queue
- 《算法竞赛入门经典2ndEdition 》例题5-6 团体队列(Team Queue, Uva540)
- ACM学习历程——UVA540 Team Queue(队列,map:Hash)
- UVa 540 Team Queue 【STL】【queue】