2019牛客暑期多校训练营(第一场) - E - ABBA - 贪心 - dp - 组合
https://ac.nowcoder.com/acm/contest/881/E
从dp的角度来看是比较正常的。无后效性来源于前面只要的合法的方案分配,那么对后面造成的影响就只有A,B的数目。
从贪心的角度看,前n个A必定是给AB用的,前m个B必定是给BA用的。否则假如有一个BA用了一个前n个A里的A,那么可以把这个A和后面的某个A互换所在的组。
dp参考JHSeng大佬的题解。
设 \(dp[i][j]\) 表示放置 \(i\) 个A,放置 \(j\) 个B的合法方案数。显然 \(dp[0][0]=1\) ,转移有 \(dp[i][j]=dp[i-1][j]+dp[i][j-1]\) 。
但并不是每次放A和放B都可以转移到合法方案数。什么情况下是不合法的呢?
那么,
当 \(i<=n\) 的时候,再放的A一定可以给AB用,放 \(dp[i-1][j]\) 。
当 \(i>n\) 的时候,这时放的A一定是给BA用的,限制是有没有足够多的B在这个前面,也就是 \(j>(i-n)\) 时,放 \(dp[i-1][j]\) 。
另一个也是同理。
还卡memset。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ERR(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {cerr << "\n";}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << "=" << a << ", ";
err(++it, args...);
}
ll dp[2005][2005];
const int mod=1e9+7;
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
//freopen("Yinku.out", "w", stdout);
#endif // Yinku
int n,m;
while(~scanf("%d%d", &n,&m)) {
for(int i=0;i<=n+m;i++){
memset(dp[i],0,sizeof(dp[i][0])*(n+m+1));
}
dp[0][0]=1;
for(int i=0; i<=n+m; i++) {
for(int j=0; j<=n+m; j++) {
if((i+1<=n+m)&&(i+1<=n||(min(j,m)>=(i+1-n))))
dp[i+1][j]=(dp[i+1][j]+dp[i][j])%mod;
if((j+1<=n+m)&&(j+1<=m||(min(i,n)>=(j+1-m))))
dp[i][j+1]=(dp[i][j+1]+dp[i][j])%mod;
}
}
printf("%lld\n",dp[n+m][n+m]);
}
}
从组合的角度看。
https://blog.csdn.net/qq_39239844/article/details/96475068
总的方案数=从(n+m)2个位置选(n+m)个位置放A的方案数。
不合法的方案数=从(n+m)2个位置选(n-1)个位置放A的方案数+从(n+m)*2个位置选(m-1)个位置放B的方案数
2019牛客暑期多校训练营(第一场) - E - ABBA - 贪心 - dp - 组合的更多相关文章
- 2019牛客暑期多校训练营(第二场) H-Second Large Rectangle(单调栈)
题意:给出由01组成的矩阵,求求全是1的次大子矩阵. 思路: 单调栈 全是1的最大子矩阵的变形,不能直接把所有的面积存起来然后排序取第二大的,因为次大子矩阵可能在最大子矩阵里面,比如: 1 0 0 1 ...
- 2019牛客暑期多校训练营(第九场) D Knapsack Cryptosystem
题目 题意: 给你n(最大36)个数,让你从这n个数里面找出来一些数,使这些数的和等于s(题目输入),用到的数输出1,没有用到的数输出0 例如:3 4 2 3 4 输出:0 0 1 题解: 认真想一 ...
- 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题)
layout: post title: 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题) author: "luowentaoaa" c ...
- 2019牛客暑期多校训练营(第一场)A Equivalent Prefixes(单调栈/二分+分治)
链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 Two arrays u and v each with m distinct elements ...
- 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)
链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 题目描述 Two arrays u and v each with m distinct elem ...
- 2019牛客暑期多校训练营(第一场) B Integration (数学)
链接:https://ac.nowcoder.com/acm/contest/881/B 来源:牛客网 Integration 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 5242 ...
- 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)
链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...
- 2019牛客暑期多校训练营(第二场)F.Partition problem
链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...
- 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)
题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9: 对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可. 后者mod=1e9,5才 ...
- 2019牛客暑期多校训练营(第八场)E.Explorer
链接:https://ac.nowcoder.com/acm/contest/888/E来源:牛客网 Gromah and LZR have entered the fifth level. Unli ...
随机推荐
- 解决 myEclipse与tomcat 不同步的问题
在我们使用eclipse做web调试的过程中,一般只需要在eclipse修改程序,然后在浏览器刷新就能发现文件更改,今天突然发现保存后不能更改了.1.检查tomcat中的文件发现文件没有更新.2.检查 ...
- vue报错TypeError: Cannot read property 'protocol' of undefined
错误信息如下所示: isURLSameOrigin.js?3934:57 Uncaught (in promise) TypeError: Cannot read property 'protocol ...
- log4j常用的配置文件
# priority :debug<info<warn<error #you cannot specify every priority with different file fo ...
- codeforces Summer Earnings(bieset)
Summer Earnings time limit per test 9 seconds memory limit per test 256 megabytes input standard inp ...
- 前端每日实战:37# 视频演示如何把握好 transition 和 animation 的时序,创作描边按钮特效
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/mKdzZM 可交互视频教程 此视频 ...
- hdu 1693 : Eat the Trees 【插头dp 入门】
题目链接 题意: 给出一个n*m大小的01矩阵,在其中画线连成封闭图形,其中对每一个值为1的方格,线要恰好穿入穿出共两次,对每一个值为0的方格,所画线不能经过. 参考资料: <基于连通性状态压缩 ...
- Flask学习笔记03之路由
1. endpoint from flask import Flask, url_for # 实例化一个Flask对象 app = Flask(__name__) # 打印默认配置信息 # 引入开发环 ...
- python学习笔记(九)内置函数
print(all([1,2,3,4]))#判断可迭代的对象里面的值是否都为真 True print(any([0,1,2,3,4]))#判断可迭代的对象里面的值是否有一个为真 True print( ...
- Cenos7下nginx+mysql+php环境的搭建
首先更新系统软件 1 $ yum update 第一步:安装nginx 1.安装nginx源 1 $ yum localinstall http://nginx.org/packages/centos ...
- 20180821-Java封装
java 封装 在面向对象程式设计方法中,封装(英语:Encapsulation)是指,一种将抽象性函式接口的实作细节部份包装.隐藏起来的方法. 封装可以被认为是一个保护屏障,防止该类的代码和数据被外 ...