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.

Input

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.

Output

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.

Examples
input

Copy
8
2 2
1 4
2 3
3 1
3 4
1 1
4 3
1 2
output

Copy
15
input

Copy
5
2 1
1 0
2 0
3 2
0 3
output

Copy
9
Note

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的更多相关文章

  1. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  2. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  3. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  4. Codeforces Round #515 (Div. 3)

    Codeforces Round #515 (Div. 3) #include<bits/stdc++.h> #include<iostream> #include<cs ...

  5. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  6. Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)

    题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...

  7. Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)

    题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...

  8. 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 ...

  9. Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)

    题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...

随机推荐

  1. 「C基础」C运算符的优先级

    一.运算符的优先级表 C 语言的符号众多,由这些符号又组合成了各种各样的运算符.既然是运算符就一定有其特定的优先级,下表就是C 语言运算符的优先级表: 注:同一优先级的运算符,运算次序由结合方向所决定 ...

  2. URL跨项目调用方法,获取返回的json值,并解析

    package com.mshc.util; import java.io.BufferedReader; import java.io.IOException; import java.io.Inp ...

  3. thinkphp 去掉URL 里面的index.php(?s=)

    例如你的原路径是 http://localhost/test/index.php/home/goods/index.html 那么现在的地址是 http://localhost/test/home/g ...

  4. World Wind Java开发之十四——添加WMS地图服务资源(转)

    数据是GIS的核心,没有数据一切无从谈起,Internet上有很多在线WMS地图服务资源,我们可以好好利用这些数据资源,比如天地图.必应地图.NASA.OGC数据服务等等. 在我们国家常用的还是天地图 ...

  5. iOS开发学习之大牛们的博客

    http://blog.csdn.net/iosbird/article/details/51981023 唐巧:http://blog.devtang.com/blog/archives/ 王巍:h ...

  6. 第八章 熟练dom的几个常用方法

    显示“缩略词语” <abbr> 标签指示简称或缩写,比如 <abbr title="World Wide Web Consortium">W3C</a ...

  7. PAT (Advanced Level) Practise - 1094. The Largest Generation (25)

    http://www.patest.cn/contests/pat-a-practise/1094 A family hierarchy is usually presented by a pedig ...

  8. MintLinux

    最近将我的mintlinux进行了一系列外观优化,总算好看了一点

  9. Dart Socket 与Java Socket连接

    -------------------------------------------------------------  Dart    SocketClient----------------- ...

  10. java中如何设置HTTP协议的头信息(header)

    首先,我们先看一下http的头信息到底是什么:HTTP(HyperTextTransferProtocol) 即超文本传输协议,目前网页传输的的通用协议.HTTP协议采用了请求/响应模型,浏览器或其他 ...