pat 1109 Group Photo (25分)

Formation is very important when taking a group photo. Given the rules of forming K rows with N people as the following:

  • The number of people in each row must be / (round down to the nearest integer), with all the extra people (if any) standing in the last row;

  • All the people in the rear row must be no shorter than anyone standing in the front rows;

  • In each row, the tallest one stands at the central position (which is defined to be the position (, where m is the total number of people in that row, and the division result must be rounded down to the nearest integer);

  • In each row, other people must enter the row in non-increasing order of their heights, alternately taking their positions first to the right and then to the left of the tallest one (For example, given five people with their heights 190, 188, 186, 175, and 170, the final formation would be 175, 188, 190, 186, and 170. Here we assume that you are facing the group so your left-hand side is the right-hand side of the one at the central position.);

  • When there are many people having the same height, they must be ordered in alphabetical (increasing) order of their names, and it is guaranteed that there is no duplication of names.

Now given the information of a group of people, you are supposed to write a program to output their formation.

Input Specification:

Each input file contains one test case. For each test case, the first line contains two positive integers N (≤), the total number of people, and K (≤), the total number of rows. Then N lines follow, each gives the name of a person (no more than 8 English letters without space) and his/her height (an integer in [30, 300]).

Output Specification:

For each case, print the formation -- that is, print the names of people in K lines. The names must be separated by exactly one space, but there must be no extra space at the end of each line. Note: since you are facing the group, people in the rear rows must be printed above the people in the front rows.

Sample Input:

10 3
Tom 188
Mike 170
Eva 168
Tim 160
Joe 190
Ann 168
Bob 175
Nick 186
Amy 160
John 159
 

Sample Output:

Bob Tom Joe Nick
Ann Mike Eva
Tim Amy John

解题思路:

这道题目主要是题目长度一大串,开始理解错题目了,这道题目主要意思是,有n个人,排成k行,按照身高排序,每一行中,身高最高的站在中间,然后依次从中间依次往这个人的右边,这个人的左边反复站好,输出排好的人的姓名。

我只想是c++的vector真的非常好用。

假设给一个序列,排成一行。

d     60

c     60

b     60

a    60

e    70

这时候应该是站成 e  -->  a   e  -->   a  e  b  --> c   a  e  b   -->   c  a   e  b   d  ,大家发现规律了吗?  前提是要按照这样的顺序排好序,分数从小到大排序,相同分数的人,名字按字典从大到小排序。因为

When there are many people having the same height, they must be ordered in alphabetical (increasing) order of their names, and it is guaranteed that there is no duplication of names.

我先用一个vector把e装进去,然后在e的左边插入a,然后e的右边插入b,往复。输出这个vector就行了。代码如下:

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
typedef struct Stu{
	string name;
	int height;
}Stu;
Stu stus[10010];
bool cmp(Stu a,Stu b){  //从大到小排序,身高相同,从小到大排序 
     if(a.height!=b.height) return a.height<b.height;
     else return strcmp(a.name.c_str(),b.name.c_str()) >0;
}
void printout(int first,int last){
	vector<string> V;
	V.push_back(stus[last].name);
	int flag = 0;
	for(int i=last-1;i>=first;i--){
		if(flag ==0){  //从左边插入 
			V.insert(V.begin(),stus[i].name);
			flag = 1;
		}else{//从右边插入 
			V.push_back(stus[i].name);
			flag = 0;			
		}
	}
	//输出 
	printf("%s",V[0].c_str());
	for(int i=1;i<V.size();i++){
		printf(" %s",V[i].c_str());
	}
	printf("
");
	V.clear();
}

int main(){
	#ifdef ONLINE_JUDGE
	#else
		freopen("C:\Users\zzloyxt\Desktop\1.txt","r",stdin);	
	#endif
	int n,k;
	scanf("%d %d",&n,&k);
	for(int i=0;i<n;i++){
		cin>>stus[i].name>>stus[i].height;
	}
	sort(stus,stus+n,cmp);
	int m = n/k; //每行有m个
	int sm = n%k; // 最后一行多出的人
	int slast = m + sm;
	printout(n-slast,n-1);
	for(int i=k-1;i>0;i--){
		printout((i-1)*m, i*m-1);
	}
	return 0;
}

  

 

原文地址:https://www.cnblogs.com/zzlback/p/12426687.html