1055. Group photo (+)Time limit MS
Memory Limit 65536 KB
Code length limit 8000 B
Program Standard author CHEN, Yue
The formation of the group is very important, here for a given n individual K-row formation design queuing rules are as follows: Each row of the number of n/k (rounded down), the number of people are all standing in the last row; everyone in the back row is not shorter than anyone in the front row; the center of the tallest person in the platoon (middle position is m/2+1 , Division is rounded down); the other person in each row with the man-in-the-middle axis, according to the height of non-sequential, first right after the left alternately to stand on the side of the middle man (for example, 5 people are 190, 188, 186, 175, 170, the formation of 175, 188, 190, 186, 170. This assumes that you are facing the photographer, so your left side is the right hand side of the middleman, and if more than one person is of the same height, the dictionary order in ascending order of the name. No duplicate names are guaranteed here.
Now given a group of photographers, write programs to output their formations.
Input Format:
Each input consists of 1 test cases. The 1th line of each test case gives two positive integers n (<=10000, total number) and K (<=10, total rows). Then n rows, each row gives a person's name (no spaces, no more than 8 letters in length) and height (integers in the [30, 300] interval).
output Format:
Output the formation of a photograph. That is, the name of the K row, between the space separated by the end of the line must not have extra space. Note: Suppose you face the photographer, the back of the person output is above, the front row output is below. Input Sample:
3
Tom 188
Mike-
Eva 168
Tim
168 Joe-175-
186
-Bob-Ann John 159
Sample output:
Bob Tom Joe Nick
Ann Mike Eva
Tim Amy John
#include <iostream> #include <algorithm> #include <sstream> #include <string> using NAMESP
Ace STD;
Class Man1 {public:string name;
int height;
};
BOOL Compare (Man1 &a, Man1 &b) {if (a.height! = b.height) return a.height > B.height;
return A.name < B.name;
} int Main () {string S; int N;
int column, I, J, Count, j_1, temp = 1;
CIN >> N >> column;
Man1 *people = new Man1[n];
for (i = 0; i < N; i++) cin >> people[i].name >> people[i].height;
Sort (people, people + N, compare);
j = N/column;
J_1 = N-(COLUMN*J-J);
for (i = 0; i < j_1; i++) {if (temp = = 1) {if (I! = 0) s = s + "+ people[i].name;
else S = people[i].name;
} else S = people[i].name + ' + s;
Temp *=-1;
} cout << s << endl;
S.clear ();
temp = 1;
for (i = j_1; i < N; i++) {if (temp = = 1) {if (S.empty ()) s = people[i].name;
else S = s + ' + people[i].name;
}else s = people[i].name+ ' + S;
Temp *=-1;
if ((i-j_1+1)% J = = 0) {cout << s << endl;
S.clear ();
temp = 1; }
}
}
Impressions: 1. The overloaded + number operation with string is much more concise.
2. Note the format, the processing of space is very important