【综合】体育比赛高手榜
虽然但是,还是不建议同学们直接抄我的代码_(:з」∠)_,我写博客的主要目的还是提供思路,代码也不一定就是最优解,有写的不清楚的部分或者是有更好的算法都可以留言awa
背景
某地举办冬季体育比赛,参者如云。比赛共设三个科目,每项比赛结果均折算为0200分的成绩,现按总成绩统计前99名予以特别奖励本题为[文件+结构]综合类题,主要考查点:
1.文件(二进制)2.结构3.数组4.排序算法5.输入输出控制
实际上就是结构体的 TOP K 问题,有兴趣的同学可以了解下,不过本文应该会包含大部分解法
要求:
编写程序,从指定的比赛成绩文件(二进制)中读入全部选手的比赛数据得到参赛人数,根据总成绩将前99名选手的信息输出到屏幕上。比赛组委会规定,本次参赛选手不得超过280000名,如果总成绩相同ID号码较小(先报名)的排名靠前,奖金有限,只取前99名。
输入:
输入仅一行,为存储本次体育比赛成绩结果文件的文件名,格式如下:
VX2018A1.VDF\n
输出:
输出包括五行表头,然后为TOP99选手数据,每人占一行,为方便观看报表,每五个选手之间加入分隔行,输出格式见右图。注意需要在表头输出参赛人数。
样例:
输入:
VX2018A1.VDF\n
输出:
+-----------------------------------------------------+
| TOP 99 of 10000 |
+-----+--------+----------+---+-----+-----+-----+-----+
| TOP | ID | Name |Sex| SUM | KM1 | KM2 | KM3 |
+-----+--------+----------+---+-----+-----+-----+-----+
| 1 | 008838 | C.I. | M | 584 | 198 | 197 | 189 |
| 2 | 005939 | C.I.Z. | M | 582 | 184 | 200 | 198 |
| 3 | 005760 | K.R.J. | F | 575 | 192 | 194 | 189 |
| 4 | 004852 | Ralap | M | 573 | 190 | 198 | 185 |
| 5 | 005304 | C.F.M. | M | 573 | 199 | 199 | 175 |
+-----+--------+----------+---+-----+-----+-----+-----+
| 6 | 007379 | F.A.E. | F | 573 | 197 | 192 | 184 |
| 7 | 007671 | R.Q. | M | 572 | 195 | 183 | 194 |
| 8 | 009193 | F.L.U. | F | 572 | 186 | 186 | 200 |
| 9 | 000328 | Darlene | F | 571 | 180 | 200 | 191 |
| 10 | 002585 | V.X.G. | F | 571 | 179 | 200 | 192 |
+-----+--------+----------+---+-----+-----+-----+-----+
| 11 | 000278 | I.M.F. | M | 567 | 193 | 197 | 177 |
| 12 | 009582 | R.D.R. | F | 567 | 171 | 196 | 200 |
| 13 | 009597 | H.X.C. | M | 567 | 173 | 195 | 199 |
| 14 | 009834 | Y.H.J. | F | 565 | 178 | 197 | 190 |
| 15 | 005609 | J.D.E. | M | 564 | 187 | 191 | 186 |
+-----+--------+----------+---+-----+-----+-----+-----+
| 16 | 000015 | Cash | M | 563 | 196 | 186 | 181 |
| 17 | 000996 | O.L.N. | M | 562 | 177 | 197 | 188 |
| 18 | 007516 | F.Y.F. | F | 561 | 199 | 177 | 185 |
| 19 | 008391 | H.B.T. | F | 561 | 200 | 190 | 171 |
| 20 | 002051 | X.N.T. | F | 560 | 187 | 175 | 198 |
+-----+--------+----------+---+-----+-----+-----+-----+
| 21 | 002097 | Z.I.K. | M | 560 | 178 | 190 | 192 |
| 22 | 006276 | Barry | M | 559 | 192 | 190 | 177 |
| 23 | 008622 | Berton | M | 559 | 183 | 183 | 193 |
| 24 | 000654 | C.R.Z. | F | 558 | 186 | 188 | 184 |
| 25 | 004469 | L.R.P. | F | 558 | 182 | 188 | 188 |
+-----+--------+----------+---+-----+-----+-----+-----+
| 26 | 005346 | J.A.O. | M | 557 | 165 | 197 | 195 |
| 27 | 009867 | Byron | M | 557 | 196 | 172 | 189 |
| 28 | 007519 | Z.J.N. | F | 556 | 189 | 181 | 186 |
| 29 | 001504 | R.U.S. | M | 555 | 176 | 199 | 180 |
| 30 | 003461 | Queena | F | 555 | 197 | 169 | 189 |
+-----+--------+----------+---+-----+-----+-----+-----+
| 31 | 005825 | L.G.J. | F | 555 | 158 | 198 | 199 |
| 32 | 000739 | J.A.I. | F | 554 | 176 | 199 | 179 |
| 33 | 003904 | Hermosa | F | 554 | 194 | 185 | 175 |
| 34 | 003964 | E.C.N. | M | 554 | 199 | 163 | 192 |
| 35 | 004818 | V.P.O. | M | 553 | 175 | 193 | 185 |
+-----+--------+----------+---+-----+-----+-----+-----+
| 36 | 008422 | Phil | M | 553 | 180 | 191 | 182 |
| 37 | 003032 | Daphne | F | 552 | 179 | 185 | 188 |
| 38 | 004929 | T.I.W. | M | 552 | 199 | 167 | 186 |
| 39 | 007111 | W.G.D. | M | 552 | 166 | 197 | 189 |
| 40 | 000397 | Penny | F | 551 | 185 | 193 | 173 |
+-----+--------+----------+---+-----+-----+-----+-----+
| 41 | 004350 | G.S.U. | F | 551 | 197 | 174 | 180 |
| 42 | 009129 | Adonis | M | 551 | 182 | 176 | 193 |
| 43 | 000219 | K.B.F. | M | 549 | 183 | 174 | 192 |
| 44 | 002309 | O.Q.J. | M | 549 | 197 | 191 | 161 |
| 45 | 003385 | Thera | F | 549 | 181 | 177 | 191 |
+-----+--------+----------+---+-----+-----+-----+-----+
| 46 | 007020 | Lewis | M | 549 | 178 | 187 | 184 |
| 47 | 008121 | D.D.B. | M | 549 | 186 | 196 | 167 |
| 48 | 009260 | D.S.D. | M | 549 | 174 | 181 | 194 |
| 49 | 000352 | K.P. | M | 548 | 182 | 185 | 181 |
| 50 | 008515 | Rod | M | 548 | 183 | 198 | 167 |
+-----+--------+----------+---+-----+-----+-----+-----+
| 51 | 000188 | Y.Q.I. | F | 546 | 183 | 192 | 171 |
| 52 | 007547 | Griselda | F | 545 | 186 | 163 | 196 |
| 53 | 000647 | Antoine | M | 544 | 200 | 159 | 185 |
| 54 | 003545 | Elliot | M | 544 | 195 | 178 | 171 |
| 55 | 007521 | Atwood | M | 544 | 146 | 199 | 199 |
+-----+--------+----------+---+-----+-----+-----+-----+
| 56 | 009383 | Roberta | F | 544 | 160 | 189 | 195 |
| 57 | 007900 | S.F.E. | F | 543 | 167 | 178 | 198 |
| 58 | 001739 | D.W.O. | M | 541 | 198 | 153 | 190 |
| 59 | 005904 | A.V. | F | 541 | 185 | 179 | 177 |
| 60 | 003876 | Q.R. | F | 540 | 198 | 182 | 160 |
+-----+--------+----------+---+-----+-----+-----+-----+
| 61 | 006043 | S.R.X. | M | 540 | 171 | 170 | 199 |
| 62 | 006758 | C.D.U. | M | 540 | 173 | 193 | 174 |
| 63 | 009749 | G.U.G. | F | 540 | 194 | 187 | 159 |
| 64 | 000225 | U.F.P. | F | 539 | 163 | 196 | 180 |
| 65 | 000411 | P.E. | F | 539 | 167 | 174 | 198 |
+-----+--------+----------+---+-----+-----+-----+-----+
| 66 | 003118 | Y.B.X. | F | 539 | 199 | 155 | 185 |
| 67 | 007927 | Channing | M | 538 | 172 | 172 | 194 |
| 68 | 001380 | Y.M.S. | M | 536 | 150 | 196 | 190 |
| 69 | 002016 | U.F.X. | F | 536 | 184 | 167 | 185 |
| 70 | 002806 | Edwina | F | 536 | 167 | 181 | 188 |
+-----+--------+----------+---+-----+-----+-----+-----+
| 71 | 002858 | Deborah | F | 536 | 181 | 195 | 160 |
| 72 | 004379 | P.T.T. | F | 536 | 181 | 183 | 172 |
| 73 | 009214 | T.Z.S. | M | 536 | 181 | 173 | 182 |
| 74 | 000389 | Griselda | F | 535 | 143 | 193 | 199 |
| 75 | 009823 | A.Z.L. | F | 535 | 187 | 151 | 197 |
+-----+--------+----------+---+-----+-----+-----+-----+
| 76 | 001471 | W.C. | F | 534 | 150 | 193 | 191 |
| 77 | 002592 | E.B.A. | M | 534 | 177 | 198 | 159 |
| 78 | 005191 | X.T.D. | M | 534 | 182 | 195 | 157 |
| 79 | 007961 | Andy | M | 534 | 165 | 195 | 174 |
| 80 | 000460 | I.N.S. | F | 533 | 199 | 162 | 172 |
+-----+--------+----------+---+-----+-----+-----+-----+
| 81 | 002768 | Q.R.W. | F | 533 | 178 | 183 | 172 |
| 82 | 004443 | Aubrey | M | 533 | 154 | 192 | 187 |
| 83 | 004922 | K.G.I. | F | 533 | 199 | 166 | 168 |
| 84 | 005277 | S.Z. | F | 533 | 190 | 163 | 180 |
| 85 | 008761 | O.E.H. | F | 533 | 171 | 166 | 196 |
+-----+--------+----------+---+-----+-----+-----+-----+
| 86 | 004070 | R.M.I. | M | 532 | 184 | 186 | 162 |
| 87 | 007027 | Bishop | M | 532 | 199 | 158 | 175 |
| 88 | 007184 | Ryan | M | 532 | 168 | 186 | 178 |
| 89 | 000022 | M.Z. | M | 531 | 136 | 200 | 195 |
| 90 | 000997 | Frederic | M | 531 | 140 | 199 | 192 |
+-----+--------+----------+---+-----+-----+-----+-----+
| 91 | 002704 | Q.J.V. | F | 531 | 186 | 149 | 196 |
| 92 | 003254 | G.D.F. | F | 531 | 188 | 184 | 159 |
| 93 | 007697 | V.T. | F | 531 | 181 | 185 | 165 |
| 94 | 008233 | Elton | M | 531 | 175 | 196 | 160 |
| 95 | 008750 | A.C. | F | 531 | 194 | 182 | 155 |
+-----+--------+----------+---+-----+-----+-----+-----+
| 96 | 001695 | X.K. | M | 530 | 198 | 166 | 166 |
| 97 | 003148 | Venus | F | 530 | 181 | 195 | 154 |
| 98 | 003158 | Primo | M | 530 | 192 | 163 | 175 |
| 99 | 008085 | Adam | M | 530 | 198 | 142 | 190 |
+-----+--------+----------+---+-----+-----+-----+-----+
常规思路
将全部数据读入后排序,输出前99个
先写读入部分:
const int N = 280005;struct athlete
{unsigned int ID;char name[8],sex;unsigned char km1,km2,km3;int sum;
}temp[N];int main()
{int num = 0,i=1;char pos[20];FILE *file = fopen(gets(pos),"rb");while(!feof(file)){num ++;fread(&temp[num].ID,16,1,file);temp[num].sum = temp[num].km1 + temp[num].km2 + temp[num].km3;}fclose(file);
}
因为采用了结构体,所以在以一字节对齐的情况下直接读入整条数据(一个人)即可
然后写排序部分,这里主要采用 归并排序/快速排序,想看冒泡的可以看乐学之前的题目
struct athlete
{unsigned int ID;char name[8],sex;unsigned char km1,km2,km3;int sum;bool operator < (const athlete &a)const{if(sum != a.sum) return sum>a.sum;else return ID<a.ID;}
}temp[N];void merge_sort(struct athlete q[],int l,int r)//归并排序
{if(l >= r) return;int mid = l + r >> 1;merge_sort(q,l,mid),merge_sort(q,mid + 1,r);int k = 0,i = l,j = mid + 1;while(i <= mid && j <= r){if(q[i] < q[j]) tmp[k++] = q[i++];else tmp[k++] = q[j++];}while(i <= mid) tmp[k++] = q[i++];while(j <= r) tmp[k++] = q[j++];for(i = l,j = 0;i <= r;) q[i++] = tmp[j++];
}void quick_sort(const int &l,const int &r) //快速排序
{ if(l >= r) return;struct athlete x = temp[(l + r) >> 1];int i = l - 1,j = r + 1; while(i < j) { do i++; while(x < temp[i]);do j--; while(temp[j] < x);if(i < j) swap(temp[i],temp[j]);} quick_sort(l,j); quick_sort(j + 1,r);
}
这里对结构体的排序采用的是重载运算符的方式,写成cmp函数也是可以的:
bool cmp(struct athlete a,struct athlete b)
{if(a.sum != b.sum) return a.sum>b.sum;else return a.ID<b.ID;
}
最后输出前99位即可,速度大概可以到达0.135s,如果采用冒泡则在0.159左右
虽然我是用C++写的,但是用C完全没问题
但仅管如此,快排的时间复杂度仍在O(nlogn)水平,还能不能更快?
常规plus
其实我们可以发现,只需要前99名,这样问题就转化为了求第k个数的问题,可以用99次冒泡或者二分查找,时间复杂度为O(n)
这里提供的是快速查找+快速排序的算法,速度可以达到0.023-0.025
当然了,只用快速查找找99次也是可以的,速度差不多
#include <bits/stdc++.h>
using namespace std;
struct athlete {unsigned int ID;char name[8], sex;unsigned char km1, km2, km3;int sum;bool operator < (const athlete& a)const {if (sum != a.sum) return sum < a.sum;else return ID > a.ID;}struct athlete* next;
}temp[280005];void quick_find(const int &l,const int &r,const int &k)
{if(l == r) return;int i = l - 1,j = r + 1;while(i < j){while(temp[l] < temp[++i]);while(temp[--j] < temp[l]);if(i < j) swap(temp[i],temp[j]);}int sl = j - l + 1;if(k <= sl) quick_find(l,j,k);else quick_find(j + 1,r,k - sl);
}
void quick_sort(const int &l,const int &r)
{ if(l >= r) return;struct athlete x = temp[(l + r) >> 1];int i = l - 1,j = r + 1; while(i < j) { do i++; while(x < temp[i]);do j--; while(temp[j] < x);if(i < j) swap(temp[i],temp[j]);} quick_sort(l,j); quick_sort(j + 1,r);
}
int main()
{int num = 0;char pos[20];FILE* file = fopen(gets(pos), "rb");while (!feof(file)){fread(&(temp[++num].ID), 16, 1, file);temp[num].sum = temp[num].km1 + temp[num].km2 + temp[num].km3;}fclose(file);printf("+-----------------------------------------------------+\n| TOP 99 of %-6d |\n+-----+--------+----------+---+-----+-----+-----+-----+\n| TOP | ID | Name |Sex| SUM | KM1 | KM2 | KM3 |\n+-----+--------+----------+---+-----+-----+-----+-----+\n", --num);quick_find(1,num,99);quick_sort(1,99);for (int t = 1; t < 100; t++) {printf("| %2d | %06d | %-8.8s | %c | %3d | %3d | %3d | %3d |\n", t, temp[t].ID, temp[t].name, temp[t].sex, temp[t].sum, temp[t].km1, temp[t].km2, temp[t].km3);if (t % 5 == 0 || t == 99) printf("+-----+--------+----------+---+-----+-----+-----+-----+\n");}return 0;
}
淘汰法
但是这样需要太多内存了,就没有别的方法吗?
我们可以将每次读入的数据排序,直到数据量达到99个,之后读入的数据与前99个数据进行比较,如果小于第99个直接丢弃,否则插入对应位置并删去已存储的做后一个元素
可能已经有同学想到了,是的对于这样频繁的插入删除操作,我们可以使用链表实现
#include <bits/stdc++.h>
using namespace std;
struct athlete{unsigned int ID;char name[8],sex;unsigned char km1,km2,km3;int sum;bool operator < (const athlete &a)const{if(sum != a.sum) return sum<a.sum;else return ID>a.ID;}struct athlete * next;
};struct athlete * head = (struct athlete *)malloc(sizeof(struct athlete));struct athlete* New()
{struct athlete * t = (struct athlete *)malloc(sizeof(struct athlete));t->next = NULL;return t;
}int len(struct athlete *p)
{int i = 0;struct athlete *q = p->next;while(q != NULL){i++;q = q->next;}return i;
}void pop(struct athlete *p)
{struct athlete *a,*b;a = p;b = a->next;while(b->next){a = a->next;b = a->next;}a->next = NULL;free(b);
}void insert(struct athlete *h,struct athlete *x)
{struct athlete *p,*q;int l = len(h);p = h;q = h->next;if(l < 1){h->next = x;return;}while(q && *x < *q){q = q->next;p = p->next;}x->next = q;p->next = x;
}int main()
{head->next = NULL;int num = 0;char pos[20];FILE *file = fopen(gets(pos),"rb");while(!feof(file)){num ++;struct athlete * temp = New();fread(&(temp->ID),16,1,file);temp->sum = temp->km1 + temp->km2 + temp->km3;insert(head,temp);if(len(head) == 100) pop(head);}fclose(file);printf("+-----------------------------------------------------+\n| TOP 99 of %-6d |\n+-----+--------+----------+---+-----+-----+-----+-----+\n| TOP | ID | Name |Sex| SUM | KM1 | KM2 | KM3 |\n+-----+--------+----------+---+-----+-----+-----+-----+\n", --num);head = head->next;for(int i = 1;i < 100;i++){printf("| %2d | %06d | %-8.8s | %c | %3d | %3d | %3d | %3d |\n", i, head->ID, head->name, head->sex, head->sum, head->km1, head->km2, head->km3);head = head->next;if (i % 5 == 0 || i == 99) printf("+-----+--------+----------+---+-----+-----+-----+-----+\n");}return 0;
}
但是有的同学可能会问了,诶,你这“高级做法”怎么还更慢了呢?
是的没错,因为这段代码的主要目的是“减少内存使用”,事实上,时间复杂度是很高的,因为每次询问链表长度都要遍历一次链表,删除链表尾又要遍历一次,这些都是可以优化的,实测可以到0.12左右
红黑树(淘汰法)
其实就是把数据结构优化了一下
(我这里是直接用了set,胆大的自己敲个红黑树出来也行)
#include <bits/stdc++.h>
#include <set>
using namespace std;
struct athlete
{ unsigned int ID; char name[8],sex; unsigned char km1,km2,km3; int sum; bool operator < (const athlete &a)const{ if(sum != a.sum) return sum>a.sum; else return ID<a.ID; }
}temp;
int main()
{ set<struct athlete> pl; int num = 0,i=1; char pos[20]; FILE *file = fopen(gets(pos),"rb"); while(!feof(file)){ num ++; fread(&temp.ID,16,1,file); temp.sum = temp.km1 + temp.km2 + temp.km3; set<struct athlete>::reverse_iterator it = pl.rbegin(); if(pl.size() == 99 && *it < temp) continue; pl.insert(temp); if(pl.size()==100) pl.erase(--pl.end()); } fclose(file); printf("+-----------------------------------------------------+\n| TOP 99 of %-6d |\n+-----+--------+----------+---+-----+-----+-----+-----+\n| TOP | ID | Name |Sex| SUM | KM1 | KM2 | KM3 |\n+-----+--------+----------+---+-----+-----+-----+-----+\n", --num); for(set<struct athlete>::iterator it = pl.begin(); i<100; it++,i++){ printf("| %2d | %06d | %-8.8s | %c | %3d | %3d | %3d | %3d |\n", i, (*it).ID, (*it).name, (*it).sex, (*it).sum, (*it).km1, (*it).km2, (*it).km3); if (i % 5 == 0 || i == 99) printf("+-----+--------+----------+---+-----+-----+-----+-----+\n"); } return 0;
}
这样写的速度可以到达极限0.020-0.023,而且内存小
分治法
将数据每2000个为一组,每组找出前99个,再在剩下的数里找出前99个,这里就不提供代码了,可以通过快排或二分查找实现
2022.11.13 更新
今天突然想想到,可以采用桶排序,先按总分分桶,再在桶内按ID排序即可,速度能达到0.016
#include <stdio.h>
struct athlete {unsigned int ID;char name[8], sex;unsigned char km1, km2, km3;int sum;
}temp[150][25],tt;
int s[150] = {0};
void quick_sort(const int score,const int l,const int r)
{ if(l >= r) return;int x = temp[score][l].ID;int i = l - 1,j = r + 1;while(i < j) {while(x > temp[score][++i].ID);while(temp[score][--j].ID > x);if(i < j){tt = temp[score][i];temp[score][i] = temp[score][j];temp[score][j] = tt;}}quick_sort(score,l,j); quick_sort(score,j + 1,r);
}
int main()
{int num = 0;char pos[20];FILE* file = fopen(gets(pos), "rb");while (!feof(file)) {num ++;fread(&(tt.ID), 16, 1, file);tt.sum = tt.km1 + tt.km2 + tt.km3;if(tt.sum > 450) temp[tt.sum - 450][s[tt.sum - 450]++] = tt;}printf("+-----------------------------------------------------+\n| TOP 99 of %-6d |\n+-----+--------+----------+---+-----+-----+-----+-----+\n| TOP | ID | Name |Sex| SUM | KM1 | KM2 | KM3 |\n+-----+--------+----------+---+-----+-----+-----+-----+\n", --num);for(int t = 150, n = 0; t >= 0 && n < 99; t --){if(!s[t]) continue;quick_sort(t,0,s[t] - 1);for(int u = 0;u < s[t] && n < 99;u ++){printf("| %2d | %06d | %-8.8s | %c | %3d | %3d | %3d | %3d |\n", ++n, temp[t][u].ID, temp[t][u].name, temp[t][u].sex, temp[t][u].sum, temp[t][u].km1, temp[t][u].km2, temp[t][u].km3);if (n % 5 == 0) printf("+-----+--------+----------+---+-----+-----+-----+-----+\n");}}printf("+-----+--------+----------+---+-----+-----+-----+-----+\n");return 0;
}
11.29更新
在尝试了116次,成功让自己的罚分翻倍之后,我的用时终于来到了0.008s,同学们也可以尝试一下, 算法就在这篇博客中,只需要简单的修改就可以。