Codeforces 626F Group Projects(滚动数组+差分dp)

F. Group Projects

time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

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
Input
4 3
7 8 9 10
Output
13
Input
4 0
5 10 20 21
Output
1
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.

题目链接:http://codeforces.com/contest/626/problem/F

题意:给n个人, 让我们分成若干组, 每组的值是最大值减去最小值, 求所有组的和。

思路:显然, 需要用DP的思想, 但是该题DP设计的非常巧妙, 而且状态转移的情况容易考虑不全。

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

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

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

1. 第i个数单组一组

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

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

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

需要用滚动数组, 否则会爆内存。

吐槽一句:cf的测评机真的是让人心态爆炸,跑的太慢了QAQ

心态崩了,数组开大了,开三维的神TM知道会开大了一点点QAQ

下面给出AC代码:【就当学习了QAQ】

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int mod=1e9+7;
 5 const int maxn=222;
 6 int vis[maxn][maxn][1020];
 7 int a[maxn];
 8 int T,n,k,kase=1;
 9 ll d[2][maxn][1020];
10 inline int read()
11 {
12     int x=0,f=1;
13     char ch=getchar();
14     while(ch<'0'||ch>'9')
15     {
16         if(ch=='-')
17             f=-1;
18         ch=getchar();
19     }
20     while(ch>='0'&&ch<='9')
21     {
22         x=x*10+ch-'0';
23         ch=getchar();
24     }
25     return x*f;
26 }
27 int main()
28 {
29     n=read();
30     k=read();
31     for(int i=1;i<=n;i++)
32     {
33         a[i]=read();
34     }
35     sort(a+1,a+1+n);
36     a[0]=a[1];
37     int u=0;
38     d[u][0][0]=1;
39     for(int i=1;i<=n;i++)
40     {
41         u^=1;
42         memset(d[u],0,sizeof(d[u]));
43         for(int j=0;j<=n;j++)
44         {
45             int add=a[i]-a[i-1];
46             for(int v=0;v<=k;v++)
47             {
48                 if(!d[u^1][j][v])
49                     continue;
50                 if(v+j*add>k)//剪枝, 别忘了, 取模是很费时间的
51                     break;
52                 d[u][j][v+j*add]=(d[u][j][v+j*add]+d[u^1][j][v])%mod;//自己一组
53                 d[u][j][v+j*add]=(d[u][j][v+j*add]+d[u^1][j][v]*j%mod)%mod;//随便扔到一组
54                 if(j>0)
55                 {
56                     d[u][j-1][v+j*add]=(d[u][j-1][v+j*add]+d[u^1][j][v]*j%mod)%mod;//关闭一组,作为终点
57                 }
58                 d[u][j+1][v+j*add]=(d[u][j+1][v+j*add]+d[u^1][j][v])%mod;//开启一组,作为起点
59             }
60         }
61     }
62     ll ans=0;
63     for(int i=0;i<=k;i++)
64     {
65         ans=(ans+d[u][0][i])%mod;
66     }
67     cout<<ans<<endl;
68     return 0;
69 }

 

posted @ 2017-06-29 21:00  Angel_Kitty  阅读(784)  评论(1编辑  收藏  举报