hdu 6040 卡时间+找第k个最小的数 2017 Multi-University Training Contest - Team 1

Hints of sd0061

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 907    Accepted Submission(s): 240


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+bjbk is satisfied if bibj, 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 . (1n107,1m100)

The second line contains m integers, the i -th of which is the number bi of the i -th hint. (0bi<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 (1im) 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

网上好多都是嵌套的数组,三个数组嵌套,刚开始看还真的看不懂啊,我写的应该很容易懂


题意:

给出一个递推的函数,求出前 n 个 ,给你m个数字 di,让你求 第di +1 个最小的数字

题解:

这里主要就是用到了一个STL的函数

nth_element(num,num+t[i].cnt,num+t[i+1].cnt);
没有返回值,表示在num这个数组里面,选择第 t[i].cnt个最小的数字,在 0 ~ t[ i +1 ].cnt这个范围内

此函数特性:线性复杂度,把比第t[i].cnt个最小的数还小的数放这个数的前面,其余的放后面


输出依然按照输入的时候的顺序,m 比较小,直接排序没问题

然后再注意一下数据类型


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

const int MAXN=1e7;
struct node
{
    unsigned pos;
    unsigned cnt;
    unsigned ans;
}t[110];
unsigned num[MAXN];

unsigned x,y,z;
unsigned deal()
{
  unsigned t;
  x ^= x << 16;
  x ^= x >> 5;
  x ^= x << 1;
  t = x;
  x = y;
  y = z;
  z = t ^ x ^ y;
  return z;
}

int cmp1(const void *a,const void *b)
{
    node * A=(node *)a;
    node * B=(node *)b;
    return A->cnt - B->cnt;
}
int cmp2(const void *a,const void *b)
{
    node * A=(node *)a;
    node * B=(node *)b;
    return A->pos - B->pos;
}

int main()
{
    unsigned n,m,a,b,c;
    //freopen("in.txt","r",stdin);
    for(int cases=1;scanf("%u%u%u%u%u",&n,&m,&a,&b,&c)==5;cases++)
    {
        x=a,y=b,z=c;
        for(int i=0;i<m;i++){
            scanf("%u",&t[i].cnt);
            t[i].pos=i;
        }
        for(int i=0;i<n;i++)
            num[i]=deal();

        qsort(t,m,sizeof(t[0]),cmp1);
        t[m].pos=m;
        t[m].cnt=n;
        for(int i=m-1;i>=0;i--){
            nth_element(num,num+t[i].cnt,num+t[i+1].cnt);
            t[i].ans=num[t[i].cnt];
        }

        qsort(t,m,sizeof(t[0]),cmp2);
        printf("Case #%d:",cases);
        for(int i=0;i<m;i++)
            printf(" %u",t[i].ans);
        puts("");
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值