Codeforces 626F 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.
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.
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.
3 2
2 4 5
3
4 3
7 8 9 10
13
4 0
5 10 20 21
1
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)的更多相关文章
- [Codeforces 626F]Group Projects
题目大意: 给定\(n\)个数\(a[1]\sim a[n]\),让你把它分为若干个集合,使每个集合内最大值与最小值的差的总和不超过\(K\).问总方案数. 解题思路: 一道很神的dp题. 首先将数进 ...
- Codeforces 626F Group Projects (DP)
题目链接 8VC Venture Cup 2016 - Elimination Round 题意 把$n$个物品分成若干组,每个组的代价为组内价值的极差,求所有组的代价之和不超过$k$的方案数. ...
- 【滚动数组】 dp poj 1036
题意:一群匪徒要进入一个酒店.酒店的门有k+1个状态,每个匪徒的参数是:进入时间,符合的状态,携带的钱. 酒店的门刚开始状态0,问最多这个酒店能得到的钱数. 思路: dp数组为DP[T][K]. 转移 ...
- [BZOJ1044][HAOI2008]木棍分割 二分 + 单调队列优化dp + 滚动数组优化dp
Description 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, 砍完后n根木棍被分成了很多段,要求满足总长度最大的一段长 ...
- hdu 3392(滚动数组优化dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3392 Pie Time Limit: 6000/3000 MS (Java/Others) Me ...
- bzoj21012101: [Usaco2010 Dec]Treasure Chest 藏宝箱(滚动数组优化dp)
2101: [Usaco2010 Dec]Treasure Chest 藏宝箱 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 592 Solved: ...
- POJ 1159 Palindrome-最长公共子序列问题+滚动数组(dp数组的重复利用)(结合奇偶性)
Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...
- POJ3624 0-1背包(dp+滚动数组)
Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 47440 Accepted: 20178 ...
- HDU 5119 Happy Matt Friends (背包DP + 滚动数组)
题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt ...
随机推荐
- cocoapods管理以及常遇到的问题
CocoaPods使用 安装成功啦,咱们来创建Podfile文件 //咱们先滚去项目的根目录,如果不会,你就先滚去看看shell命令教程吧 $ cd /Users/JamesGu/Desktop/Co ...
- [Maven] Missing artifact
今天从朋友那拷过来一个maven工程,eclipse中maven配置好了,maven仓库也配置完毕,但是一直报Missing artifact,然后开网执行maven update,下载完jar后,还 ...
- echarts异步数据加载(在下拉框选择事件中异步更新数据)
接触echarts 大半年了,从不会到熟练也做过不少的图表,隔了一段时间没使用这玩意,好多东西真心容易忘了.在接触echarts这期间也没有总结什么东西,今天我就来总结一下如何在echart中异步加载 ...
- Achartengine.jar绘制动态图形一 --饼图
PS:我们在做安卓程序的时候,免不了会做一些图形,自己可以选择自定义view ,就是用Canvas画,也可以用写好的jar包,就是achartengine.jar,使用jar包的好处就快速绘制图形,不 ...
- 微信小程序+“芝麻小客服”可设自动关注公众号,助力运营闭环
微信小程序全面上线已经接近1年的时间,从最初的"用完即走"理念到2017年总计更新开放60余次的功能创新,微信小程序不一定会爆发下一次的红利,但绝对是微信生态中重要的一环. 芝麻小 ...
- java RTTI笔记 之Class学习笔记(摘自java编程思想)
1.java 使用Class对象来执行其RTTI.java 中每个类在编译后都会对应产生一个Class对象(更恰当地说是被保存在一个同名的.class文件中),甚至void和基本类型也都对应一个cla ...
- 妙味课堂:JavaScript初级--第11课:字符串、查找高亮显示
1.数字字母 Unicode 编码 <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content- ...
- 使用 BeanUtils 报错解决记录
在使用BeanUtils.populate方法时,报错如下: java.lang.NoClassDefFoundError: org/apache/commons/collections/FastHa ...
- CSS3 美女动画相框
把下面的内容放到一个body内,运行看一看:) <style> *{ margin:0; padding:0;} .bg1{ background-image:-moz-linear-gr ...
- 带以太网的MicroPython开发板:TPYBoardv201温湿度上传实例
转载请以链接形式注明文章来源,MicroPythonQQ交流群:157816561,公众号:MicroPython玩家汇 历来关于温湿度的检测都是没有间断过的,这次我们继续检测温湿度,同样还是使用DH ...