2014辽宁省ACM程序设计大赛网络赛 问题 E: Help Your Teacher

题目来源:http://acm.sau.edu.cn/JudgeOnline/problem.php?cid=1002&pid=4

Problem E: Help Your Teacher

Time Limit: 1 Sec Memory Limit: 32 MB
Submit: 164 Solved: 93

Description

You are a good student, when your teacher needs help you will be the first one that helps him. Now after the final examination, everyone has two kinds of scores, Math and English. You know nowadays English is very important, so the teacher will arrange your scores firstly according to your English score, then, according to Math. Now there are N(1<=N<=100000) students, everybody has his own scores and name .The teacher will ask you who is the Kth student after arranging them by score. We ensure that there are no two students have the same scores.

Input

There are several tests.

In the first line there is two integers N , Q , represent there are N students and the teacher will ask Q (1<=Q<=1000) times . Then N lines follow, each line has three parts, the student’s name (the length is no longer than 20) the Math score and the English score (the scores are between 1 and 1000).Then following Q lines, each line contains a number K (1<=K<=N), means the teacher ask you the name of the Kth student after arranging them by scores.

Output

There are Q lines of each test case; each line contains the answer of the teacher’s question.

Sample Input

5 3
abcde 100 200
defg 150 300
hyzd 260 150
pku 50 400
heu 500 500
1
2
3

Sample Output

heu
pku
defg

HINT

题意:求名次,将英语成绩从大到小排序,英语成绩高的名次靠前,如果英语成绩相同,那数学成绩大的成绩靠前。



//考察排序,只不过在第一次排序的基础上,找相同的数继续排序而已

/*********************************
*
*	acm: hustoj-1014
*
*	title: Help Your Teacher
*
*	time : 2014.4.28
*
**********************************/

#include 
#include 

#define MAXSIZE 1000 //最大学生数

typedef struct Name
{
	char name[21];
	int math;
	int english;
} Name[MAXSIZE + 1];

void sort(Name n, int N)
{
	int i = 0;
	int j;
	int k;

	int temp;
	char c[21];

	for (; i < N - 1; i++)
	{
		k = i;
		for (j = i + 1; j < N; j++)
		{
			if (n[k].english < n[j].english)
			{
				k = j;
			}
		}

		if (k != i)
		{
				
			temp = n[k].english;
			n[k].english = n[i].english;
			n[i].english = temp;

			temp = n[k].math;
			n[k].math = n[i].math;
			n[i].math = temp;

			strcpy(c,n[k].name);
			strcpy(n[k].name, n[i].name);
			strcpy(n[i].name, c);
		}
	}

	for (i = 0; i < N - 1; i++)
	{
		for (j = i + 1; j < N; j++)
		{
			if (n[i].english != n[j].english)
			{
				break;
			}
			else
			{
				do
				{
					j++;
				}
				while (n[i].english == n[j].english && j < N);

				for (; i < j - 1; i++)
				{
					for (k = i + 1; k <= j - 1; k++)
					{
						if (n[i].math < n[k].math)
						{
							temp = n[k].english;
							n[k].english = n[i].english;
							n[i].english = temp;
			
							temp = n[k].math;
							n[k].math = n[i].math;
							n[i].math = temp;

							strcpy(c, n[k].name);
							strcpy(n[k].name, n[i].name);
							strcpy(n[i].name, c);
						}	
					}
				}
			}
		
		}
	}

}


int main()
{
	int N;  //学生数
	int Q;  //老师提问次数

	while (~scanf("%d%d", &N, &Q))
	{
		Name n;
		int i = 0;
		int j;

		while (i < N)  //初始化人
		{
			scanf("%s", n[i].name);
			scanf(" %d%d", &n[i].math, &n[i].english);
			i++;
		}

		sort(n, N);

		for (i = 0; i < Q; i++)
		{
			scanf("%d", &j);

			printf("%s\n", n[j-1].name);
		}

	}

	return 0;
}




你可能感兴趣的:(排序,acm水题)