解题报告:HDU_6040 Hints of sd0061 STL::nth_element

来源:互联网 发布:韩服数据 编辑:程序博客网 时间:2024/06/15 23:26

题目链接


题意:

给你一个序列的递推函数,m次询问,询问这个序列上第bi小的数。


官方题解:


最关键的部分在于了解STL里的nth_element函数,简单介绍一下:


这个函数的时间复杂度近似线性

函数的调用(取第n小):nth_element ( arr.begin() ,   arr+n,arr.end() )

调用函数后保证比第n个数小的数都在前面,比它大的数在后面




代码:

#include<bits/stdc++.h>using namespace std;int id[105],A[105];unsigned s[10000005];unsigned ans[105];int n,m;unsigned x,y,z;unsigned oper(){   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 a,int b){   return A[a] < A[b];}int main(){   int cas = 0;   while(scanf("%d%d%u%u%u",&n,&m,&x,&y,&z)==5){      for(int i=0;i<m;i++){         id[i] = i;         scanf("%d",&A[i]);      }A[m]=n;id[m]=m;      for(int i=0;i<n;i++)s[i] = oper();      sort(id,id+m,cmp);      for(int now,i=m-1,last=A[id[m]];i>=0;i--,last=now){         now = A[id[i]];         if(now==last){            ans[id[i]] = ans[id[i+1]];         }else {            nth_element(s,s+now,s+last);            ans[id[i]] = s[now];         }      }printf("Case #%d:",++cas);      for(int i=0;i<m;i++){         printf(" %u",ans[i]);      }printf("\n");   }return 0;}