Formation is very important when taking a group photo. Given the rules of forming K rows with N people as the following:
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 159Sample Output:
Bob Tom Joe Nick Ann Mike Eva Tim Amy John
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define PAUSE system("pause") using namespace std; const int maxn = 10000 + 10; struct node { int h; char name[10]; bool operator < (const node& on) const { if (on.h == h) { //return (int)name[0] < (int)on.name[0]; //debug 总是犯这种SB的错误(-.-)! return strcmp(name, on.name) < 0; } else { return h > on.h; } } }p[maxn], ans[maxn]; int N, K; void out(int len) { for (int i = 1; i <= len; i++) { printf(i == 1 ? "%s" : " %s", ans[i].name); } puts(""); } int main() { int row, lr, len, mid, cnt, i, j, cou; scanf("%d%d", &N, &K); row = N / K; lr = row + (N - row * K); if (N < K) { row = lr = 1; } for (int i = 1; i <= N; i++) { scanf("%s%d", p[i].name, &p[i].h); } sort(p + 1, p + 1 + N); cou = 1; for (int k = 1; k <= K; k++) { j = -1; i = 1; mid = (k == 1 ? lr : row) / 2 + 1; ans[mid] = p[cou++]; cnt = 1; len = (k == 1 ? lr : row); while (cnt <= len && (mid + j >= 1 || mid + i <= len)) { if (mid + j >= 1) { ans[mid + j] = p[cou++]; cnt++; j--; } if (mid + i <= len) { ans[mid + i] = p[cou++]; cnt++; i++; } } out(cnt); } return 0; }