码迷,mamicode.com
首页 > 其他好文 > 详细

[Test3.3-T3] Sorting (卡常)

时间:2020-03-03 20:50:35      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:tin   getchar   stdout   sig   script   temp   line   amp   fine   

Description:

排序一组1e8的数列并输出

Solution:

数列大小是32为的无符号整形
拆2的8次方基数排序即可

Code:

%:pragma GCC optimize("Ofast,inline,unroll-loops")
%:pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,tune=native")
#include<bits/stdc++.h>
#define ll long long
#define R register
#define I inline
using namespace std;
template<class T>
I void rea(T &x)
{
    char ch=getchar();int f(0);x = 0;
    while(!isdigit(ch)) {f|=ch=='-';ch=getchar();}
    while(isdigit(ch)) {x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
    x = f?-x:x;
}
const int N = 100000005;
unsigned int n, seed, a[N], b[N], bucket[256];
I unsigned int Rand()
{
    seed^=seed<<5, seed^=seed>>7, seed^=seed<<15;
    return seed;
}
I unsigned int get(unsigned int x, unsigned int d)
{
    return ((x>>8*(d-1))&255);
}
I void sorting()
{
    unsigned int *x(a), *y(b);
    for(R int d = 1; d <= 4; ++d)
    {
        for(R int i = 0; i < 256; ++i) bucket[i] = 0;
        for(R int i = 1; i <= n; ++i) bucket[get(x[i], d)]++;
        for(R int i = 1; i < 256; ++i) bucket[i] = bucket[i-1]+bucket[i];
        for(R int i = n; i >= 1; --i) y[bucket[get(x[i], d)]--] = x[i];
        swap(x, y);
    }
}
int main()
{
    freopen("sorting.in","r",stdin);
    freopen("sorting.out","w",stdout);
    rea(n), rea(seed);
    for(R int i = 1; i <= n; ++i) a[i] = Rand();
    sorting();
    unsigned int res = 0;
    for(R int i = 1; i <= n; ++i) res ^= a[i]+Rand();
    cout<<res<<endl;
    return 0;
}

[Test3.3-T3] Sorting (卡常)

标签:tin   getchar   stdout   sig   script   temp   line   amp   fine   

原文地址:https://www.cnblogs.com/heanda/p/12404290.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!