博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #346 (Div. 2) G. Fence Divercity dp
阅读量:5896 次
发布时间:2019-06-19

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

G. Fence Divercity

题目连接:

Description

Long ago, Vasily built a good fence at his country house. Vasily calls a fence good, if it is a series of n consecutively fastened vertical boards of centimeter width, the height of each in centimeters is a positive integer. The house owner remembers that the height of the i-th board to the left is hi.

Today Vasily decided to change the design of the fence he had built, by cutting his top connected part so that the fence remained good. The cut part should consist of only the upper parts of the boards, while the adjacent parts must be interconnected (share a non-zero length before cutting out of the fence).

You, as Vasily's curious neighbor, will count the number of possible ways to cut exactly one part as is described above. Two ways to cut a part are called distinct, if for the remaining fences there is such i, that the height of the i-th boards vary.

As Vasily's fence can be very high and long, get the remainder after dividing the required number of ways by 1 000 000 007 (109 + 7).

Input

The first line contains integer n (1 ≤ n ≤ 1 000 000) — the number of boards in Vasily's fence.

The second line contains n space-separated numbers h1, h2, ..., hn (1 ≤ hi ≤ 109), where hi equals the height of the i-th board to the left.

Output

Print the remainder after dividing r by 1 000 000 007, where r is the number of ways to cut exactly one connected part so that the part consisted of the upper parts of the boards and the remaining fence was good.

Sample Input

2

1 1

Sample Output

0

Hint

题意

有一个围墙,这个人想拆掉一些围墙

拆掉的围墙必须是一个连通块,且不能将某一围墙的高度拆成0

且如果a[i][j]被拆除了,a[i][j+1]也必须被拆除。

现在问你拆除的方案数有多少个。

题解:

先让所有的h[i]--,那么:

定义cal(l,r)表示从左端点为l,右端点为r的拆除方案是多少个。

答案就是sigma(l,r)cal(l,r)

那么cal(l,l) = h[l]

cal(l,r) = min(h[l],h[l+1])*min(h[r],h[r-1])*PI(i=l+1,i=r-1)min(h[i],h[i-1],h[i+1])

这个化成递推式

cal(1,r+1) = h[r+1]+cal(1,r)*min(h[r-1],h[r],h[r+1])+min(h[r],h[r+1])

代码

#include
using namespace std;const int maxn = 1e6+7;const int mod = 1e9+7;long long h[maxn],dp[maxn][2];int n;int main(){ scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%lld",&h[i]),h[i]--; for(int i=1;i<=n;i++) { dp[i][0]=(dp[i-1][0]+dp[i-1][1]*min(h[i],h[i-1])+h[i])%mod; dp[i][1]=(min(h[i+1],h[i])+min(min(h[i],h[i-1]),h[i+1])*dp[i-1][1])%mod; } printf("%d\n",dp[n][0]);}

转载地址:http://uhxsx.baihongyu.com/

你可能感兴趣的文章
13-1 jQuery操作cookie
查看>>
2017-2018 20172309 《程序设计与数据结构(下)》第七章学习总结
查看>>
netbeans 调试 php
查看>>
js的事件流你真的弄明白了吗?
查看>>
sql 循环 ,随机数,循环插入一年数据
查看>>
快排模板(附求第k大的数)
查看>>
PAT L3-005. 垃圾箱分布
查看>>
BNU OJ 50998 BQG's Messy Code
查看>>
kibana连接elasticsearch集群做负载均衡
查看>>
逸鹏说道:性格色彩读后感
查看>>
eclipse快捷键(shift+ctrl+l能出来所有的快捷键)
查看>>
一个类的实例化对象所占空间的大小
查看>>
局部变量的指针和局部指针变量是两个不同概念
查看>>
Mysql数据库基础
查看>>
分享一款好看的城市选择器
查看>>
Math对象常用方法
查看>>
juqery提交表单所有内容包括图片
查看>>
jetty 入门
查看>>
Django中反向生成models
查看>>
Java正则表达式细节1
查看>>