博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AtCoder Grand Contest 026 D - Histogram Coloring
阅读量:4706 次
发布时间:2019-06-10

本文共 1749 字,大约阅读时间需要 5 分钟。

一列中有两个连续的元素,那么下一列只能选择选择正好相反的填色方案(因为连续的地方填色方案已经确定,其他地方也就确定了)

我们现将高度进行离散化到Has数组中,然后定义dp数组
dp[i][j] 表示前i列的方案数,其中第i列中最小的连续元素(k-1, k)处在[ Has[j-1] + 1, Has[j] ]中间
dp[i][0] 表示没有连续元素的方案
然后更新就好了

#include 
#include
#include
#include
#include
#include
using namespace std;const int N = 105;const int INF = 0x3f3f3f3f;typedef long long ll;#define lson l, m, rt<<1#define rson m+1, r, rt<<1|1const int MOD = 1e9+7;int h[N];int Has[N]; int tot;ll dp[N][N];ll Pow(ll x, ll y) { if(y <= 0) return 1; ll result = 1; while(y) { if(y & 1) result = result * x % MOD; y >>= 1; x = x*x % MOD; } return result;}int main() { int n; while(~scanf("%d", &n)) { tot = 0; memset(dp, 0, sizeof(dp)); h[0] = 0; dp[0][0] = 1; for(int i = 1; i <= n; ++i) { scanf("%d", &h[i]); Has[++tot] = h[i]; } sort(Has + 1, Has + tot + 1); tot = unique(Has+1, Has + tot + 1) - Has - 1; for(int i = 1; i <= n; ++i) { h[i] = lower_bound(Has + 1, Has + tot + 1, h[i]) - Has; } for(int i = 1; i <= n; ++i) { dp[i][0] = dp[i-1][0] * 2 % MOD; for(int j = h[i] + 1; j <= h[i-1]; ++j) dp[i][0] = (dp[i][0] + dp[i-1][j] * 2 % MOD) % MOD; ll tmpPow = Pow(2, Has[h[i]] - Has[h[i-1]]); for(int j = 1; j <= min(h[i-1], h[i]) ; ++j) { dp[i][j] = dp[i-1][j] * tmpPow % MOD; } for(int j = h[i-1] + 1; j <= h[i]; ++j) { dp[i][j] = (dp[i][j] + j==1? ( dp[i-1][0] * ( Pow(2, Has[j]) - 2) % MOD * Pow(2, Has[h[i]] - Has[j]) % MOD ) : ( dp[i-1][0] * 2 * (Pow(2, Has[j]-Has[j-1]) - 1) % MOD * Pow(2, Has[h[i]]-Has[j]) % MOD ) ) %MOD; } } ll result = 0; for(int i = 0; i <= tot; ++i) { result = (result + dp[n][i]) % MOD; } printf("%lld\n", result); } return 0;}

转载于:https://www.cnblogs.com/Basasuya/p/9373547.html

你可能感兴趣的文章
UVA 116 Unidirectional TSP (白书dp)
查看>>
第三方测速工具
查看>>
数据访问 投票习题
查看>>
cnblog!i'm coming!
查看>>
使用点符号代替溢出的文本
查看>>
fatal: remote origin already exists.
查看>>
LeetCode 242. Valid Anagram
查看>>
JSP表单提交乱码
查看>>
如何适应现代雇佣关系
查看>>
团队项目(第五周)
查看>>
SQL 优化经验总结34条
查看>>
开源 视频会议 收藏
查看>>
核心J2EE模式 - 截取过滤器
查看>>
.net开源CMS
查看>>
JdbcTemplate
查看>>
第一次使用maven记录
查看>>
SharePoint服务器端对象模型 之 使用CAML进展数据查询
查看>>
Building Tablet PC Applications ROB JARRETT
查看>>
Adobe® Reader®.插件开发
查看>>
【POJ 3461】Oulipo
查看>>