Good Bye 2015 F - New Year and Cleaning
这题简直是丧心病狂折磨王。。
思路:容易想到这样一个转换,把整个矩形一起移动,矩形移出去的时候相当于一行或者一列。
为了优化找到下一个消去的点,我先把原数组扩大两倍,用了st表加二分去找,然后就MLE, 我又换了
线段树TLE,最后不把数组扩大两倍ST表+二分过的。。
每次消去的点都是不变的,所以可以做到线性复杂度。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define PLI pair<LL, int>
#define ull unsigned long long
using namespace std; const int N = 5e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ; int k, n, m, X[N], Y[N], Log[N];
char s[N]; struct ST {
int dp[N][],ty;
void build(int n, int b[], int _ty) {
ty = _ty;
for(int i = ; i <= n; i++) dp[i][] = ty * b[i];
for(int j = ; j <= Log[n]; j++)
for(int i = ; i+(<<j)- <= n; i++)
dp[i][j] = max(dp[i][j-], dp[i+(<<(j-))][j-]);
}
int query(int x, int y) {
int k = Log[y - x + ];
return ty * max(dp[x][k], dp[y-(<<k)+][k]);
}
} mxx, mnx, mxy, mny; void walk(int &x, int &y, char c) {
if(c == 'R') y++;
else if(c == 'L') y--;
else if(c == 'U') x--;
else x++;
} bool check(int l, int r, int x, int y, int lenr, int lenc) {
int mxX = mxx.query(l, r) - X[l-];
int mnX = mnx.query(l, r) - X[l-];
int mxY = mxy.query(l, r) - Y[l-];
int mnY = mny.query(l, r) - Y[l-];
if(x+mxX+lenr > n || x+mnX < || y+mxY+lenc > m || y+mnY < ) return true;
return false;
} int main() {
for(int i = -(Log[]=-); i < N; i++)
Log[i] = Log[i - ] + ((i & (i - )) == ); scanf("%d%d%d%s", &k, &n, &m, s + );
int x = , y = , L = , R = , D = , U = , p = ;
for(int i = ; i <= k; i++) {
walk(x, y, s[i]);
L = min(L, y); R = max(R, y);
U = min(U, x); D = max(D, x);
}
if(!x && !y) {
if(R - L + <= m && D - U + <= n) {
puts("-1");
return ;
}
}
x = , y = ;
for(int i = ; i <= k; i++) {
walk(x, y, s[i]);
X[i] = x; Y[i] = y;
} mxx.build(k, X, );
mnx.build(k, X, -);
mxy.build(k, Y, );
mny.build(k, Y, -); x = , y = ;
int lenr = n, lenc = m;
LL ans = , t = ;
while(lenr && lenc) {
int l = p + , r = min(k, p + k), pos = -;
while(l <= r) {
int mid = l + r >> ;
if(check(p + , mid, x, y, lenr, lenc)) r = mid - , pos = mid;
else l = mid + ;
} if(pos != -) {
x += X[pos] - X[p];
y += Y[pos] - Y[p];
t = (t + pos - p) % mod;
p = pos;
if(s[p] == 'U') {
lenr--; ans = (ans + 1ll*t*lenc) % mod;
x = ;
} else if(s[p] == 'D') {
lenr--; ans = (ans + 1ll*t*lenc) % mod;
} else if(s[p] == 'L') {
lenc--; ans = (ans + 1ll*t*lenr) % mod;
y = ;
} else {
lenc--; ans = (ans + 1ll*t*lenr) % mod;
}
if(p == k) p = ;
} else {
x += X[k] - X[p];
y += Y[k] - Y[p];
t = (t + k - p) % mod;
p = ;
} }
printf("%lld\n", ans);
return ;
} /*
*/
Good Bye 2015 F - New Year and Cleaning的更多相关文章
- Codeforces Round #597 (Div. 2) F. Daniel and Spring Cleaning 数位dp
F. Daniel and Spring Cleaning While doing some spring cleaning, Daniel found an old calculator that ...
- Good Bye 2015 D. New Year and Ancient Prophecy
D. New Year and Ancient Prophecy time limit per test 2.5 seconds memory limit per test 512 megabytes ...
- Good Bye 2015 B. New Year and Old Property 计数问题
B. New Year and Old Property The year 2015 is almost over. Limak is a little polar bear. He has re ...
- Good Bye 2015 A. New Year and Days 签到
A. New Year and Days Today is Wednesday, the third day of the week. What's more interesting is tha ...
- Codeforces Good bye 2015 B. New Year and Old Property dfs 数位DP
B. New Year and Old Property 题目连接: http://www.codeforces.com/contest/611/problem/B Description The y ...
- Codeforces Good Bye 2015 A. New Year and Days 水题
A. New Year and Days 题目连接: http://www.codeforces.com/contest/611/problem/A Description Today is Wedn ...
- 【最大流】ECNA 2015 F Transportation Delegation (Codeforces GYM 100825)
题目链接: http://codeforces.com/gym/100825 题目大意: N(N<=600)个点,每个点有个名字Si,R(R<=200)个生产商在R个点上,F(F<= ...
- Looksery Cup 2015 F - Yura and Developers 单调栈+启发式合并
F - Yura and Developers 第一次知道单调栈搞出来的区间也能启发式合并... 你把它想想成一个树的形式, 可以发现确实可以启发式合并. #include<bits/stdc+ ...
- Good Bye 2014 F - New Year Shopping
F - New Year Shopping 对于一种特殊的不可逆的dp的拆分方法.. 也可以用分治写哒. #include<bits/stdc++.h> #define LL long l ...
随机推荐
- Ubuntu在vncviewer下Tab键失效
打开命令行,运行如下命令即可解决: xfconf-query -c xfce4-keyboard-shortcuts -p /xfwm4/custom/'<'Super'>'Tab -r ...
- Google Cast和ChromeCast
Google Cast类似于DLNA,AirPlayer,Miracast,就是一种投屏技术.我们ATV产品是对Google Cast和ChromeCast都是支持的. Google Cast 大致工 ...
- python基础之列表、字典、元祖等 (二)
一.作用域 if 1==1: name = 'weibinf' print name 下面的结论对吗? 外层变量,可以被内层变量使用 内层变量,无法被外层变量使用 二.三元运算 result = 值1 ...
- duilib 修复Text控件无法设置宽度的bug,增加自动加算宽度的属性
转载请说明原出处,谢谢~~: 今天有朋友反映CTextUI控件无法设置宽度,于是修复了这个bug,顺便给Text控件增加了一个自动计算宽度的属性,描述如下 <Attribute name=&qu ...
- MySQL建表时列名同保留字重复问题解决办法
建表时遇到遇到属性名同MySQL的保留字相同导致不能建表的问题,如下SQL语句: CREATE TABLE TBL_ACCOUNT_FROZEN_RECORD ( ID BIGINT NOT NULL ...
- 用shell获取目录/文件夹/文件的时间戳
命令: date +%s -r 目录名/文件名 输出内容形如: 1276225332
- spring boot 2.0.3+spring cloud (Finchley)9、 安全组件Spring Boot Security
官方文档 一.Spring Security介绍 Spring Security是Spring Resource社区的一个安全组件,Spring Security为JavaEE企业级开发提供了全面的安 ...
- 新手应知道的ASP.NET代码编写规范
1.局部变量的名称要有意义,尽量用对应的英文命名,比如“用户姓名”变量,不要用aa bb cc等来命名,而要使用userName. 2.不要使用单个字母的变量,如i.n.x等.而要使用index.te ...
- Linux下使用tree命令查看目录结构
Linux下的文件虽然是层次型组织结构的,但是我们平时登录到主机上的时候都是使用的各种shell并没有图形界面,看上去很不直观,Linux下有个小命令叫做tree,可以以目录树的形式显示文件结构,类似 ...
- c语言学习笔记.内存管理.
内存: 每个程序的内存是分区的:堆区.栈区.静态区.代码区. 1.代码区:放置所有的可执行代码,包括main函数. 2.静态区:存放所有的全局变量和静态变量. 3.栈区:栈(stack),先进后出.存 ...