Codeforces 626F Group Projects(滚动数组+差分dp)

摘要:
我们首先对它们进行排序,这样对于开放组,每次累加都是j*。您需要使用滚动数组,否则内存将爆裂。吐槽:cf的评估机器真的让人的思维爆炸,运行太慢,QAQ的思维崩溃,阵列扩大,3D ShenTM知道QAQ会扩大一点。这里是AC代码:1#include2usingspacestd;3型超长;4约束模=1e9+7;5约束最大值=222;6intvis[maxn][maxn][1020];7英寸[maxn];8intT,n,k,kase=1;9lld[2][maxn][1020];10inlinethread()11{12intx=0,f=1;13charch=getchar();14while15{16if17f=-1;18ch=getchar();19}20while21{22x=x*10+ch-'0';23ch=gethar();24}25returnx*f;26}27intmain()28{29n=read();30k=read);31,32{33a[i]=read;34}35排序;36a[0]=a[1];37intu=0;38d[u][0][0]=1;39对于40{41u^=1;42内存集;43对于44{45intadd=a[i]-a[i-1];46对于47{48if(!D[u^1][j][v-1][v+j*add]=%m
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

Codeforces 626F Group Projects(滚动数组+差分dp)第1张

心态崩了,数组开大了,开三维的神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 }

免责声明:文章转载自《Codeforces 626F Group Projects(滚动数组+差分dp)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇给tomcat配置manager-gui账号关于Ubuntu18.04 linux系统使用安装JDK Mysql下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

【使用 DOM】为DOM元素设置样式

DOCTYPE html˃设置DOM元素的样式p{border:中双绿色;背景颜色:浅灰色;}#block1{color:白色;}table{border:thinsolided;border collapse:collapse;margin:5px;float:left;}td{padding:2px;}#block2{color:yellow;font-...

windows server2012 nVME和网卡等驱动和不识别RAID10问题

安装2012--不识别M.2nVME,下载官方驱动程序,并将其注入没有多个驱动程序的系统--添加ITSK通用驱动程序:|Win8012R2.x64网卡驱动程序无法打开--提取官方驱动程序EXE文件以添加网卡驱动程序不识别SATARAID10--超过2T,最大Legacy为2T。...

JAVA 实现CLOB转String

CLOB定义了用于在数据库中保存文件的类型。SQLCLOB是一种内置类型,它将一个大型字符对象作为列值存储在数据库表的一行中。默认情况下,驱动程序使用SQLlocator实现Clob对象,这意味着Clob对象包含指向SQLCLOB数据的逻辑指针,而不是数据本身。Clob对象在其创建的事务期间有效。在一些数据库系统中,文本也用作CLOB的别名。例如,SQL S...

js 预览 excel,js-xlsx的使用

js-xlsx简介SheetJS生成的js-xls x是一个非常方便的工具库,只能使用纯js读取和导出excel。它功能强大,支持多种格式,支持xls、xlsx和ods等十几种格式。本文以xlsx格式为例。官方github:https://github.com/SheetJS/js-xlsx支持演示在线演示地址:http://demo.haoji.me/20...

Excel数据透视表、高级筛选

目录:1.数据透视表:数据透视表格式和操作说明:多个表一起创建数据透视表创建组创建计算字段创建计算项值显示方法切片器2。高级过滤:高级过滤和或关系精确过滤和模糊过滤通配符过滤原则查询不重复值(使用高级过滤)高级过滤区分大小写使用高级过滤查找空数据使用高级过滤查询两个表中相同的记录或未使用的记录过滤记录1和数据透视表1.正确的数据透视表格式:① 数据源的第一行...

rm 命令(删除文件和目录)

Rm是常用命令。其功能是删除目录中的一个或多个文件或目录。它还可以删除目录及其下的所有文件和子目录。对于链接文件,只删除链接,原始文件保持不变。如果使用rm删除文件,仍然可以将文件恢复到原始状态。yroot@localhosttest1]#Ll总计0[root@localhosttest1]#注意:输入rmlog.log命令后,系统将询问是否删除。输入y后,...