pat乙级1055 集体照 (25 分) 结构体 模拟 二分

题目传送门
这道题 最好先从测试用例分析,我反正测试过了交了一次就ac了。
可以先写测试用例的第一行,然后再扩展到整题。
沉下心来写,注意小细节,多用printf输出检查

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
using namespace std;
struct node{
    int ht;
    string name;
}a[10010];
bool cmp(node a ,node b){
    if(a.ht!=b.ht)
        return a.ht>b.ht;
    else
        return a.name<b.name;
}
int main()
{
    int N,K,i,j;
    node b[10010];
    while(~scanf("%d %d",&N,&K)){
        for(i=0;i<N;i++){
            cin>>a[i].name>>a[i].ht;
        }
        sort(a,a+N,cmp);
        int m,h,j=0,t1,t2;
        for(h=0;h<K;h++){
            if(h==0){
                m=N/K+N%K;
                t1=0;
                t2=m;
            }
            else{
                m=N/K;
                t1=N/K+N%K+(h-1)*N/K;
                t2=N/K+N%K+h*N/K;
            }
            j=t1;
            b[m/2+j]=a[j];
            int you=m/2-1+t1,zuo=m/2+1+t1;
            while(j<=(t2-1)){
                j++;
                if(you>=t1){
                    b[you].name=a[j].name;
                    b[you].ht=a[j].ht;
                    you--;
                }
                if(j==t2)break;
                j++;
                if(zuo<t2){
                    b[zuo].name=a[j].name;
                    b[zuo].ht=a[j].ht;
                    zuo++;
                }
            }
            for(i=t1;i<t2;i++){
                if(i!=t1)
                    cout<<" ";
                cout<<b[i].name;
                }
            printf("\n");
        }
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值