POJ 2388-Who's in the Middle
来源:互联网 发布:单片机课程 编辑:程序博客网 时间:2024/05/16 05:21
- 中位数,参考算法导论
#include<cstdio>#include<cstdlib>using namespace std;#define maxN 10001int milk[maxN];int swap(int &x,int &y){ x ^= y; y ^= x; x ^= y; return 1;}short partition(short start,short dest){ short i; i = rand()%(dest-start)+start; swap(milk[i],milk[dest]); int tmpMed = milk[dest]; short medPos = start-1; //下标小于等于medPos的都小于等于tmpMed for(i = start;i <= dest-1;i++) { if(milk[i] <= tmpMed) { medPos++; if(medPos != i) swap(milk[i],milk[medPos]); } } medPos++; if(medPos != dest) swap(milk[dest],milk[medPos]); return medPos;}int select(short start,short dest,short dest_mid){ if(start == dest) return milk[start]; short med = partition(start,dest); short k = med-start+1; if(k == dest_mid) return milk[med]; else if(dest_mid < k) return select(start,med-1,dest_mid); else return select(med+1,dest,dest_mid-k);}int main(){ short i,N; while(scanf("%hd",&N)!=EOF) { for(i = 0;i < N;i++) scanf("%d",milk+i); printf("%d\n",select(0,N-1,N/2+1)); } return 0;}
0 0
- POJ--2388 Who's in the Middle
- poj 2388 Who's in the Middle
- POJ-2388 who's in the middle
- poj-2388 Who's in the Middle
- poj 2388 Who's in the Middle
- POJ 2388 Who's in the Middle
- poj 2388 Who's in the Middle
- POJ 2388 Who's in the Middle
- Poj 2388 Who's in the Middle
- POJ 2388 Who's in the Middle
- POJ 2388 Who's in the Middle
- poj 2388 Who's in the Middle
- POJ-2388-Who's in the Middle
- poj 2388 Who's in the Middle
- POJ 2388 Who's in the Middle
- poj 2388 Who's in the Middle
- Who's in the Middle(poj 2388)
- POJ 2388:Who's in the Middle
- Java带有双重校验锁的单例模式
- uva 531 - Compromise
- louis vuitton 2004 Suburban Service Air Bag light came on and
- res资源命名规则
- nike mercurial Bank of England rates policy to be axed
- POJ 2388-Who's in the Middle
- 写一套代码跨越IOS和Android两个平台
- oakley outlet Billingham Hadley Pro Review
- Let Me Count The Ways - UVa 357 dp
- 寒城攻略:Listo 教你用Swift 语言编写 IOS 平台流媒体播放器
- Spring Data JPA动态查询 maven项目简单例子
- Apache DbUtils 源码分析 | Apache DbUtils Source Code Analysis
- 【模拟+hash+并查集】2014 Multi-University Training Contest 2|HDU_4879 ZCC loves march
- Android 线程优先级设置方法