HUD4393 Throw nails(思维模拟)

                      Throw nails

                                   Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                  Total Submission(s): 2096    Accepted Submission(s): 672

Problem Description
The annual school bicycle contest started. ZL is a student in this school. He is so boring because he can't ride a bike!! So he decided to interfere with the contest. He has got the players' information by previous contest video. A player can run F meters the first second, and then can run S meters every second.
Each player has a single straight runway. And ZL will throw a nail every second end to the farthest player's runway. After the "BOOM", this player will be eliminated. If more then one players are NO.1, he always choose the player who has the smallest ID.
 

Input
In the first line there is an integer T (T <= 20), indicates the number of test cases.
In each case, the first line contains one integer n (1 <= n <= 50000), which is the number of the players.
Then n lines follow, each contains two integers Fi(0 <= Fi <= 500), Si (0 < Si <= 100) of the ith player. Fi is the way can be run in first second and Si is the speed after one second .i is the player's ID start from 1.
Hint

Huge input, scanf is recommended.
Huge output, printf is recommended.

Output
For each case, the output in the first line is "Case #c:".
c is the case number start from 1.
The second line output n number, separated by a space. The ith number is the player's ID who will be eliminated in ith second end.

Sample Input
2 3 100 1 100 2 3 100 5 1 1 2 2 3 3 4 1 3 4

Sample Output
Case #1: 1 3 2 Case #2: 4 5 3 2 1
Hint
Hint The first case: 1st Second end Player1 100m (BOOM!!) Player2 100m Player3 3m 2nd Second end Player2 102m Player3 103m (BOOM!!) 3rd Second end Player2 104m (BOOM!!)

Source

Recommend
zhuyuanchen520

题目意思:

       题目说学校进行跑步比赛,但是有人想要搞破坏。他将丢东西到第一名的面前,把他炸走,然后重复操作,直至跑到上没有人。

即求每次跑第一名的编号,如果同一条线上有多个人,取编号最小的那个人。

解题思路:

       每个人都要一个初始位置和速度,题目要我们求每一秒钟跑第一的人的编号(如果在同一位置有多人,取编号最好的那人的编号),然后消除它在求下一秒。

 

题目是个模拟,但每个人的位置都是动态变化的,每一次寻找之前没有被消除且跑在第一的人,时间花费太大。按照题目意思直接去写,会超时。

由于题目给的每个人的位置信息和速度信息有个范围限制,可以发现,当时间超过500秒以后,无论人的速度再怎么快,都不能追上之前的人。(如果500秒以后还能追上的话,那500前早就追上了。取位置和速度两个极限模拟一下就能得出这个结论)

因此,在500秒以后的位置,就是最后的最终位置,即是答案。500秒前暴力,按照题目意思模拟,500秒后对现在的位置排序即可。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
#include<queue>
using namespace std;

bool tag[50005];
int F[50005],S[50005];
int t,n;
struct node
{
	int now;
	int id;
	bool operator < (const node &a) const
	{
		if(now==a.now)
			return id>a.id;
		return now<a.now;
	}
}tmp;
bool cmp(struct node a,struct node b)
{
	if(a.now==b.now)
		return a.id>b.id;
	return a.now<b.now;
}
int main()
{
	while(scanf("%d",&t)!=EOF)
	{
		int i,j,k,num;
		int a,b,ed;
		int M,ans;
		for(i=1;i<=t;i++)
		{
			priority_queue<struct node> que;
			memset(tag,0,sizeof tag);
			M=-1; ans=0;
			scanf("%d",&num);
			num>502?ed=502:ed=num;
			for(j=1;j<=num;j++)
			{
				scanf("%d%d",&a,&b);
				F[j]=a; S[j]=b;
				if(F[j]>M)
				{
					M=F[j];
					ans=j;
				}
			}
			printf("Case #%d:\n",i);
			printf("%d",ans);
			tag[ans]=1;
			for(j=2;j<=ed;j++)
			{
				M=-1; ans=0;
				for(k=1;k<=num;k++)
				{
					if(tag[k])
						continue;
					else
					{
						F[k]+=S[k];
						if(F[k]>M)
						{
							M=F[k];
							ans=k;
						}
					}
				}
				tag[ans]=1;
				printf(" %d",ans);
			}
			if(num>502)
			{
				for(k=1;k<=num;k++)
				{
					if(tag[k])
						continue;
					else
					{
						F[k]+=S[k];
						tmp.now=F[k];
						tmp.id=k;
						que.push(tmp);
					}
				}
				while(que.size())
				{
					tmp=que.top();
					printf(" %d",tmp.id);
					que.pop();
				}
			}
			cout<<endl;
		}
	}
}

/*
4
5
1 1
2 2
3 3
4 1
3 4
*/



 

全部评论

相关推荐

09-29 17:44
已编辑
蔚来_测(准入职员工)
//鲨鱼辣椒:见不了了我实习了四个月上周再投筛选了一天就给我挂了
点赞 评论 收藏
分享
头像
11-06 10:58
已编辑
门头沟学院 嵌入式工程师
双非25想找富婆不想打工:哦,这该死的伦敦腔,我敢打赌,你简直是个天才,如果我有offer的话,我一定用offer狠狠的打在你的脸上
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务