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 }
- 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 数组属性和方法
- 如何使用Cython对python代码进行加密
- PHP快速排序算法实现的原理及代码详解
- 从ThinkPHP3.2.3过渡到ThinkPHP5.0学习笔记图文详解
- keras实现VGG16 CIFAR10数据集方式
- PyTorch: Softmax多分类实战操作
- 为什么称python为胶水语言
- opencv 图像礼帽和图像黑帽的实现
- python文件及目录操作代码汇总
- 使用pyplot.matshow()函数添加绘图标题
- 如何卸载python插件
- Keras实现支持masking的Flatten层代码
- 用opencv给图片换背景色的示例代码
- 解决Keras 自定义层时遇到版本的问题
- Laravel框架运行出错提示RuntimeException No application encryption key has been specified.解决方法
- Laravel访问出错提示:`Warning: require(/vendor/autoload.php): failed to open stream: No such file or di解决方法