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] 内的数的出现次数的平方 * 该数字. 题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了. 这个题目中发现了一个很神奇的事情,本来数组开 ...
随机推荐
- Linux的bond模式绑定及模式区别
[Linux的bond模式配置] 原理: 多块网卡虚拟成一张,实现冗余:多张网卡对外显示一张,具有同一个IP: 工作在网卡是混杂模式的情况下: 对于多物理网卡的 Bond 网卡而言,其中一块物理网卡会 ...
- Storm开发——环境配置部署
配置开发环境:http://storm.apache.org/releases/current/Setting-up-development-environment.html 开发环境定义: Stor ...
- 第六章:Reminders实验:第二部分[Learn Android Studio 汉化教程]
Learn Android Studio 汉化教程 Reminders Lab: Part 2 This chapter covers capturing user input through the ...
- sql查询语句常用例子
1.查找与jams在同一个单位的员工姓名.性别.部门和职称:select emp_no, emp_name, dept, title from employee where emp_name< ...
- 第二章ARP——地址解析协议
本章我们要讨论的问题是只对 T C P / I P协议簇有意义的I P地址.数据链路如以太网或令牌环网都有自己的寻址机制(常常为 48 bit地址),这是使用数据链路的任何网络层都必须遵从的.一个网络 ...
- RHCE7 学习里程-3基本命令
一.centos7 基本命令 #创建文件 touch a.b #创建文件夹 mkdir abc #删除文件 rm -f a.b #删除空文件夹 rm -rf abc #重命名文件 mv 源文件 新文 ...
- OrderedDict 有序字典以及读取json串时如何保持原有顺序
1. OrderedDict 有序字典 OrderedDict是dict的子类,它记住了内容添加的顺序.比较时,OrderedDict要内容和顺序完全相同才会视为相等 import collectio ...
- Eclipse 常用插件地址大全
安装方式: 使用Eclipse 的自动升级功能,菜单栏选[ Help ]→[ install new Software] 点击[Add]按钮,在“ Name ”中填入“ name (填写一个自己喜欢 ...
- vertex shader(1)
Vertex shader Architecture: 所有在vertex shader中的数据都用128-bit的quad-floats表示(4x32-bit). vertex shader线性地执 ...
- 退出telnet 命令
很多时候 telnet 完就无法退出了,ctrl+c 有时也无法退出 后来找到了正确的命令 ctrl+] 然后在telnet 命令行输入 quit 就可以退出了