PAT_A1109#Group Photo

Source:

PAT A1109 Group Photo (25 分)

Description:

Formation is very important when taking a group photo. Given the rules of forming K rows with Npeople 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

Keys:

  • 简单模拟

Attention:

  • 各行的站位,就是按照从第2高到最矮,依次进栈,入队,进栈,入队...,最高者插在栈和队之间,这样理解起来就很容易了

Code:

 1 /*
 2 Data: 2019-08-18 20:17:26
 3 Problem: PAT_A1109#Group Photo
 4 AC: 38:15
 5 
 6 题目大意:
 7 拍集体照,要求如下:
 8 各行人数为N/K,多余的放在最后一行
 9 各列身高依次递增
10 各行最高的人站在中间(m/2+1)
11 各行其余的人,按照身高从大到小,依次站在中间的左,右,左,右...
12 输入:
13 第一行给出,总人数N,行数K
14 接下来N行,姓名,身高
15 输出:
16 最后一行的姓名,
17 ...
18 第一行的姓名;
19 */
20 #include<cstdio>
21 #include<string>
22 #include<map>
23 #include<stack>
24 #include<queue>
25 #include<vector>
26 #include<iostream>
27 #include<algorithm>
28 using namespace std;
29 const int M=1e4+10;
30 string line[M];
31 map<string,int> mp;
32 
33 bool cmp(string s1, string s2)
34 {
35     if(mp[s1] != mp[s2])
36         return mp[s1] > mp[s2];
37     else
38         return s1 < s2;
39 }
40 
41 int main()
42 {
43 #ifdef ONLINE_JUDGE
44 #else
45     freopen("Test.txt", "r", stdin);
46 #endif // ONLINE_JUDGE
47 
48     int n,k,high;
49     scanf("%d%d", &n,&k);
50     for(int i=0; i<n; i++)
51     {
52         cin >> line[i] >> high;
53         mp[line[i]]=high;
54     }
55     sort(line,line+n,cmp);
56     for(int i=0; i<k; i++)
57     {
58         int st=i*(n/k)+(i==0?0:n%k);
59         int ln=n/k+(i==0?n%k:0);
60         stack<string> l;
61         queue<string> r;
62         vector<string> ans;
63         for(int j=0; j<ln; j++)
64         {
65             if(j%2==0)
66                 r.push(line[j+st]);
67             else
68                 l.push(line[j+st]);
69         }
70         while(!l.empty())
71         {
72             ans.push_back(l.top());
73             l.pop();
74         }
75         while(!r.empty())
76         {
77             ans.push_back(r.front());
78             r.pop();
79         }
80         for(int j=0; j<ans.size(); j++)
81             printf("%s%c", ans[j].c_str(),j==ans.size()-1?'\n':' ');
82     }
83 
84 
85     return 0;
86 }

 

posted @ 2019-06-16 16:59  林東雨  阅读(265)  评论(0编辑  收藏  举报