[CF576C] Points on Plane - 莫队

Description

给出\(N\)个整点\((x_i,y_i)\),求一个排列\(p_i\),使得\(\sum\limits_{i=2}^N |x_{p_i} - x_{p_{i-1}}| + |y_{p_i} - y_{p_{i-1}}| \leq 2.5 \times 10^9\)

Solution

这个要求让人想到莫队

加上奇偶优化刚好能过

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int siz = 1e3;

signed main()
{
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    struct point
    {
        int x, y, id;
        bool operator<(const point &rhs)
        {
            int block = x / siz;
            int block_rhs = rhs.x / siz;
            if (block != block_rhs)
                return block < block_rhs;
            if (block & 1)
                return y < rhs.y;
            else
                return y > rhs.y;
        }
    };

    vector<point> vec(n);
    for (int i = 0; i < n; i++)
    {
        cin >> vec[i].x >> vec[i].y;
        vec[i].id = i + 1;
    }

    sort(vec.begin(), vec.end());

    for (int i = 0; i < n; i++)
    {
        cout << vec[i].id << " ";
    }
}

相关文章:

  • 2021-08-13
  • 2022-02-26
  • 2022-12-23
  • 2021-09-23
  • 2021-09-24
  • 2021-12-10
  • 2021-11-29
猜你喜欢
  • 2021-06-24
  • 2021-09-06
  • 2021-12-07
  • 2022-01-12
  • 2021-09-29
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案