Codeforces 1117C Magic Ship (二分)
题意:
船在一个坐标,目的地在一个坐标,每天会有一个风向将船刮一个单位,船也可以移动一个单位或不动,问最少几天可以到目的地
思路:
二分天数,对于第k天
可以分解成船先被吹了k天,到达坐标(x1+sumx[k%n]+k/n*sumx[n], y1+sumy[k%n]+k/n*sumy[n])
然后船在无风的情况下自己走k天是不是能到目的地就行了
注意二分的上限,如果能走到的情况下船可能每轮风只能移动一个有效单位,所以上限应该是1e9*1e5
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
//#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = ;
const int maxn = 2e6+;
const int maxm = 2e6+;
const int inf = 0x3f3f3f3f;
//const db pi = acos(-1.0); ll x1, x2;
ll y1, y2;
ll n;
char a[maxn];
ll sumx[maxn];
ll sumy[maxn];
bool ck(ll k){
ll x = x1;
ll y = y1;
x += k/n*sumx[n] + sumx[k%n];
y += k/n*sumy[n] + sumy[k%n];
ll d = abs(x-x2)+abs(y-y2);
if(d<=k)return true;
return false;
}
int main() {
scanf("%lld %lld", &x1,&y1);
scanf("%lld %lld", &x2,&y2);
scanf("%lld", &n);
scanf("%s",a+);
ll l = , r = 1e15;
for(int i = ; i <= n; i++){
sumx[i] = sumx[i-];
sumy[i] = sumy[i-];
if(a[i]=='U'){
sumy[i]++;
}
if(a[i]=='D'){
sumy[i]--;
}
if(a[i]=='L'){
sumx[i]--;
}
if(a[i]=='R'){
sumx[i]++;
}
}
ll ans = -;
while(l <= r){
ll mid = (l+r)/;
//printf("%lld %lld %lld\n", l, r, mid);
if(ck(mid)){
r = mid-;
ans = mid;
}
else{
l = mid+;
}
}
printf("%lld", ans);
return ; }
/*
10
59 -17 5 67 87 50 -71 54 27 -10 */
Codeforces 1117C Magic Ship (二分)的更多相关文章
- CodeForces 1117C Magic Ship (循环节+二分答案)
<题目链接> 题目大意: 给定起点和终点,某艘船想从起点走到终点,但是海面上会周期性的刮风,船在任何时候都能够向四个方向走,或者选择不走,船的真正行走路线是船的行走和风的走向叠加的,求船从 ...
- CodeForces 670D2 Magic Powder 二分
D2. Magic Powder - 2 The term of this problem is the same as the previous one, the only exception — ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- C. Magic Ship cf 二分
C. Magic Ship time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- CF1117C Magic Ship
CF1117C Magic Ship 考虑到答案具单调性(若第 \(i\) 天能到达目的点,第 \(i+1\) 天只需向风向相反的方向航行),可以二分答案. 现在要考虑给出一个天数 \(m\) ,问 ...
- 题解-Magic Ship
Magic Ship 你在 \((x_1,y_1)\),要到点 \((x_2,y_2)\).风向周期为 \(n\),一个字符串 \(s\{n\}\) 表示风向(每轮上下左右),每轮你都会被风向吹走一格 ...
- [Codeforces 1199C]MP3(离散化+二分答案)
[Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...
- 【Codeforces 1117C】Magic Ship
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 我们可以把这个行船的过程分解成两个过程 1.船经过时间t被风吹到了某个地方 2.船用这t时间尝试到达终点(x2,y2) 会发现如果时间t能最终 ...
- C. Magic Ship (思维+二分)
https://codeforces.com/contest/1117/problem/C 你是一个船长.最初你在点 (x1,y1) (显然,大海上的所有点都可以用平面直角坐标描述),你想去点 (x2 ...
随机推荐
- 关于MySQL幻读的实验
该实验基于 CentOS 7 + MySQL 5.7 进行 打开两个窗口连接到MySQL 第一个连接的事务我们命名为 T1 第二个连接的事务我们命名为 T2 T2 发生在 T1 的 O1 操作结束以 ...
- [推荐]icheck-bootstrap(漂亮的ckeckbox/radiobox)
适用于Twitter Bootstrap框架的纯CSS样式的复选框/单选框按钮的插件. GitHub:https://github.com/bantikyan/icheck-bootstrap 如果你 ...
- 6.7 Mapreduce作业流JobControl和Oozie
1.1 Mapreduce作业流JobControl和Oozie 更复杂的任务,需要多个mapreduce作业,形成作业流,而不是增加map和reduce的复杂度.复杂问题,可以用高级语言pig.h ...
- 基于Antd框架的通信与交互
基于Antd框架的通信与交互 1.与用户交互 对于input输入框,在于用户交互的过程中,用户在输入任何东西时,都会引起该组件的onChange事件(如果写有这个方法的话). <FormItem ...
- ipaclient 4.5.4 requires jinja2, which is not installed. rtslib-fb 2.1.63 has requirement pyudev>=0.16.1, but you'll have pyudev 0.15 which is incompatible. ipapython 4.5.4 has requirement dnspython>=
- 【转】c#中数组赋值方法
C#中数组复制有多种方法,数组间的复制 ,,,};int [] alias = pins; 这里出了错误,也是错误的根源,以上代码并没有出错,但是根本不是复制,因为pins和alias都是引用,存在于 ...
- 【转】在MyEclipse 8.6上搭建Android开发环境
内容导航 第 1 页:基本环境准备 第 2 页:下载Android SDK 第 3 页:配置SDK环境变量 第 4 页:给MyEclipse安装ADT插件 第 5 页:配置MyEclipse 第 6 ...
- 中国传统色JSON数据
提取自中国色/colors.json 解析后存入数据库,导出插入语句chinese_colors.sql,提取码:5inu [ { "CMYK": [ 4, 5, 18, 0 ], ...
- dp - LIS
某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于 ...
- springcloud复习1
1.SpringCloud是什么?SpringCloud=分布式微服务架构下的一站式解决方案,是各个微服务架构落地技术的集合体,俗称微服务全家桶. 2.SpringCloud和SpringBoot是什 ...