HDU 6040 Hints of sd0061 nth_element函数

Hints of sd0061

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.

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

题意:

  给你一个长度n的排列,给你生成排列的函数

  求m次第k小

题解:

  nth_element

  表示在数组A的[0,n-1]中找到第k小的并放在第k个位置,并且前k-1个位置均为小于A[k]的数

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 1e7+10, M = 1e3+20,inf = 2e9;

unsigned x,y,z;
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;
}
struct ss{
    int id,x;
    bool operator<(const ss& r) const{
        return x < r.x;
    }
}b[N];
int n,m;
unsigned ans[N],a[N];
int main() {
    int cas = 1;
    while(scanf("%d%d%u%u%u",&n,&m,&x,&y,&z)!=EOF) {
        for(int i = 1; i <= m; ++i)
            scanf("%d",&b[i].x),b[i].id = i;
        for(int i = 0; i < n; ++i) a[i] = rng61();
        sort(b+1,b+m+1);
        b[m+1].x = n;
        for(int i = m; i >= 1; --i) {
            nth_element(a,a+b[i].x,a+b[i+1].x);
            ans[b[i].id] = a[b[i].x];
        }
        printf("Case #%d: ",cas++);
        for(int i = 1; i < m; ++i) printf("%u ",ans[i]);
        printf("%u\n",ans[m]);
    }
}
时间: 2025-01-01 09:36:21

HDU 6040 Hints of sd0061 nth_element函数的相关文章

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)Total Submission(s): 2297    Accepted Submission(s): 687 Problem Description sd0061, the legend of Beihang University ACM-ICPC Team, retired last yea

HDU 6040 Hints of sd0061(nth_element)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6040 [题目大意] 给出一个随机数生成器,有m个询问,问第bi小的元素是啥 询问中对于bi<bk,bj<bk,有bi+bj<=bk [题解] 我们将所有的询问排序,我们发现倒着处理询问的时候询问区间大小下降非常快, nth_element(start,start+k,end) 可以近似O(n)查询区间中第k小的数字, 并且在处理后保证比第k小小的数字均在其前面(虽然不一定有序), 所以我

HDU 6040 Hints of sd0061 思维

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6040 题目大意: 给定数组a, b.要求输出和B长度相同的数组C, C[i]为在A中排名为B[i]的数, 重新排A数组, 使得a[i]必须是a数组中第b[i]+1大的数 解题思路: 首先我们要把数组A求出来.第一想法就是最单纯的将A排个序, 然后输出A[B[i]], 自己还恬不知耻的去交了一发, 要是这都不会T, 还叫啥ACM啊...... 好吧, 我题意理解错了......自己太浮躁了, 检讨一

hdu 6040 -Hints of sd0061(STL)

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

HDU 6040 stl

Hints of sd0061 Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2421    Accepted Submission(s): 736 Problem Description sd0061, the legend of Beihang University ACM-ICPC Team, retired last yea

hdu 3501 Calculation 2 (欧拉函数)

题目 题意:求小于n并且 和n不互质的数的总和. 思路:求小于n并且与n互质的数的和为:n*phi[n]/2 . 若a和n互质,n-a必定也和n互质(a<n).也就是说num必定为偶数.其中互质的数成对存在.其和为n. 公式证明: 反证法:如果存在K!=1使gcd(n,n-i)=k,那么(n-i)%k==0而n%k=0那么必须保证i%k=0k是n的因子,如果i%k=0那么gcd(n,i)=k,矛盾出现; 所以先求出1--n-1 的和, 再用这个和 减去 上面公式求出来的值. 欧拉函数phi(m)

HDU 3501 Calculation 2(欧拉函数的应用)

HDU 3501 Calculation 2 大意:求1~n之间与n不互质的数的总和. 思路:欧拉函数的应用:先用欧拉函数求出与n互质的总数m,计算m个数的总和,用n的总和减去m的总和就是想要的结果. 1 #include <stdio.h> 2 #define LL __int64 3 4 int eular(int n){ 5 int ret = 1; 6 for(int i = 2; i*i <= n;i++) 7 if(n%i == 0){ 8 n /= i, ret *= i-

hdu 2814 快速求欧拉函数

1 /** 2 大意: 求[a,b] 之间 phi(a) + phi(a+1)...+ phi(b): 3 思路: 快速求欧拉函数 4 **/ 5 6 #include <iostream> 7 #include <cstring> 8 using namespace std; 9 #define Max 3000000 10 11 long long phi[Max+5]; 12 int prime[Max/10]; 13 bool flag[Max+5]; 14 15 void

nth_element函数

使用方法:nth_element(start, start+n, end) 使第n大元素处于第n位置(从0开始,其位置是下标为n的元素),并且比这个元素小的元素都排在这个元素之前,比这个元素大的元素都排在这个元素之后,但不能保证他们是有序的. #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int