1. 程式人生 > >PAT (Advanced Level) Practice 1109 Group Photo (25 分)

PAT (Advanced Level) Practice 1109 Group Photo (25 分)

模擬,高的且字典序小的排前面,一排排模擬下去即可。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;

const int N=1e4+5;

struct People
{
    char name[12];
    int height;
    bool operator<(const People&p)const
    {
        if(height!=p.height)
return height>p.height; return strcmp(name,p.name)<0; } }p[N]; string mp[15][N]; int main() { int n,k; scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) scanf("%s%d",p[i].name,&p[i].height); sort(p+1,p+n+1); int sum=0; for(int i=1;i<=k;i++
) { int num=n/k; if(i==1) num+=n-num*k; int pre=num/2+1; for(int j=1;j<=num;j++) { mp[i][pre]=p[sum+j].name; if(j&1) pre-=j; else pre+=j; } sum+=num; } for(int i=1;i<=k;i++) { int
num=n/k; if(i==1) num+=n-num*k; for(int j=1;j<=num;j++) printf("%s%c",mp[i][j].c_str()," \n"[j==num]); } return 0; }