HDU 6040 Hints of sd0061(nth_element)
Description
给出一个长度为n的序列a[i],以及m次查询b[1],…,b[m],第i次查询a序列中第b[i]+1大的数
Input
多组用例,每组用例首先输入五个整数n,m,A,B,C,n表示序列长度,m表示查询数,A,B,C经过下列操作逐一生成a序列
unsigned x = A, y = B, z = C;
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;
}
之后输入m个整数b[i]表示查询,b序列满足b[i]+b[j] < b[k],其中b[i]!=b[j],b[i],b[j] < b[k],以文件尾结束输入(1<=n<=1e7,1<=m<=100,0<=b[i] < n)
Output
对于每组用例,输出m个整数表示这m次操作的答案
Sample Input
3 3 1 1 1
0 1 2
2 2 2 2 2
1 1
Sample Output
Case #1: 1 1 202755
Case #2: 405510 405510
Solution
nth_element在O(n)时间内就可以找到一个长度为n的序列中第k大的数,但是此题O(mn)的复杂度也是不能接受的,注意到b序列排好序去重之后满足斐波那契性质,故每次序列长度会缩短很多,当b是斐波那契数列时每次会序列会变为原来的1/1.618,故不同的b最多只有个,每次更新一下查询区间的上限即可
时间复杂度为
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
const int maxn=10000001;
int n,m;
unsigned A,B,C,a[maxn],ans[101];
struct node
{int val,id;bool operator<(const node&b)const{return val>b.val;}
}b[101];
unsigned x,y,z;
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;
}
int main()
{int res=1;while(~scanf("%d%d%u%u%u",&n,&m,&A,&B,&C)){x=A,y=B,z=C;for(int i=0;i<m;i++){scanf("%d",&b[i].val);b[i].id=i;}sort(b,b+m);for(int i=0;i<n;i++)a[i]=rng61();int pos=n;for(int i=0;i<m;i++){nth_element(a,a+b[i].val,a+pos);ans[b[i].id]=a[b[i].val];pos=b[i].val;}printf("Case #%d: ",res++);for(int i=0;i<m;i++)printf("%u%c",ans[i],i==m-1?'\n':' ');}return 0;
}
HDU 6040 Hints of sd0061(nth_element)相关推荐
- HDU 6040 Hints of sd0061 思维
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6040 题目大意: 给定数组a, b.要求输出和B长度相同的数组C, C[i]为在A中排名为B[i]的 ...
- HDU 6040 Hints of sd0061
nth_element(arr,arr+x,arr+n); 可以将x在[0,n]范围内,将第x小的数字移动到arr[x]上,其余比arr[x]大的,在x后面,比arr[x]小的,在x前面. #incl ...
- HDU各种比赛题题解(一)
HDU各种比赛题题解(一) Gardon-DYGG Contest 1 HDU1178 Heritage from father[水题] - 海岛Blog - CSDN博客 HDU1181 变形课[D ...
- 【HDU 2612 Find a Way(BFS)】(兼BFS入门笔记)
[HDU 2612 Find a Way(BFS)](兼BFS入门笔记) 原题入口: http://acm.hdu.edu.cn/showproblem.php?pid=2612 第一篇在CSDN的博 ...
- 【HDU 3400】Line belt(三分法)
题目链接 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3400 题意 有两条传送带AB和CD,移动速度分别为p,q. 除了传送带的其他区域移动速度为r ...
- HDU 1222 Wolf and Rabbit(gcd)
HDU 1222 Wolf and Rabbit (最大公约数)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- HDU 2639 Bone Collector II (dp)
题目链接 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took part in ...
- hdu 3183 A Magic Lamp (rmq)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 #include <stdio.h> #include <stdlib.h&g ...
- HDU 6354 Everything Has Changed(余弦定理)多校题解
题意:源点处有个圆,然后给你m个圆(保证互不相交.内含),如果源点圆和这些原相交了,就剪掉相交的部分,问你最后周长(最外面那部分的长度). 思路:分类讨论,只有内切和相交会变化周长,然后乱搞就行了.题 ...
最新文章
- 如何在html中写javascript代码,如何在本地html代码中使用javascript脚本
- tensorflow 之tensorboard 对比不同超参数训练结果
- scanf函数使用遇到的问题
- Chromebook开发者模式Linux,Chromebook 的开发者模式和开发者版本有什么区别?
- 【今晚7点半】:GVoice 千万在线语音传输的那些事
- 【Spring】入门HelloWorld
- lEO数值资产系统某平台c2c币数值合约交易平台自动撮合松机器人功能
- c++访问数据库代码示例 occi_使用Python操作SQL Server数据库
- 数组异或交换出现的问题
- SQL入门经典(第5版)
- 网络抓包工具之Wireshark
- 物料搬运设备中的链条和链轮的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
- mysql按照音序排列_如何按音序排列
- Windows安装Android软件,win7系统安装安卓软件WindowsAndroid的方法
- 背包客旅行札记-html
- 开放正成为华为下一阶段的战略竞争力
- 淘宝店铺怎么发逛逛?发逛逛有什么要求?
- 人力资源数据分析(包含数据来源文件)
- 忽略wx公众号网页授权域名验证
- 第21节迁移学习原理及实例