CodeForces 478D Red-Green Towers (DP)
题意:给定 n 块红砖,m 块绿砖,问有多少种方式可以建造成最高的塔,每一层颜色必须一样。
析:首先要确定最高是多少层h,大约应该是用 h * (h+1) <= (m+n) * 2,然后dp[i][j] 表示 前 i 层用 j 块红砖,dp[i][j] += dp[i-1][j-i],
但是这个空间复杂度受不了,那么就变成滚动数组就好,dp[j] += dp[j-i],一个较简单的DP。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2e5 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} int dp[maxn]; int main(){
scanf("%d %d", &n, &m);
int h = (int)sqrt(n+m+m+n);
while((h+1) * h <= n+n+m+m) ++h;
dp[0] = 1;
for(int i = 1; i < h; ++i)
for(int j = n; j >= i; --j)
dp[j] = (dp[j] + dp[j-i]) % mod;
int ans = 0;
int all = h * (h-1) / 2;
for(int i = n; i >= 0; --i){
if(all - i > m) break;
ans = (ans + dp[i]) % mod;
}
printf("%d\n", ans);
return 0;
}
CodeForces 478D Red-Green Towers (DP)的更多相关文章
- Codeforces 1108D - Diverse Garland - [简单DP]
题目链接:http://codeforces.com/problemset/problem/1108/D time limit per test 1 secondmemory limit per te ...
- UVA.10066 The Twin Towers (DP LCS)
UVA.10066 The Twin Towers (DP LCS) 题意分析 有2座塔,分别由不同长度的石块组成.现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少. 问题的实质可以转化 ...
- [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)
[BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...
- Codeforces 478D Red-Green Towers:dp
题目链接:http://codeforces.com/problemset/problem/478/D 题意: 给你r个红方块和g个绿方块,让你用这些方块堆一个塔. 最高层有1个方块,每往下一层块数+ ...
- Codeforces Round #273 (Div. 2)D. Red-Green Towers DP
D. Red-Green Towers There are r red and g green blocks for construction of the red-green tower. Re ...
- Codeforces 478D Red-Green Towers
http://codeforces.com/problemset/problem/478/D 思路:dp:f[i][j]代表当前第i层,用了j个绿色方块的方案数,用滚动数组,还有,数组清零的时候一定要 ...
- Codeforces 1106E. Lunar New Year and Red Envelopes(DP)
E. Lunar New Year and Red Envelopes 题意: 在长度为n的时间轴上,有k个红包,每个红包有领取时间段[s,t],价值w,以及领了个这个红包之后,在时间d到来之前无法再 ...
- Codeforces 1106 E. Lunar New Year and Red Envelopes 优先队列+dp
题意大致是Bob新年拿红包,每个红包可以在s-t时间内取,但是取了之后得在d+1时间开始才能继续取红包. 同时他女儿能在m个时间点阻止他取红包,求女儿阻止后Bob取得的w总和最小值. Bob取红包的策 ...
- codeforces 721C (拓排 + DP)
题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...
随机推荐
- windowsError:32
Traceback (most recent call last): File "/usr/lib/python2.7/logging/handlers.py", line 78, ...
- hadoop-pig学习笔记
A1 = LOAD '/luo/lzttxt01.txt' AS (col1:chararray,col2:int,col3:int,col4:int,col5:double,col6:double) ...
- webpack学习(一)—— 入门
,我们通常采用的是组件化开发方式,这样就会对应有很多个js文件,而打包工具的出现则是为了正确处理这些js文件的依赖关系,并生成一个最终的文件,这样,我们最后只需要加载打包以后的文件就可以了,而无须加载 ...
- Yii错误404页面
'errorHandler'=>array( // use 'site/error' action to display errors 'errorAction'=>YII_DEBUG ? ...
- BZOJ2120:数颜色(分块版)
浅谈分块:https://www.cnblogs.com/AKMer/p/10369816.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?i ...
- es6变量的解构赋值学习笔记
1. 解构赋值的规则是,只要等号右边的值不是对象,就先将其转为对象.由于undefined和null无法转为对象,所以对它们进行解构赋值,都会报错. let { prop: x } = undefin ...
- hl7 v2.X 版本中RSP_K23消息的构造
RSP_K23消息有MSH, MSA, ERR, QAK, QPD, PID几个segment,其中ERR,PID为可选. 1. 当MSA有err时,ERR段填充出错的详细信息. 2. 当MSA为AA ...
- dijstra+输出路径总结
#include<iostream> #include<math.h> #include<memory.h> using namespace std; #defin ...
- 侯捷STL学习(一)--顺序容器测试
开始跟着<STL源码剖析>的作者侯捷真人视频,学习STL,了解STL背后的真实故事! 视频链接:侯捷STL 还有很大其他视频需要的留言 第一节:STL版本和重要资源 STL和标准库的区别 ...
- Python代码规范总结
1.缩进问题: Tip:用4个空格来缩进代码 不要用Tab键或者是Tab和空格混用, vim用户可以将tab键设置为4个空格的长度.要么选择垂直对齐换行的元素, 或者是使用4空格悬挂式缩进(第一行没有 ...