POJ 2388(排序)

http://poj.org/problem?id=2388

题意:就N个数的中位数。

思路:用快排就行了。但我没用快排,我自己写了一个堆来做这个题。主要还是因为堆不怎么会,这个拿来练练手。

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int arr[10005],ans,n;
 5 
 6 void inset(int x,int y)    //插入,并排序。
 7 {
 8     int i;
 9     for(i = y; arr[ i / 2 ] > x;i /= 2)
10         arr[ i ] = arr[ i / 2 ];
11     arr[ i ] = x;
12 }
13 
14 int deleteMin(int x )     //弹出顶点,相当于优先队列中的,先top(),然后pop();
15 { 16 int i, child; 17 int Min,last; 18 Min = arr[1],last = arr[ x ]; 19 for(i = 1; i * 2 <= x; i = child ) 20 { 21 child = i * 2; 22 if( child != x && arr[ child+1 ] 23 < arr[ child ] ) 24 child++; 25 if(last > arr[ child ]) 26 arr[ i ] = arr[ child ]; 27 else 28 break; 29 } 30 31 arr[ i ] = last; 32 return Min; 33 } 34 int main() 35 { 36 // freopen("in.txt","r",stdin); 37 int x,i; 38 scanf("%d",&n); 39 arr[0] = -10000000; 40 for(i=1;i<=n;i++) 41 { 42 scanf("%d",&x); 43 inset(x,i); 44 } 45 for(i=1;i<=n/2+1;i++) 46 { 47 ans = deleteMin(n-i+1); 48 } 49 printf("%d\n",ans); 50 }

 

转载于:https://www.cnblogs.com/Tree-dream/p/5720886.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值