题意:给定 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)的更多相关文章

  1. Codeforces 1108D - Diverse Garland - [简单DP]

    题目链接:http://codeforces.com/problemset/problem/1108/D time limit per test 1 secondmemory limit per te ...

  2. UVA.10066 The Twin Towers (DP LCS)

    UVA.10066 The Twin Towers (DP LCS) 题意分析 有2座塔,分别由不同长度的石块组成.现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少. 问题的实质可以转化 ...

  3. [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)

    [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...

  4. Codeforces 478D Red-Green Towers:dp

    题目链接:http://codeforces.com/problemset/problem/478/D 题意: 给你r个红方块和g个绿方块,让你用这些方块堆一个塔. 最高层有1个方块,每往下一层块数+ ...

  5. 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 ...

  6. Codeforces 478D Red-Green Towers

    http://codeforces.com/problemset/problem/478/D 思路:dp:f[i][j]代表当前第i层,用了j个绿色方块的方案数,用滚动数组,还有,数组清零的时候一定要 ...

  7. Codeforces 1106E. Lunar New Year and Red Envelopes(DP)

    E. Lunar New Year and Red Envelopes 题意: 在长度为n的时间轴上,有k个红包,每个红包有领取时间段[s,t],价值w,以及领了个这个红包之后,在时间d到来之前无法再 ...

  8. Codeforces 1106 E. Lunar New Year and Red Envelopes 优先队列+dp

    题意大致是Bob新年拿红包,每个红包可以在s-t时间内取,但是取了之后得在d+1时间开始才能继续取红包. 同时他女儿能在m个时间点阻止他取红包,求女儿阻止后Bob取得的w总和最小值. Bob取红包的策 ...

  9. codeforces 721C (拓排 + DP)

    题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...

随机推荐

  1. 元素为指针的vector的使用说明

    该程序演示了vector中的元素为指针的时候的对对象的操作. /* 功能说明: 元素为指针的vector的使用说明 实现方式: 使用this成员来显示各个对象的地址. 限制条件或者存在的问题: 无 * ...

  2. HDU - 1506 Largest Rectangle in a Histogram (单调栈/笛卡尔树)

    题意:求一个直方图中最大矩形的面积. 很经典的一道问题了吧,可以用单调栈分别求出每个柱子左右两边第一个比它低的柱子(也就相当于求出了和它相连的最后一个比它高的柱子),确定每个柱子的左右边界,每个柱子的 ...

  3. CodeForces - 633H :Fibonacci-ish II(正解:莫对+线段树)

    Yash is finally tired of computing the length of the longest Fibonacci-ish sequence. He now plays ar ...

  4. IP地址的基础划分

    1.基础知识 1.1  IP地址是由网络号(net ID)与主机号(host ID)两部分组成的. 1.2  IP地址的分类 IP地址长度为32位,点分十进制(dotted decimal)地址: 采 ...

  5. Two Cakes

    It's New Year's Eve soon, so Ivan decided it's high time he started setting the table. Ivan has boug ...

  6. BZOJ5334: [Tjoi2018]数学计算

    BZOJ5334: [Tjoi2018]数学计算 https://lydsy.com/JudgeOnline/problem.php?id=5334 分析: 线段树按时间分治即可. 代码: #incl ...

  7. LeetCode Employee Importance

    原题链接在这里:https://leetcode.com/problems/employee-importance/description/ 题目: You are given a data stru ...

  8. Tomcat 工作原理 1 (转)

    Tomcat 系统架构与设计模式,第 1 部分: 工作原理 这个分为两个部分的系列文章将研究 Apache Tomcat 的系统架构以及其运用的很多经典设计模式.本文是第 1 部分,将主要从 Tomc ...

  9. 使用 DOM对象,控制HTML元素 来制作的一个简单的表格

    制作一个表格,显示班级的学生信息. 要求: 1. 鼠标移到不同行上时背景色改为色值为 red,移开鼠标时则恢复为原背景色 white 2. 点击添加按钮,能动态在最后添加一行 3. 点击删除按钮,则删 ...

  10. Poj 2488 A Knight's Journey(搜索)

    Background The knight is getting bored of seeing the same black and white squares again and again an ...