/*#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;
}
HNU 12266
最新推荐文章于 2023-02-18 12:01:08 发布