hdu6040 Hints of sd0061(寻找第b[i]+1小的数)
来源:互联网 发布:找到网络打印机 编辑:程序博客网 时间:2024/05/29 19:00
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6040
题意:
用给定函数生成n个unsigned类型的数,用a数组保存,询问m次,每次询问a数组中第b[i]+1小的数是什么。
思路:
对a数组排序是不行的。题目中case<=10, n<1e7, m<=100, O(case * m *nlog(n))的复杂度是会超时的。
使用nth_element()函数。题目中有一点,b[i]+b[j] <= b[k], b[i]!=b[j], b[i] < b[k] , b[j] < b[k],我们把b[i],b[j],b[k]看成a数组中的下标ii, jj, kk, 那么kk可能比ii和jj大很多。那么,我们先把b数组排序,先查询a[kk], 再查询a[ii]和a[jj],这样我们可以把n的规模减小。
排序:
对b数组下标进行排序。另开一个数组idx,记录每个b[i]中的i,重定义小于号,将数组idx中下标i满足b[i]>b[j]的idx[i]排到前面。
代码:
#include <iostream>#include <algorithm>#include <cstring>#include <vector>#include <cstdio>using namespace std;const int maxn = 1e7+10;unsigned x, y, z;int n, m, A, B, C, Case, b[110], idx[110];unsigned a[maxn], ans[110];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;}bool cmp(int i, int j){ return b[i] > b[j];}int main(){ Case = 0; while(cin>>n>>m>>A>>B>>C){ x=A; y=B; z=C; for(int i=0; i<m; ++i){ cin>>b[i]; idx[i] = i; } for(int i=0; i<n; ++i){ a[i] = rng61(); } sort(idx, idx+m, cmp); int last = n; for(int i=0; i<m; ++i){ int pos = b[idx[i]]; nth_element(a, a+pos, a+last); ans[idx[i]] = a[pos]; last = pos; } cout<<"Case #"<<++Case<<":"; for(int i=0; i<m; ++i){ cout<<" "<<ans[i]; } cout<<endl; } return 0;}
阅读全文
0 0
- hdu6040 Hints of sd0061(寻找第b[i]+1小的数)
- HDU6040-Hints of sd0061
- HDU6040-Hints of sd0061(STL)
- HDU6040 Hints of sd0061【STL】
- 【HDU6040】Hints of sd0061(nth_element)
- hdu6040 Hints of sd0061【nth_element使用】
- HDU-6040 Hints of sd0061(线性找第k小)
- HDU-6040 Hints of sd0061(线性找第k小)
- 寻找第i小的数
- hdu 6040 Hints of sd0061(STL)
- HDU 6040 Hints of sd0061 (技巧)
- hdu 6040 Hints of sd0061(STL)
- HDU 6040 Hints of sd0061(nth_element)
- Hints of sd0061 HDU
- 寻找数组中第i小的数(C++)
- HDU 6040 Hints of sd0061 找第K大数 (nth_element函数)
- HDU 6040 Hints of sd0061
- [HDU]-6040 Hints of sd0061
- python3安装使用xadmin
- JSP使用include命令报HTTP Status 500错误
- linux 单进程cpu特别高
- 二维测量(比较)
- cookie 和session 的区别详解
- hdu6040 Hints of sd0061(寻找第b[i]+1小的数)
- Google FCM推送(Firebase Cloud Messaging)
- C语言-堆和栈的生长方向
- java.lang.NoClassDefFoundError: scala/xml/MetaData
- 几种常见的微服务架构方案简述——ZeroC IceGrid、Spring Cloud、基于消息队列
- web前端-图片,优化,压缩
- 2017多校联合第二场 1001题 hdu 6045 Is Derek lying?
- Python基础知识(一)
- PHP使用ip2long跳过的坑