1022 Poor contestant Prob

TAG 插入排序  二分法

 

我是用插入排序做的,在插入的时候使用二分法查找位置。用数组idx记录下标,只移动索引,提高速度。

/* source code of submission 426993, Zhongshan University Online Judge System */ #include <stdio.h> const int N=100000; struct node { char name[11]; int pro; }; int idx[N]; node stu[N]; int num,t; bool flag,first; char commamd[10]; int bin_search(int s, int t, int x) { int mid; int ret=t; while ( s<=t ) { mid=(s+t)/2; if ( stu[ idx[mid] ].pro > x ) { t=mid-1; } else { ret=mid; s=mid+1; } } return ret; } void add() { ++num; scanf("%s%d", stu[num].name, &stu[num].pro); idx[num]=num; if ( num>0 ) { int p=bin_search(0,num-1,stu[num].pro); for (int i=num-1; i>p; --i) { idx[i+1]=idx[i]; } idx[p+1]=num; } } int main(int argc, char *argv[]) { scanf("%d", &t); first=true; while ( t-- ) { if ( !first ) { printf("/n"); } else { first=false; } flag=true; num=-1; while ( flag ) { scanf("%s",commamd); switch( commamd[0] ) { case 'A':add(); break; case 'Q': if (num%2==0) { printf("%s/n", stu[ idx[num/2] ].name); } else { printf("No one!/n"); } break; case 'E': flag=false; if (num%2==0) { printf("%s is so poor./n", stu[ idx[num/2] ].name); } else { printf("Happy BG meeting!!/n"); } break; } } } return 0; }

你可能感兴趣的:(1022 Poor contestant Prob)