POJ-2388 Who's in the Middle
来源:互联网 发布:淘宝直通车在哪里找图 编辑:程序博客网 时间:2024/04/30 06:16
题目大意:求一个数组的中位数
题目链接:http://poj.org/problem?id=2388
数据量水,直接sort也能过。或者用算法导论第9章的方法,类似快排方法来找。
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <stdlib.h>#include <time.h>#define MAXN 10001int A[MAXN];void exchange(int& x, int& y){ int tmp = x; x = y; y = tmp;}int r_partition(int p, int r){ int i = rand() % (r - p + 1) + p; exchange(A[r], A[i]); int x = A[r]; i = p - 1; for (int j = p; j < r; j++) { if (A[j] <= x) { i++; exchange(A[i], A[j]); } } exchange(A[i + 1], A[r]); return i + 1;}int r_select(int p, int r, int i){ if (p == r) return A[p]; int q = r_partition(p, r); int k = q - p + 1; if (i == k) return A[q]; else if (i < k) return r_select(p, q - 1, i); else return r_select(q + 1, r, i - k);}int main(){ int n; srand((unsigned)time(NULL)); while (~scanf("%d", &n)) { for (int i = 0; i < n; i++) { scanf("%d", &A[i]); } printf("%d\n", r_select(0, n - 1, (n + 1) / 2)); } 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
- Kernel启动流程源码解析 1 head.S
- dsquery心得
- 如何在interlliJ IDEA上配置tomcat
- Linux常用命令:nl命令
- 如何快速成为APP制作、H5制作高手?
- POJ-2388 Who's in the Middle
- weblogic 与jms事例配置
- ExtJs6 checkboxgroup组件根据数据库数据默认选中设置方法
- early_printk使用方法
- 写在前面
- 析构函数virtual与非virtual的区别
- [ZooKeeper]纠正官网的Queue示例
- IntelliJ IDEA15如何配置tomcat
- Linux常用命令:more命令