HDU6040-Hints_of_sd0061-STL/快排思想/线性时间找第k大
来源:互联网 发布:马歇尔 知乎 编辑:程序博客网 时间:2024/05/21 20:23
Hints of sd0061
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1638 Accepted Submission(s): 479
Problem Description
sd0061, the legend of Beihang University ACM-ICPC Team, retired last year leaving a group of noobs. Noobs have no idea how to deal with m coming contests. sd0061 has left a set of hints for them.
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
Input
There are multiple test cases (about 10).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;}
Output
For each test case, output "**Case #x: y1 y2 ⋯ ym**" in one line (without quotes), where x indicates the case number starting from 1 and yi (1≤i≤m) denotes the rating of noob for the i-th contest of corresponding case.
Sample Input
3 3 1 1 10 1 22 2 2 2 21 1
Sample Output
Case #1: 1 1 202755Case #2: 405510 405510
Source
2017 Multi-University Training Contest - Team 1
题目真的难读……
大意就是给你递推式和递推初始条件,对应n个数字,要你回答m个“第bi小的数是谁”问题
因为数据量达到了
这里运用的就是快排时候的一个性质:快排时只需要知道这个数是在数列中哪个位置就好,那么考虑在已经知道这个数为n,对于任意小于n的询问,我们就没必要管比n大的数,也就是说我们排序的范围就减小了
同时运用快排的方式随机的找第n小元素时间复杂度是O(n),而且每次询问时的n都不增,这样时间也就是可以接受的
要么手写找第k大,要么就用STL中的nth_element()方法
AC代码
#include <iostream>#include <queue>#include <algorithm>#include <cstring>#include <string>#define LL long long#define ULL unsigned long long#define mem(a,n) memset(a,n,sizeof(a))#define fread freopen("in.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)#define N 10001000#define INF 0x3f3f3f3f#define eps 1e-9using namespace std;unsigned x,y,z,num[N],ans[110];int query[110],index[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;}inline bool cmp(const int &idx1,const int &idx2){ return query[idx1]<query[idx2];}int main(){ ios::sync_with_stdio(false); int n,m,A,B,C; int kase=0; while(cin>>n>>m>>A>>B>>C){ cout<<"Case #"<<++kase<<": "; x=A,y=B,z=C; for(int i=0;i<n;++i){ num[i]=rng61(); } for(int i=0;i<m;++i){ cin>>query[i]; index[i]=i; } sort(index,index+m,cmp); query[index[m]=m]=n; for(int i=m-1;~i;--i){ if(index[i]==index[i+1]){ ans[index[i]]=ans[index[i+1]]; continue; } nth_element(num,num+query[index[i]],num+query[index[i+1]]); ans[index[i]]=num[query[index[i]]]; } for(int i=0;i<m;++i){ cout<<ans[i]<<(i==m-1)[" \n"]; } } return 0;}
- HDU6040-Hints_of_sd0061-STL/快排思想/线性时间找第k大
- 找最轻最重 快排思想 第K大
- //快排思想第k大
- 快排思想找第k小的数
- 寻找第K大的数(快排思想)
- 快排算法及利用快排思想寻找第k大(小)的数
- 寻找第k大数(快排思想)
- O(n)线性时间找第K大,中位数
- 快排应用-第k大元素
- 利用快排思想找出数组中第k大的元素
- 寻找无序数组中第k大的数——快排思想
- 线性时间找第k小
- 快排的思想求第K小的数
- 数组中最大第K元素(快排思想)
- 快排思想 求第(前)k大数
- java快排,找第K小的数,找前K个数
- 使用快排思想 求第n大的值
- 分治法 快排 输出第k大的数
- 【spring mvc】PUT/POST 请求参数接收
- maven的profiles节点
- Mosquitto Windows7 编译&Debug
- Linux内核编译
- CVPR 2017—Deep Joint Rain Detection and Removal from a Single Image
- HDU6040-Hints_of_sd0061-STL/快排思想/线性时间找第k大
- 弹出框及层的屏蔽功能实现
- 万圣节派对
- 二叉排序树:POJ2418-Hardwood Species(外加字符串处理)
- Mybatis知识点整理
- unity切换窗口焦点后,程序依然运行
- 关于action向JSP传值
- Webpack2 入门踩坑教程(一)
- vim常用命令及操作