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 去维护它. 但是后来又发现, 它可以不通过树上最短路径过去, 我们考虑这样一种情况, 起点 ...
随机推荐
- OpenTSDB设计解读
OpenTSDB是基于HBase存储时间序列数据的一个开源数据库,确切地说,它仅仅是一个HBase的应用而已,其对于时间序列数据的处理能够供其它系统參考和借鉴.本文会针对它在数据库的设计方面展开一些探 ...
- lambda表达式转换sql
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin ...
- ETL拉链算法汇总大全
拉链算法总结大全: 一.0610算法(追加) 1.删除仓库表的载入日期是本次载入日期的数据,以支持重跑 delete from xxx where start_dt >=$tx_date; 2. ...
- phpdoctor 安装,配置,生成文档
window 下安装phpdoctor 1 安装php,设置环境变量path ,把php 的安装路径加上,比如php 安装在d:/php5/ 2下载phpdoctor,可以去官网下载 http://p ...
- GIT简单使用——私人库篇
1.生成公钥公钥是远程库识别您的用户身份的一种认证方式,通过公钥,您可以将本地git项目与远程库建立联系,然后您就可以很方便的将本地代码上传到远程库,或者将远程库代码下载到本地了.$ ssh-keyg ...
- String 的fomat方法日期转换
一.常规类型.字符类型和数值类型的格式说明符的语法如下:%[argument_index$][flags][width][.precision]conversion 可选的 argument_inde ...
- 把对象写入Postgresql中
工作中,遇到把大对象写入Postgresql数据库中 package com.geni_sage.gdme.cws.dic; import java.io.BufferedInputStream; i ...
- oracle 推断字符是否为字母
create or replace function ischar(chr varchar2) return varchar2 is ischr varchar2(5); begin sele ...
- Android NDK开发初步
在配置好NDK开发之后就能够使用C/C++开发android了.以下以一个HelloWorld项目来说明 1.新建一个Androidproject 新建一个HelloWorldproject 代码例如 ...
- Log4j2升级jar包冲突问题
升级Log4j2后日志提示jar包冲突: SLF4J: Class path contains multiple SLF4J bindings.SLF4J: Found binding in [jar ...