TZOJ--4997: Waiting for Change (模拟)

4997: Waiting for Change 

时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte

描述

Each student in a certain high school Calculus class must pay $15 for the latest review book. The problem is that not all of the students have exact change and the teacher, of course, has no money at all. Every student has the money to pay but some do not have exact change. To keep it simple, each student has exactly one $5 bill and one $10 bill or has exactly one $20 bill. Eager to begin their review, the students line up to purchase their new review books. This line will be called line A. The teacher forms a second line as needed for students who will have to wait for the teacher to make change. The second line is line B and will start out empty.

Review books will be purchased as follows. If the teacher is able to give the correct change, s/he will sell a review book to the first student in line B, if there is one, and give back the correct change. Otherwise, if the first student in line A has exact change, the teacher will sell that student a review book. However, if the first student in line A does not have exact change, then that student will go to the back of line B. Sadly, it is possible that not every student will be able to get a review book because there might not be enough $5 bills in the room.

Your program must print out the names of the students in line B when line B is at its longest.

输入

The input consists of several test cases. The input starts with the number of test cases.

Each test case starts with the number of students followed by a list of students names and the amount that they can use to pay for the book (either 15 or 20). Fortunately, there are never two students by the same name.

There has one blank line before each test case.

输出

For each test case, your output should be a list of the names of the students waiting in line B when it is at its longest, from the front of the line to the back. If there is a tie, it should print the state of the line the first time it reached its maximum length. If no students ever get in line B, your output should print “Line B stayed empty.”

There will be at most 100 students in line A.

样例输入

3

7
Alfred 15
Beth 15
Calista 20
Desdemona 20
Ezekiel 15
Fred 20
Georgina 15

11
Hazel 20
Izzy 15
James 15
Karl 20
Lucinda 20
Malia 20
Nicholas 15
Oscar 20
Petra 20
Quantasia 20
Redd 15

8
Sasha 20
Tabitha 20
Ursula 20
Victor 15
Wanda 15
Xavier 20
Yolanda 20
Zane 15

样例输出

Line B stayed empty.
Malia Oscar Petra Quantasia
Sasha Tabitha Ursula

题目来源

Cornell University High School Programming Contest 2015

 

题目链接:http://tzcoder.cn/acmhome/problemdetail.do?&method=showdetail&id=4997

 

题目大意:一群人排队买书,他们带的钱15美元(10美元和5美元构成)和20美元,因为卖书的人美元零钱了,所以后面一位的人发现前面没有零钱会和他一起买,否则他就会因为没有零钱而不能买书了,全部人都可以买书输出“Line B stayed empty.”,否则输出不能买书的人名。

模拟题,按照题目意思模拟就可以了,判断一下这个20美元的人前面和后一个是否存在15美元的人即可

 

​
#include<stdio.h>
struct ren
{
	char name[100];
	int s;
};
int main()
{
	ren a[110];
	int m[100];
	int t,n,i,j;
	scanf("%d",&t);
	while(t--)
	{
		j=0;
		int q=0,p=0,s=0;
		scanf("%d",&n);
		for(i=0;i<n;i++)
		{
			getchar();
			scanf("%s %d",a[i].name,&a[i].s);
		}
		int max=0,l;
		for(i=0;i<n;i++)
		{
			if(a[i].s==20)j++;
			else if(a[i].s==15)j--;
			if(j>max)
			{
				max=j;
				l=i;
			}
		}
		if(max==0)printf("Line B stayed empty.
");
		else
		{
			s=max;
			for(i=l;i>=0;i--)
			{
				if(a[i].s==20)s--;
				if(!s)break;
			}
			int o=0;
			for(;i<n;i++)
			{
				if(max==0)break;
				if(a[i].s==20)
				{
					if(o==1)printf(" ");
					printf("%s",a[i].name);
					o=1;
					max--;
				}
			}
			printf("
");
		}
	
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/Anidlebrain/p/10055530.html