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)
Total Submission(s): 2262 Accepted Submission(s): 673
There are n noobs in the team, the i-th of which has a rating ai. sd0061 prepares one hint for each contest. The hint for the j-th contest is a number bj, which means that the noob with the (bj+1)-th lowest rating is ordained by sd0061 for the j-th contest.
The coach asks constroy to make a list of contestants. constroy looks into these hints and finds out: bi+bj≤bk is satisfied if bi≠bj, bi<bk and bj<bk.
Now, you are in charge of making the list for constroy.
For each test case:
The first line contains five integers n,m,A,B,C. (1≤n≤107,1≤m≤100)
The second line contains m integers, the i-th of which is the number bi of the i-th hint. (0≤bi<n)
The n noobs' ratings are obtained by calling following function n times, the i-th result of which is ai.
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;
}
题意:给你一个长度为n的序列,序列由题面给的函数生成。然后m次询问,询问这个序列上第bi小的数。
解题思路:新学习了一个STL,C++中的nth_element(arr,arr+k,arr+n),将长度为n的数组arr进行划分,第k-1位置上就是第k大的数(下标从0开始算),这个函数近似线性,在找到第k大的时候,前k-1个数均是小于arr[k]的,因为输入保证任意两个小的之和小于第三个,所以查询数列的间隔一定大于等于斐波那契,所以从大到小查询的话,每次至少能去掉一半的区间,根据这个可以减少搜索量
hdu上要用G++交,C++超时
#include <iostream>
#include<cstdio>
#include<functional>
#include<cstring>
#include<algorithm>
using namespace std; unsigned a[];
struct node
{
int k,id;
}b[];
int n,m;
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(node a,node b)
{
return a.k>b.k;
}
bool cmp2(node a,node b)
{
return a.id<b.id;
}
int main()
{
int cas=;
while(~scanf("%d%d%u%u%u",&n,&m,&x,&y,&z))
{
for(int i=;i<=m;i++)
{
scanf("%d",&b[i].k);
b[i].id=i;
}
for(int i=;i<n;i++) a[i]=rng61(); sort(b+,b++m,cmp);
b[].k=n;
for(int i=;i<=m;i++)
nth_element(a,a+b[i].k,a+b[i-].k);
sort(b+,b++m,cmp2); printf("Case #%d:",++cas);
for(int i=;i<=m;i++)
printf(" %u",a[b[i].k]);
printf("\n");
}
return ;
}
hdu 6040 Hints of sd0061(stl: nth_element(arr,arr+k,arr+n))的更多相关文章
- HDU 6040 Hints of sd0061(nth_element)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6040 [题目大意] 给出一个随机数生成器,有m个询问,问第bi小的元素是啥 询问中对于bi< ...
- 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 nth_element函数
Hints of sd0061 Problem Description sd0061, the legend of Beihang University ACM-ICPC Team, retired ...
- HDU 6040 Hints of sd0061(划分高低位查找)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6040 [题目大意] 给出一个随机数生成器,有m个询问,问第bi小的元素是啥 询问中对于bi< ...
- 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 ...
- HDU 6040 stl
Hints of sd0061 Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Hints of sd0061(快排思想)
Hints of sd0061 Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- C/C++ char* arr与char arr[]的区别(反汇编解析)
写作日期:2016.08.31 修改日期:2016.09.01 .2016.09.02. 交流qq:992591601 用了几天时间复习了下C语言.对于C语言的字符串操作有些不习惯,于是作为练习,写下 ...
- Java: arr==null vs arr.length==0
当 arr 是一个array时,写Java开始的corner case常常会写类似下面的语句: if(arr == null || arr.length == 0){ return 0; } 其实这是 ...
随机推荐
- HDU - 2732 Leapin' Lizards (拆点最大流)
题意:有N*M的矩形,每个格点有一个柱子,每根柱子有高度c,允许蜥蜴经过这根柱子c次,开始有一些蜥蜴在某些柱子上,它们要跳出这个矩形,每步最大能跳d个单位,求最少有多少蜥蜴不能跳出这个矩形. 分析:转 ...
- zookeeper 监听事件 CuratorWatcher
zookeeper 监听事件 CuratorWatcher CuratorWatcher一次注册只监听一次,不监听查询. 1.监听测试类 package com.qy.learn.zk.curator ...
- [转]关于Navicat和MYSQL字符集不统一出现的中文乱码问题
原文链接:关于Navicat和MYSQL字符集不统一出现的中文乱码问题 最近遇到一串关于MYSQL中文乱码的问题,问题背景是这样的: 在此之前,服务器上安装好MySQL之后就立马重新配置了字符集为ut ...
- 20145312 《Java程序设计》第十周学习总结
20145312 <Java程序设计>第十周学习总结 学习笔记 Chapter 17反射与类加载器 17.1 运用反射 .class文档反应了类基本信息,因而从Class等API取得类信息 ...
- C++求矩阵的鞍点
矩阵的鞍点就是指它在本行中的值最大,在本列中的值最小. 求解思路: 求出每行的最大值MaxRow以及每列的最小值MinColumn 保存行最大值的位置和列最小值的位置 如果行最大值得位置和列最小值的相 ...
- v4l2的学习建议和流程解析
v4l2,一开始听到这个名词的时候,以为又是一个很难很难的模块,涉及到视频的处理,后来在网上各种找资料后,才发现其实v4l2已经分装好了驱动程序,只要我们根据需要调用相应的接口和函数,从而实现视频的获 ...
- openwrt下如何只编译linux内核
答:make target/linux/install V=s (此操作也会将最终镜像打包好,V=s会输出日志) 拓展 清空内核: make target/linux/clean V=s 解压内核: ...
- springBoot的文件上传功能
知识点: 后台:将上传的图片写入指定服务器路径,保存起来,返回上传后的图片路径(在springBoot中,参考博客:http://blog.csdn.net/change_on/article/det ...
- Objective C NSString 编码成URL 特殊字符处理
找了一下网上的教程都是使用类似以下代码,Xcode提示这个CoreFoundation不受ARC管理,所以折中的方式是添加__bridge. NSString *encodedValue = (__b ...
- 秒懂算法1——冒泡排序,及一种小改进(C#实现)
算法思路: 重复走访每两个相邻元素,比较大小交换位置,直至排序完成. 有兴趣电话可以看一下这个[冒泡排序踢踏舞]的视频,很形象的演示了排序过程,额呵呵~~ 性质: 冒泡排序是一种原地排序(只有常数个元 ...