思路:固定一个数,把这个数放到合法的位置,然后左边的数都是比它小,右边的数都是比它大

固定权值选的是第一个数,或者一个随机数

因为固定的是左端点,所以一开始需要在右端点开始,找一个小于权值的数,从左端点开始,找一个大于权值的数

那么交换他们即可、最后的话,One == two那个位置就是权值应该去到的位置,这个时候把原问题划分为更小的子问题

就是[be, one - 1]和[one + 1, en]这两个子问题。

下面的是有bug的,当rand去到en的时候,就会wa  (修复了)

比如数据是,4、5、1、2就很容易wa

用随机做了一个TLE了,还没wa就TLE了

http://codeforces.com/contest/732/problem/D

ID = be也是TLE,不明白STL的sort是怎么来的

int myRand(int be, int en) {return be + (rand() % (en - be + 1));
}void quickSort(int a[], int be, int en) {if (be >= en) return ;int id = myRand(be, en); // 这样不行,需要id = beint one = be, two = en;while (one != two) {while (two > one && a[two] >= a[id]) --two; // 找第一个比id小的, 必须先找小的while (one < two && a[one] <= a[id]) ++one; // 找第一个比id大的, 因为基准数是beif (one < two) swap(a[one], a[two]);//固定id = be,需要从右到左是因为,如果是从左到右,例子1、2、3、4、5//找到第一个比1大的,是2,然后找不到第一个比1小,在2中相遇//然后swap(a[1], a[2])  GG
    }swap(a[id], a[one]);quickSort(a, be, one - 1);quickSort(a, one + 1, en);
}

View Code

修复:

随机一个id,然后和第一那个数交换,就可以了。和id = be一样了

int myRand(int be, int en) {return be + (rand() % (en - be + 1));
}void quickSort(int a[], int be, int en) {if (be >= en) return ;int id = myRand(be, en); //
    swap(a[be], a[id]);id = be;int one = be, two = en;while (one != two) {while (two > one && a[two] >= a[id]) --two; // 找第一个比id小的, 必须先找小的while (one < two && a[one] <= a[id]) ++one; // 找第一个比id大的, 因为基准数是beif (one < two) swap(a[one], a[two]);//固定id = be,需要从右到左是因为,如果是从左到右,例子1、2、3、4、5//找到第一个比1大的,是2,然后找不到第一个比1小,在2中相遇//然后swap(a[1], a[2])  GG
    }swap(a[id], a[one]);quickSort(a, be, one - 1);quickSort(a, one + 1, en);
}

View Code

随机生成区间里的一个数字思路:

区间大小是len,那么就生成一个0---len-1的数,加上起始点,就是落在[be, en]的数

代码在上面。

O(n)找第k小的数

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
const int maxn = 1e5 + 20;
int myRand(int be, int en) {return be + (rand() % (en - be + 1));
}
int findKthMin(int a[], int be, int en, int k) {if (be == en) return a[be];swap(a[be], a[myRand(be, en)]);int one = be, two = en, id = be;while (one != two) {while (two > one && a[two] >= a[id]) --two; // 找第一个比id小的, 必须先找小的while (one < two && a[one] <= a[id]) ++one; // 找第一个比id大的, 因为基准数是beif (one < two) swap(a[one], a[two]);//需要从右到左是因为,如果是从左到右,例子1、2、3、4、5//找到第一个比1大的,是2,然后找不到第一个比1小,在2中相遇//然后swap(a[1], a[2])  GG
}swap(a[id], a[one]);int hasKey = one - be + 1; // 有多少个元素if (hasKey >= k) return findKthMin(a, be, one, k);else return findKthMin(a, one + 1, en, k - hasKey);
}
int a[maxn];
void work() {int n;scanf("%d", &n);for (int i = 1; i <= n; ++i) {scanf("%d", a + i);}printf("%d\n", findKthMin(a, 1, n, 4));
}int main() {
#ifdef localfreopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endifwork();return 0;
}

View Code

最热门K串

找前k大的数,首先找到第n - k + 1小的数,那么这些数的右边,都是比它大的,这个时候就是前k大了,直接排序一下就好

#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
const int maxn = 1000000 + 20;
int myRand(int be, int en) {return be + (rand() % (en - be + 1));
}
int findKthMin(int a[], int be, int en, int k) {if (be == en) return be;swap(a[be], a[myRand(be, en)]);int one = be, two = en, id = be;while (one != two) {while (two > one && a[two] >= a[id]) --two; // 找第一个比id小的, 必须先找小的while (one < two && a[one] <= a[id]) ++one; // 找第一个比id大的, 因为基准数是beif (one < two) swap(a[one], a[two]);//需要从右到左是因为,如果是从左到右,例子1、2、3、4、5//找到第一个比1大的,是2,然后找不到第一个比1小,在2中相遇//然后swap(a[1], a[2])  GG
}swap(a[id], a[one]);int hasKey = one - be + 1; // 有多少个元素if (hasKey >= k) return findKthMin(a, be, one, k);else return findKthMin(a, one + 1, en, k - hasKey);
}
int a[maxn];
void work() {int n, k;scanf("%d%d", &n, &k);for (int i = 1; i <= n; ++i) {scanf("%d", a + i);}int id = findKthMin(a, 1, n, n - k + 1);sort(a + id, a + 1 + n);for (int i = n; i >= id; --i) {printf("%d ", a[i]);}
}int main() {
#ifdef localfreopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endifwork();return 0;
}

View Code

转载于:https://www.cnblogs.com/liuweimingcprogram/p/7775525.html

快速排序算法的实现 随机生成区间里的数 O(n)找第k小 O(nlogk)找前k大...相关推荐

  1. Linux环境下编写一个shell程序,此程序的功能:随机生成一个1-100的数(答案)让用户猜

    题目:编写一个shell程序,此程序的功能:随机生成一个1-100的数(答案)让用户猜,如果用户猜的数大于答案,则提示大了,如果用户猜的数小于答案,则提示小了.当用户猜对时提示:猜对了. #! /bi ...

  2. C语言实现随机生成0~100的数

    #include <iostream> #include <time.h>int main() {srand((unsigned)time(NULL));//srand()就是 ...

  3. 随机生成区间范围的整数(含负数)

    coding时需要随机生成一个区间内的正负数,发现网上很多例子很无聊,所以自己写一个吧,供参考 /*** 随机生成指定范围内的整数(含负值)* 使用方法: generateRandomInt(最小数, ...

  4. 猜数字游戏python程序_Python猜数游戏,程序随机生成一个0-100的数,猜对后退出【实例源码】...

    Python猜数字游戏: 写程序,随机生成一个0~100之间的数用变量x绑定 循环让用户输入一个数用y绑定, 输出猜数字的结果 1. 如果y等于生成的数x,则提示"您猜对了", 打 ...

  5. 菜鸟学习算法:随机生成互不相等的数

    最近在写单机版客户端服务器列表展示时遇到随机选取服务器列表的问题,正常情况下服务器列表是服务器返回给客户端(比如服务器定义:proto_gc_server_list_noti(message),当客户 ...

  6. C++基础代码--20余种数据结构和算法的实现

    C++基础代码--20余种数据结构和算法的实现 过年了,闲来无事,翻阅起以前写的代码,无意间找到了大学时写的一套C++工具集,主要是关于数据结构和算法.以及语言层面的工具类.过去好几年了,现在几乎已经 ...

  7. RSA算法和RSA数字签名算法的实现

    RSA算法和RSA数字签名算法的实现 http://blog.chinaunix.net/uid-21880738-id-1813146.html 顾婷婷 李涛 (四川大学计算机系(西区) 成都 61 ...

  8. (python代码+讲解)重叠社区发现EAGLE层次算法的实现

    EAGLE是一种基于最大团聚类的层次聚类算法,用来揭示网络的层次化和层次化社区结构.该算法采用一种聚类框架,来处理最大派系. 1.算法的实现:   首先用Bron-Kerbosch算法找到网络中的最大 ...

  9. 随机生成m个和为n的数

    方法一: 转换为将 n 随机分给m个数 这个问题的难点在于:如何保证m个随机数 和为 n.所以不妨转换思路为:将 n 随机分给 m 个数.eg:如果要生成 9 个和为 100 的随机数,且随机数为 i ...

最新文章

  1. Java基础学习总结(3)——抽象类
  2. MATLAB符号计算
  3. JS 获取控件的绝对位置
  4. 区块链BaaS云服务(20)百度可信跨链BCP
  5. 企业云桌面-03-安装第1个企业 CA-013-CA01
  6. 3.14 01串排序
  7. 新年快乐轮播特效html,基于owl-carousel的卡片水平轮播展示特效
  8. 华为鸿蒙热水器,美的华为跨界联合!搭载鸿蒙OS的美的产品双11上市
  9. gradle对java插件的扩展_Gradle之java插件入门
  10. 蜘蛛侠论坛 2.0 源代码发布
  11. popupWindow的使用心得
  12. IBM宣布:成功研制出了量子计算机原型机,量子计算机商业化正在加速!
  13. HTML 5 会为 Flash 和 Silverlight 送终吗?
  14. html5 xml的区别,xhtml和html5区别 html与xhtml和xml有什么区别
  15. 写作技巧~100段作文排比句(1-20段),考试一定用得上,赶紧收藏!
  16. 你想要的虚幻UE游戏人物模型素材都在这里,细致又齐全
  17. 【webGoat】Path traversal
  18. 如何连接两台电脑,实现两台电脑的共享
  19. win10系统下找不到hosts文件解决方案
  20. centos编译Aegisub

热门文章

  1. DIV周边添加投影及背景固定
  2. centos6.4安装nagios—4.0.8
  3. 回发或回调参数无效。下拉菜单中使用ajax,联动菜单引起的问题解决方案
  4. Spring Security OAuth笔记
  5. 写给java初学者,从零开始学习java开发的完整学习路线
  6. oracle11g中SQL优化(SQL TUNING)新特性之Adaptive Cursor Sharing (ACS)
  7. myeclipse 删除历史工作空间记录
  8. 版本效果MoonWarrior cocos2d-x版本 --1
  9. 使用AMDU工具从无法MOUNT的DISKGROUP中抽取数据文件
  10. DRCNN超分辨重建2016年