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<bk and bj<bk .
Now, you are in charge of making the list for constroy.
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.
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 .
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 1 0 1 2 2 2 2 2 2 1 1
Sample Output
Case #1: 1 1 202755 Case #2: 405510 405510
Source
Recommend
题意:
给你一个长度为n的序列,序列由题面给的函数生成。然后m次询问,询问这个序列上第bi小的数
思路:题解中用到了一个stl库中的函数nth_element();
首先介绍一下这个函数,STL中的nth_element()方法的使用 通过调用nth_element(start, start+n, end) 方法可以使第n大元素处于第n位置(从0开始,其位置是下标为 n的元素),并且比这个元素小的元素都排在这个元素之前,比这个元素大的元素都排在这个元素之后,但不能保证他们是有序的,该函数无返回值 平均时间复杂度为o(n)
学会运用这个函数后只需要进行一点点优化就可以了,如果每次查找都查找整个数组的话就要超时了,需要优化一下
这个题目中的询问是离线的,我们可以先给b数组排序,这样的话我们就不需要每次都查找整个数组了,我们就可以nth_element(s, s + p[o[i]], s + p[o[i + 1]])这样了,
但是我们给询问排序的时候要注意离散化处理,最后根据原来的id输出就可以了
ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAXN=1e7;
struct node
{
unsigned pos;
unsigned cnt;
unsigned ans;
}t[110];
unsigned num[MAXN];
unsigned x,y,z;
unsigned deal()
{
unsigned t;
x ^= x << 16;
x ^= x >> 5;
x ^= x << 1;
t = x;
x = y;
y = z;
z = t ^ x ^ y;
return z;
}
int cmp1(const void *a,const void *b)
{
node * A=(node *)a;
node * B=(node *)b;
return A->cnt - B->cnt;
}
int cmp2(const void *a,const void *b)
{
node * A=(node *)a;
node * B=(node *)b;
return A->pos - B->pos;
}
int main()
{
unsigned n,m,a,b,c;
//freopen("in.txt","r",stdin);
for(int cases=1;scanf("%u%u%u%u%u",&n,&m,&a,&b,&c)==5;cases++)
{
x=a,y=b,z=c;
for(int i=0;i<m;i++){
scanf("%u",&t[i].cnt);
t[i].pos=i;
}
for(int i=0;i<n;i++)
num[i]=deal();
qsort(t,m,sizeof(t[0]),cmp1);
t[m].pos=m;
t[m].cnt=n;
for(int i=m-1;i>=0;i--){
nth_element(num,num+t[i].cnt,num+t[i+1].cnt);
t[i].ans=num[t[i].cnt];
}
qsort(t,m,sizeof(t[0]),cmp2);
printf("Case #%d:",cases);
for(int i=0;i<m;i++)
printf(" %u",t[i].ans);
puts("");
}
return 0;
}
#include<string.h>
#include<algorithm>
using namespace std;
const int MAXN=1e7;
struct node
{
unsigned pos;
unsigned cnt;
unsigned ans;
}t[110];
unsigned num[MAXN];
unsigned x,y,z;
unsigned deal()
{
unsigned t;
x ^= x << 16;
x ^= x >> 5;
x ^= x << 1;
t = x;
x = y;
y = z;
z = t ^ x ^ y;
return z;
}
int cmp1(const void *a,const void *b)
{
node * A=(node *)a;
node * B=(node *)b;
return A->cnt - B->cnt;
}
int cmp2(const void *a,const void *b)
{
node * A=(node *)a;
node * B=(node *)b;
return A->pos - B->pos;
}
int main()
{
unsigned n,m,a,b,c;
//freopen("in.txt","r",stdin);
for(int cases=1;scanf("%u%u%u%u%u",&n,&m,&a,&b,&c)==5;cases++)
{
x=a,y=b,z=c;
for(int i=0;i<m;i++){
scanf("%u",&t[i].cnt);
t[i].pos=i;
}
for(int i=0;i<n;i++)
num[i]=deal();
qsort(t,m,sizeof(t[0]),cmp1);
t[m].pos=m;
t[m].cnt=n;
for(int i=m-1;i>=0;i--){
nth_element(num,num+t[i].cnt,num+t[i+1].cnt);
t[i].ans=num[t[i].cnt];
}
qsort(t,m,sizeof(t[0]),cmp2);
printf("Case #%d:",cases);
for(int i=0;i<m;i++)
printf(" %u",t[i].ans);
puts("");
}
return 0;
}