zoukankan      html  css  js  c++  java
  • Codeforces Round #346 (Div. 2) G. Fence Divercity dp

    G. Fence Divercity

    题目连接:

    http://www.codeforces.com/contest/659/problem/G

    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<bits/stdc++.h>
    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
    ",dp[n][0]);
    }
  • 相关阅读:
    arcgis 图幅结合图表工具
    Geoprocessor clip
    Spring Boot总结,核心功能,优缺点
    代码下载python 简体和繁体的转换
    电话错误fmxl Missing handler script
    nullnull使用ps绘制像word里面的箭头
    nullnulljava把过滤掉的 % 号拿回来
    问题总结两天来两场实习面试(中科创达、华为)
    模板拷贝【便利贴】Struts性能优化
    匹配优先级Spring攻略学习笔记(3.04)指定Aspect优先级
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5352071.html
Copyright © 2011-2022 走看看