Click on the link full solution summary PAT B -AC
Title:
formation is very important when a group photo shoot, where formation of the following design rules given queuing K N individual row:
- The number of each row of N / K (rounded down), all the extra people standing in the last row;
- Back all child not shorter than anyone in the front row;
- Each intermediate station the highest row (middle position m / 2 + 1, where m for the number of rows, the division rounded down);
- In each row others intermediary axis, according to a non-increasing order of height, the first left and right sides alternately enqueue standing intermediary (e.g. 190,188,186,175,170 height of 5, the formation 175, . 188,190,186,170 this assumes you are facing the photographer, so your left is the right of intermediaries);
- If more than the same height, press the name of lexicographic ascending order. Here to ensure that no duplicate names.
Now given a set of pictures of people, write a program output their formation.
Input format:
Each input comprises a test. Each test case is given row of the first two positive integers N (≦ 10 . 4 , the total number) and K (≤10, total number of rows). Then N rows, each row gives the name of a person (not including spaces, length of no more than 8 letters) and height ([30, 300] integer in the interval).
Output formats:
Output to take pictures of the formation. That K row names, separated by spaces between them, the end of the line may not have the extra space. Note: if you face the photographer, who back in the top of the output, the output of the front row at the bottom.
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
My code:
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstring>
#include<sstream>
using namespace std;
//有的时候题目是一起做的,所以会有不需要的头文件
int cmp(pair<string,int>a,pair<string,int>b){
if(a.second!=b.second) return a.second>b.second;
else return a.first<b.first;
}
void print_first_to_last(pair<string,int>student[],int first,int last)
{
int m=last-first+1;
int first_=(m%2==0)?(last):(last-1);
int end_=(m%2==0)?(last-1):(last);
int i=first_;
while(i>=first+1)
{
cout<<student[i].first<<" ";
i-=2;
}
i=first;
while(1)
{
cout<<student[i].first;
i+=2;
if(i<=end_) cout<<" ";
else
{
cout<<endl;
return;
}
}
}
int main()
{
int N,K;
cin>>N>>K;
pair<string,int>student[N];
for(int i=0;i<N;i++)
{
string name;
int height;
cin>>name>>height;
student[i]=make_pair(name,height);
}
sort(student,student+N,cmp);
int num_line=N/K;
print_first_to_last(student,0,num_line+N%K-1);
for(int line=2;line<=K;line++)
print_first_to_last(student, N%K+(line-1)*num_line, N%K + line*num_line -1);
return 0;
}
My thoughts:
Determining the range of each row: A total of N individuals, in addition to the first line, there is K Line
-
First line of
N/K+N%K
peopleThe subscript
0
~N/K+N%K-1
-
Then 2 ~ K lines each have
N/K
peopleFor the first
line
row,
the first index is aN/K + N%K + (line-2)*N/K
=N%K + (line-1)*N/K
the last indexN%K + (line-1)*N/K + N/K-1
=N%K + line*N/K -1
For the output of a row: Index for sorting, then according to the following target sequence one by one like the output
- Sorting according to height, corresponding to subscript
0
~N-1
- For example, a line
m
number then there are two cases
-
m is an even number: (m-1) ... 3 1 0 2 4 ... (m-2)
- The first is: the last one
- An intermediate stop at: 1 (after a first one)
- The last one is: the last one before a
-
m is odd: (m-2) ... 3 1 0 2 4 ... (m-3) (m-1)
- The first is: the last one before a
- An intermediate stop at: 1 (after a first one)
- The last one is: the last one
- For each row, one of the first index
first
, a last indexlast
,m=last-first+1
- m is an even number: from
last
-2 -2 tofirst+1
, and fromfirst
+2 to +2last-1
- m is odd: from
last-1
-2 -2 tofirst+1
, and fromfirst
+2 to +2last