码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces 478D Red-Green Towers (DP)

时间:2017-07-13 23:39:14      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:put   div   scan   应该   link   logs   lin   codeforce   tac   

题意:给定 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)

标签:put   div   scan   应该   link   logs   lin   codeforce   tac   

原文地址:http://www.cnblogs.com/dwtfukgv/p/7163419.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!