Codeforces 466D Increase Sequence(dp+组合数学)

题目链接:Codeforces 466D Increase Sequence

题目大意:给定一个序列,现在可以选中一段区间,使得整段区间上每个位置数加1,要求最后每个位置都为h,并且选中的区间不能有相同l或则r。

解题思路:因为每个位置最多有一个起始和一个终止(区间)。

  • ai和ai+1差的绝对值超过1,则肯定是不行的,
  • ai+1?ai=1,那么一定要从i+1的位置新起一段区间
  • ai+1?ai=?1,那么一定要在i+1的位置上终止一段区间,C(1ai)
  • ai+1?ai=0,可以不变,也可以终止并新起一段。
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;
const int maxn = 2005;
const ll mod = 1e9+7;

int N, H, arr[maxn];

int solve () {
    N++;
    ll ret = 1;

    for (int i = 1; i <= N; i++) {
        int t = arr[i] - arr[i-1];

        if (t > 1 || t < -1)
            return 0;
        else if (t == 0)
            ret = ret * (arr[i] + 1) % mod;
        else if (t == -1)
            ret = ret * arr[i-1] % mod;
    }
    return ret;
}

int main() {
    scanf("%d%d", &N, &H);
    for (int i = 1; i <= N; i++) {
        scanf("%d", &arr[i]);
        arr[i] = H - arr[i];
    }
    printf("%d\n", solve());
    return 0;
}
时间: 2024-12-17 06:40:03

Codeforces 466D Increase Sequence(dp+组合数学)的相关文章

[Codeforces 1295F]Good Contest(DP+组合数学)

[Codeforces 1295F]Good Contest(DP+组合数学) 题面 有一个长度为\(n\)的整数序列,第\(i\)个数的值在\([l_i,r_i]\)中随机产生.问这个序列是一个不上升序列的概率(模\(998244353\)意义下). \(n \leq 50,l_i,r_i \leq 998244351\) 分析 和[APIO2016]划艇几乎一模一样.可惜比赛的时候时间不够. 首先把问题转化成求最长不上升序列的数量. 我们把这些区间离散化,分割成两两之间不互相覆盖的若干个区间

Codeforces 13C Sequence --DP+离散化

题意:给出一个 n (1 <= n <= 5000)个数的序列 .每个操作可以把 n 个数中的某一个加1 或 减 1.问使这个序列变成非递减的操作数最少是多少 解法:定义dp[i][j]为将前i个数变为以j为结尾的非递减序列的最少操作次数. 则有: dp[i][j] = min(dp[i][j], min(dp[i][k]) + Cost(原来第i个位置上的数转换到j))  (1 <= k <= j) 即前i个数以j结尾的状态可以由前i-1个数以小于等于j的k结尾的状态转移过来,取

hdu4908 &amp; BestCoder Round #3 BestCoder Sequence(组合数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4908 BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 618    Accepted Submission(s): 214 Problem Description Mr Potato is a code

CodeForces 18E Flag 2 dp

题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h> #include<queue> #include<string> #include<stdlib.h> #include<a

HDU 4908 BestCoder Sequence(组合数学)

HDU 4908 BestCoder Sequence 题目链接 题意:给定一个序列,1-n的数字,选定一个作为中位数m,要求有多少连续子序列满足中位数是m 思路:组合数学,记录下m左边和右边一共有多少种情况大于m的数字和小于n数组的差,然后等于左边乘右边所有的和,然后最后记得加上左右两边差为0的情况. 当时也是比较逗,还用树状数组去搞了,其实完全没必要 代码: #include <cstdio> #include <cstring> #define lowbit(x) (x&am

Codeforces 41D Pawn 简单dp

题目链接:点击打开链接 给定n*m 的矩阵 常数k 下面一个n*m的矩阵,每个位置由 0-9的一个整数表示 问: 从最后一行开始向上走到第一行使得路径上的和 % (k+1) == 0 每个格子只能向或走一步 求:最大的路径和 最后一行的哪个位置作为起点 从下到上的路径 思路: 简单dp #include <cstdio> #include <algorithm> #include<iostream> #include<string.h> #include &

CodeForces 19B Checkout Assistant dp

题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <iostream> #include <map> #include <set> #include <math.h> using namespace std; #define inf 115292150460684697

poj 2081 Recaman&#39;s Sequence (dp)

Recaman's Sequence Time Limit: 3000MS   Memory Limit: 60000K Total Submissions: 22566   Accepted: 9697 Description The Recaman's sequence is defined by a0 = 0 ; for m > 0, am = am−1 − m if the rsulting am is positive and not already in the sequence,

Codeforces 451D Count Good Substrings(组合数学)

题目链接:Codeforces 451D Count Good Substrings 题目大意:定义good string,就是就一个字符串的连续相同字符用一个该字符替代后,形成回文串的字符串.现在给出一个字符串,问说该字符串的子串中,为good string的串有多少个,分长度为奇数和偶数的输出. 解题思路:因为字符串的组成为a和b,所以只要是头尾相同的子串都是满足的.所以我们计算在奇数和偶数位置的奇数个数和偶数个数即可,然后用组合数学求出答案. #include <cstdio> #inc