【codeforces 466D】Increase Sequence

【题目链接】:http://codeforces.com/problemset/problem/466/D

【题意】

给你n个数字;
让你选择若干个区间;
且这些区间[li,ri];
左端点不能一样;
右端点也不能有一样的;
你能把这些区间内的元素递增1;
问你把所有的n个元素都变成h有多少种方案;.

【题解】

设dp[i][j];
设前i个数字都已经变成h,且有j个区间的左端点没有被匹配的方案数;
每个位置都有些一些选择;
‘-’ 什么都不放
‘]’ 放一个右端点在这个位置;
‘][‘放一个右端点在这,同时立刻开始一个新的区间
‘[]’放一个左端点然后立刻放一个右端点;
‘[‘放一个左端点在这;
①对于a[i]+j==h
则第i个位置有两种选择;
1.’[’ ->则dp[i][j] += dp[i-1][j-1]
2.’-‘->则dp[i][j] += dp[i-1][j];
其他的可以自己试试,会发现不行的
②对于a[i]+j==h-1
则第i个位置有3个选择
1.if (j > 0) ‘][‘->则dp[i][j]+=dp[i-1][j]*j; // 这里的j表示和之前的j个左端点中的某一个匹配;
2.’]’ dp[i][j] += dp[i-1][j+1]*(j+1);//和之前的j+1个左端点中的某一个匹配;
3.’[]’ dp[i][j] += dp[i-1][j];
…其他的都不行的;
最后输出dp[n][0]就好;
③a[i]+j==其他
没有方案;

【Number Of WA

3(判断dp[i][j]>=MOD之后,要一直减MOD,不能只判断一个if);

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 2000+100;
const LL MOD = 1e9+7;

int n,h;
LL dp[N][N],a[N];

void add(LL &a,LL b){
    a = a+b;
    while (a>=MOD) a-=MOD;
}

int main(){
    //Open();
    Close();
    cin >> n >> h;
    rep1(i,1,n){
        cin >> a[i];
    }
    dp[1][0] = ((h==a[1])||(h==a[1]+1))?1:0;
    dp[1][1] = (h==a[1]+1)?1:0;
    rep1(i,2,n){
        rep1(j,max((long long) 0,h-a[i]-1),min((long long)i,h-a[i])){
            if (a[i]+j==h){
                if (j) add(dp[i][j],dp[i-1][j-1]);// [
                add(dp[i][j],dp[i-1][j]);// -
                //][ x
                // ] x
                // [] x
            }
            if (a[i]+j+1==h){
                    //[ x
                    // - x
                if (j > 0) add(dp[i][j],dp[i-1][j]*j);//][
                add(dp[i][j],dp[i-1][j+1]*(j+1));// ]
                add(dp[i][j],dp[i-1][j]);//[]
            }
        }
    }
    cout << dp[n][0] << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626252.html