Codeforces 1066 F Yet another 2D Walking —— sort+DP

202 篇文章 5 订阅

Maksim walks on a Cartesian plane. Initially, he stands at the point (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), he can go to any of the following points in one move:

(1,0);
(0,1);
(−1,0);
(0,−1).
There are also n distinct key points at this plane. The i-th point is pi=(xi,yi). It is guaranteed that 0≤xi and 0≤yi and there is no key point (0,0).

Let the first level points be such points that max(xi,yi)=1, the second level points be such points that max(xi,yi)=2 and so on. Maksim wants to visit all the key points. But he shouldn’t visit points of level i+1 if he does not visit all the points of level i. He starts visiting the points from the minimum level of point from the given set.

The distance between two points (x1,y1) and (x2,y2) is |x1−x2|+|y1−y2| where |v| is the absolute value of v.

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 n (1≤n≤2⋅105) — the number of key points.

Each of the next n lines contains two integers xi, yi (0≤xi,yi≤109) — x-coordinate of the key point pi and y-coordinate of the key point pi. It is guaranteed that all the points are distinct and the point (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
inputCopy
8
2 2
1 4
2 3
3 1
3 4
1 1
4 3
1 2
outputCopy
15
inputCopy
5
2 1
1 0
2 0
3 2
0 3
outputCopy
9
Note

题意:

给你n个点,你从0,0开始走,每次只能上下左右走一格,每次都要把max(a.x,a.y)相同的走完才能走下一个点,问你最少走几步

题解:

可以看出最短的走法肯定是从这个框的最左端到最下端或者从最下端走到最左端,就像这样:
在这里插入图片描述
那么dp[i][0]表示在max(a.x,a.y)这个框的时候的最左端结束,dp[i][1]表示在最下端结束,然后从上一个地方转移过来即可

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll inf=1e18;
const int N=2e5+5;
ll dp[N][2];
struct node
{
    ll x,y;
    bool operator< (const node& a)const
    {
        if(max(x,y)==max(a.x,a.y))
        {
            if(x==a.x)
                return y>a.y;
            return x<a.x;
        }
        return max(x,y)<max(a.x,a.y);
    }
}a[N];
int dis(node x,node y)
{
    return abs(x.x-y.x)+abs(x.y-y.y);
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%lld%lld",&a[i].x,&a[i].y),dp[i][0]=dp[i][1]=inf;
    sort(a+1,a+1+n);
    /*cout<<endl;
    for(int i=1;i<=n;i++)
        cout<<a[i].x<<" "<<a[i].y<<endl;
    cout<<endl;*/
    int sta=1,fin=1,cnt=1,psta=0,pfin=0;
    while(sta<=n)
    {
        while(max(a[fin].x,a[fin].y)==max(a[sta].x,a[sta].y)&&fin<=n)
            fin++;
        ll d=dis(a[sta],a[fin-1]);
        dp[cnt][1]=min(dp[cnt][1],dp[cnt-1][0]+dis(a[psta],a[sta])+d);
        dp[cnt][1]=min(dp[cnt][1],dp[cnt-1][1]+dis(a[pfin],a[sta])+d);
        dp[cnt][0]=min(dp[cnt][0],dp[cnt-1][0]+dis(a[psta],a[fin-1])+d);
        dp[cnt][0]=min(dp[cnt][0],dp[cnt-1][1]+dis(a[pfin],a[fin-1])+d);
        psta=sta,pfin=fin-1;
        sta=fin;
        cnt++;
    }
    printf("%lld\n",min(dp[cnt-1][1],dp[cnt-1][0]));
    return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值