A Za, A Za, Fighting...

坚信:勤能补拙

PKU 2388 Who's in the Middle

问题:
http://acm.pku.edu.cn/JudgeOnline/problem?id=2388

思路:
1. 排序
这题直接用快排就能AC,很水...

2. 求解第i个顺序统计量(详见CLRS)
   利用快速排序过程中的思路,期望时间复杂度是O(n)

   ps: partition函数的执行过程是比较有意思的呵呵
 1 int 
 2 partition(long *arr, int begin, int end)
 3 {
 4     int i, j;
 5     i = begin;
 6     long pivot = arr[begin];
 7     for(j=begin+1; j<=end; j++)
 8         if(arr[j] <= pivot)
 9             swap(arr, ++i, j);
10     swap(arr, i, begin);
11     return i;
12 }
13 
14 long
15 find_ith_os(long *arr, int begin, int end, int ith)
16 {
17     if(begin == end)
18         return arr[begin];
19     int p = partition(arr, begin, end);
20     int k = p-begin+1;
21     if(k == ith)
22         return arr[p];
23     else if(ith < k)
24         return find_ith_os(arr, begin, p-1, ith);
25     else
26         return find_ith_os(arr, p+1, end, ith-k);
27 }




posted on 2010-07-04 10:19 simplyzhao 阅读(219) 评论(0)  编辑 收藏 引用 所属分类: A_排序


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2010年7月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜