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 }
- R语言的三种聚类方法
- ArrayList foreach 循环里进行元素的 remove add 操作有什么现象?
- 10个令人相见恨晚的R语言包
- 小心Windows旧版认证暴露你的系统帐户密码
- DNS Shell初体验
- 如何在Weka中加载CSV机器学习数据
- 老司机教你部署Cowrie蜜罐
- 时间序列预测如何变成有监督学习问题?
- 追踪溯源 | 希拉里邮箱泄露事件
- DNS Tunneling及相关实现
- 创建代码生成器可以很简单:如何通过T4模板生成代码?[下篇]
- 创建代码生成器可以很简单:如何通过T4模板生成代码?[上篇]
- 从Trace和Debug来看条件编译(Conditional Compilation)
- 解码针对工业工程领域的网络攻击 Operation Ghoul「食尸鬼行动」
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法