CodeForce Educational round Div2 C - Vasya and Robot
思路:
先分别求x轴,y轴上的前缀和,偏于之后判断区间是否满足条件。详细见代码。
固定左端点,二分枚举右端点。判断左右端点的区间是否能够达成目标(区间长度是否大于未完成量,奇偶性是否相同)
注意点:
strlen是O(n)的复杂度,超时了。
之前没遇到过固定一个点,然后另一个点二分逼近值的。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#define ll long long
#define local using namespace std; const int MOD = 1e9+;
const int inf = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int maxn = 2e5+; int n;
ll endx, endy;
ll total;
char str[maxn];
int sumx[maxn];
int sumy[maxn]; bool ok(int l, int r) {
int nowx = sumx[n-]-sumx[r];
int nowy = sumy[n-]-sumy[r];
if (l > ) {
nowx += sumx[l-];
nowy += sumy[l-];
}
ll diff = abs(endx-nowx)+abs(endy-nowy);
if (diff <= r-l+ && ((diff&))==((r-l+)&)) return ;
else return ;
} int main() {
#ifdef local
if(freopen("/Users/Andrew/Desktop/data.txt", "r", stdin) == NULL) printf("can't open this file!\n");
#endif
scanf("%d", &n);
scanf("%s", str);
scanf("%lld%lld", &endx, &endy);
total = abs(endx)+abs(endy);//总的步数
//如果两者奇偶性不同也不行
if (total>n || ((total&) != (n&))) {
printf("-1\n");
return ;
}
sumx[] = ;
sumy[] = ;
int len = int(strlen(str));//strlen(str)是O(n)的复杂度 orz...
for (int i = ; i < len; ++i) {
int incx = ; int incy = ;
if (str[i] == 'U') {
incy = ;
} else if (str[i] == 'R') {
incx = ;
} else if (str[i] == 'D') {
incy = -;
} else {
incx = -;
}
if (i) {
sumx[i] += sumx[i-]+incx;
sumy[i] += sumy[i-]+incy;
}
else {
sumx[i] = incx;
sumy[i] = incy;
}
}
if (sumx[n-]==endx && sumy[n-]==endy) {
printf("0\n");
return ;
}
int mn = inf;
//枚举点
for (int i = ; i < n; ++i) {
int l = i-; int r = n-;
while (r-l > ) {
int m = (l+r)>>;
if (ok(i, m)) {
r = m;
} else {
l = m;
}
}
//判断一下r是否可行
if (ok(i, r)) mn = min(mn, r-i+);
}
printf("%d\n", mn);
#ifdef local
fclose(stdin);
#endif
return ;
}
CodeForce Educational round Div2 C - Vasya and Robot的更多相关文章
- Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】
任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second mem ...
- CF Educational Round 78 (Div2)题解报告A~E
CF Educational Round 78 (Div2)题解报告A~E A:Two Rival Students 依题意模拟即可 #include<bits/stdc++.h> us ...
- [CodeForces]Educational Round 52
幸好我没有打这场,我VP的时候在C题就卡死了,我果然还是太菜了. A Vasya and Chocolate 题意:一个巧克力\(c\)元,买\(a\)赠\(b\),一共有\(n\)元,问能买几个巧克 ...
- [Educational Round 5][Codeforces 616F. Expensive Strings]
这题调得我心疲力竭...Educational Round 5就过一段时间再发了_(:з」∠)_ 先后找了三份AC代码对拍,结果有两份都会在某些数据上出点问题...这场的数据有点水啊_(:з」∠)_[ ...
- [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]
这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...
- CF 1073C Vasya and Robot(二分答案)
C. Vasya and Robot time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces Educational Round 33 题解
题目链接 Codeforces Educational Round 33 Problem A 按照题目模拟,中间发现不对就直接输出NO. #include <bits/stdc++.h> ...
- Codeforces Educational Round 92 赛后解题报告(A-G)
Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...
- Codeforces 1073C:Vasya and Robot(二分)
C. Vasya and Robot time limit per test: 1 secondmemory limit per test: 256 megabytesinput: standard ...
随机推荐
- ubuntu16.04安装pycharm
Ubuntu16.04下,默认安装了python2.7和python3.5,在终端下,输入“Python” 或“python3”可查看具体版本. 1.安装PyCharm前,先配置PyCharm的JD ...
- COM/DCOM简述
这些组件对象可以互相通讯与交互,而与它们的语言.分布及原始平台无关.COM规程包括一套标准API.一个标准的接口集以及COM用于支持分布式计算的网络协议.而DCOM模型则是一套用于分布式环境中的COM ...
- linux ubuntu 安装后没有root密码
终端中输入:sudo passwd root 此时重新设置原登录用户的密码. 设置成功后在终端继续输入: su root 则出现#号,原用户名得到root权限.此时可以进行超级用户操作.
- FPGA设计中的复位
(1)异步复位与同步复位的写法 1.异步复位与同步复位的区别? 同步复位:若复位信号在时钟有效边沿到来时刻为有效,则执行一次复位操作. 优点: 1)同步复位是离散的,所以非常有利于仿真器的仿真: 2) ...
- PLL详解
PLL 时钟是时序逻辑的灵魂. 在实际应用中,时钟信号在频率或者相位上通常并不满足直接使用的需求,而内部时序逻辑又只能对时钟信号进行整数倍的分频,并且不能保证产生新时钟信号的相位稳定性,所以需要用到 ...
- 干货 | SSMS客户端连接京东云RDS SQL Server配置方法
干货 | SSMS客户端连接京东云RDS SQL Server配置方法 原创: 于振江 京东云开发者社区 微软SQL Server, Oracle数据库以及MySQL系列占据了关系型数据库市场的绝对 ...
- shell练习题3
需求如下: 请按照这样的日期格式(xxxx-xx-xx)每天生成一个文件,例如今天生成的文件为2018-10-19.log, 并把磁盘的使用情况入到这个文件,(不需要写cron,写脚本即可) 参考解答 ...
- 使用VB6读取数据库资源并发送邮件(原创)
Private Sub Form_Load() Call conndb End Sub Private Function conndb() Dim cn As New ADODB.Connection ...
- 手机端flex、字体设置、快速点击
;(function flexible (window, document) { var docEl = document.documentElement ♥1 var dpr = window.de ...
- LINUX磁盘分区
在学习 Linux 的过程中,安装 Linux 是每一个初学者的第一个门槛.在这个过程中间,最大的困惑莫过于给硬盘进行分区.虽然,现在各种发行版本的 Linux 已经提供了友好的图形交互界面,但是很多 ...