Educational Codeforces Round 60 (Rated for Div. 2)C. Magic Ship(二分)

C. Magic Ship

题意:

输入 ( x 1 , y 1 ) , ( x 2 , y 2 ) ( 1 e 9 ) (x1,y1),(x2,y2)(1e9) (x1,y1),(x2,y2)(1e9),表示两个点;
输入 n ( 1 e 5 ) n(1e5) n(1e5),接下来输入长度为 n n n的字符串,由 U D L R UDLR UDLR表示风吹的方向,船也可以自己动,问最短在多长时间使船从 ( x 1 , y 1 ) (x1,y1) (x1,y1) ( x 2 , y 2 ) (x2,y2) (x2,y2)

题解:

此题的难点在于很难想到二分,其实从反方向来考虑,很容易就可以得到,当船在 t t t时刻可以到达目的地,那么以后的任意时刻都可以保持(只要往风反方向开就可以了)。

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
int dx,dy;
const int N=2e5+9;
int n;string s;
#define P pair<int,int>
P a[N];
int sx,sy,tx,ty;
bool check(ll x){
    ll res=x%n,y=x/n;
    ll dx,dy;
    dx=1ll*y*a[n].first+a[res].first;
    dy=1ll*y*a[n].second+a[res].second;
    return abs(tx-sx-dx)+abs(ty-sy-dy)<=x;
}
int main(){
 //   freopen("tt.in","r",stdin),freopen("tt.out","w",stdout);
    cin>>sx>>sy>>tx>>ty>>n;
    cin>>s;s=" "+s;
    for(int i=1;i<=n;i++){
        a[i]=a[i-1];
        if(s[i]=='U')a[i].second++;
        if(s[i]=='D')a[i].second--;
        if(s[i]=='L')a[i].first--;
        if(s[i]=='R')a[i].first++;
    }
    ll l=0,r=1e18,mid,ans=-1;
        while(l<=r){
        mid=(l+r)>>1;
        if(check(mid))ans=mid,r=mid-1;
        else l=mid+1;
    }
    cout<<ans<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值