CodeForces Round #515 DIv.3 F. Yet another 2D Walking
http://codeforces.com/contest/1066/problem/F
Maksim walks on a Cartesian plane. Initially, he stands at the point (0,0)(0,0) and in one move he can go to any of four adjacent points (left, right, up, down). For example, if Maksim is currently at the point (0,0)(0,0), he can go to any of the following points in one move:
- (1,0)(1,0);
- (0,1)(0,1);
- (−1,0)(−1,0);
- (0,−1)(0,−1).
There are also nn distinct key points at this plane. The ii-th point is pi=(xi,yi)pi=(xi,yi). It is guaranteed that 0≤xi0≤xi and 0≤yi0≤yi and there is no key point (0,0)(0,0).
Let the first level points be such points that max(xi,yi)=1max(xi,yi)=1, the second level points be such points that max(xi,yi)=2max(xi,yi)=2 and so on. Maksim wants to visit all the key points. But he shouldn't visit points of level i+1i+1 if he does not visit all the points of level ii. He starts visiting the points from the minimum level of point from the given set.
The distance between two points (x1,y1)(x1,y1) and (x2,y2)(x2,y2) is |x1−x2|+|y1−y2||x1−x2|+|y1−y2| where |v||v| is the absolute value of vv.
Maksim wants to visit all the key points in such a way that the total distance he walks will be minimum possible. Your task is to find this distance.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of key points.
Each of the next nn lines contains two integers xixi, yiyi (0≤xi,yi≤1090≤xi,yi≤109) — xx-coordinate of the key point pipi and yy-coordinate of the key point pipi. It is guaranteed that all the points are distinct and the point (0,0)(0,0) is not in this set.
Print one integer — the minimum possible total distance Maksim has to travel if he needs to visit all key points in a way described above.
8
2 2
1 4
2 3
3 1
3 4
1 1
4 3
1 2
15
5
2 1
1 0
2 0
3 2
0 3
9
The picture corresponding to the first example:
There is one of the possible answers of length 1515.
The picture corresponding to the second example:
There is one of the possible answers of length 99.
代码:
#include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int maxn = 200010;
int N, len = 1, t = 1;
ll dp[maxn][5]; struct Node{
int x;
int y;
}node[maxn], st;
vector<Node> v[maxn]; bool cmp(const Node& a, const Node& b) {
if(max(a.x, a.y) == max(b.x, b.y))
return a.x == b.x ? a.y < b.y : a.x > b.x;
return max(a.x, a.y) < max(b.x, b.y);
} ll solve(ll x) {
return x >= 0 ? x : -x;
} ll Solve() {
dp[1][0] = dp[1][1] = 0;
for(int i = 1; i <= len; i ++) {
ll dis00 = solve(v[i - 1][0].x - v[i][0].x) + solve(v[i - 1][0].y - v[i][0].y);
ll dis01 = solve(v[i - 1][0].x - v[i][1].x) + solve(v[i - 1][0].y - v[i][1].y);
ll dis10 = solve(v[i - 1][1].x - v[i][0].x) + solve(v[i - 1][1].y - v[i][0].y);
ll dis11 = solve(v[i - 1][1].x - v[i][1].x) + solve(v[i - 1][1].y - v[i][1].y);
dp[i][0] = min(dp[i - 1][0] + dis10, dp[i - 1][1] + dis00);
dp[i][1] = min(dp[i - 1][1] + dis01, dp[i - 1][0] + dis11);
}
return min(dp[len][0], dp[len][1]);
} int main() {
scanf("%d", &N);
for(int i = 1; i <= N; i ++)
scanf("%d%d", &node[i].x, &node[i].y); st.x = 0, st.y = 0;
v[0].push_back(st);
sort(node + 1, node + 1 + N, cmp); ll ans = 0;
while(t <= N) {
v[len].push_back(node[t]); int now = t;
while(t < N && max(node[t + 1].x, node[t + 1].y) == max(node[now].x, node[now].y))
t ++;
v[len ++].push_back(node[t]); ans += (solve((ll)(node[t].x - node[now].x)) + solve((ll)(node[t].y - node[now].y)));
t ++;
}
len --;
ans += Solve();
printf("%lld\n", ans);
//printf("%d\n", len);
return 0;
}
CodeForces Round #515 DIv.3 F. Yet another 2D Walking的更多相关文章
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces Round #515 (Div. 3)
Codeforces Round #515 (Div. 3) #include<bits/stdc++.h> #include<iostream> #include<cs ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)
题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
- Codeforces Round #325 (Div. 2) F. Lizard Era: Beginning meet in the mid
F. Lizard Era: Beginning Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)
题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...
随机推荐
- 工作流性能优化(敢问activiti有扩展性?)(2)
2015/4/17 粗略看了activiti的sql的,在ativity engine包里边: 没什么头绪,先用excel记录数据量少的时候本机的性能情况: 不打印hibernate的sql:一刷 ...
- 2018.2.7 css 的一些方法盒子模型
css 的一些方法 1.盒模型代码简写 盒模型的外边距(margin).内边距(padding)和边框(border)设置上下左右四个方向的边距是按照顺时针方向设置的:上右下左.具体应用在margin ...
- axiospost请求向后端提交数据
Axios向后端提交数据容易接收不到原因是传参方式是request payload,参数格式是json,而并非用的是form传参,所以在后台用接收form数据的方式接收参数就接收不到了.post表单请 ...
- vitrual box安装centos时一直黑屏的解决办法
趁着清明节没事,昨天看了mysql性能优化后,想装个linux系统学习下,linux一直是我的短板...之前是在vmware上安装ubuntu,买了新电脑后,听过virtual box相比vmware ...
- Java - 静态方法不具有多态性
class A1 { public static void f() { System.out.println("A1.f()"); }}class A2 extends A1 { ...
- [Wolfgang Mauerer] 深入linux 内核架构 第二章 进程管理与调度【未完】
作为Linux开发爱好者,从事linux 开发有三年多时间.做过bsp移植,熟悉u-boot代码执行流程:看过几遍<linux 设备驱动程序开发>,分析过kernel启动流程,写过驱动, ...
- 通过sudo提权方式控制公司人员权限
#通过visudo编辑/etc/sudoers Runas_Alias OP = root #定义使用sudo的时候以哪个用户执行命令,一般都是使用root #命令别名 Cmnd_Alias NETW ...
- 网站动态加载JS脚本
Demo_1 function loadJS(url, fn) { var ss = document.getElementsByName('script'), loaded = false; for ...
- LeetCode#5 两个排序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 ...
- lnmp启用pathinfo并隐藏index.php
编辑如下区段: location ~ [^/]\.php(/|$) { # comment try_files $uri =404; to enable pathinfo try_files $uri ...