Alisha’s Party 5437 (字符串+优先队列)好题

211 篇文章 1 订阅
9 篇文章 0 订阅

Alisha’s Party

Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2518    Accepted Submission(s): 681


Problem Description
Princess Alisha invites her friends to come to her birthday party. Each of her friends will bring a gift of some value v , and all of them will come at a different time. Because the lobby is not large enough, Alisha can only let a few people in at a time. She decides to let the person whose gift has the highest value enter first.

Each time when Alisha opens the door, she can decide to let p people enter her castle. If there are less than p people in the lobby, then all of them would enter. And after all of her friends has arrived, Alisha will open the door again and this time every friend who has not entered yet would enter.

If there are two friends who bring gifts of the same value, then the one who comes first should enter first. Given a query n Please tell Alisha who the nth person to enter her castle is.

Input
The first line of the input gives the number of test cases, T , where 1T15 .

In each test case, the first line contains three numbers k,m and q separated by blanks. k is the number of her friends invited where 1k150,000 . The door would open m times before all Alisha’s friends arrive where 0mk . Alisha will have q queries where 1q100 .

The ith of the following k lines gives a string Bi , which consists of no more than 200 English characters, and an integer vi , 1vi108 , separated by a blank. Bi is the name of the ith person coming to Alisha’s party and Bi brings a gift of value vi .

Each of the following m lines contains two integers t(1tk) and p(0pk) separated by a blank. The door will open right after the tth person arrives, and Alisha will let p friends enter her castle.

The last line of each test case will contain q numbers n1,...,nq separated by a space, which means Alisha wants to know who are the n1th,...,nqth friends to enter her castle.

Note: there will be at most two test cases containing n>10000 .

Output
For each test case, output the corresponding name of Alisha’s query, separated by a space.

Sample Input
  
  
1 5 2 3 Sorey 3 Rose 3 Maltran 3 Lailah 5 Mikleo 6 1 1 4 2 1 2 3

Sample Output
  
  
Sorey Lailah Rose
 
//这个一直超时,先保存一下,明天再看
 
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct zz
{
	char c[210];
	int v;
	int l;
}s[300100],ss[300100];
int cmp(zz x,zz y)
{
	if(x.v==y.v)
		return x.l<y.l;
	return x.v>y.v;
}
int a[150100];
int main()
{
	int T,i,j,x,y;
	int n,m,t,sum,num;
	scanf("%d",&T);
	while(T--)
	{
		num=1;
		scanf("%d%d%d",&n,&m,&t);
		for(i=1;i<=n;i++)
		{
			scanf("%s",s[i].c);
			scanf("%d",&s[i].v);
			s[i].l=i;
		}
		int k=0;
		while(m--)
		{
			scanf("%d%d",&x,&y);
			sort(s+num,s+x+1,cmp);
			for(i=num;i<num+y;i++)
				strcpy(ss[i].c,s[i].c);
			
			num+=x;
		}
		for(i=1;i<=t;i++)
			scanf("%d",&a[i]);
		for(i=1;i<t;i++)
			printf("%s ",ss[i].c);
			printf("%s\n",ss[i].c);
	}
	return 0;
}
 
 
长见识了,自己学的太少了,大神是用优先队列写的
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
#include<algorithm>
#define MAXN 150000
using namespace std;
struct s
{
	int a;
	int b;
}come[MAXN];
struct ss
{
	int cas;
	int value;
	friend bool operator<(ss a,ss b)
	{
		if(a.value==b.value)
		return a.cas>b.cas;
		return a.value<b.value;
	}
}d,e;
int val[MAXN+10];
char man[MAXN+10][210];
int num[MAXN+10];
int qq[MAXN+10];
bool cmp(s aa,s bb)
{
	return aa.a<bb.a;
}
int main()
{
	int t;
	int i,j,k,m,pro,qqq;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d%d",&k,&m,&qqq);
		priority_queue<ss>q;
		for(i=1;i<=k;i++)
		{
			scanf("%s%d",man[i],&val[i]);
			qq[i]=i;
		}
		for(i=1;i<=m;i++)
		scanf("%d%d",&come[i].a,&come[i].b);
		sort(come+1,come+m+1,cmp);//对要求排序,刚开始以为题目不会要求这个,但是后来一想,还是写吧
		int j=1;
		int ans=1;
		for(i=1;i<=m;i++)
		{
			while(j<=come[i].a&&j<=k)
			{
				d.cas=qq[j];
				d.value=val[j];
				q.push(d);
				j++;
			}
			int w=0;
			while(w<come[i].b&&!q.empty())
			{
				e=q.top();
				num[ans++]=e.cas;
				q.pop();
				w++;
			}
		}
		for(i=j;i<=k;i++)
		{
			d.cas=qq[i];
			d.value=val[i];
			q.push(d);
		}
		while(!q.empty())
		{
		    e=q.top();
			num[ans++]=e.cas;
			q.pop();
		}
		for(i=0;i<qqq;i++)
		{
			scanf("%d",&pro);
			printf(i==qqq-1?"%s\n":"%s ",man[num[pro]]);
		}
	}
	return 0;
}

//自己又写了一遍,感觉收获不少
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
struct zz
{
	int dw;//定位来的人进入的次序 
	int val;//所带礼物的价值 
	char name[201];
	friend bool operator<(zz x,zz y)
	{
		if(x.val==y.val)
			return x.dw>y.dw;
			return x.val<y.val;
	}
}E,num[150010];
struct z
{
	int km;//开门时的截止人数 
	int rs;//每次开门进去的人数 
}door[150010];
int cmp(z x,z y)//这块排序是为了防止出现t[i]>t[i+1]的情况 
{
	return x.km<y.km;
}
int mm[150010];
int main()
{
	int K,M,P;
	int T;
	int i;
	scanf("%d",&T);
	while(T--)
	{
		priority_queue<zz>q;
		scanf("%d%d%d",&K,&M,&P);
		for(i=1;i<=K;i++)
			scanf("%s%d",num[i].name,&num[i].val);
		for(i=1;i<=M;i++)
			scanf("%d%d",&door[i].km,&door[i].rs);
		sort(door+1,door+M+1,cmp);
		int top=1;
		int cnt=1;//开门时到来的人数
		for(i=1;i<=M;i++)//开门的次数 
		{
			while(cnt<=door[i].km)
			{
				strcpy(E.name,num[cnt].name);
				E.val=num[cnt].val;
				E.dw=cnt;
				cnt++;
				q.push(E);//在他们都还没进去之前,先将他们入队列,进行相应的排序 
			}
			while(door[i].rs&&!q.empty())//开门后,进去相应的人数,此时进去的人已经按题中要求排好序了 
			{
				mm[top++]=q.top().dw;//进去的直接定位。 
				q.pop();
				door[i].rs--;
			}
		}
		while(cnt<=K)//这块处理开了M次门后,还没进去的人 
		{
			strcpy(E.name,num[cnt].name);
			E.val=num[cnt].val;
			E.dw=cnt;
			cnt++;
			q.push(E);
		}
		while(!q.empty())
		{
			mm[top++]=q.top().dw;
			q.pop();
		}
		int n;
		for(i=0;i<P;i++)
		{
			scanf("%d",&n);
			if(i)
				printf(" ");
				printf("%s",num[mm[n]].name);
		}
		printf("\n");
	}
	return 0;
} 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值