/*************************
题意:
根据身高 去排方阵
要排成K行(不是每行K个。。)
如果人数不够整除, 则多余的人去第一行。
第i行的人必须比第i+1行的人要高或相等(整行都>= 的意思,不需要对应列)
且插入顺序是
从中间
插左边
再插右边
再插左边
这样去插满一行
************************/
/***********************
解题思路:
先建立树
先排序,然后取出一行的人
再按照规则去插。
难点就在于计算每行数量,和怎么去插入,弄清题意
*************************/
/***********************
*********************/
#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<queue>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<stack>
#include<map>
#include<set>
#include<unordered_map>
using namespace std;
#define M 10005
#define INF 0x7ffffff
struct People{
string name;
int high;
};
bool cmp(People a, People b){
if(a.high > b.high){
return true;
}
else if(a.high == b.high)
return a.name < b.name;
else return false;
}
vector<People> v;
People num[M];
int main(){
int n, i, k, j;
string name;
int high;
People p;
cin>>n>>k;
for(i = 0;i<n;i++){
cin>>p.name>>p.high;
v.push_back(p);
}
sort(v.begin(), v.end(), cmp);
int firstk = n%k + n/k;
int mid, f, place, d;
i = 0;
place = firstk/2;
f = -1;
d = 1;
while(place >= 0 && place < firstk){
num[place] = v[i];
place += f*d;
f *= -1;
d++;
i++;
}
for(j = 0;j< firstk;j++){
if(j != firstk-1)
printf("%s ",num[j].name.c_str());
else printf("%s\n",num[j].name.c_str());
}
int m = k - 1;
k = n/k;
while(m--){
place = k/2;
f = -1;
d = 1;
while(place >= 0 && place < k){
num[place] = v[i];
place += f*d;
f *= -1;
d++;
i++;
}
for(j = 0;j< k;j++){
if(j != k-1)
printf("%s ",num[j].name.c_str());
else printf("%s\n",num[j].name.c_str());
}
}
return 0;
}
PAT 1109. Group Photo (25) 整队问题
最新推荐文章于 2020-02-02 16:39:36 发布