PAT 1109 Group Photo

来源:互联网 发布:js 计数器 滚动效果 编辑:程序博客网 时间:2024/06/13 03:41

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 3Tom 188Mike 170Eva 168Tim 160Joe 190Ann 168Bob 175Nick 186Amy 160John 159
Sample Output:
Bob Tom Joe NickAnn Mike EvaTim Amy John
#include <cstdio>#include <algorithm>#include <iostream>using namespace std;struct node{string name;int height;}stu[10000];int n,k;int peop;bool cmp(node a,node b){if(a.height!=b.height){return a.height>b.height;}else{return a.name<b.name;}}int main(){int flag=0;string num[10000];int left,right,middle;cin>>n>>k;//人数 行数peop=n/k;for(int i=0;i<n;i++){cin>>stu[i].name>>stu[i].height;}sort(stu,stu+n,cmp);if(n%k==0){left=0;right=peop-1;}else{left=0;right=n-(k-1)*peop-1;}for(int i=1;i<=k;i++){int e=-1;int count=1;int times=0;middle=(right-left+1)/2;num[middle]=stu[left].name;for(int j=left+1;j<=right;j++){num[middle+e*count]=stu[j].name;e=-e;times++;if(times%2==0){count++;}}for(int i=0;i<right-left+1;i++){cout<<num[i];cout<<(i==right-left?'\n':' ');}left=right+1;right=left+peop-1;}return 0;}
//对比某网友代码:
#include<cstdio>  #include<vector>  #include<cstring>  #include<algorithm>  using namespace std;  typedef long long LL;  const int maxn = 1e5 + 10;  int n, m, c[maxn], ans[maxn], a[maxn];  char s[maxn][20];    bool cmp(const int &x, const int &y)  {      return c[x] == c[y] ? strcmp(s[x], s[y]) < 0 : c[x] > c[y];  }    void write(int l, int r)  {      int m = (r - l) / 2 + 1, len = r - l;      a[m] = ans[l++];      for (int q = m, h = m; l < r;)      {          a[--q] = ans[l++];          a[++h] = ans[l++];      }      for (int i = 1; i <= len; i++)      {          printf("%s", s[a[i]]);          if (i == len) printf("\n"); else printf(" ");      }  }    int main()  {      scanf("%d%d", &n, &m);      for (int i = 0; i < n; i++) scanf("%s%d", s[i], &c[i]), ans[i] = i;      sort(ans, ans + n, cmp);      for (int i = 0; i < n; )      {          if (!i) { write(i, n / m + n%m); i = n / m + n%m; }          else { write(i, i + n / m); i += n / m; }      }      return 0;  }  


//思路一样,但自己的更为繁琐
1 0
原创粉丝点击