1109 Group Photo (25)

Posted mr-stn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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 (<=10000), 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.

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

题目大意:给出一些人的身高信息,把他们排成k行, 后一行的人都比前一行的人高, 每一样呈三角形形状排列
思路:对所有人对身高进行降序排序,对于每一行先输出右边的人,再输出中间的人,再输出左边的人,右边是身高依次升高, 左边身高依次降低;
把每一行身高最高的人的序号记为mid,可以发现左边人的序号是mid+2*i,i是从中间向左边数的序列
对于右边的人来说要分两种情况1.人数为奇数:右边人的序号是mid+m-2*i-2
2.人数为偶数:右边人的序号是mid+m-2*i-1, i是从第一个人从右往左开始计数
第一次提交的时候,没有分奇偶导致有两个点没能通过,后面经过修改分了奇偶,但是只在前r-1行分了奇偶, 没有在最后一行分奇偶; 所以说在程序调试的时候,应该有这样的思维,在相同的结构中一部分进行了修改,对于的部分也应该进行修改
 1 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 #include<cstring>
 5 using namespace std;
 6 struct node{
 7   char name[10];
 8   int h;
 9 };
10 
11 bool cmp(node a, node b){
12   if(a.h != b.h) return a.h>b.h;
13   return strcmp(a.name, b.name)<0;
14 }
15 int main(){
16   int n, row, i, j;
17   cin>>n>>row;
18   vector<node> v(n);
19   for(i=0; i<n; i++) scanf("%s %d", v[i].name, &v[i].h);
20   sort(v.begin(), v.end(), cmp);
21   int m = (int)(1.0*n/row + 0.5), last=n-m*(row-1) ;
22   int mid=0, r=(last-1)-(last-1)/2, l=(last-1)/2;
23   for(i=0; i<r; i++){
24    if(last%2==0) printf("%s ", v[last-1-2*i].name);
25     else printf("%s ", v[last-2-2*i].name);
26   }
27   printf("%s", v[mid].name);
28   for(i=0; i<l; i++) printf(" %s", v[2*i+2].name);
29   r=(m-1)-(m-1)/2; l=(m-1)/2;
30   cout<<endl;
31   for(i=1; i<row; i++){
32     mid = last+(i-1)*m;
33     for(j=0; j<r; j++){
34         if(m%2!=0)printf("%s ", v[mid+m-2-2*j].name);
35         else printf("%s ", v[mid+m-1-2*j].name);
36     }
37     printf("%s", v[mid].name);
38     for(j=0; j<l; j++)printf(" %s", v[mid+2*j+2].name);
39     cout<<endl;
40   }
41   return 0;
42 }

 








以上是关于1109 Group Photo (25)的主要内容,如果未能解决你的问题,请参考以下文章

1109 Group Photo (25分)

1109 Group Photo (25分)

1109 Group Photo (25分)

PAT A1109 Group Photo (25) [逻辑题]

PAT (Advanced Level) 1109. Group Photo (25)

1109 Group Photo (25 分)难度: 一般 / 知识点: 模拟 排序