PAT 1109 Group Photo

个人学习记录,代码难免不尽人意。
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.

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

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
struct node{
    string name;
    int height;
}Node[10010];
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 n,k;
    scanf("%d %d",&n,&k);
    for(int i=0;i<n;i++){
        string str;
        int height;
        cin>>str>>height;
        node a;a.name=str;a.height=height;
        Node[i]=a;
    }
    sort(Node,Node+n,cmp);
//    for(int i=0;i<n;i++)
//     cout << Node[i].name << endl;
    int a=n/k; 
    node newgroup[n];
    for(int i=0;i<k;i++){
        int begin,end;
        if(i!=k-1){
        begin=i*a;
            end=(i+1)*a-1;
        }else{
            begin=i*a,end=n-1;
            a=n-begin;
        }
            int mid=begin+a/2;
            newgroup[mid]=Node[end];
            bool flag=true;
            int count=1;

            for(int j=end-1;j>=begin;j--){
                
                if(mid-count>=begin&&flag){
                    newgroup[mid-count]=Node[j];
                    flag=false;
                }
                else if(mid+count<=end&&!flag){
                    newgroup[mid+count]=Node[j];
                    flag=true;
                    count++;
                }
                else if(mid+count>end){
                    newgroup[mid-count]=Node[j];
                    count++;
                }
                else if(mid-count<begin){
                    newgroup[mid+count]=Node[j];
                    count++;
                }
            }
    }
    a=n/k;
    int extra=n%k;
//    cout << "df";
//    for(int i=0;i<n;i++){
//    	cout << newgroup[i].name << endl;
//	}cout << "df";

    for(int i=n-a-extra;i<n;i++){
        cout << newgroup[i].name;
        if(i==n-1) cout << endl;
        else cout << " ";
    }

     
    for(int i=n-a-extra-1;i>0;i-=a){
    	for(int j=i-a+1;j<=i;j++){
    		cout << newgroup[j].name;
		if(j==i) cout << endl;
        else cout << " ";
		}
        
    }
}

说实话这个题题干的这个round down to the nearest integer我一开始不是很懂什么意思,后来看了好几遍才根据down这个词发现是向下取整的意思,和正常的除法一样。
然后这道题我也想不出什么比较好的做法,就打算暴力来做,考虑到暴力可能会超时,但是可以节约思考时间来做下一道题,拿到部分分数也是不错的选择。
我的想法就是将队列分为两种情况,一种是前k-1排,一种是最后1排(当然这种情况也包含了只有一排),然后用了一个小技巧,题目要求我们在最高的左右反复横跳插入,我们可以设置一个bool变量来反复赋值true和false达到循环反复横跳的效果。
还有一点,如果像我这样做的话一开始的数组排序函数cmp应该将相同身高的名字小的放在后面,因为题目要求的是最后的输出的结果名字小的在前面,因此我们遍历的时候应该每次取较小的名字。
PS:后来我发现题目说的they must be ordered in alphabetical (increasing) order of their names应该是按照字母表顺序插入,而不是展示的意思,这一点当时我也很迷惑。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值