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】

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+;
const int maxn=;
int vis[maxn][maxn][];
int a[maxn];
int T,n,k,kase=;
ll d[][maxn][];
inline int read()
{
int x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')
f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
int main()
{
n=read();
k=read();
for(int i=;i<=n;i++)
{
a[i]=read();
}
sort(a+,a++n);
a[]=a[];
int u=;
d[u][][]=;
for(int i=;i<=n;i++)
{
u^=;
memset(d[u],,sizeof(d[u]));
for(int j=;j<=n;j++)
{
int add=a[i]-a[i-];
for(int v=;v<=k;v++)
{
if(!d[u^][j][v])
continue;
if(v+j*add>k)//剪枝, 别忘了, 取模是很费时间的
break;
d[u][j][v+j*add]=(d[u][j][v+j*add]+d[u^][j][v])%mod;//自己一组
d[u][j][v+j*add]=(d[u][j][v+j*add]+d[u^][j][v]*j%mod)%mod;//随便扔到一组
if(j>)
{
d[u][j-][v+j*add]=(d[u][j-][v+j*add]+d[u^][j][v]*j%mod)%mod;//关闭一组,作为终点
}
d[u][j+][v+j*add]=(d[u][j+][v+j*add]+d[u^][j][v])%mod;//开启一组,作为起点
}
}
}
ll ans=;
for(int i=;i<=k;i++)
{
ans=(ans+d[u][][i])%mod;
}
cout<<ans<<endl;
return ;
}

Codeforces 626F Group Projects(滚动数组+差分dp)的更多相关文章

  1. [Codeforces 626F]Group Projects

    题目大意: 给定\(n\)个数\(a[1]\sim a[n]\),让你把它分为若干个集合,使每个集合内最大值与最小值的差的总和不超过\(K\).问总方案数. 解题思路: 一道很神的dp题. 首先将数进 ...

  2. Codeforces 626F Group Projects (DP)

    题目链接  8VC Venture Cup 2016 - Elimination Round 题意  把$n$个物品分成若干组,每个组的代价为组内价值的极差,求所有组的代价之和不超过$k$的方案数. ...

  3. 【滚动数组】 dp poj 1036

    题意:一群匪徒要进入一个酒店.酒店的门有k+1个状态,每个匪徒的参数是:进入时间,符合的状态,携带的钱. 酒店的门刚开始状态0,问最多这个酒店能得到的钱数. 思路: dp数组为DP[T][K]. 转移 ...

  4. [BZOJ1044][HAOI2008]木棍分割 二分 + 单调队列优化dp + 滚动数组优化dp

    Description 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, 砍完后n根木棍被分成了很多段,要求满足总长度最大的一段长 ...

  5. hdu 3392(滚动数组优化dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3392 Pie Time Limit: 6000/3000 MS (Java/Others)    Me ...

  6. bzoj21012101: [Usaco2010 Dec]Treasure Chest 藏宝箱(滚动数组优化dp)

    2101: [Usaco2010 Dec]Treasure Chest 藏宝箱 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 592  Solved:  ...

  7. POJ 1159 Palindrome-最长公共子序列问题+滚动数组(dp数组的重复利用)(结合奇偶性)

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  8. POJ3624 0-1背包(dp+滚动数组)

    Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 47440   Accepted: 20178 ...

  9. HDU 5119 Happy Matt Friends (背包DP + 滚动数组)

    题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt ...

随机推荐

  1. Function:html结构转字符串形式显示

    //Html结构转字符串形式显示 支持<br>换行 function ToHtmlString(htmlStr) { return toTXT(htmlStr).replace(/\&am ...

  2. MySQL创建一个固定频率执行且自定义"开始"时间的定时任务event

    drop event if exists evt_test;create event evt_teston schedule every 10 SECOND  -- 每10秒执行一次(second可以 ...

  3. 深度优先搜索(DFS)——部分和问题

    对于深度优先搜索,这里有篇写的不错的博客:DFS算法介绍 .总得来说是从某个状态开始,不断的转移状态知道无法转移,然后回到前一步的状态.如此不断的重复一直到找到最终的解.根据这个特点,常常会用到递归. ...

  4. bzoj 4199: [Noi2015]品酒大会

    Description 一年一度的"幻影阁夏日品酒大会"隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发"首席品酒家"和"首席猎手&quo ...

  5. centOS7 jdk安装

    1.查找需要卸载的OpenJDK: #  rpm -qa | grep java 2:依次卸载 rpm -e --nodeps javapackages-tools-3.4.1-6.el7_0.noa ...

  6. vue2.0路由变化1

    路由的步骤 1.定义组件 var Home={ template:'<h3>我是主页</h3>' }; var News={ template:'<h3>我是新闻& ...

  7. Spring基础篇——Spring容器和应用上下文理解

    上文说到,有了Spring之后,通过依赖注入的方式,我们的业务代码不用自己管理关联对象的生命周期.业务代码只需要按照业务本身的流程,走啊走啊,走到哪里,需要另外的对象来协助了,就给Spring说,我想 ...

  8. BeanShell Sample与控制台交互

    应用的场景:业务当中涉及到一些敏感信息,如验证码,银行卡密码等或是动态变化的,不能直接写在脚本中,就需要通过控制台手动输入 如密码已键盘的方式输入,在控制台中以密码的方式显示 处理方法:添加 bean ...

  9. java递归实现文件夹文件的遍历输出

    学习java后对一个面试小题(今年年初在团结湖面试的一个题目) 的习题的编写. ''给你一个文件,判断这个文件是否是目录,是目录则输入当前目录文件的个数和路径,''' /** * @author li ...

  10. thinkinginjava学习笔记02_对象

    对象 1. 对象通过一个引用来操作,但是java中的对象是按值传递的,基本上可以在操作中认为对象本身,在内部结构中仍然要记得是对象实体的引用:如:String s = "abcd" ...