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小小的数字均在其前面(虽然不一定有序),
  所以我们直接倒着处理调用nth_element即可。

【代码】

#include <cstdio>
#include <algorithm>
using namespace std;
int T,n,m,id[110],b[110];
unsigned s[10000010],a[110],x,y,z;
unsigned xorshf96(){
    unsigned t;
    x^=x<<16;
    x^=x>>5;
    x^=x<<1;
    t=x; x=y; y=z;
    z=t^x^y;
    return z;
}
bool cmp(int x,int y){return b[x]<b[y];}
int main(){
    int Cas=1;
    while(~scanf("%d%d%u%u%u",&n,&m,&x,&y,&z)){
        for(int i=0;i<m;i++){id[i]=i;scanf("%d",&b[i]);}
        for(int i=0;i<n;i++)s[i]=xorshf96();
        sort(id,id+m,cmp);
        b[id[m]=m]=n;
        for(int i=m-1;~i;i--){
            if(b[id[i]]==b[id[i+1]]){
                a[id[i]]=a[id[i+1]];
                continue;
            }
            nth_element(s,s+b[id[i]],s+b[id[i+1]]);
            a[id[i]]=s[b[id[i]]];
        }printf("Case #%d: ",Cas++);
        for(int i=0;i<m-1;i++)printf("%u ",a[i]);
        printf("%u\n",a[m-1]);
    }return 0;
}
时间: 2024-11-13 11:07:36

HDU 6040 Hints of sd0061(nth_element)的相关文章

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

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 思维

题目链接: 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 5640 King&#39;s Cake(模拟)

Problem Description It is the king's birthday before the military parade . The ministers prepared a rectangle cake of size n×m(1≤n,m≤10000) . The king plans to cut the cake himself. But he has a strange habit of cutting cakes. Each time, he will cut

hdu 4930 Fighting the Landlords (模拟)

Fighting the Landlords Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 160    Accepted Submission(s): 52 Problem Description Fighting the Landlords is a card game which has been a heat for ye

HDU 4738 Caocao&#39;s Bridges(割边)

乍一看一个模板题,仔细一看还是模板题,但是三个坑.1,不是连通图,放0个.2 守卫为0,放1个. 3注意重边. #include<iostream> #include<cstdio> #include<vector> #include<queue> #include<algorithm> #include<stack> #include<cstring> using namespace std; #define maxn

hdu 5623 KK&#39;s Number(dp)

问题描述 我们可爱的KK有一个有趣的数学游戏:这个游戏需要两个人,有N\left(1\leq N\leq 5*{10}^{4} \right)N(1≤N≤5∗10?4??)个数,每次KK都会先拿数.每次可以拿任意多个数,直到NN个数被拿完.每次获得的得分为取的数中的最小值,KK和对手的策略都是尽可能使得自己的得分减去对手的得分更大.在这样的情况下,最终KK的得分减去对手的得分会是多少? 输入描述 第一行一个数T\left( 1\leq T\leq 10\right)T(1≤T≤10),表示数据组

HDU 5624 KK&#39;s Reconstruction(最小生成树)

题目链接:点击打开链接 题意:n个城市, m条可以修建的路, 修每条路有一个费用, 要求修建路将n个城市全部联通,并且最大费用减去最小费用最小. 思路:枚举最小边, 然后重新求一遍最小生成树,复杂度m^2, 出的数据水了, 左边BC水过了.. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #inc