【CF1247E】Rock Is Push(DP,二分)
题意:有一个n*m的方格,每一格可能为空也可能有石头,要从(1,1)走到(n,m),每次可以往右或往下走
每次走的时候都会将自己面前的所有石头向移动方向推一格,如果碰到了边界就推不过去
问方案数模1e9+7
n,m<=2e3
思路:设dp[i][j][0/1]分别为当前走到(i,j),上一次从左/上走的合法方案数
合法的转移是行坐标或列坐标连续的一段,而且受障碍物个数和当前行/列号限制
写出式子之后可以发现决策范围对于i相同或者j相同是单调的,可以用队列维护,但显然二分更好写
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> PII;
typedef pair<ll,ll> Pll;
typedef vector<int> VI;
typedef vector<PII> VII;
//typedef pair<ll,ll>P;
#define N 2010
//#define M 200010
#define INF 1e9
#define fi first
#define se second
#define MP make_pair
#define pb push_back
#define pi acos(-1)
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
#define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
#define lowbit(x) x&(-x)
#define Rand (rand()*(1<<16)+rand())
#define id(x) ((x)<=B?(x):m-n/(x)+1)
#define ls p<<1
#define rs p<<1|1 const ll MOD=1e9+,inv2=(MOD+)/;
double eps=1e-;
int dx[]={-,,,};
int dy[]={,,-,}; char ch[N];
ll dp[N][N][],s[N][N][];
int b[N][N]; int read()
{
int v=,f=;
char c=getchar();
while(c<||<c) {if(c=='-') f=-; c=getchar();}
while(<=c&&c<=) v=(v<<)+v+v+c-,c=getchar();
return v*f;
} int calcb(int x1,int y1,int x2,int y2)
{
return b[x2][y2]-b[x1-][y2]-b[x2][y1-]+b[x1-][y1-];
} ll calc(int x1,int y1,int x2,int y2,int op)
{
ll res=s[x2][y2][op]-s[x1-][y2][op]-s[x2][y1-][op]+s[x1-][y1-][op];
res=(res%MOD+MOD)%MOD;
return res;
} void add(ll &a,ll b)
{
a+=b;
if(a>=MOD) a-=MOD;
if(a<) a+=MOD;
} int main()
{
int n=read(),m=read();
if(n==&&m==)
{
printf("1\n");
return ;
}
rep(i,,n)
{
scanf("%s",ch+);
rep(j,,m)
if(ch[j]=='R') b[i][j]=;
}
rep(i,,n)
rep(j,,m) b[i][j]=b[i-][j]+b[i][j-]-b[i-][j-]+b[i][j];
dp[][][]=dp[][][]=;
s[][][]=s[][][]=;
rep(len,,n+m)
{
rep(i,,len-)
{
int j=len-i;
if(i>n||j>m) continue;
int l=,r=j-,last=j;
while(l<=r)
{
int mid=(l+r)>>;
if(calcb(i,mid+,i,m)<=m-j){last=mid; r=mid-;}
else l=mid+;
}
if(last<j) add(dp[i][j][],calc(i,last,i,j-,));
add(s[i][j][],s[i-][j][]);
add(s[i][j][],s[i][j-][]);
add(s[i][j][],-s[i-][j-][]);
add(s[i][j][],dp[i][j][]); l=,r=i-,last=i;
while(l<=r)
{
int mid=(l+r)>>;
if(calcb(mid+,j,n,j)<=n-i){last=mid; r=mid-;}
else l=mid+;
}
if(last<i) add(dp[i][j][],calc(last,j,i-,j,));
add(s[i][j][],s[i-][j][]);
add(s[i][j][],s[i][j-][]);
add(s[i][j][],-s[i-][j-][]);
add(s[i][j][],dp[i][j][]);
} }
ll ans=(dp[n][m][]+dp[n][m][])%MOD;
printf("%I64d\n",ans);
return ;
}
【CF1247E】Rock Is Push(DP,二分)的更多相关文章
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) E. Rock Is Push dp
E. Rock Is Push You are at the top left cell (1,1) of an n×m labyrinth. Your goal is to get to the b ...
- [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】
[CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...
- HDU 3433 (DP + 二分) A Task Process
题意: 有n个员工,每个员工完成一件A任务和一件B任务的时间给出,问要完成x件A任务y件B任务所需的最短时间是多少 思路: DP + 二分我也是第一次见到,这个我只能说太难想了,根本想不到. dp[i ...
- 两种解法-树形dp+二分+单调队列(或RMQ)-hdu-4123-Bob’s Race
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 题目大意: 给一棵树,n个节点,每条边有个权值,从每个点i出发有个不经过自己走过的点的最远距离 ...
- POJ-2533最长上升子序列(DP+二分)(优化版)
Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 41944 Acc ...
- hdu2993之斜率dp+二分查找
MAX Average Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- poj3208 Apocalypse Someday 数位dp+二分 求第K(K <= 5*107)个有连续3个6的数。
/** 题目:poj3208 Apocalypse Someday 链接:http://poj.org/problem?id=3208 题意:求第K(K <= 5*107)个有连续3个6的数. ...
- hdu 1025:Constructing Roads In JGShining's Kingdom(DP + 二分优化)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- 【bzoj3312】[Usaco2013 Nov]No Change 状态压缩dp+二分
题目描述 Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 ...
- [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)
[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...
随机推荐
- java基础语法详细介绍
一.概述 1.java语言概述 是SUN(Stanford University Network,斯坦福大学网络公司 ) 1995年推出的一门高级编程语言; java之父---James Goslin ...
- Tomcat控制台中文乱码
参考:https://blog.csdn.net/zhaoxny/article/details/79926333 1.找到${CATALINA_HOME}/conf/logging.properti ...
- CSRF Failed: CSRF token missing or incorrect
Django设置本身没有关闭CSRF Django设置已经关闭CSRF,可能是由于两个项目都使用同一个端口,调试的时候就会出现Cookie里面csrftoken重用的问题,清理Cookie就好
- mysql支持的存储引擎
1.InnoDB 存储引擎 支持事务,其设计目标主要面向联机事务处理(OLTP)的应用.其特点是行锁设计.支持外键,并支持类似 Oracle 的非锁定读,即默认读取操作不会产生锁. 从 MySQL 5 ...
- HTTPS和HTTP的区别,http协议的特征
http协议传输的数据都是没有经过加密的,也就是明文,所以http用于传输数据并不安全.而https是是使用了ssl(secure socket layer)协议+http协议构成的可加密传输,身份认 ...
- IBM公司的面试题,看看你能做出多少。
进入IBM差不多是每一个IT人的梦想.IBM公司向来以高素质人才作为企业持续竞争力的保证,所以经常出一些千奇百怪的面试题,来考验一个人的综合能力,以下是5道IBM曾经出过的面试题,看看你能作出几道: ...
- There are multiple modules with names that only differ in casing. This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.
There are multiple modules with names that only differ in casing.This can lead to unexpected behavio ...
- Memcached下载安装和使用
一.简介:Memcached 是一个高性能的分布式,基于内存的key-value存储的对象缓存系统(并不是一个数据库),用于动态Web应用以减轻数据库负载. 二.下载和安装1.下载和安装Memcach ...
- Android数据库使用指南(下)
前言 上面已经说了,对表进行修改,其实就是对数据库进行升级,删除表也算升级啊,反正就是发生变化,数据库就需要升级. 所以老实说其实有个地方决定了数据库的版本 public class DBHelper ...
- <input type="radio">单选按钮
转自:http://www.divcss5.com/html/h490.shtml1 <form> 男性: <input type="radio" checked ...