Codeforces 8VC Venture Cup 2016 - Elimination Round F. Group Projects 差分DP*****

F. Group Projects
 

There are n students in a class working on group projects. The students will divide into groups (some students may be in groups alone), work on their independent pieces, and then discuss the results together. It takes the i-th student ai minutes to finish his/her independent piece.

If students work at different paces, it can be frustrating for the faster students and stressful for the slower ones. In particular, the imbalance of a group is defined as the maximum ai in the group minus the minimum ai in the group. Note that a group containing a single student has an imbalance of 0. How many ways are there for the students to divide into groups so that the total imbalance of all groups is at most k?

Two divisions are considered distinct if there exists a pair of students who work in the same group in one division but different groups in the other.

Input

The first line contains two space-separated integers n and k (1 ≤ n ≤ 200, 0 ≤ k ≤ 1000) — the number of students and the maximum total imbalance allowed, respectively.

The second line contains n space-separated integers ai (1 ≤ ai ≤ 500) — the time it takes the i-th student to complete his/her independent piece of work.

Output

Print a single integer, the number of ways the students can form groups. As the answer may be large, print its value modulo 109 + 7.

Examples
input
3 2
2 4 5
output
3

Note

In the first sample, we have three options:

  • The first and second students form a group, and the third student forms a group. Total imbalance is 2 + 0 = 2.
  • The first student forms a group, and the second and third students form a group. Total imbalance is 0 + 1 = 1.
  • All three students form their own groups. Total imbalance is 0.

In the third sample, the total imbalance must be 0, so each student must work individually.

 

题意:

  给n个人, 让我们分成若干组, 每组的价值是最大值减去最小值,所有组价值之和不能超过k

  求方案数

题解:

  这个是一道很强的DP

  我们用dp[i][j][k]表示考虑了前i个数了, 有j个组是开放的(所谓开放指的是只有最小值, 还没有最大值, 还可以进人), 当前值之和为k 的方案数。

  我们先排序, 这样, 对于开放的组, 每次的累加量就都是 j*(a[i] - a[i-1])。

  那么转移的情况要考虑这么几个:

  1. 第i个数单组一组

  2.第i个数新开一组, 作为新组的最小值

  3.第i个数关闭一组, 作为这个组的最大值。

  4.第i个数进入j个组中的某一组。

  提示,要理解差分思想

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 200+10, M = 1e3+20,inf = 2e9,mod = 1e9+7;


LL dp[2][N][M];
int n,m,a[N];
int main() {
    scanf("%d%d",&n,&m);
    for(int i = 1; i <= n; ++i) scanf("%d",&a[i]);
    sort(a+1,a+1+n);
    dp[0][0][0] = 1;
    a[0] = a[1];
    int now = 0;
    for(int i = 1; i <= n; ++i) {
        now ^= 1;
        memset(dp[now],0,sizeof(dp[now]));
        for(int j = 0; j <= i; ++j) {
            for(int k = 0; k <= m; ++k) {
                if(dp[now^1][j][k] == 0) continue;


                if(j*(a[i]-a[i-1])+k > m) break;

                dp[now][j][j*(a[i]-a[i-1])+k] += dp[now^1][j][k],dp[now][j][j*(a[i]-a[i-1])+k] %= mod;
                dp[now][j+1][j*(a[i]-a[i-1])+k] += dp[now^1][j][k],dp[now][j+1][j*(a[i]-a[i-1])+k] %= mod;

                if(j)dp[now][j-1][k+j*(a[i]-a[i-1])] += 1LL*j*dp[now^1][j][k],dp[now][j-1][k+j*(a[i]-a[i-1])]%=mod;

                dp[now][j][k+j*(a[i]-a[i-1])] += 1LL*j*dp[now^1][j][k],dp[now][j][k+j*(a[i]-a[i-1])] %= mod;

            }
        }
    }
    LL ans = 0;
    for(int i = 0; i <= m; ++i) {
        ans = (ans + dp[now][0][i])%mod;
    }
    printf("%lld\n",ans);
    return 0;
}

 

posted @ 2017-06-25 11:48  meekyan  阅读(245)  评论(0编辑  收藏  举报