2019牛客暑期多校训练营(第十场)E Hilbert Sort 排序

链接:https://ac.nowcoder.com/acm/contest/890/E
来源:牛客网
 

Hilbert Sort

时间限制:C/C++ 6秒,其他语言12秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld

题目描述

The Hilbert curve, invented by the German mathematician David Hilbert, is one of the most famous fractal curves. The i-th Hilbert curve gives a sequential ordering of the cells in a 2i×2i2^i \times 2^i2i×2i grid.

The formal definition of Hilbert curves is recursive. The i-th Hilbert curve can be formed by connecting four (i-1)-th Hilbert curves, in the following way:
1. partition the 2i×2i2^i \times 2^i2i×2i grid into four 2i−1×2i−12^{i-1} \times 2^{i-1}2i−1×2i−1 quadrants;
2. reflect an (i-1)-th Hilbert curve across the main diagonal (from top left to bottom right), and place it in the upper left quadrant;
3. place two copies of the (i-1)-th Hilbert curve in the lower left and lower right quadrants, respectively;
4. reflect an (i-1)-th Hilbert curve across the secondary diagonal (from top right to bottom left), and place it in the upper right quadrant.
The first three Hilbert curves are shown below.

 

2019牛客暑期多校训练营(第十场)E Hilbert Sort 排序_第1张图片


The Hilbert sort, just as the name implies, is to sort a set of the cells according to the Hilbert curve. A cell is ranked before another if it is encountered earlier when walking along the Hilbert curve. The Hilbert sort is widely used in many areas, because such sort maps a set of 2-dimensional data points into 1-dimensional space, with spatial locality preserved fairly well.

Give you an integer k and a set of cells in the 2k×2k2^k \times 2^k2k×2k grid, please sort them in the order induced from the k-th Hilbert curve.

输入描述:

The first line contains two integers n, k (1≤n≤106,1≤k≤30)(1 \leq n \leq 10^6, 1 \leq k \leq 30)(1≤n≤106,1≤k≤30), the number of cells to sort and the order of the Hilbert curve.

The next n lines, each containing two integers xi,yix_i, y_ixi​,yi​ (1≤xi,yi≤2k)(1 \leq x_i, y_i \leq 2^k)(1≤xi​,yi​≤2k), denoting the cell in the xix_ixi​-th row and the yiy_iyi​-th column. All cells in the input are distinct.

输出描述:

Output the coordinates of the sorted cells in n lines.

示例1

输入

复制

5 2
2 4
2 3
1 1
3 1
4 3

输出

复制

1 1
3 1
4 3
2 4
2 3

示例2

输入

复制

4 1
1 1
1 2
2 1
2 2

输出

复制

1 1
2 1
2 2
1 2

题解:对于每一层分了4部分,如果在同一部分,那就继续判断下去,我就先预处理了一下

#include 
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
struct node {
    int x, y;
    int rank[33];
}p[N];
node now;
int n, k, tmp, rank1, rank2;
int f[32];
int judge(node p, int x) {
    if(p.x / x == 0 && p.y / x == 0) return 1;
    if(p.x / x != 0 && p.y / x == 0) return 2;
    if(p.x / x != 0 && p.y / x != 0) return 3;
    if(p.x / x == 0 && p.y / x != 0) return 4;
     
}
bool cmp(const node &a, const node &b) {
    tmp = k;
    while(tmp) {
        if(a.rank[tmp] != b.rank[tmp]) return a.rank[tmp] < b.rank[tmp];
        tmp--; 
    }
}
int main() {
    f[1] = 1;
    int x, y;
    int flag;
    for(int i = 2; i <= 30; i++) f[i] = f[i - 1] * 2;
    scanf("%d %d", &n, &k);
    for(int i = 1; i <= n; i++) {
        scanf("%d %d", &p[i].x, &p[i].y), p[i].x--, p[i].y--;
        x = p[i].x, y = p[i].y;
        tmp = k;
        while(tmp) {
            now.x = x, now.y = y;
            flag = judge(now, f[tmp]);
            p[i].rank[tmp] = flag;
            if(flag == 1) {
                x = now.y;
                y = now.x;
            } else if(flag == 2) {
                x = now.x - f[tmp];
                y = now.y;
            } else if(flag == 3) {
                x = now.x - f[tmp];
                y = now.y - f[tmp];
            } else {
                now.y -= f[tmp];
                x = f[tmp] - 1 - now.y;
                y = f[tmp] - 1 - now.x;
            }
            tmp--;
        //  cout << tmp + 1 << " " << p[i].rank[tmp + 1] << endl;
        }
         
    }
    sort(p + 1, p + 1 + n, cmp);
    for(int i = 1; i <= n; i++)
        printf("%d %d\n", p[i].x + 1, p[i].y + 1);
    return 0;
}

 

你可能感兴趣的:(预处理,排序)