codeforces 659G G. Fence Divercity(dp)
题目链接:
2 seconds
256 megabytes
standard input
standard output
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 thei-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).
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.
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.
2
1 1
0
3
3 4 2
13 题意: 给你这样的一块东西(啊啊啊,不知道怎么说好),然后把它分成两块,当然两块肯定是在连续的,而且要求下面的一块每列至少为1,问有多少种切法;
思路: dp[i]表示在第i列包含高为min(a[i],a[i+1])的那一块的切法,转移的方程详细看代码;
一开始dp[i]想表示前i列的答案,后来发现这样转移方程还得用回溯各种麻烦,想了一下午才想到这样表示,感觉智商不够啊啊啊; AC代码:
/*
2014300227 659G - 28 GNU C++11 Accepted 234 ms 17820 KB
*/
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+;
const int mod=1e9+;
typedef long long ll;
ll dp[N],a[N];
int n,x;
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&x);
//cin>>a[i];//我去,此处居然就是传说中的cin会tle
a[i]=(ll)(x-);
}
ll ans=;
a[]=;
ans=a[];
dp[]=min(a[],a[]);
a[n+]=a[n]+;//dp[i]=包含min(a[i],a[i+1])的切法;
for(int i=;i<=n;i++)
{
dp[i]=min(a[i],a[i+])+min(a[i-],min(a[i],a[i+]))*dp[i-];
dp[i]%=mod;
if(a[i]<=a[i+])
{
ans+=dp[i];
}
else
{
ans+=a[i]+min(a[i],a[i-])*dp[i-];
}
ans%=mod;
}
cout<<ans<<"\n";
return ;
}
codeforces 659G G. Fence Divercity(dp)的更多相关文章
- Codeforces Round #346 (Div. 2) G. Fence Divercity dp
G. Fence Divercity 题目连接: http://www.codeforces.com/contest/659/problem/G Description Long ago, Vasil ...
- codeforces 659 G. Fence Divercity 组合数学 dp
http://codeforces.com/problemset/problem/659/G 思路: f(i,0/1,0/1) 表示到了第i个,要被切的块开始了没有,结束了没有的状态的方案数 递推看代 ...
- Codeforces 659G Fence Divercity dp
Fence Divercity 我们设a[ i ] 为第 i 个围栏被切的最靠下的位置, 我们发现a[ i ] 的最大取值有一下信息: 如果从i - 1过来并在 i 结束a[ i ] = min(h ...
- Codeforces 478D Red-Green Towers:dp
题目链接:http://codeforces.com/problemset/problem/478/D 题意: 给你r个红方块和g个绿方块,让你用这些方块堆一个塔. 最高层有1个方块,每往下一层块数+ ...
- [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)
[BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...
- [codeforces 549]G. Happy Line
[codeforces 549]G. Happy Line 试题描述 Do you like summer? Residents of Berland do. They especially love ...
- codeforces 337D Book of Evil(dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Book of Evil Paladin Manao caught the tra ...
- Codeforces 840C On the Bench dp
On the Bench 两个数如果所有质因子的奇偶性相同则是同一个数,问题就变成了给你n个数, 相同数字不能相邻的方案数. dp[ i ][ j ]表示前 i 种数字已经处理完, 还有 j 个位置需 ...
- Codeforces 1140G Double Tree 倍增 + dp
刚开始, 我以为两个点肯定是通过树上最短路径过去的, 无非是在两棵树之间来回切换, 这个可以用倍增 + dp 去维护它. 但是后来又发现, 它可以不通过树上最短路径过去, 我们考虑这样一种情况, 起点 ...
随机推荐
- swoole新手教程01-环境搭建及扩展安装
写在前面的废话 <swoole源代码分析>已经写了13章,整个swoole的核心架构基本都分析的差点儿相同了.于是心里一直以来想整理swoole的文档并写一份教程的想法就再度浮了出来. 实 ...
- IT项目管理-----给年轻工程师的十大忠告
http://blog.csdn.net/hbqhdlc/article/details/6201179给年轻工程师的十大忠告 诸位,咱当电子工程师也是十余年了,不算有出息,环顾四周,也没有看见几个有 ...
- Git小玩
早就听说了GitHub的强大. 一直没有机会去看, 在公司实习的几个月里也没机会接触SVN和Git, 可是抱着对Linus大神的崇敬, 和开源的崇敬之情. 趁着不忙的几天, 来学习一下Git. 希 ...
- python学习(二)python中的核心数据类型
数据类型是编程语言中的很重要的一个组成部分,我所知道的有数据类型的好处有:在内存中存放的格式知道,规定了有哪几种可用的操作. 我的埋点:为什么要有数据类型 那么python中的数据类型有哪几种呢? 对 ...
- KVM+VNC 虚拟机远程管理
1.安装kvm grep -E -o 'vmx|svm' /proc/cpuinfo #检查服务器是否支持虚拟化(vmx为interl平台.svm是AMD平台) #安装KVM所需软件包: yum gr ...
- JSP——Web应用
1.EL表达式 2.jstl fmt功能说明 3.jsp 自定义标签 4.QR码————二维码等条码
- onscreen and offscreen
本文来自stackoverflow一位网友的解答,感觉非常不错就摘录了. --------------------------------------------------------------- ...
- 开源项目Universal Image Loader for Android 说明文档 (1) 简单介绍
When developing applications for Android, one often facesthe problem of displaying some graphical ...
- 【BZOJ3956】Count 主席树+单调栈
[BZOJ3956]Count Description Input Output Sample Input 3 2 0 2 1 2 1 1 1 3 Sample Output 0 3 HINT M,N ...
- 编译EasyDSS rtmp流媒体服务器遇到stray '_239' inprogram,stray '_187' inprogram,stray '_191' inprogram的解决办法
使用用户提供的交叉编译工具链编译easydss时遇到一个编译错误 stray '\239' inprogram stray '\187' inprogram stray '\191' inprogra ...