HDU6040-Hints_of_sd0061-STL/快排思想/线性时间找第k大

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 1
0 1 2
2 2 2 2 2
1 1

Sample Output

Case #1: 1 1 202755
Case #2: 405510 405510

Source

2017 Multi-University Training Contest - Team 1

题目真的难读……

大意就是给你递推式和递推初始条件,对应n个数字,要你回答m个“第bi小的数是谁”问题

因为数据量达到了

107
,排个序就直接T了,所以不能这样做

这里运用的就是快排时候的一个性质:快排时只需要知道这个数是在数列中哪个位置就好,那么考虑在已经知道这个数为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-9
using 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值