Codeforces 626F Group Projects (DP)

原创 2016年05月31日 15:50:22

官方题解很详细

This is a dynamic programming problem. Notice that the total imbalance of the groups only depends on which students are the maximum in each group and which are the minimum in each group. We thus can think of groups as intervals bounded by the minimum and maximum student. Moreover, the total imbalance is the sum over all unit ranges of the number of intervals covering that range. We can use this formula to do our DP.

If we sort the students in increasing size, DP state is as follows: the number of students processed so far, the number of g groups which are currently “open” (have a minimum but no maximum), and the total imbalance k so far. For each student, we first add the appropriate value to the total imbalance (g times the distance to the previous student), and then either put the student in his own group (doesn’t change g), start a new group (increment g), add the student to one of the g groups (doesn’t change g), or close one of the g groups (decrement g).

Runtime: O(n2k)


[code]:

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
const LL MOD = 1e9+7;

int n,m,a[205];
LL dp[2][205][1005];

void sol(){
    int i,j,k,k_,cr,nt;
    LL val;
    dp[0][0][0] = 1;
    for(i = 0;i < n;i++){
        cr = i&1,nt = (i+1)&1;
        for(j = 0;j <= n;j++){
            for(k = 0;k <= m;k++){
                dp[nt][j][k] = 0;
            }
        }
        for(j = 0;j <= n;j++){
            for(k = 0;k <= m;k++){
                val = dp[cr][j][k];
                k_ = k + j*(a[i+1]-a[i]);
                if(val==0||k_>m) continue;
                dp[nt][j+1][k_] = (dp[nt][j+1][k_]+val)%MOD;
                dp[nt][j][k_] = (dp[nt][j][k_]+(j+1)*val)%MOD;
                if(j) dp[nt][j-1][k_] = (dp[nt][j-1][k_]+j*val)%MOD;
            }
        }
    }
    LL ans = 0;
    for(k = 0;k <= m;k++) ans = (ans + dp[n&1][0][k])%MOD;
    printf("%I64d\n",(ans+MOD)%MOD);
}

int main(){
    int i,j,k;
    scanf("%d%d",&n,&m);
    for(i = 1;i <= n;i++) scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    sol();
    return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

CodeForces 626F Group Projects(dp)

题意:有n个商品,每件商品有一个价值,现在要把商品分组,求每组的最大价值与最小价值的价格差的和不超过m种的分组种数。 思路: 这道题dp状态不太好想,很容易想到一种状态是用dp[I][j][k]表示...

codeforces626F Group Projects(dp)

Description There are n students in a class working on group projects. The students will divide into...

8VC Venture Cup 2016 - Elimination Round F. Group Projects(DP)

题目链接:点击打开链接 题意:给n个人, 让我们分成若干组, 每组的值是最大值减去最小值, 求所有组的和。 思路:显然, 需要用DP的思想, 但是该题DP设计的非常巧妙, 而且状态转移的情况容易考...

Codeforces626F-Group Projects-神奇状态之DP

题意: 有n个商品,每件商品有一个价值,现在要把商品分组,求每组的最大价值与最小价值的价格差的和不超过m的分组种数。Solution: DP,朴素的dp状态(f[i][j][k]表示前i件分成j组...

codeforces 626F DP 如何设置状态,方便转移

想到用动态规划来做,但是考虑到第i个数的时候,前面数的分组情况并不知道,开始准备用类似并查集的方法,但是也不好保存 能够利用的性质是只和最大最小有关,所以想到排序之后解决,但是还是不知道应该怎么扩展...

CodeForces 633F The Chocolate Spree(树形DP)

题意:给出一棵树,每个节点有一个权值,求出不相交的两条链的最大权值和。 思路:树形DP。 可以发现一棵树的两条链一共会出现以下七种情况,七种情况又可以进一步划分成四种情况 f[u][0] 表示以u为...

Codeforces Round #427 (Div. 2) F-Roads in the Kingdom (树的直径,DFS+DP思想)

F. Roads in the Kingdom In the Kingdom K., there are n towns numbered with integers from 1 to n. The...

DP Codeforces Round #322 (Div. 2) F. Zublicanes and Mumocrates

树形dp,记录一下根节点的状态即可... #include using namespace std; typedef long long LL; const int maxn = 5005; c...

Educational Codeforces Round 19 F(dp+队列优化)

网址:http://codeforces.com/contest/797/problem/F 显然,要先排序。 我们可以先处理出所有老鼠到所有洞的距离,p[i][j]表示第j只老鼠到第i个洞的距离。...

Round 3 F - k-Tree CodeForces - 431C - K叉树 树形DP

题目链接: http://codeforces.com/problemset/problem/431/C大意: 无限的完全 K 叉树,每层边权值分别为 1.2.3…K 要求至少经过一个 大于 ...
  • Joovo
  • Joovo
  • 2017年07月17日 15:15
  • 153
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces 626F Group Projects (DP)
举报原因:
原因补充:

(最多只允许输入30个字)