E. Tutorial Groupings (DP + Thinking)

Title:

gives you NNN number, then let you group, each group has at mostsss elements, and the entire sequence is required to be strictly increasing, that is,T i T_iTiThe largest element of the group is strictly less than T i + 1 T_{i+1}Ti+1the smallest element in . And the range in a group is at most kkk . Ask the number of programs? Modulo 1e9+7.

analyze:

First we look at the data range n is 10000 100001 0 0 0 0 , s is100 1001 0 0 ,
then we can usedp [ i ] [ j ] dp[i][j]d p ​​[ i ] [ j ] represents the i-th position, the number of solutions ending in a group of length j.

Two cases:

  • d = 1 d=1j=1. Then we can think ofdp[i][1] dp[i][1]d p ​​[ i ] [ 1 ] represents the i-th position, and he ends up as a group. What state can he transfer from there? He can get it fromdp [ i − 1 ] [ 1 ] , dp [ i − 1 ] [ 2 ] . . . . . . dp [ i − 1 ] [ s ] dp[i-1][1],dp[i -1][2]......dp[i-1][s]dp[i1][1],dp[i1][2]......dp[i1 ] [ s ] Transfer over, which is equivalent to starting a new stove.

  • Then we look at j != 1 j!=1j!=1 d p [ i ] [ j ] dp[i][j] d p ​​[ i ] [ j ] is definitely fromdp [ i − 1 ] [ j − 1 ] dp[i-1][j-1]dp[i1][j1 ] , and then add one more last time. If you can put it down, you can take over the previous state. If you can't, it means you can't reach it.
    Whether it can be put down depends on whether it is satisfied: whether the range within this group is less than or equal to k. That is, whether the difference between the largest and the smallest meets the conditions. If the condition is met:dp [ i ] [ j ] = dp [ i − 1 ] [ j − 1 ] ; dp[i][j]=dp[i-1][j-1];dp[i][j]=dp[i1][j1 ] ; if notdp [ i ] [ j ] = 0 ; dp[i][j]=0;dp[i][j]=0;

///苟利国家生死以,岂因祸福避趋之。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 2e5 + 7;
const int mod = 1e9 + 7;
const double pi = 3.14159265358979;
ll n,k,s;
ll a[maxn];
ll dp[20000][200];
ll sum;
int main()
{
    
    
    cin >> n >> k >> s;
    for(int i = 1; i <= n; i++)
    {
    
    
        cin >> a[i];
    }
    sort(a + 1, a + 1 + n);

    for(int i = 1; i <= n; i++) dp[i][1] = 1;    
	///直接dp[1][1]=1也行。
    for(ll i = 2; i <= n; i++)
    {
    
    
        sum=0;
        for(ll j=1;j<=s;j++) sum+=dp[i-1][j],sum%=mod;

        dp[i][1]=sum;

        for(ll j=2;j<=min(i,s);j++){
    
    ///j 是i位置长度 那么 i-1位的长度为j-1
            if(a[i]-a[i-j+1]<=k){
    
    
                dp[i][j]=dp[i-1][j-1];
            }else {
    
    
                dp[i][j]=0;
            }
        }
    }
    sum=0;
    for(int i=1;i<=s;i++){
    
    
        sum+=dp[n][i];
        sum%=mod;
    }
    cout<<sum<<endl;
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326263491&siteId=291194637