[CF576C] Points on Plane - 莫队
Description
给出(N)个整点((x_i,y_i)),求一个排列(p_i),使得(sumlimits_{i=2}^N |x_{p_i} - x_{p_{i-1}}| + |y_{p_i} - y_{p_{i-1}}| leq 2.5 imes 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 << " ";
}
}