• Educational Codeforces Round 53 (Rated for Div. 2) C Vasya and Robot 二分


    题目:题目链接

    思路:对于x方向距离与y方向距离之和大于n的情况是肯定不能到达的,另外,如果n比abs(x) + abs(y)大,那么我们总可以用UD或者LR来抵消多余的大小,所以只要abs(x) + abs(y) <= n && (n - abs(x) + abs(y)) % 2 == 0,就一定可以到达终点,判断完之后二分答案长度就可以了

    AC代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <string>
     6 #include <vector>
     7 #include <set>
     8 #include <map>
     9 #include <stack>
    10 #include <queue>
    11 #include <list>
    12 #include <cstdlib>
    13 #include <cmath>
    14 
    15 #define FRER() freopen("in.txt", "r", stdin)
    16 #define FREO() freopen("out.txt", "w", stdout)
    17 #define INF 0x3f3f3f3f
    18 
    19 using namespace std;
    20 
    21 const int maxn = 200000 + 5;
    22 
    23 int n, cx[maxn], cy[maxn], x, y;
    24 char str[maxn];
    25 
    26 bool judge(int t) {
    27     for(int i = t; i <= n; ++i) {
    28         int lx = cx[n] - cx[i] + cx[i - t];
    29         int ly = cy[n] - cy[i] + cy[i - t];
    30         if (abs(x - lx) + abs(y - ly) <= t)
    31             return true;
    32     }
    33     return false;
    34 }
    35 
    36 int main()
    37 {
    38     cin >> n >> str >> x >> y;
    39     if(abs(x) + abs(y) > n || (abs(x) + abs(y)) % 2 != n % 2) {
    40         cout << -1 << endl;
    41     }
    42     else {
    43         for(int i = 0; i < n; ++i) {
    44             if(str[i] == 'U') cx[i + 1] = cx[i], cy[i + 1] = cy[i] + 1;
    45             else if(str[i] == 'D') cx[i + 1] = cx[i], cy[i + 1] = cy[i] - 1;
    46             else if(str[i] == 'L') cx[i + 1] = cx[i] - 1, cy[i + 1] = cy[i];
    47             else if(str[i] == 'R') cx[i + 1] = cx[i] + 1, cy[i + 1] = cy[i];
    48         }
    49         int l = 0, r = n;
    50         while(l < r) {
    51             int m = (l + r) >> 1;
    52             if(judge(m))
    53                 r = m;
    54             else
    55                 l = m + 1;
    56         }
    57         cout << r << endl;
    58     }
    59     return 0;
    60 }
  • 相关阅读:
    第八周学习进度总结
    全国(球)疫情信息可视化
    第六周学习进度总结
    手把手教你爬取B站弹幕!
    Xpath基础学习
    团队成员及选题介绍
    第五周学习进度
    课堂练习之疫情APP
    SpringMVC02
    06 | 链表(上):如何实现LRU缓存淘汰算法?
  • 原文地址:https://www.cnblogs.com/fan-jiaming/p/9859778.html
Copyright © 2020-2023  润新知