Codeforces478D-Red-Green Towers-DP

不是特别难的一道dp题。

给r个红块,g个绿块,计算这些块能磊出的最高塔的方案数。

塔的每一层都比上一层多一块,每一层只能有一种颜色。

dp[i][j]表示第i层,j个红块的方案数。

则dp[i][j] = dp[i-1][j] + dp[i-1][j-i].注意一下方案的转移和最终结果的统计。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4
 5 using namespace std;
 6
 7 const int maxn = 2e5+10;
 8 const int mod = 1e9+7;
 9
10 int r,g;
11 int dp[maxn];
12
13 int main()
14 {
15     scanf("%d%d",&r,&g);
16     memset(dp,0,sizeof dp);
17
18     int ans = 0,oans;
19     if(r > 0) dp[1]++;
20     if(g > 0) dp[0]++;
21     oans = dp[0]+dp[1];
22     for(int i=2;i<1000;i++)
23     {
24         bool flag = false;
25         int tol = i*(i+1)/2;
26         int bgn = min(r,tol);
27         ans = 0;
28
29         for(int j=bgn;j>=0;j--)
30         {
31             bool flag_g = false;
32             if((tol-j) <= g && dp[j])
33             {
34                 dp[j] = dp[j];
35                 dp[j] %= mod;
36                 flag = true;
37                 flag_g = true;
38                 ans += dp[j];
39                 ans %= mod;
40             }
41             if(j >= i && dp[j-i])
42             {
43                 dp[j] += dp[j-i];
44                 dp[j] %= mod;
45                 flag = true;
46                 flag_g = true;
47                 ans += dp[j-i];
48                 ans %= mod;
49             }
50             if(!flag_g)
51                 dp[j] = 0;
52             //printf("i:%d j:%d dp:%d\n",i,j,dp[j]);
53         }
54         if(!flag)
55         {
56             break ;
57         }
58         oans = ans;
59     }
60     printf("%d\n",oans);
61 }
时间: 2024-08-10 02:11:16

Codeforces478D-Red-Green Towers-DP的相关文章

[补题][Codeforces478D]Red-Green Towers(DP)

题目链接 http://codeforces.com/problemset/problem/478/D 题意 叠放塔:有红.绿两种色块.从第一层开始,第一层1块,第二层2块,第i层i块. 要求每一层只能用同一种颜色的块. 输入:红块和绿块数目 输出:能叠放出的最高高度h的塔的种数.定义塔某一层的颜色不同,则为不同种. 题解 状态表示 dp[i][j]表示i层的塔,使用了j个红块 转移方程 dp[i][j]=dp[i-1][j]+dp[i-1][j-i],当j>=i dp[i][j]=dp[i-1

zoj2059 The Twin Towers (dp)

Description Twin towers we see you standing tall, though a building's lost our faith will never fall. Twin towers the world hears your call, though you're gone it only strengthens our resolve. We couldn't make it through this without you Lord, throug

uva--10066The Twin Towers +dp

其实就是求两个序列的最长公共子序列 代码如下: #include<iostream> #include<cstdio> #include<cstring> using namespace std; int main() { int n1,n2,Case=0; while(scanf("%d%d",&n1,&n2)&&n1) { int a[110],b[110]; int dp[110][110],i,j; memse

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,10

codeforces 478D D. Red-Green Towers(dp)

题目链接: codeforces 478D 题目大意: 给出r个红砖,g个绿砖,问有多少种方法搭成最高的塔. 题目分析: 定义状态dp[i][j]表示构造i层的塔需要j块绿砖的方案数. 转移方程: dp[i][j]=dp[i?1][j?i]+dp[i?1][j] 分别代表当前这一层放绿砖还是放红砖(当然要先判断当前状态转移是否合法) AC代码: #include <iostream> #include <cstring> #include <cstdio> #inclu

Codeforces Round #273 (Div. 2) D. Red-Green Towers DP

链接: http://codeforces.com/problemset/problem/478/D 题意: 给出r个红砖,g个绿砖,问有多少种方法搭成最高的塔. 题解: 举红色球的层数,当第i层为红色是,i层上面有[0,r]个 红色的,可推出dp[i+j]=dp[i+j]+dp[j],最后 再统计红色的个数就行了,红色最少为max(h*(h+1)/2-g,0). 代码: 31 int dp[MAXN]; 32 33 int main() { 34 ios::sync_with_stdio(fa

UVa 10066 Twin Towers (DP 最长公共子序列)

题意  求两串数字最长公共子序列的长度 裸的lcs没啥说的 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=105; int a[maxn],b[maxn],d[maxn][maxn],na,nb; void lcs() { memset(d,0,sizeof(d)); for(int i=1; i<=na; ++i) for(in

D. Red-Green Towers Dp

题意:有两种颜色的积木 向上垒,每层只能是同一种颜色,且每层的个数等于层数.问有多少种垒法. Dp滚动数组搞下就好了. #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib>

topcoder 643 DIV2

太弱了,太弱了! A:基本的判断吧,然后就是边界问题,写了好久,结果发现时房间第二个交的.. B:真心跪了,还好想出来了,思路想的太慢太慢,结果交上去,落后太多,不过HACK时很多人挂了, 这也是DIV1的A题.做法是: 如果对于一个long long 的数质因数分解师很难做到的. 但是题目告诉了m/2个数,m是分解后质因数的个数. 然后我们想先刷法求出1-10^6的质因数. 如果n有大于10^6的质因数最多2个(n<=10^18)对吧. 然后已经写出了1个,一定会写出一个. 所以 我们对其用1

ARGB—Alpha,Red,Green,Blue

ARGB—Alpha,Red,Green,Blue        一种色彩模式,也就是RGB色彩模式附加上Alpha(透明度)通道,常见于32位位图的存储结构.        Alpha,图像通道,全强度为FF,表示无透明度,即不透明:无强度为00 ,表示全透明.因此,透明像素颜色值为 Alpha位-00,Red位.Green位.Blue位为任意值,例如:0x00FFFFFF为透明色,即透明. 如果图形卡具有32位总线,附加的8位信号就被用来保存不可见的透明度信号以方便处理用,这就是Alpha通