Categories
不学无术

1056. Mice and Rice (25)

时间限制
30 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
========
Mice and Rice is the name of a programming contest in which each programmer must write a piece of code to control the movements of a mouse in a given map. The goal of each mouse is to eat as much rice as possible in order to become a FatMouse.

First the playing order is randomly decided for NP programmers. Then every NG programmers are grouped in a match. The fattest mouse in a group wins and enters the next turn. All the losers in this turn are ranked the same. Every NGwinners are then grouped in the next match until a final winner is determined.
For the sake of simplicity, assume that the weight of each mouse is fixed once the programmer submits his/her code. Given the weights of all the mice and the initial playing order, you are supposed to output the ranks for the programmers.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive integers: NP and NG (<= 1000), the number of programmers and the maximum number of mice in a group, respectively. If there are less than NG mice at the end of the player’s list, then all the mice left will be put into the last group. The second line contains NP distinct non-negative numbers Wi (i=0,…NP-1) where each Wiis the weight of the i-th mouse respectively. The third line gives the initial playing order which is a permutation of 0,…NP-1 (assume that the programmers are numbered from 0 to NP-1). All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the final ranks in a line. The i-th number is the rank of the i-th programmer, and all the numbers must be separated by a space, with no extra space at the end of the line.
Sample Input:

11 3
25 18 0 46 37 3 19 22 57 56 10
6 0 8 7 10 5 9 1 4 2 3

Sample Output:

5 5 5 2 5 5 5 3 1 3 5

===================================
这个题目主要的难度在时间限制,30ms看起来很吓人,但是其实问题的规模不算大…最多才1000只老鼠嘛!
主要思路就是矩阵数组能搞多大搞多大,能用静态用静态,因为内存范围太宽…
为了加快程序速度,在对组内的老鼠排序的时候,显然不能使用标准库里面的算法,就算快速排序也有O(nlog2n)的时间复杂度,这边只要选一只最肥的就行,用最笨的冒泡或者插入排序做一次就行了,O(n)的时间复杂度。
另外第二个难度可能在比赛结果的存储上,由于时间限制紧,我就直接弄了个1000*1000的矩阵存结果,第一行存的是第一轮比赛的结果,把没有晋级的老鼠们的序号存在第一行的各个列中,用-1作为本列结束标志…这样以此类推,当某行第一个元素就是-1的时候,所有肥老鼠就比完赛了(当然这个首列-1的设置在后面的排名中没起什么结果,因为有个计数器已经记下一共比了多少轮了)
另外,记得cin,cout用scanf,printf替换,不然读个数列写个结果估计就能让程序超时了。这个可能是PAT设计的不合理的地方,应该分程序语言来限定时间,30ms这么搞让写Java的情何以堪??…
别的没啥了,自己的代码5个测试点都是0ms..内存消耗或许大了点,哈哈
====================================

#include 
using namespace std;
const int MAX_ELEM_SIZE = 1000;
int Player_values[MAX_ELEM_SIZE];
int Player_result[MAX_ELEM_SIZE][MAX_ELEM_SIZE] = {0}; //比赛结果,0表示还没有出最后成绩
int contest_size = 0; //每场比赛的总人数
int stadium[MAX_ELEM_SIZE]; //赛场,存储序号
int sequence[MAX_ELEM_SIZE];//比赛顺序,没有资格参赛的设置为-1
int ranks[MAX_ELEM_SIZE];
int N_P, N_G;
int main()
{
	scanf("%d%d", &N_P, &N_G);
	for(int i=0; i 0)
	{
		//一轮比赛一个循环
		int max_seq=-1;
		int max_val=-1;
		int true_count = 0;
		int loser_count = 0;
		for(int i=0; i max_val)
			{
				if(contest_size == 1)
				{
					//he is the winner!
					Player_result[turn][loser_count++] = sequence[i];
					contest_size = 0;
					continue;
				}
				max_val = Player_values[ sequence[i] ];
				if( max_seq != -1)
				{
					//把原来的人标记为Loser
					Player_result[turn][loser_count++] = sequence[max_seq];
					contest_size --;
					sequence[max_seq] = -1;
				}
				max_seq = i;
			}
			else
			{
				//loser
				Player_result[turn][loser_count++] = sequence[i];
				contest_size -- ;
				sequence[i] = -1;
			}
			true_count ++;
		}
		Player_result[turn][loser_count] = -1; //终止这一行!
		turn++;
	}
	Player_result[turn][0] = -1;
	//开始分配名次
	int rank = 1;
	int r_count = 1;
	while(turn --)
	{
		int col = 0;
		rank = r_count;
		while(true)
		{
			if( Player_result[turn][col] == -1)
				break;
			ranks[ Player_result[turn][col] ] = rank;
			r_count++;
			col ++;
		}
	}
	for(int i=0; i

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.