hdu6040 Hints of sd0061 [nth_element use]

Keywords: PHP less

Topic link: http://acm.hdu.edu.cn/showproblem.php?pid=6040
Title: Give you a function, a sequence of length n is generated by this function, now give you a sequence of length m, but you output a length of m, each of which represents the small element b[i]+1 i n a sequence.
Resolution: The first reaction must be to sort a, but the complexity of fast queuing is nlogn, and the scope of n is not allowed, and then found that m is very small, so starting from b sequence, should be a good choice. Looking at someone else's blog found a very interesting STL, nth_element(arr.begin(),arr+n, arr. end (), after calling this function, will ensure that the elements before a[n] are less than. The average complexity of the function is linear. If the sequence of b is executed from large to small, the length will be reduced continuously. So the time should be consistent in theory.

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e7+100;
struct node
{
    int id,x;
    bool operator < (const node &b)const
    {
        return x>b.x;
    }
}b[maxn];
vector<pair<int,unsigned> >ans;
unsigned a[maxn];
unsigned x,y,z,A,B,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;
}
int main(void)
{
    int n,m,case_t = 1;
    while(~scanf("%d %d %u %u %u",&n,&m,&A,&B,&C))
    {
        for(int i=0;i<m;i++)
        {
            scanf("%d",&b[i].x);
            b[i].id = i;
        }
        x = A,y = B,z = C;
        for(int i=0;i<n;i++)
            a[i] = rng61();
        sort(b,b+m);
        ans.clear();
        int last = n;
        for(int i=0;i<m;i++)
        {
            int now = b[i].x;
            nth_element(a,a+now,a+last);
            last = now;
            ans.push_back(make_pair(b[i].id,a[now]));
        }
        sort(ans.begin(),ans.end());
        printf("Case #%d:",case_t++);
        for(int i=0;i<m;i++)
            printf(" %u",ans[i].second);
        puts("");
    }
    return 0;
}

Posted by zhangy on Sat, 09 Feb 2019 08:06:17 -0800