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. 【51nod1705】七星剑(成环DP)

    点此看题面 大致题意: 你要把一把剑从0星升至7星,有n颗宝石供你选择,第i颗宝石的价值是c[i],用第i颗宝石将剑从k-1星升至k星的成功率是prob[k][i],而失败后会掉lose[k][i], ...

  2. Nginx+proxy实现简单的负载均衡

    环境说明:操作系统centos6.6 64位web操纵系统是:web1=192.168.10.10(LAMP) web2=192.168.10.11(LNMP),这里只是测试nginx实现负载均衡效果 ...

  3. python剑指offer 合并两个排序的链表

    题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. # -*- coding:utf-8 -*- # class ListNode: # def _ ...

  4. Maven父子模块引入依赖问题

    公共模块如何放到父pom中,而子pom无需再次引入???

  5. Ubuntu下安装pip3和Python的第三方库

    一.Ubuntu原有环境说明 无论是在服务器上面还是在我们自己的电脑上面,当我们成功安装了Ubuntu系统之后,系统一般情况下会自带Python2.x和Python3.x环境.比如我在自己的阿里云服务 ...

  6. SHOI2001 小狗散步

    题目传送门 感觉这题最大的难点是发现它的解法是二分图最大匹配 主人的路线是固定的,对于每一段的路线,我们可以枚举小狗想去的景点,如果时间够,我们就将这段路线的起点和小狗想去的点连起来 这样就形成了一个 ...

  7. 非负随机变量X满足:(1-F(x)) 在 (0,+∞)积分为= E[X]

    机器学习作业的第一题最后一问卡住了,要证明 非负随机变量X满足 1 - F(X) 在 (0,+∞)上的积分是E(X);  关键的地方是积分换序,看原来的答案真的很难理解,画个图一下就懂了,码个链接,便 ...

  8. Python学习——numpy.random

    numpy.random.rand numpy.random模块作用是生成随机数,其中numpy.random.rand(d0, d1, ..., dn):生成一个[0,1)之间的随机浮点数或N维浮点 ...

  9. Thinkphp5封装上传图片

    <?php namespace app\api\controller; use think\Controller; use think\Db; class Plus extends Base{ ...

  10. MySQL 时间戳与日期互相转换

    MySQL 时间戳与日期互相转换 1.时间戳转换成日期 函数:FROM_UNIXTIME() ,'%Y年%m月%d日') 结果为:2015年04月15日 2.把日期转换为时间戳,和 FROM_UNIX ...