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】
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 }
作 者:Angel_Kitty
出 处:https://www.cnblogs.com/ECJTUACM-873284962/
关于作者:阿里云ACE,目前主要研究方向是Web安全漏洞以及反序列化。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信我
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是作者坚持原创和持续写作的最大动力!
欢迎大家关注我的微信公众号IT老实人(IThonest),如果您觉得文章对您有很大的帮助,您可以考虑赏博主一杯咖啡以资鼓励,您的肯定将是我最大的动力。thx.
我的公众号是IT老实人(IThonest),一个有故事的公众号,欢迎大家来这里讨论,共同进步,不断学习才能不断进步。扫下面的二维码或者收藏下面的二维码关注吧(长按下面的二维码图片、并选择识别图中的二维码),个人QQ和微信的二维码也已给出,扫描下面👇的二维码一起来讨论吧!!!
欢迎大家关注我的Github,一些文章的备份和平常做的一些项目会存放在这里。