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

我们现将高度进行离散化到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. Python cx_oracle自动化操作oracle数据库增删改查封装,优化返回查询数据

    # coding=utf-8 import cx_Oracle import os import json os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_C ...

  2. Linux文本处理

    作为一名 Linux 研发人员,几乎每天都要面对文本处理场景. 因此 掌握文本处理套路 并 熟练运用文本处理命令 ,对于 提升工作效率 意义重大. 本文以一个实战例子抛砖引玉,介绍如何运用 grep ...

  3. mina 通讯框架

    Apache Mina Server 是一个网络通信应用框架,也就是说,它主要是对基于TCP/IP.UDP/IP协议栈的通信框架(当然,也可以提供JAVA 对象的序列化服务.虚拟机管道通信服务等),M ...

  4. No MyBatis mapper was found in '[com.wuji.springboot]' package. Please check your configuration

    No MyBatis mapper was found in '[com.wuji.springboot]' package. Please check your configuration. 这个原 ...

  5. jquery里遍历普通数组和多维数组的方法及实例

    jquery里遍历数组用的是$.each,下面站长给大家几个具体的实例: 实例1.遍历一个普通的一维数组: 1 2 3 4 5 6 7 8 <script> //声明数据有下面两种方式 / ...

  6. 记如何用树莓派3开一个无线AP

    开热点 忘掉自己手动配置吧 create_ap git clone git@github.com:oblique/create_ap.git cd create_ap sudo make instal ...

  7. iOS 12 越狱支持 Cydia

    Geosn0w在1月31日宣布推出 OsirisJailbreak12 越狱工具,是目前公开的第一个支持 iOS 12 的越狱,支持 iOS 12.0-12.1.2.项目地址:https://gith ...

  8. ETL项目1:大数据采集,清洗,处理:使用MapReduce进行离线数据分析完整项目

    ETL项目1:大数据采集,清洗,处理:使用MapReduce进行离线数据分析完整项目 思路分析: 1.1 log日志生成 用curl模拟请求,nginx反向代理80端口来生成日志. #! /bin/b ...

  9. urllib库使用方法 4 create headers

    import urllib.requestimport urllib.parse url = "https://www.baidu.com/"#普通请求方法response = u ...

  10. vue 解决跨域问题

    1.后端处理允许跨域 2.反向代理跨域 代理服务器英文全称是Proxy Server,其功能就是代理网络用户去去的网络信息.形象的说:它是网络信息的中转站. vue中设置代理: 1.config/in ...