【HDU6040】Hints of sd0061(nth_element)

来源:互联网 发布:儿童故事书软件 编辑:程序博客网 时间:2024/06/05 07:29

记录一个菜逼的成长。。

2017 Multi-University Training Contest - Team 1

题目链接

题目大意:

给你n,m,A,B,C,由A,B,C生成a数组n个数,给出b数组m个数,根据这m个数排a数组。
输出排名为bi的对应的a数组中的数

笔记

黑科技:nthelement
第一次知道还有这个函数
nthelement(first,nth,last)
[first,last)将排名为nth的值放在相应的位置上

要从大到小选择nth_element,这样复杂度才是接近O(n)

#include <bits/stdc++.h>using namespace std;#define rep(i,l,r) for( int i = l; i <= r; i++ )#define rep0(i,l,r) for( int i = l; i < r; i++ )#define ALL(v) (v).begin(),(v).end()#define cl(a,b) memset(a,b,sizeof(a))#define clr clear()#define pb push_back#define mp make_pair#define fi first#define se secondtypedef long long LL;typedef pair<int,int> PII;const int INF = 0x3f3f3f3f;const int maxn = 10000000 + 10;int b[110],c[110],id[110];unsigned a[maxn];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;}bool cmp(int aa,int bb){  return b[aa] < b[bb]; }int main(){  int n,m,cas = 1;  while(~scanf("%d%d%u%u%u",&n,&m,&x,&y,&z)){    rep0(i,0,m)scanf("%d",b+i),id[i] = i;    rep0(i,0,n)a[i] = rng61();    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]]){        c[id[i]] = c[id[i + 1]];        continue;      }      nth_element(a,a + b[id[i]],a + b[id[i + 1]]);      c[id[i]] = a[b[id[i]]];    }    printf("Case #%d: ",cas++);    for( int i = 0; i < m; i++ )printf("%u%c",c[i]," \n"[i == m-1]);  }  return 0;}
原创粉丝点击