他来了他来了!目前做过最难的一题,程序架构很好写就是基本的结构体,但是用例push的也太狠了。debug了一晚上没做出来转天早上翘了节线代终于写完了,分享几个错误处理。
- 段错误
这题N<=10^5就明确了这不可能用局部变量,否则直接堆栈溢出了(PAT会显示段错误,也就是咱们常见的sigmentation fault )像我这样没有怎么处理过大数据的新手看到这个错误会往自己数组下标越界了的方向去想,对着程序撸了半天下标最后看别人的题解才意识到这是数据量太大导致堆栈溢出qwq)
- 超时
在改完段错误我以为万事大吉,结果爆两个超时。。。我开始用的交换排序,看到超时改成了选择排序不过选择排序好像更慢了-_-||最后看别人题解知道必须用快排,现学快排做了果然过了,快排真是比选择和交换快不少!CSDN上几篇讲快排的文章写的很好,不太明白快排的兄弟可以去看看
这题给我一个很好的发现问题解决问题的过程,令我不禁感叹PAT的用例设计的真是巧妙!今年内一定要刷完乙级!
AC代码如下(注释掉的是之前写的选择排序)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct stu STU;
struct stu{
char id[10];
int DE;
int CAI;
};
STU bothDC[100010];
STU highD[100010];
STU DmoreC[100010];
STU CmoreD[100010];
STU grp[100010];
//void downsort(STU *grp, int len){
//
// for(int i = 0; i + 1 < len; i++){
// int flag = i;
// for(int j = i + 1; j < len; j++){
// int sumf = grp[flag].DE + grp[flag].CAI;
// int sumj = grp[j].DE + grp[j].CAI;
// if(sumf < sumj){
// flag = j;
// }else if(sumf == sumj){
// if(grp[flag].DE < grp[j].DE){
// flag = j;
// }else if(grp[flag].DE == grp[j].DE){
// if(strcmp(grp[flag].id, grp[j].id) > 0){
// flag = j;
// }
// }
// }
// if(flag != i){
// STU tmp = grp[flag];
// grp[flag] = grp[i];
// grp[i] = tmp;
// }
// }
// }
//}
int DCcmp(STU* a, STU* b){//a < b 为真
int suma = (*a).DE + (*a).CAI;
int sumb = (*b).DE + (*b).CAI;
int flag = 0;
if(suma < sumb){
flag = 1;
}else if(suma == sumb){
if((*a).DE < (*b).DE){
flag = 1;
}else if((*a).DE == (*b).DE){
if(strcmp((*a).id, (*b).id) > 0){
flag = 1;
}
}
}
return flag;
}
void myqsort(STU *grp, int idxf, int idxb){
if(idxf < idxb){
int tf, tb;
tf = idxf;
tb = idxb;
STU temp = grp[idxf];
while(tf < tb){
while(tf < tb && DCcmp(&grp[tb], &temp)){
tb--;
}
if(tf < tb){
grp[tf] = grp[tb];
}
while(tf < tb && DCcmp(&grp[tf], &temp) == 0){
tf++;
}
if(tf < tb){
grp[tb] = grp[tf];
}
}
grp[tf] = temp;
myqsort(grp, idxf, tf - 1);
myqsort(grp, tf + 1, idxb);
}
}
void grp_print(STU *grp, int len){
for(int i = 0; i < len; i++){
printf("%s %d %d\n", grp[i].id, grp[i].DE, grp[i].CAI);
}
}
int main()
{
int N, L, H;
scanf("%d %d %d", &N, &L, &H);
int ctgrp = 0;
int ctb, cth, ctD, ctC;
ctb = cth = ctD = ctC = 0;
for(int i = 0; i < N; i++){
scanf("%s %d %d", grp[ctgrp].id, &grp[ctgrp].DE, &grp[ctgrp].CAI);
if(grp[ctgrp].DE >= L && grp[ctgrp].CAI >= L){
if(grp[ctgrp].DE >= H && grp[ctgrp].CAI >= H){
bothDC[ctb] = grp[ctgrp];
ctb++;
}else if(grp[ctgrp].DE >= H){
highD[cth] = grp[ctgrp];
cth++;
}else if(grp[ctgrp].DE >= grp[ctgrp].CAI){
DmoreC[ctD] = grp[ctgrp];
ctD++;
}else{
CmoreD[ctC] = grp[ctgrp];
ctC++;
}
ctgrp++;
}
}
int M = ctgrp;
printf("%d\n", M);
// downsort(bothDC, ctb);
// downsort(highD, cth);
// downsort(DmoreC, ctD);
// downsort(CmoreD, ctC);
// downsort(bothDC, ctb);
myqsort(bothDC, 0, ctb - 1);
myqsort(highD, 0, cth- 1);
myqsort(DmoreC, 0, ctD - 1);
myqsort(CmoreD, 0, ctC - 1);
grp_print(bothDC, ctb);
grp_print(highD, cth);
grp_print(DmoreC, ctD);
grp_print(CmoreD, ctC);
return 0;
}