# CodeForces 659G Fence Divercity (DP)

[code]:

#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;
typedef long long LL;
const LL MOD = 1e9+7;
const int maxn = 1e6+6;

int n,h[maxn];
LL dp[maxn][3];

int main(){
int i,j;
scanf("%d",&n);
for(i = 1;i <= n;i++) scanf("%d",&h[i]);
int a,b,c;
h[0] = 1;h[n+1] = 1;
for(i = 1;i <= n;i++){
a = min(h[i-1]-1,h[i]-1);
b = h[i];
c = min(h[i]-1,h[i+1]-1);
//printf("%d %d %d %d\n",i,a,b,c);
dp[i][0] = (a*dp[i-1][2]+a)%MOD;
dp[i][1] = (dp[i][0]+b-a)%MOD;
if(c <= a) dp[i][2] = (c*dp[i-1][2]+c)%MOD;
else dp[i][2] = (dp[i][0]+c-a)%MOD;
//printf("--> 0 : %I64d | 1 : %I64d | 2 : %I64d\n",dp[i][0],dp[i][1],dp[i][2]);
}
LL res = 0;
for(i = 1;i <= n;i++){
res = (res+dp[i][1]-1)%MOD;
}
res = (res + MOD)%MOD;
printf("%I64d\n",res);
return 0;
}

#### CodeForces - 659G Fence Divercity （DP）好题

2016-04-05 18:08:08

#### 【Codeforces659G】Fence Divercity【数学】【计数DP？】

2016-03-31 22:48:43

#### Codeforces 659G Fence Divercity (组合计数)

2016-03-31 15:43:16

#### POJ刷题顺序

2016-03-27 12:34:03

#### codeforces div2 346 G Fence Divercity Dp + 计数

2016-12-16 21:29:40

#### CodeForces 659G Fence Divercity (DP)

2018-04-15 18:18:45

#### CodeForces - 659G Fence Divercity （DP）★ ★ ★

2016-09-15 15:57:29

#### 占坑补题。。最近占的坑有点多。。。

2016-04-03 09:24:00

#### Codeforces 659G Fence Divercity

2016-07-17 08:14:04

#### codeforces 448CPainting Fence

2014-07-18 18:23:06