快排有问题,出现段错误,后来改用qsort过了
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct pat Element;
struct pat{
char id[15];
int score;
int local;
int localrank;
int totalrank;
} stu[30005];
int compare(struct pat a,struct pat b){
if(a.score == b.score)
return strcmp(a.id,b.id);
else return b.score - a.score;
}
void swamp(Element *a,Element *b){
Element tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
Element median3(Element a[],int begin,int end){
int mid;
mid = (begin + end)/2;
if(compare(a[begin],a[mid])>0)
swamp(&a[begin],&a[mid]);
if(compare(a[begin],a[end])>0)
swamp(&a[begin],&a[end]);
if(compare(a[mid],a[end])>0)
swamp(&a[mid],&a[end]);
swamp(&a[mid],&a[end-1]);
return a[end-1];
}
void Insertsort(Element a[],int n){
int i,j;
Element tmp;
for(i = 1;i<n;i++){
tmp = a[i];
for(j = i;compare(tmp,a[j-1])<0&&j>0;j--)
a[j] = a[j-1];
a[j] = tmp;
}
}
void partition(Element a[],int begin,int end){
int i,j;
Element pivot;
if(end - begin>4){
pivot = median3(a,begin,end);
i = 0;
j = end-1;
while(1){
while(compare(a[++i],pivot)<0);
while(compare(a[--j],pivot)>0);
if(i<j)
swamp(&a[i],&a[j]);
else break;
}
swamp(&a[i],&a[end-1]);
partition(a,begin,i-1);
partition(a,i+1,end);
}
else Insertsort(a + begin,end - begin + 1); //the array start from a + begin
}
void quicksort(Element a[],int n){
partition(a,0,n-1);
}
void merge(struct pat a[],int n,struct pat b[],int m){
int ap,bp,tp;
struct pat *tmp;
tmp = (struct pat *)malloc(sizeof(struct pat)*(m+5));
int i;
for(i = 0;i<m;i++)
tmp[i] = b[i];
ap = bp = tp = 0;
while(ap!=n&&tp!=m){
if(compare(a[ap],tmp[tp])<0)
b[bp++] = a[ap++];
else b[bp++] = tmp[tp++];
}
while(ap!=n){
b[bp++] = a[ap++];
}
while(tp!=m){
b[bp++] = tmp[tp++];
}
free(tmp);
}
int main(){
int n,k,total;
struct pat *tmp;
int i,j;
FILE *fp;
//fp = fopen("a.in","r");
fscanf(stdin,"%d",&n);
for(i = 0,total=0;i<n;i++){
fscanf(stdin,"%d",&k);
tmp = (struct pat*)malloc(sizeof(struct pat)*(k+5));
for(j = 0;j<k;j++){
fscanf(stdin,"%s%d",tmp[j].id,&tmp[j].score);
tmp[j].local = i+1;
}
quicksort(tmp,k);
tmp[0].localrank = 1;
for(j = 1;j<k;j++)
if(tmp[j].score == tmp[j-1].score)
tmp[j].localrank = tmp[j-1].localrank;
else tmp[j].localrank = j+1;
merge(tmp,k,stu,total);
total += k;
free(tmp);
}
printf("%d\n",total);
stu[0].totalrank = 1;
for(i = 1;i<total;i++)
if(stu[i].score == stu[i-1].score)
stu[i].totalrank = stu[i-1].totalrank;
else stu[i].totalrank = i+1;
for(i = 0;i<total;i++)
printf("%s %d %d %d\n",stu[i].id,stu[i].totalrank,stu[i].local,stu[i].localrank);
return 0;
}