题意:
给你一个函数求出1e7个数字,然后对这1e7个数字有m次询问,每次问数字中第dx大的数字是多少。
题解:
主要是要用到复杂度为O(n)的stl函数。
nth_element(num.start(),num.start()+n,num.end());
可以把序列中比第n大的数小的放左边,比第n大的放右边。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
#define clr(a,b) memset(a,b,sizeof(a))
#define pb(a) push_back(a)
#define fir first
#define se second
#define LL long long
typedef pair<int,int> pii;
typedef pair<LL,int> pli;
typedef pair<LL,LL> pll;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5+5;
LL mod = 1e9+7;
double eps = 0.00000001;
double PI = acos(-1);
unsigned x,y,z,num[10000005],n,m,ans[105];
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;
}
pii p[105];
int main() {
int ca = 1;
while(scanf("%u%u%u%u%u",&n,&m,&x,&y,&z)!=EOF) {
for(int i = 0;i < n;i++) num[i] = rng61();
for(int i = 0;i < m;i++) {
scanf("%d",&p[i].fir);
p[i].se = i;
}
sort(p,p+m);
p[m].fir = n;
for(int i = m-1;i >= 0;i--) {
if(p[i+1].fir == p[i].fir) {
ans[p[i].se] = ans[p[i+1].se];
continue;
}
nth_element(num,num+p[i].fir,num+p[i+1].fir);
ans[p[i].se] = num[p[i].fir];
}
printf("Case #%d:",ca++);
for(int i = 0;i < m;i++) printf(" %u",ans[i]);
printf("\n");
}
}