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 (≤10​4​​), 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.

思路:

先排序

John Tim Amy, Eva Ann Mike, Bob Nick Tom Joe

man_in_row = n/k; 以每行为单位进行排列,从个子矮的行开始排

注意* 最初排序的方式应该是身高升序,名降序:这样才能保证在每行中进行排列的时候,先排的是个子高的,身高相同时是名字典序小的

  * 对每行进行排列,对每行的循环应该这样写 for(; ptr+man_in_row*2<=n; ptr+=man_in_row)!!

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <string>
  5. #include <map>
  6. using namespace std;
  7. const int maxn = ;
  8. struct mantype{
  9. string name;
  10. int height;
  11. };
  12. struct mantype manList[];
  13. bool cmp(struct mantype m1,struct mantype m2){
  14. if(m1.height==m2.height){
  15. return m1.name>m2.name;
  16. }
  17. return m1.height<m2.height;
  18. }
  19. int main(){
  20. int n,k; scanf("%d %d",&n,&k);
  21. string tmps; int height;
  22. for(int i=;i<n;i++){
  23. cin >> manList[i].name >> manList[i].height; // !!
  24. }
  25. sort(manList, manList+n, cmp);
  26. /*for(int i=0;i<n;i++){
  27. cout << manList[i].name << " ";
  28. }*/
  29. string matrix[][];
  30. int man_in_row = n/k;
  31. int ptr = ; int lv=;
  32. int t,i;
  33. for(; ptr+man_in_row*<=n; ptr+=man_in_row){ // !!
  34. int mid = man_in_row/+;
  35. matrix[lv][mid] = manList[ptr+man_in_row-].name;
  36. t=;
  37. for(i=ptr+man_in_row-;i>=ptr;i-=){
  38. matrix[lv][mid-t] = manList[i].name;
  39. if(i->=ptr) matrix[lv][mid+t] = manList[i-].name;
  40. t++;
  41. }
  42. lv++;
  43. }
  44. int remain = (n-ptr);
  45. int mid = remain/+;
  46. matrix[lv][mid] = manList[n-].name;
  47. t=;
  48. for(i=n-;i>=ptr;i-=){
  49. matrix[lv][mid-t] = manList[i].name;
  50. if(i->=ptr) matrix[lv][mid+t] = manList[i-].name;
  51. t++;
  52. }
  53. //printf("\n------\n");
  54. for(int j=;j<=remain;j++){
  55. if(j!=) printf(" ");
  56. cout<<matrix[lv][j];
  57. }
  58. printf("\n");
  59. for(int i=lv-;i>=;i--){
  60. for(int j=;j<=man_in_row;j++){
  61. if(j!=) printf(" ");
  62. cout<<matrix[i][j];
  63. }
  64. printf("\n");
  65. }
  66. return ;
  67. }

Sample Input:

  1. 10 3
  2. Tom 188
  3. Mike 170
  4. Eva 168
  5. Tim 160
  6. Joe 190
  7. Ann 168
  8. Bob 175
  9. Nick 186
  10. Amy 160
  11. John 159

Sample Output:

  1. Bob Tom Joe Nick
  2. Ann Mike Eva
  3. Tim Amy John

1109 Group Photo的更多相关文章

  1. 1109 Group Photo (25 分)

    1109 Group Photo (25 分) Formation is very important when taking a group photo. Given the rules of fo ...

  2. PAT 1109 Group Photo[仿真][难]

    1109 Group Photo(25 分) Formation is very important when taking a group photo. Given the rules of for ...

  3. 1109. Group Photo (25)

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  4. PAT 1109 Group Photo

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  5. 1109 Group Photo (25分)

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  6. PAT (Advanced Level) 1109. Group Photo (25)

    简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  7. PAT甲题题解-1109. Group Photo (25)-(模拟拍照排队)

    题意:n个人,要拍成k行排队,每行 n/k人,多余的都在最后一排. 从第一排到最后一排个子是逐渐增高的,即后一排最低的个子要>=前一排的所有人 每排排列规则如下: 1.中间m/2+1为该排最高: ...

  8. 【PAT甲级】1109 Group Photo (25分)(模拟)

    题意: 输入两个整数N和K(N<=1e4,K<=10),分别表示人数和行数,接着输入N行每行包括学生的姓名(八位无空格字母且唯一)和身高([30,300]的整数).按照身高逆序,姓名字典序 ...

  9. A1109. Group Photo

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

随机推荐

  1. stark组件开发之提取公共视图函数

     路由问题, 已经解决! 然后就是视图函数的问题了: 不想重复写的解决途径就是, python  类的继承了! 写一个基类, 基类定义 增删改查. 然后其他的,全部去继承他! from django. ...

  2. Maven 系列 二 :Maven 常用命令,手动创建第一个 Maven 项目

    1.根据 Maven 的约定,我们在D盘根目录手动创建如下目录及文件结构: 2.打开 pom.xml 文件,添加如下内容: 1 <project xmlns="http://maven ...

  3. 关于ip包长度

    http://blog.csdn.net/naturebe/article/details/6712153 这篇文章总结的不错,转自:http://hi.baidu.com/to_wait/blog/ ...

  4. POJ1966 Cable TV Network

    原题链接 割去点使得无向图不连通,和最小割相似. 我们可以将点转化成边,这样就能跑最小割了. 枚举每两个不能直接到达的点\(S,T\),使得删去一些点(除去这两个点)使得这两个点不连通(若两点能直接到 ...

  5. 08. pt-find

    vim pt-find.cnf host=192.168.100.101port=3306user=adminpassword=admin pt-find --config pt-find.cnf d ...

  6. Java Http协议处理类

    public class HttpRequest { public static String doGet(String url,String params) throws Exception{ re ...

  7. memcache的add和set区别

    add可以做memcache锁 使用场景:用户兑换商品,在网络不好的情况下,点击多次,set会将多次提交全纪录下来,add只会记录一次

  8. JSP概述、API、注释

    JSP自带的API包含4个包,可通过Tomcat的官网查看,JSP和EL的API是分开的 javax.servlet.jsp // 包含用于Servlet/JSP容器将JSP页面翻译为Servlet的 ...

  9. Arithmetic Slices LT413

    A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...

  10. js实现环形菜单效果

    点击中间的圆点,会弹出环形菜单,效果图: 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF- ...