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数相等的个数. ...
随机推荐
- Object.keys 函数 (JavaScript)
Object.keys 函数 (JavaScript) 返回对象的可枚举属性和方法的名称. 在实际开发中,我们有时需要知道对象的所有属性,原生js给我们提供了一个很好的方法:Object.keys() ...
- UVA Live Archive 4490 Help Bubu(状压dp)
难点在于状态设计,从左向右一本书一本书的考虑,每本书的决策有两种拿走或者留下, 对于拿走后的书,之后要放回,但是决策过程中不知道到往哪里放, 虽然前面的书的种类确定,可能是往后面放更优,而后面的书的类 ...
- bzoj2600 [Ioi2011]ricehub
Description 乡间有一条笔直而长的路称为“米道”.沿着这条米道上 R 块稻田,每块稻田的坐标均为一个 1 到 L 之间(含 1 和 L)的整数.这些稻田按照坐标以不减的顺序给出,即对于 0 ...
- 【洛谷1486】[NOI2004] 郁闷的出纳员(Splay的小运用)
点此看题面 大致题意: 你是一个公司的出纳员,现在有\(n\)个操作,操作有4种:新来一个员工.增加全体员工工资.减少全体员工工资.查询第\(k\)多的工资.若一个员工的工资在某一时刻低于合同上的工资 ...
- ReentrantReadWriteLock的使用
ReentrantReadWriteLock的规则是: 多线程情况下:读-写互斥.写-读互斥.写-写互斥.读-读共享 验证“读-写互斥.写-读互斥.写-写互斥.读-读共享” //单个线程 读-读 不互 ...
- ifup/ifdown ethX 和 ifconfig ehtX up/down的区别
相同点:[启用]和[禁止]网卡 ifup ethX 和 ifconfig ethX up 用来启用网卡设备 ifdown ethX 和 ifconfig ethX ...
- 记Tea使用中遇到的问题及解决过程
学习Markdown时,在小众软件看到一个叫Tea的软件.UI设计是简约风格:"所见即所得"的Markdown:支持插件等原因让我选择去尝试这杯"茶". 最近一 ...
- GPU并行编程:内核及函数的实现
原文链接 回想一下我们之前在设备上使用“kernelFunction<<<1,1>>>(..)”执行一个函数的代码,我在那里还曾说过后面会细说,本文就详细介绍一下参 ...
- red hat的防火墙怎么关闭
查看是否开启: service iptables status 关闭方法: service iptables stop 永远关闭: Ntsysv 把iptables前的*号去掉. 查看SELinux状 ...
- 对mysql快速批量修改,查重
更新UPDATE mytable SET myfield = CASE id WHEN 1 THEN 'value' WHEN 2 THEN 'value' WHEN 3 THEN 'value' E ...