Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

Time Limit: 2000 mSec

Problem Description

Input

Output

The only line should contain the minimal number of days required for the ship to reach the point (x2,y2)(x2,y2).

If it's impossible then print "-1".

Sample Input

0 0
4 6
3
UUU

Sample Output

5

题解:第一感觉是bfs,然后大概背包一下之类的,但是数据范围不允许呀,做出这个题的关键点就是注意到运动独立性(呵呵),和如果在第x天能到,那么对于y >= x的都能到,因此就可以二分,固定了天数之后,风吹的距离就是固定的,并且可以用前缀和O(1)求,然后就是判断风吹到的点和目标点的哈密顿距离是否<=天数即可。

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define REP(i, n) for (int i = 1; i <= (n); i++)
  6. #define sqr(x) ((x) * (x))
  7.  
  8. const int maxn = + ;
  9. const int maxm = + ;
  10. const int maxs = + ;
  11.  
  12. typedef long long LL;
  13. typedef pair<int, int> pii;
  14. typedef pair<double, double> pdd;
  15.  
  16. const LL unit = 1LL;
  17. const int INF = 0x3f3f3f3f;
  18. const double eps = 1e-;
  19. const double inf = 1e15;
  20. const double pi = acos(-1.0);
  21. const int SIZE = + ;
  22. const LL MOD = ;
  23.  
  24. LL sx, sy, ex, ey;
  25. LL x[maxn], y[maxn];
  26. LL n;
  27. string str;
  28.  
  29. bool Judge(LL lim)
  30. {
  31. LL m = lim / n, remain = lim % n;
  32. LL xx = sx + m * x[n] + x[remain];
  33. LL yy = sy + m * y[n] + y[remain];
  34. if (abs(xx - ex) + abs(yy - ey) <= lim)
  35. return true;
  36. return false;
  37. }
  38.  
  39. int main()
  40. {
  41. ios::sync_with_stdio(false);
  42. cin.tie();
  43. //freopen("input.txt", "r", stdin);
  44. //freopen("output.txt", "w", stdout);
  45. cin >> sx >> sy >> ex >> ey;
  46. cin >> n;
  47. cin >> str;
  48. LL len = str.size();
  49. for (LL i = ; i < len; i++)
  50. {
  51. if (str[i] == 'U')
  52. {
  53. x[i + ] = x[i];
  54. y[i + ] = y[i] + ;
  55. }
  56. else if (str[i] == 'D')
  57. {
  58. x[i + ] = x[i];
  59. y[i + ] = y[i] - ;
  60. }
  61. else if (str[i] == 'L')
  62. {
  63. x[i + ] = x[i] - ;
  64. y[i + ] = y[i];
  65. }
  66. else
  67. {
  68. x[i + ] = x[i] + ;
  69. y[i + ] = y[i];
  70. }
  71. }
  72. LL le = , ri = LLONG_MAX / ;
  73. LL ans = -;
  74. while (le <= ri)
  75. {
  76. LL mid = (le + ri) / ;
  77. if (Judge(mid))
  78. {
  79. ri = mid - ;
  80. ans = mid;
  81. }
  82. else
  83. {
  84. le = mid + ;
  85. }
  86. }
  87. cout << ans << endl;
  88.  
  89. return ;
  90. }

Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship的更多相关文章

  1. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  2. Educational Codeforces Round 60 (Rated for Div. 2) D. Magic Gems(矩阵快速幂)

    题目传送门 题意: 一个魔法水晶可以分裂成m个水晶,求放满n个水晶的方案数(mol1e9+7) 思路: 线性dp,dp[i]=dp[i]+dp[i-m]; 由于n到1e18,所以要用到矩阵快速幂优化 ...

  3. Educational Codeforces Round 60 (Rated for Div. 2) 题解

    Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...

  4. Educational Codeforces Round 60 (Rated for Div. 2)

    A. Best Subsegment 题意 找 连续区间的平均值  满足最大情况下的最长长度 思路:就是看有几个连续的最大值 #include<bits/stdc++.h> using n ...

  5. Educational Codeforces Round 60 (Rated for Div. 2)D(思维,DP,快速幂)

    #include <bits/stdc++.h>using namespace std;const long long mod = 1e9+7;unordered_map<long ...

  6. Educational Codeforces Round 60 (Rated for Div. 2)E(思维,哈希,字符串,交互)

    #include <bits/stdc++.h>using namespace std;int main(){ string t; cin>>t; int n=t.size() ...

  7. Educational Codeforces Round 60 (Rated for Div. 2) 即Codeforces Round 1117 C题 Magic Ship

    time limit per test 2 second memory limit per test 256 megabytes input standard inputoutput standard ...

  8. Educational Codeforces Round 60 (Rated for Div. 2) E. Decypher the String

    题目大意:这是一道交互题.给你一个长度为n的字符串,这个字符串是经过规则变换的,题目不告诉你变换规则,但是允许你提问3次:每次提问你给出一个长度为n的字符串,程序会返回按变换规则变换后的字符串,提问3 ...

  9. Educational Codeforces Round 76 (Rated for Div. 2) B. Magic Stick 水题

    B. Magic Stick Recently Petya walked in the forest and found a magic stick. Since Petya really likes ...

随机推荐

  1. 探索JS引擎工作原理

    JavaScript 从定义到执行,JS引擎在实现层做了很多初始化工作,因此在学习 JS 引擎工作机制之前,我们需要引入几个相关的概念:执行环境栈.全局对象.执行环境.变量对象.活动对象.作用域和作用 ...

  2. Java使用Try with resources自动关闭资源

    Try-with-resources Try-with-resources是Java7中一个新的异常处理机制,它能够很容易地关闭在try-catch语句块中使用的资源. 利用Try-Catch-Fin ...

  3. 关于.Net mvc 项目在本地vs运行响应时间过长无法访问时,解决方法!

    最近可能是刚升级了电脑使用了window10操作系统,总是遇到了一些以前没有遇到过的事情! 今早来到公司本来准备写bug的,但是当我打开vs运行的时候发现今天的电脑响应的时间明显的要比之前打开网页调试 ...

  4. 一个比Spring Boot快44倍的Java框架!

    最近栈长看到一个框架,官方号称可以比 Spring Boot 快 44 倍,居然这么牛逼,有这么神奇吗?今天带大家来认识一下. 这个框架名叫:light-4j. 官网简介:A fast, lightw ...

  5. 在Linux(Centos7)上使用Docker运行.NetCore

    在上一篇中我们写了如何在windows中使用docker运行.netcore,既然我们了解了windows下的运行发布,我们也可以试试linux下使用docker运行.netcore项目,那么今天我们 ...

  6. AssetsUtils【读取assets、res/raw、./data/data/包名/目录下的文件】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 封装了以下功能: 1.读取assets目录下的资源html.文件.图片,将文件复制到SD卡目录中: 2.读取res/raw目录下的文 ...

  7. ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)解决办法

    一,报错原因及分析 mysql的这个报错的原因是mysql服务没有正确启动就是mysqld这个程序. mysql要想运行需要mysql和mysqld两个都启动才行 二,解决办法 右键我的电脑——> ...

  8. Mac下安装多版本python

    1.安装Homebrew 将命令行复制至终端,进行安装. /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/H ...

  9. 使用nginx搭建高可用,高并发的wcf集群

    很多情况下基于wcf的复杂均衡都首选zookeeper,这样可以拥有更好的控制粒度,但zk对C# 不大友好,实现起来相对来说比较麻烦,实际情况下,如果 你的负载机制粒度很粗糙的话,优先使用nginx就 ...

  10. golang命令行库cobra的使用

    简介 Cobra既是一个用来创建强大的现代CLI命令行的golang库,也是一个生成程序应用和命令行文件的程序.下面是Cobra使用的一个演示: Cobra提供的功能 简易的子命令行模式,如 app ...