hdu6040 (nth_element函数)
来源:互联网 发布:淘宝什么叫直通车 编辑:程序博客网 时间:2024/06/10 20:03
题意:
构造出一个数组,查询整个数组第k大。
思路:可以运用nth_element函数,nth_element函数是把第k大的数放在第k位,然后左边都是比它小的数,右边都是比它大的数,顺序任意。
为了更优化一些,先将要查询的数列排个序,然后从后往前进行查询,因为输入保证任意两个小的之和小于第三个
所以查询数列的间隔一定大于等于斐波那契,也就是从大到小查询的话,每次至少能去掉一半的区间.
#include<bits/stdc++.h>using namespace std;const int maxn = 1e7 + 9;unsigned x ,y, z ;inline unsigned rng61() { unsigned t; x ^= x << 16; x ^= x >> 5; x ^= x << 1; t = x; x = y; y = z; z = t ^ x ^ y; return z;}unsigned a[maxn];int b[110];unsigned ans[110];int pos[110];bool cmp(int x,int y){return b[x] < b[y];}int main(){int n,m;int T = 1;while( ~ scanf("%d%d%u%u%u",&n,&m,&x,&y,&z)){for(int i = 0; i < n;++ i){a[i] = rng61();}for(int i = 0; i < m; i ++)scanf("%d",&b[i]),pos[i] = i;sort(pos,pos + m,cmp);//因为最后要按输入顺序输出,所以用下标对查询数组进行排序printf("Case #%d:",T ++);b[pos[m] = m] = n;for(int i = m - 1; i >= 0; -- i){if(b[pos[i] ] == b[pos[i + 1]]){ans[pos[i]] = ans[pos[i + 1]];continue;}nth_element(a,a + b[pos[i] ],a + b[pos[i + 1] ]);//不用到最后ans[pos[i]] = a[b[pos[i]] ];}for(int i = 0; i < m;++ i)printf(" %u",ans[i]);printf("\n");}return 0;}
阅读全文
0 0
- hdu6040 (nth_element函数)
- 【HDU6040】Hints of sd0061(nth_element)
- hdu6040 Hints of sd0061【nth_element使用】
- C/C++中nth_element函数
- nth_element
- nth_element
- C++中nth_element函数的用法
- 牛逼神器第三弹--nth_element函数
- C++中nth_element函数的用法
- 【快速选择算法与nth_element函数】【续UVA11300 】
- HDU 6040 Hints of sd0061(STL nth_element函数)
- HDU6040(思维题)
- HDU6040-Hints of sd0061
- std::nth_element
- nth_element使用
- nth_element()
- partial_sort()和nth_element() 两个和排序有关的函数的性能问题
- #1133 : 二分·二分查找之k小数 ( 快速排序, 分治 OR nth_element() 函数)
- ESP8266交叉编译器xtensa-lx106-elf 在Linux下编译与生成
- 数组、List、ArrayList的区别
- Linux 常用基本命令 which
- selenium2library期望值关键字总结
- hdu 1387 team queue 有问题
- hdu6040 (nth_element函数)
- Mysql中的存储过程
- C语言-基本数据类型
- 2017多校联合1(hdu6033)水Add More Zero
- 万能头文件#include <bits/stdc++.h>
- 项目一:绘制地图
- linux命令相关
- 判断当前应用是否启动/是否在前台
- SDUT-顺序表应用3:元素位置互换之移位算法