A1056 Mice and Rice (25point(s))

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);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值