CodeForces 577E Points on Plane(莫队思维题)
题目描述
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.
输入输出样例
5
0 7
8 10
3 4
5 0
9 12
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(莫队思维题)的更多相关文章
- 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 ...
- 清橙A1206.小Z的袜子 && CF 86D(莫队两题)
清橙A1206.小Z的袜子 && CF 86D(莫队两题) 在网上看了一些别人写的关于莫队算法的介绍,我认为,莫队与其说是一种算法,不如说是一种思想,他通过先分块再排序来优化离线查询问 ...
- [2009国家集训队]小Z的袜子(hose)(BZOJ2038+莫队入门题)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2038 题目: 题意:中文题意,大家都懂. 思路:莫队入门题.不过由于要去概率,所以我们假 ...
- wa自动机 的 莫队 刷题记录
洛谷P2709小B的询问 莫队裸题,模板题 莫队就是把询问区间排个序,先按左端点的Pos排序(POS是分块那个数组),pos一样的按右端点排序 代码: #include <bits/stdc++ ...
- codeforces 86D,Powerful array 莫队
传送门:https://codeforces.com/contest/86/problem/D 题意: 给你n个数,m次询问,每次询问问你在区间l,r内每个数字出现的次数的平方于当前这个数的乘积的和 ...
- Codeforces 877F Ann and Books 莫队
转换成前缀和, 预处理一下然后莫队. #include<bits/stdc++.h> #define LL long long #define fi first #define se se ...
- Codeforces 86D Powerful array (莫队算法)
题目链接 Powerful array 给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和. $1<=n,m<=200000, 1<=s< ...
- codeforces 940F 带修改的莫队
F. Machine Learning time limit per test 4 seconds memory limit per test 512 megabytes input standard ...
- CodeForces 86 D Powerful array 莫队
Powerful array 题意:求区间[l, r] 内的数的出现次数的平方 * 该数字. 题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了. 这个题目中发现了一个很神奇的事情,本来数组开 ...
随机推荐
- nginx的下载和安装
安装 下载必要组件 nginx下载地址 http://nginx.org/en/download.html pcre库下载地址,nginx需要[解析正则] http://sourceforge.ne ...
- openStack nova nova valid hosts 优化
scheduler_default_filters=AllHostsFilterallow_resize_to_same_host=Trueallow_migrate_to_same_host=Tru ...
- GPRS 通信
最近使用SIM900A GSM/GPRS模块,做一些简单物联网信息投递. 1.选取何种流量卡 一般来讲,需要带身份证去运营商办理新的电话卡.对于小流量用户,实际上就是办一个最低月租即可,因为套餐会赠送 ...
- jssip音视频及短信开发demo(中文注释完整版)
完整案例demo下载地址:http://download.csdn.net/download/qq_39421580/10214712 <!DOCTYPE html> <html l ...
- 由于安装Android设备驱动异常,ADB无法识别安卓设备的解决方案
体验更优排版请移步原文:http://blog.kwin.wang/programming/android-driver-exception-solution.html 最近换了台新电脑,在Andro ...
- 更改AD域安全策略-密码必须符合复杂性要求
在域环境中,修改域用户密码时,会提示不符合密码策略, 更改"本地安全策略"是不会对域产生任何的作用的. 上图中可以看,这里按钮都是灰色的! 下面这个步骤教你如何找到"域安 ...
- YUV图像合成原理<转>
YUV图像合成原理 引言:在视频监控中最常用的就是图像拼接和字符叠加,25FPS的视频流,如果每隔40MS就从各个通道中取一幅图像来合成,则可以看到一个实时的合成视频.合成的过程也就是原始图像的拼接. ...
- Qt(自适应窗口)
关于窗口布局: 默认控件的大小为最小尺寸: 如果此时右键设置布局时,窗口大小会自动缩放对应大小,不利于调整. 建议窗口控件设置好最小尺寸,便于窗口布局时,控件不会变形,例如: 改变minimumsiz ...
- [iOS]在xcode的iOS虚拟机中对BLE(蓝牙4.0)进行调试
本文档的主要内容可参考苹果官方资料: http://developer.apple.com/library/ios/#technotes/tn2295/_index.html 1. 买一个CSR蓝牙4 ...
- input子系统分析之三:驱动模块
内核版本:3.9.5 本节将以even handler来分析设备的注册和打开的过程,分析之前不妨回顾一下上节介绍的数据结构. 结合前两节分析可知,input子系统分为3层,最上一层是event han ...