A1056 Mice and Rice (25point(s))
模拟。
题意
输入np,ng,一共np个老鼠,每轮每组ng个老鼠,如果不够就余下的为一组,每组中最肥的老鼠胜出,被淘汰的老鼠的排名相同,输出全部老鼠的排名。
思路
用两个链表进行操作比较直观。一个用来存放此时的所有老鼠,另一个存放本轮胜出的老鼠。
总结
Sample Input:
11 3
25 18 0 46 37 3 19 22 57 56 10
6 0 8 7 10 5 9 1 4 2 3
Sample Output:
5 5 5 2 5 5 5 3 1 3 5
#include"bits/stdc++.h"
using namespace std;
struct Node {
int id;
int w;
int pass = 0;
int rank;
};
int main() {
// freopen("input.txt","r",stdin);
int n,m;
cin >> n >> m;
vector<Node*> nodes(n);
map<int,Node*> id2Node;
for(int i=0; i<n; i++) {
Node *p = new Node;
p->id = i;
nodes[i] = p;
id2Node[i] = p;
scanf("%d",&p->w);
}
vector<int> a(n);
for(int i=0; i<n; i++) scanf("%d",&a[i]);
list<Node*> list1,list2;
for(int id:a) list1.push_back(nodes[id]);
list<Node*> &l1 = list1;
list<Node*> &l2 = list2; // winner
while(true) {
if(l1.size() == 1) {
l1.front()->pass ++;
break;
}
while(!l1.empty()) {
// winner for this round
int cnt = 0;
Node *p_max = nullptr;
while(cnt++ < m && !l1.empty()) {
Node *p = l1.front();
l1.pop_front();
p->pass ++;
if(p_max == nullptr || p->w > p_max->w) {
p_max = p;
}
}
// add to p2
l2.push_back(p_max);
}
swap(l1,l2);
}
sort(nodes.begin(),nodes.end(),[](Node *p1,Node *p2) {
return p1->pass > p2->pass;
});
// rank
for(int i=0; i<n; i++) {
if(i == 0) {
nodes[i]->rank = i+1;
} else {
if(nodes[i]->pass == nodes[i-1]->pass)
nodes[i]->rank = nodes[i-1]->rank;
else
nodes[i]->rank = i+1;
}
}
// output
int blank = false;
for(int i=0; i<n; i++) {
if(blank) printf(" ");
else blank = true;
printf("%d",id2Node[i]->rank);
}
}