动态规划之背包才是万恶之源(CF E. K Balanced Teams)

例题

题目链接
You are a coach at your local university. There are n n n students under your supervision, the programming skill of the i i i-th student is a i a_i ai.

You have to form k k k teams for yet another new programming competition. As you know, the more students are involved in competition the more probable the victory of your university is! So you have to form no more than k k k (and at least one) non-empty teams so that the total number of students in them is maximized. But you also know that each team should be balanced. It means that the programming skill of each pair of students in each team should differ by no more than 5 5 5. Teams are independent from one another (it means that the difference between programming skills of two students from two different teams does not matter).

It is possible that some students not be included in any team at all.

Your task is to report the maximum possible total number of students in no more than k k k (and at least one) non-empty balanced teams.

If you are Python programmer, consider using PyPy instead of Python when you submit your code.

Input

The first line of the input contains two integers n n n and k k k ( 1 ≤ k ≤ n ≤ 5000 1 \le k \le n \le 5000 1kn5000) — the number of students and the maximum number of teams, correspondingly.

The second line of the input contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1ai109), where a i a_i ai is a programming skill of the i i i-th student.

Output

Print one integer — the maximum possible total number of students in no more than k k k (and at least one) non-empty balanced teams.

题目大意

因为看错题被evilboy狠狠虐了 其实就是在一串序列中找出 最多不超过k串子序列 ,并且每一个子序列的 极差不超过5;聪明的你想到了什么?对!就是背包!还是裸的01背包类型;接下来我们来看看这道题是如何转化成01背包的思考方式的;

被降维成01背包的1800的题

对于01背包我们是怎么设计dp数组的呢–>dp[i, j]:表示只考虑前i个物品,物品总体积不超过j的物品价值最大值对吧awa;就因为这个不超过,很容易联想到背包问题的思考方式;

dp数组设计

那么对于这道题,我们可以这样设计dp数组:dp[i, j]之考虑前 i 个人,组数 不超过 j 人数最大值;但是这里还需要考虑一个 极差 问题,这个好处理,直接排序,再用一个 双指针 来找到最小符合极差不大于5元素的下标就可以了;

转移方程

有了这个dp数组的定义之后,我们就可以考虑怎么转移了:在01背包中,我们考虑转移是从是否把第 i 个物品放入背包中,那么在这道题,其实也可以用类似的方式考虑,即是否把第 i 个物品放入前面一个组里面,只有这两种情况需要考虑,满足了不漏的原则(因为如果放入的话极差可能会变化,可能需要新的一个组,导致最终的人数减少);

  1. 当第i个元素不加入组中的时候 dp[i, j] = dp[i - 1, j]
  2. 当第i个元素加入组中的时候 dp[i, j] = dp[ii - 1, j - 1] + (i - ii + 1)

(这个ii的具体含义看代码里面有)
所以最终的转移方程就是 dp[i, j] = max(dp[i - 1, j], dp[ii - 1, j - 1] + (i - ii + 1));==>若理解不了就去看看01背包的笔记,这俩就是稍微有一点点不同;

上代码!

// web:https://codeforces.com/problemset/problem/1133/E
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> PII;
const int N = 1e5 + 10;
const int INF = 1e18;

inline void solve()
{
    int ans = 0;

    int n, k;
    cin >> n >> k;

    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    sort(a.begin() + 1, a.end());

    vector<vector<int>> dp(n + 1, vector<int>(k + 1, 0));
    for (int i = 1, ii = 1; i <= n; i++)
    {
        // 找到以a[i]为结尾的元素能组队的最小元素的下标
        while (a[i] - a[ii] > 5) ii++;
        for (int j = 1; j <= k; j++)
            dp[i][j] = max(dp[i - 1][j], dp[ii - 1][j - 1] + (i - ii + 1));

    }
    cout << dp[n][k] << endl;
}

signed main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int t = 1;
    // cin >> t;
    while (t--) solve();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值