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.

 


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 <bits/stdc++.h>
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;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值