题目描述

On a plane are nn points ( x_{i}xi​ , y_{i}yi​ ) with integer coordinates between 00 and 10^{6}106 . The distance between the two points with numbers aa and bb is said to be the following value:  (the distance calculated by such formula is called Manhattan distance).

We call a hamiltonian path to be some permutation p_{i}pi​ of numbers from 11 to nn . We say that the length of this path is value .

Find some hamiltonian path with a length of no more than 25×10^{8}25×108 . Note that you do not have to minimize the path length.

输入输出格式

输入格式:

 

The first line contains integer nn ( 1<=n<=10^{6}1<=n<=106 ).

The i+1i+1 -th line contains the coordinates of the ii -th point: x_{i}xi​ and y_{i}yi​ ( 0<=x_{i},y_{i}<=10^{6}0<=xi​,yi​<=106 ).

It is guaranteed that no two points coincide.

 

输出格式:

 

Print the permutation of numbers p_{i}pi​ from 11 to nn — the sought Hamiltonian path. The permutation must meet the inequality .

If there are multiple possible answers, print any of them.

It is guaranteed that the answer exists.

 

输入输出样例

输入样例#1:

5
0 7
8 10
3 4
5 0
9 12
输出样例#1:

4 3 1 2 5

说明

In the sample test the total distance is:

(|5-3|+|0-4|)+(|3-0|+|4-7|)+(|0-8|+|7-10|)+(|8-9|+|10-12|)=2+4+3+3+8+3+1+2=26(∣5−3∣+∣0−4∣)+(∣3−0∣+∣4−7∣)+(∣0−8∣+∣7−10∣)+(∣8−9∣+∣10−12∣)=2+4+3+3+8+3+1+2=26

题意:定义曼哈顿距离为两点之间x坐标差的绝对值与y坐标差的绝对值,在定义哈密顿路径为所有相邻两点间的曼哈顿距离之和,给出一些点的xy坐标,求一个点排列使哈密顿路径小于25*1e8

题解:

首先看到点的xy坐标均在1e6以内,然后如果按照直接优先x再y的顺序排序,只需要一组x坐标1-5e5的数据,每个x坐标的y坐标为1e6和0,然后距离就被卡到了5e11。

虽然上面的思想有错误,但是是有借鉴意义的,如果将哈密顿路径理解为复杂度,长度理解为变量,这显然是n^2的,然后你会想到一些优化的方法,比如说莫队。

然后就可以根据莫队的思想将x坐标分块,分成0-999,1000-1999……的1000块,每块里按照y从小到大的顺序排序,这样子块内y是单调递增的,最多增大1e6,x就算上下乱跳,也最多变化1e3*1e3=1e6,总变化最多2e9

但是还是有点锅,就是块与块之间切换的时候,如果是从最大y切到下一坐标最小y,最多要跳1e6,总变化会多增加1e9

所以按照一个块y递增,下一个块y递减的顺序排列,这样子就稳了

代码如下:

#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; struct node
{
int x,y,kd;
}; vector<node> g[];
int n; int cmp1(node x,node y)
{
return x.y<y.y;
} int cmp2(node x,node y)
{
return x.y>y.y;
} int main()
{
node tmp;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d%d",&tmp.x,&tmp.y);
tmp.kd=i;
g[tmp.x/].push_back(tmp);
}
for(int i=;i<=;i++)
{
if(i&)
{
sort(g[i].begin(),g[i].end(),cmp1);
}
else
{
sort(g[i].begin(),g[i].end(),cmp2);
}
}
for(int i=;i<=;i++)
{
for(int j=;j<g[i].size();j++)
{
printf("%d ",g[i][j].kd);
}
}
}

CodeForces 577E Points on Plane(莫队思维题)的更多相关文章

  1. CODEFORCES 340 XOR and Favorite Number 莫队模板题

    原来我直接学的是假的莫队 原题: Bob has a favorite number k and ai of length n. Now he asks you to answer m queries ...

  2. 清橙A1206.小Z的袜子 && CF 86D(莫队两题)

    清橙A1206.小Z的袜子 && CF 86D(莫队两题) 在网上看了一些别人写的关于莫队算法的介绍,我认为,莫队与其说是一种算法,不如说是一种思想,他通过先分块再排序来优化离线查询问 ...

  3. [2009国家集训队]小Z的袜子(hose)(BZOJ2038+莫队入门题)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2038 题目: 题意:中文题意,大家都懂. 思路:莫队入门题.不过由于要去概率,所以我们假 ...

  4. wa自动机 的 莫队 刷题记录

    洛谷P2709小B的询问 莫队裸题,模板题 莫队就是把询问区间排个序,先按左端点的Pos排序(POS是分块那个数组),pos一样的按右端点排序 代码: #include <bits/stdc++ ...

  5. codeforces 86D,Powerful array 莫队

    传送门:https://codeforces.com/contest/86/problem/D 题意: 给你n个数,m次询问,每次询问问你在区间l,r内每个数字出现的次数的平方于当前这个数的乘积的和 ...

  6. Codeforces 877F Ann and Books 莫队

    转换成前缀和, 预处理一下然后莫队. #include<bits/stdc++.h> #define LL long long #define fi first #define se se ...

  7. Codeforces 86D Powerful array (莫队算法)

    题目链接 Powerful array 给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和. $1<=n,m<=200000,   1<=s< ...

  8. codeforces 940F 带修改的莫队

    F. Machine Learning time limit per test 4 seconds memory limit per test 512 megabytes input standard ...

  9. CodeForces 86 D Powerful array 莫队

    Powerful array 题意:求区间[l, r] 内的数的出现次数的平方 * 该数字. 题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了. 这个题目中发现了一个很神奇的事情,本来数组开 ...

随机推荐

  1. Spring中application*的使用

    ApplicationAware 加载Spring配置文件时,如果Spring配置文件中所定义的Bean类实现了ApplicationContextAware 接口,那么在加载Spring配置文件时, ...

  2. 第一章 : Android Studio 介绍 [Learn Android Studio 汉化教程]

    摘自:http://ask.android-studio.org/?/question/789,为便于学习重新整理.. 本章将引导您完成安装和设置开发环境,然后你就可以跟随本书的例子和课程学习. 首先 ...

  3. scrapy框架的持久化存储

    一 . 基于终端指令的持久化存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的形式写入指定格式的文件中进行持久化操作. 执行输出指定格式进行存 ...

  4. 03-24 Winform图表Chart

    图表主要分为以下几个部分: 1.Annotations--图形注解集合, 2.ChartAreas--图表区域集合, 3.Legends--图例集合, 4.Series--图表序列集合(即图表数据对象 ...

  5. GNU Bash Shell 编程图解笔记

    bash,Unix shell的一种,在1987年由布莱恩·福克斯为了GNU计划而编写.1989年发布第一个正式版本,原先是计划用在GNU操作系统上,但能运行于大多数类Unix系统的操作系统之上,包括 ...

  6. 利用 Django admin 完成更多任务(转)

    利用 Django admin 完成更多任务   Django admin Django 为未来的开发人员提供了许多功能:一个成熟的标准库,一个活跃的用户社区,以及 Python 语言的所有好处.虽然 ...

  7. sublime +react+es6开发环境

    Babel Sublime3才有的插件,支持ES6.JSX语法高亮. 菜单->View->Syntax->Open all with current extension as...- ...

  8. Linux下git使用详解1

    1. git使用第一步:安装git $ sudo apt-get install git-core #ubuntu系统下使用apt-get $ yum install git-core #译者注,在r ...

  9. a different object with the same identifier value was already associated with the session解决方案

    org.springframework.orm.hibernate3.HibernateSystemException: a different ]; nested exception ] at or ...

  10. 移动端web页面input限制只能输入数字

    <input type="number" pattern="[0-9]*" /> 如上所示,在安卓端设置input类型为number,可限制键盘只输 ...