HDU-6040 Hints of sd0061(线性找第k小)

大佬的博客链接:http://blog.youkuaiyun.com/qq_31759205/article/details/76154626


传送门:HDU-6040

题意:给出n个数字,由m次查询,第i次查询问第b[i]小的数是多少

题解:线性找第k小

用到一个STL


nth_element(A, A + k, A+n)  

表示在数组A的[0,n-1]中找到第k小的并放在第k个位置,并且前k-1个位置均为小于A[k]的数(要注意的一点就是:第k小中k是从0开始算的)

注意到在找到第k大的时候,前k-1个数均是小于A[k]的,这样我们从大往小枚举,可以减少搜索量



#include<bits/stdc++.h>
using namespace std;
const int MX = 1e7 + 5;
int n, m;
unsigned a, b, c, x, y, z, ans[105];
unsigned A[MX];
struct node
{
    int k, id;
    bool operator < (const node &other) const
    {
        return k < other.k;
    }
}B[105];
unsigned cal()
{
    unsigned t;
    x ^= x << 16;x ^= x >> 5;x ^= x << 1;
    t = x;x = y;y = z;
    z = t ^ x ^ y;
    return z;
}

int main()
{
    int cas = 0;
    while(~scanf("%d%d%u%u%u", &n, &m, &a, &b, &c))
    {
        x = a;
        y = b;
        z = c;
        for(int i = 0; i < m; i++)
            scanf("%d", &B[i].k), B[i].id = i;
        for(int i = 0; i < n; i++)
            A[i] = cal();
        sort(B, B+m);
        B[m].id = m;
        B[m].k = n;
        for (int i = m - 1; i >= 0; i--)
        {
            nth_element(A, A + B[i].k, A + B[i + 1].k);
            ans[B[i].id] = A[B[i].k];
        }
        printf("Case #%d:", ++cas);
        for (int i = 0; i < m; i++) printf(" %u", ans[i]);
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值