hiho 37 二分查找k小数
2016-04-19 22:31
302 查看
问题
http://hihocoder.com/contest/hiho37/problem/1解法
二分求解。复杂度nlogn#include <bits/stdc++.h> using namespace std; int *a; int partition(int L, int R) { int pos = L; R++; for(;;){ do{++L;} while(a[L] < a[pos] && L < R); do{--R;} while(a[R] > a[pos] && R >= L); if (L>=R) break; swap(a[L], a[R]); } swap(a[pos], a[R]); return R; } int main() { int n, k; scanf("%d %d", &n, &k); a = new int ; for (int i=0; i<n; ++i) scanf("%d", a+i); int L =0; int R = n-1; while(L<=R) { int m = partition(L, R); if (m ==k-1){ printf("%d\n", a[m]); return 0; }else if (m < k-1){ L = m+1; }else R= m-1; } printf("-1\n"); return 0; }
相关文章推荐
- 算法技巧
- 前言
- @ResponseBody的配置
- 在浏览器中输入Google.com并且按下回车之后发生了什么?
- shell脚本
- iOS网络数据解析
- 在myeclipse中BASE64 与md5加密引包错误问题解决方法 --
- ASP.NET MVC 数据库依赖缓存的实现
- PLC练习一
- centos7下yum安装mysql
- Android下的图片压缩及图片和视频的上传
- oracle 启动与关闭
- Android 自定义控件onTouch事件浅析和个人总结
- LIS LCS LCIS (主要过一遍,重在做题)
- EM算法
- linux常用命令
- 电子相册系统介绍
- 第一阶段冲刺第一天
- linux的查询时间实现秒定时+gettimeofday()函数的用法
- 为什么说DOM操作很慢