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

我们现将高度进行离散化到Has数组中,然后定义dp数组

dp[i][j] 表示前i列的方案数,其中第i列中最小的连续元素(k-1, k)处在[ Has[j-1] + 1, Has[j] ]中间

dp[i][0] 表示没有连续元素的方案

然后更新就好了

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
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|1
const 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;
}

AtCoder Grand Contest 026 D - Histogram Coloring的更多相关文章

  1. AtCoder Grand Contest #026 C - String Coloring

    Time Limit: 3 sec / Memory Limit: 1024 MB Score : 600600 points Problem Statement You are given a st ...

  2. AtCoder Grand Contest 025 B - RGB Coloring

    B - RGB Coloring 求ax + by = k (0<=x<=n && 0<=y<=n)的方案数,最后乘上C(n, x)*C(n,y) 代码: #i ...

  3. AtCoder Grand Contest 030 (AGC030) C - Coloring Torus 构造

    原文链接https://www.cnblogs.com/zhouzhendong/p/AGC030C.html 题解 才发现当时是被题意杀了. 当时理解的题意是“对于任意的 (i,j) ,颜色 i 和 ...

  4. AtCoder Grand Contest 026 (AGC026) E - Synchronized Subsequence 贪心 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/AGC026E.html 题目传送门 - AGC026E 题意 给定一个长度为 $2n$ 的字符串,包含 $n$ ...

  5. AtCoder Grand Contest #026 B - rng_10s

    Time Limit: 2 sec / Memory Limit: 1024 MB Score : 600600 points Problem Statement Ringo Mart, a conv ...

  6. AtCoder Grand Contest #026 A - Colorful Slimes 2

    Time Limit: 2 sec / Memory Limit: 1024 MB Score : 200200 points Problem Statement Takahashi lives in ...

  7. Atcoder Grand Contest 026 (AGC026) F - Manju Game 博弈,动态规划

    原文链接www.cnblogs.com/zhouzhendong/AGC026F.html 前言 太久没有发博客了,前来水一发. 题解 不妨设先手是 A,后手是 B.定义 \(i\) 为奇数时,\(a ...

  8. AtCoder Grand Contest 012

    AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...

  9. AtCoder Grand Contest 011

    AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...

随机推荐

  1. scrapy---setting的字段含义

    # -*- coding: utf-8 -*- # Scrapy settings for lizi project # # For simplicity, this file contains on ...

  2. windows安装PHP IIS MYSQL

    https://www.cnblogs.com/huodong/p/4310425.html

  3. Linux Notes | Linux常用命令行笔记

    [ show all running processes ] (1) ps -aux | less 'ps' means: Process Status The -a option tells ps ...

  4. JNI由浅入深_2_C语言基础

    *含义 1.乘法 3*5 2.定义指针变量 int * p://定义了一个名字叫p的变量,能够存放int数据类型的地址 3.指针运算符, //如果p是一个已经定义好的指针变量则*p表示以p的内容为地址 ...

  5. 如何转换Xcode里打印的unicode编码日志

    转换Xcode里打印的unicode编码日志 1)打开Terminal 2)输入python 3)print(u'\u6027\u611f\u597d\u83b1\u575e\u5973\u661f\ ...

  6. js如何将一个json数组对应放置到另一数组中去

    需要将上面这个数组的每一个元素对应添加至下面的数组中 最后合成为这样的数组: 使用for循环,将第一个数组中的元素循环到第二个数组中

  7. DataGuard相关视图

    1.v$database SELECT name,open_mode,database_role,protection_mode,protection_level FROM v$database; 其 ...

  8. 修改Mac系统host文件

    第一步.在终端里面输入 sudo -i 获取临时获取管理员权限,会提示你输入密码,就是启动的密码. 第二步.输入 vi /etc/hosts  前面的vi是编辑器,当然也可以换用其他的,例如上面的na ...

  9. Qt5显示中文字符

    在cpp文件或.h文件中顶行输入: #pragma execution_character_set("utf-8")

  10. echarts通过ajax动态获取数据的方法

    echarts表格的数据一般都需要动态获取,所以总结了一下通过ajax动态获取数据的操作: 插入的方法应该不止一种,我也是接触不久,所以刚学会了一种插入方法: 灵感和经验来自:https://www. ...