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. CUDA:Supercomputing for the Masses (用于大量数据的超级计算)-第三节

    原文链接 第三节:错误处理和全局内存性能局限 恭喜!通过对CUDA(Compute Unified DeviceArchitecture,即计算统一设备架构的首字母缩写)系列文章第一节和第二节,您现在 ...

  2. ES6学习(二):函数的扩展

    chapter07 函数的扩展 7.1 函数默认值 7.1.1 参数默认值简介 传统做法的弊端(||):如果传入的参数相等于(==)false的话,仍会被设为默认值,需要多加入一个if判断,比较麻烦. ...

  3. MySQL - EXISTS 和 NOT EXISTS

    语法规则:   SELECT * FROM tableName t WHERE 1 = 1 AND 2 = 2 AND EXISTS (SELECT * FROM tableName t2 WHERE ...

  4. MySQL存储引擎MyISAM与InnoDB的区别比较

    使用MySQL当然会接触到MySQL的存储引擎,在新建数据库和新建数据表的时候都会看到. MySQL默认的存储引擎是MyISAM,其他常用的就是InnoDB了. 至于到底用哪种存储引擎比较好?这个问题 ...

  5. INNODB insert query end state

    innodb_flush_log_at_trx_commit=2 innodb_flush_method=O_DIRECT (for non-windows machine) innodb_buffe ...

  6. 42.VUE学习之--组件之子组件使用$on与$emit事件触发父组件实现购物车功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 数学算法:poweroj1026-丑数(根据固定倍数得到从小到大的序列)

    题目: 1026: 丑数 Time Limit: 1000 MS Memory Limit: 65536 KB Total Submit: 257 Accepted: 112 Page View: 1 ...

  8. C语言中可变参数的使用

    在C语言程序编写中我们使用最多的函数一定包括printf以及很多类似的变形体.这个函数包含在C库函数中,定义为 int printf( const char* format, ...); 除了一个格式 ...

  9. 序列化模块--json模块--pickle模块-shelve模块

    什么叫序列化? 序列化是指把内存里的数据类型转变成字符串,以使其能存储到硬盘或通过网络传播到远程,因为硬盘或网络传输时只能接受bytes 例: 把内存数据 转成字符 # data ={# 'roles ...

  10. Python虚拟机类机制之自定义class(四)

    用户自定义class 在本章中,我们将研究对用户自定义class的剖析,在demo1.py中,我们将研究单个class的实现,所以在这里并没有关于继承及多态的讨论.然而在demo1.py中,我们看到了 ...