
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
struct node{
char name[10];
int high;
}stu[10010],temp[10010];
bool cmp(node a,node b){
if(a.high!=b.high) return a.high>b.high;
else return strcmp(a.name,b.name)<0;
}
int main(){
int n,k;
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++){
scanf("%s%d",&stu[i].name,&stu[i].high);
}
int a,b;
a=n/k;
b=a+n%k;
sort(stu,stu+n,cmp);
int c=b/2;
int e=c,f=c;
for(int i=0;i<b;i++){
if(i==0) temp[c]=stu[i];
else if(i%2==1) temp[--e]=stu[i];
else if(i%2==0) temp[++f]=stu[i];
}
for(int i=0;i<b;i++){
printf("%s",temp[i].name);
if(i!=b-1)printf(" ");
else printf("\n");
}
for(int i=0;i<k-1;i++){
int g=a/2;
int x=g,y=g;
for(int j=0;j<a;j++){
if(j==0) temp[g]=stu[b++];
else if(j%2==1) temp[--x]=stu[b++];
else if(j%2==0) temp[++y]=stu[b++];
}
for(int l=0;l<a;l++){
printf("%s",temp[l].name);
if(l!=a-1)printf(" ");
else printf("\n");
}
}
return 0;
}
第一次,独立完成,30min
本文分享了一段使用C++实现的复杂排序算法代码,该算法应用于编程竞赛中,通过自定义比较函数对结构体数组进行排序,并采用特殊的数据处理方式以满足比赛要求。
177

被折叠的 条评论
为什么被折叠?



