HDU 6040
题意略。
思路:题目就是在询问你m次,第k小是哪个数。首先我们可以想到直接排序后,即可O(1)来查找询问。但是题目中n的范围给的是1e7,
无法承受nlogn的复杂度。从而想到另外一种求静态第k小的方法:利用快速排序来做到。时间复杂度是O(n),但是询问次数m是100,
同样无法承受O(n * m)的复杂度。这时我们应该想到题目中给的另外一个条件:if (bi < bk && bj < bk && bi != bk) then bi + bj <= bk。
从而我们知道了询问次数最坏的情况下bi数列应该是一个斐波那契数列,而斐波那契数列有一个重要性质:b[n] / b[n+1] = 0.618。如果
我们倒着来求这些询问的答案,那么后一个结果就可以利用上前一个结果来缩短自己的划分范围。我们知道第一个结果的求解复杂度是
O(n)的,那么总的复杂度是:n * (1 + 0.618 + 0.618^2 + 0.618^3 +......) = O(n)。
详见代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e7 + ; struct query{
int bi,id;
unsigned ans;
}; unsigned store[maxn];
query depot[];
int n,m,cas = ;
unsigned x,y,z; unsigned rng61(){
unsigned t;
x ^= x<<;
x ^= x>>;
x ^= x<<;
t = x;
x = y;
y = z;
z = t ^ x ^ y;
return z;
}
bool cmp(const query& a,const query& b){
return a.id < b.id;
}
bool cmp2(const query& a,const query& b){
return a.bi < b.bi;
} int main(){
while(scanf("%d%d%u%u%u",&n,&m,&x,&y,&z) == ){
for(int i = ;i < m;++i){
scanf("%d",&depot[i].bi);
depot[i].id = i;
}
for(int i = ;i < n;++i){
store[i] = rng61();
}
sort(depot,depot + m,cmp2);
int bound = n;
for(int i = m - ;i >= ;--i){
nth_element(store,store + depot[i].bi,store + bound);
depot[i].ans = store[depot[i].bi];
bound = depot[i].bi;
}
sort(depot,depot + m,cmp);
printf("Case #%d:",cas++);
for(int i = ;i < m;++i){
printf(" %u",depot[i].ans);
}
printf("\n");
}
return ;
}
HDU 6040的更多相关文章
- HDU 6040 - Hints of sd0061 | 2017 Multi-University Training Contest 1
/* HDU 6040 - Hints of sd0061 [ 第k小数查询,剪枝 ] 题意: 给出随机数列 a[N] (N < 1e7) 询问 b[M] (M < 100) ,对于每个询 ...
- HDU 6040 Hints of sd0061(划分高低位查找)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6040 [题目大意] 给出一个随机数生成器,有m个询问,问第bi小的元素是啥 询问中对于bi< ...
- HDU 6040 Hints of sd0061(nth_element)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6040 [题目大意] 给出一个随机数生成器,有m个询问,问第bi小的元素是啥 询问中对于bi< ...
- hdu 6040 Hints of sd0061(stl: nth_element(arr,arr+k,arr+n))
Hints of sd0061 Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 6040 stl
Hints of sd0061 Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 6040 Hints of sd0061 nth_element函数
Hints of sd0061 Problem Description sd0061, the legend of Beihang University ACM-ICPC Team, retired ...
- HDU 6040 Hints of sd0061 —— 2017 Multi-University Training 1
Hints of sd0061 Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
随机推荐
- eclipse(javaee windows)
百度云:链接:http://pan.baidu.com/s/1eSoO4s6 密码:54am 官方下载网址:http://www.eclipse.org/downloads/eclipse-pac ...
- memset函数怎么用嘞↓↓↓
1.我也曾天真的以为 memset(a,0,sizeof(a))中的0可以用任意数替换 实际上这是错误的 memset的功能是将一快内存中的内容以单个字节逐个拷贝的方式放到指定的内存中去. 2.介绍几 ...
- d3.js实现柱形图,饼图以及折现图
饼图 var width = 500; var height = 500; //处理数据 var dataset = [ 30 , 10 , 43 , 55 , 13 ]; var pie = d3. ...
- IOS应用无法下载、此时无法安装应用程序
无法安装应用 app开发者,进行程序测试,重试还不行,就重新打包, 个人,更改wifi的dns 在“设置” –> “WiFi” –> 进入当前的WiFi 进入之后点击旁边的叹号,然后进入之 ...
- iOS Xcode6 新建OC Category文件
首先:File -> New File 接下来界面如下,选择Objective-C File,然后Next 在这里选择 Category 即可
- Where is the clone one and how to extract it?
One cannot be in two places at once. Do you know what's "Dual Apps"? Manufactures like Xia ...
- 关于程序null值的见解
今天遇到了一个问题,查询一条数据,返回用list接,发现少了2个值(ssh框架).执行SQL少的这两个字段的值为null.上图说明一下: 可以看到第一次查询没有角标38.39的值. 是同一条SQL,第 ...
- Asp.NetCore源码学习[2-1]:配置[Configuration]
Asp.NetCore源码学习[2-1]:配置[Configuration] 在Asp. NetCore中,配置系统支持不同的配置源(文件.环境变量等),虽然有多种的配置源,但是最终提供给系统使用的只 ...
- 如何阅读JDK源码
JDK源码阅读笔记: https://github.com/kangjianwei/LearningJDK 如何阅读源码,是每个程序员需要面临的一项挑战. 为什么需要阅读源码?从实用性的角度来看,主要 ...
- 值得花费一周研究的算法 -- KMP算法(indexOf)
KMP算法是由三个科学家(kmp分别是他们名字的首字母)创造出来的一种字符串匹配算法. 所解决的问题: 求文本字符串text内寻找第一次出现字符串s的下标,若未出现返回-1. 例如 text : &q ...