1109 Group Photo

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 N/K (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 (m/2+1), 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 (104​​), the total number of people, and K (10), 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.

思路:

先排序

 John Tim Amy, Eva Ann Mike, Bob Nick Tom Joe

 man_in_row = n/k; 以每行为单位进行排列,从个子矮的行开始排

注意* 最初排序的方式应该是身高升序,名降序:这样才能保证在每行中进行排列的时候,先排的是个子高的,身高相同时是名字典序小的

  * 对每行进行排列,对每行的循环应该这样写 for(; ptr+man_in_row*2<=n; ptr+=man_in_row)!!

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 #include <string>
 5 #include <map>
 6 using namespace std;
 7 const int maxn = 12;
 8 
 9 struct mantype{
10     string name;
11     int height;
12 };
13 struct mantype manList[10005];
14 
15 bool cmp(struct mantype m1,struct mantype m2){
16     if(m1.height==m2.height){
17         return m1.name>m2.name;
18     }
19     return m1.height<m2.height;
20 }
21 
22 int main(){
23     int n,k; scanf("%d %d",&n,&k);
24     string tmps; int height;
25     for(int i=0;i<n;i++){
26         cin >> manList[i].name >> manList[i].height; // !!
27     }
28     sort(manList, manList+n, cmp);
29     
30     /*for(int i=0;i<n;i++){
31         cout << manList[i].name << " ";
32     }*/
33 
34     string matrix[13][10005];
35     int man_in_row = n/k;
36     
37     int ptr = 0; int lv=0;
38     int t,i;
39     for(; ptr+man_in_row*2<=n; ptr+=man_in_row){ // !!
40         int mid = man_in_row/2+1;
41         matrix[lv][mid] = manList[ptr+man_in_row-1].name;
42         t=1;
43         for(i=ptr+man_in_row-2;i>=ptr;i-=2){
44             matrix[lv][mid-t] = manList[i].name;
45             if(i-1>=ptr) matrix[lv][mid+t] = manList[i-1].name;
46             t++;
47         }
48         lv++;
49     }
50     int remain = (n-ptr);
51     int mid = remain/2+1;
52     matrix[lv][mid] = manList[n-1].name;
53     t=1;
54     for(i=n-2;i>=ptr;i-=2){
55         matrix[lv][mid-t] = manList[i].name;
56         if(i-1>=ptr) matrix[lv][mid+t] = manList[i-1].name;
57         t++;
58     }
59     
60     //printf("
------
");
61     
62     for(int j=1;j<=remain;j++){
63         if(j!=1) printf(" ");
64         cout<<matrix[lv][j];
65     }
66     printf("
");
67     for(int i=lv-1;i>=0;i--){
68         for(int j=1;j<=man_in_row;j++){        
69             if(j!=1) printf(" ");            
70             cout<<matrix[i][j];
71         }
72         printf("
");
73     }
74 
75     return 0;
76 }

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
原文地址:https://www.cnblogs.com/lokwongho/p/9927852.html