HNU 12266

/*#include<iostream>
#include<algorithm>
#include<vector>
#include<queue> 
using namespace std;
struct zz
{
	int time, fee, num;
}z[30005];//定义人这个结构体 
vector<zz>test;//定义一个放最终顺序的 
queue<zz>q;//定义一个用来存放被贪心挤掉的 
bool com(zz a, zz b)//sort排序用的方式 
{
	if (a.time == b.time)
		return a.fee>b.fee;
	return a.time<b.time;
}
int main()
{
	int n;
	scanf("%d", &n);

	for (int i = 0; i<n; i++) {
		scanf("%d %d", &z[i].time, &z[i].fee);
		z[i].num = i + 1;
	}
	sort(z, z + n, com);
	for (int i = 0; i<n; i++)
	{
		if (z[i].time>test.size())//可以放进来 
			test.push_back(z[i]);
		else {
			zz flag = z[i];
			int flag1 = -1;
			for (int j = 0; j<test.size(); j++) {//找到价值最小的扔出去 
				if (test[j].fee<flag.fee) {
					flag = test[j];
					flag1 = j;
				}
			}

			q.push(flag);//将扔出去的保存起来 
			if (flag1 != -1) {//扔出去的是已经存在vector里的 
				test.erase(test.begin() + flag1);
				test.push_back(z[i]);
			}
		}
	}
	while (!q.empty()) {//将扔出去的放回来,按先出先进的方法 (因此用队列) 
		test.push_back(q.front());
		q.pop();
	}
	for (int i = 0; i<test.size(); i++)//打印结果 
		printf("%d\n", test[i].num);
	return 0;
}




测试版 
*/ 
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
struct zz
{
	int time, fee, num;
}z[30005];
vector<zz>test;
queue<zz>q;
bool com(zz a, zz b)
{
	if (a.time == b.time)
		return a.fee>b.fee;
	return a.time<b.time;
}
int main()
{
	int n;
	while (scanf("%d", &n))
	{
		for (int i = 0; i<n; i++) {
			scanf("%d %d", &z[i].time, &z[i].fee);
			z[i].num = i + 1;
		}

		sort(z, z + n, com);
		for (int i = 0; i<n; i++)
			printf("排序后的队列\n%d %d\n", z[i].time, z[i].fee);
		printf("贪心后的队列:\n");

		for (int i = 0; i<n; i++)
		{
			if (z[i].time>test.size())
				test.push_back(z[i]);
			else {
				zz flag = z[i];
				int flag1 = -1;
				for (int j = 0; j<test.size(); j++) {
					if (test[j].fee<flag.fee) {
						flag = test[j];
						flag1 = j;
					}
				}
				printf("最少消费的是%d \n", flag.num);
				q.push(flag);
				if (flag1 != -1) {
					test.erase(test.begin() + flag1);
					test.push_back(z[i]);
				}
			}
		}
		while (!q.empty()) {
			test.push_back(q.front());
			q.pop();
		}
		for (int i = 0; i<test.size(); i++)
			printf("%d %d %d\n", test[i].time, test[i].fee, test[i].num);
		//printf("%d %d\n",z[i].time,z[i].fee);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值