CodeForces - 659G Fence Divercity (DP)好题
来源:互联网 发布:网络诈骗报警电话1245 编辑:程序博客网 时间:2024/06/05 12:39
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
21 1
0
33 4 2
13
Hint
From the fence from the first example it is impossible to cut exactly one piece so as the remaining fence was good.
All the possible variants of the resulting fence from the second sample look as follows (the grey shows the cut out part):
Source
#include<stdio.h>#include<string.h>#include<math.h>#include<map>#include<queue>#include<stack>#include<algorithm>#include<iostream>#define INF 0x3f3f3f3f#define ull unsigned long long#define ll long long#define IN __int64#define N 1000010#define M 1000000007using namespace std;ll h[N],dp[N][2];int n;int main(){int i,j;while(scanf("%d",&n)!=EOF){for(i=1;i<=n;i++)scanf("%lld",&h[i]),h[i]--;for(i=1;i<=n;i++){dp[i][0]=(dp[i-1][0]+dp[i-1][1]*min(h[i],h[i-1])+h[i])%M;dp[i][1]=(min(h[i+1],h[i])+min(min(h[i],h[i-1]),h[i+1])*dp[i-1][1])%M;}printf("%d\n",dp[n][0]);}return 0;}
- CodeForces - 659G Fence Divercity (DP)好题
- CodeForces 659G Fence Divercity (DP)
- CodeForces - 659G Fence Divercity (DP)★ ★ ★
- Codeforces 659G Fence Divercity
- Codeforces 659G Fence Divercity (组合计数)
- codeforces div2 346 G Fence Divercity Dp + 计数
- 【Codeforces659G】Fence Divercity【数学】【计数DP?】
- Poj1037 A decorative fence(DP好题)
- codeforces Wooden Fence 简单DP
- codeforces 240B - Fence DP
- codeforces 234F Fence (dp)
- Codeforces Round #256 (Div. 2) C. Painting Fence(分治+贪心)(好题)
- poj 1037 A decorative fence(dp,好题)
- CodeForces 349B Color the Fence (dp)
- Codeforces Round #117 (Div. 2)E---Wooden Fence(dp)
- CodeForces 448 C.Painting Fence(区间DP)
- codeforces 182E Wooden Fence(方案数DP)【模板】
- codeforces 182E Wooden Fence(方案数DP)【模板】
- linux进程
- 下拉刷新Phoenix分析
- 【Linux】AWK 简明教程
- RabbitMQ使用场景
- Java开发环境搭建(Windows7)
- CodeForces - 659G Fence Divercity (DP)好题
- Android 中一些小经验总结
- Sublime Text 输入中文显示方框问号乱码
- Linux 常用操作
- 关于RMAN-06207和RMAN-06208错误的分析以及解决办法
- iOS知识----获取HTML5 的 标题(title)
- 【Linux】Sed简明教程
- Android开发常用工具方法
- vim搜索