---
title: Picnic Cows
tags:
  - Dynamic Programming
  - Greedy
  - Monotonic Queue
id: "2508"
categories:
  - [OI, Common Skill]
  - [OI, Data Structure]
date: 2018-03-12 20:20:07
---

## 题目描述

It's summer vocation now. After tedious milking, cows are tired and wish to take a holiday. So Farmer Carolina considers having a picnic beside the river. But there is a problem, not all the cows consider it's a good idea! Some cows like to swim in West Lake, some prefer to have a dinner in Shangri-la, and others want to do something different. But in order to manage expediently, Carolina coerces all cows to have a picnic!

Farmer Carolina takes her $N$ cows to the destination, but she finds every cow's degree of interest in this activity is so different that they all loss their interests. So she has to group them to different teams to make sure that every cow can go to a satisfied team. Considering about the security, she demands that there must be no less than $T$ cows in every team. As every cow has its own interest degree of this picnic, we measure this interest degree's unit as "_Moo~_". Cows in the same team should reduce their _Moo~_ to the one who has the lowest _Moo~_ in this team - It's not a democratical action! So Carolina wishes to minimize the **TOTAL** reduced _Moo~_ s and groups $N$ cows into several teams.

For example, Carolina has $7$ cows to picnic and their _Moo~_ are '$8 \; 5 \; 6 \; 2 \; 1 \; 7 \; 6$' and at least $3$ cows in every team. So the best solution is that cow No. $2, 4, 5$ in a team (reduce $((2-1)+(5-1))$ _Moo~_) and cow No. $1, 3, 6, 7$ in a team (reduce $((7-6)+(8-6))$ _Moo~_), the answer is $8$.

## 题意概述

有$N$头牛，每头牛有一个快乐值。要将牛分成若干组，使得每组至少有$T$头牛。分在一组中的牛的快乐值都会变成那组中最小的快乐值。求分组前后总快乐值之差的最小值。

数据范围：$1 \lt T \le N \le 4 \times 10^5$。

## 算法分析

将牛的快乐值$a_i$从小到大排序。根据贪心策略，一定存在最优分组方案是将排序后的牛分成若干连续段。

令$f_i$表示排序后前$i$头牛分组前后总快乐值之差的最小值，$s_i=\sum_{j=1}^i a_j$，$b_i=a_{i+1}$。可以得到转移方程

$$
f_i=\min(f_j+(s_i-s_j)-(i-j)b_j \mid i-j \ge T)
$$

假设$k \lt j \lt i$，且决策$j$比决策$k$更优，那么

$$
\begin{align}
f_j+(s_i-s_j)-(i-j)b_j &\le f_k+(s_i-s_k)-(i-k)b_k \\\\
(f_j-s_j+j \cdot b_j)-(f_k-s_k+k \cdot b_k) &\le i(b_j-b_k)
\end{align}
$$

$i$单调递增，因此可以斜率优化。但要注意两点：当$i-j \lt T$时$f_j$不能转移到$f_i$；当$i \lt T$时$f_i$没有意义。

## 代码

```cpp
/*
 * You attempt things that you do not even plan because of your extreme
 * stupidity.
 */

#include <algorithm>
#include <cstdio>
#include <cstring>

#define int long long

static int const N = 400005;
int a[N], f[N], s[N], b[N], que[N];

int dety(int i, int j) {
  return f[j] - s[j] + b[j] * j - (f[i] - s[i] + b[i] * i);
}

int detx(int i, int j) { return b[j] - b[i]; }

signed main() {
  for (int n, k; ~scanf("%lld%lld", &n, &k);) {
    for (int i = 1; i <= n; ++i)
      scanf("%lld", &a[i]);
    std::sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; ++i)
      s[i] = s[i - 1] + a[i], b[i - 1] = a[i];
    int qb = 0, qe = 1;
    que[0] = 0;
    for (int i = 1; i <= n; ++i) {
      for (; qb + 1 < qe &&
             dety(que[qb], que[qb + 1]) <= i * detx(que[qb], que[qb + 1]);
           ++qb)
        ;
      f[i] = f[que[qb]] + s[i] - s[que[qb]] - b[que[qb]] * (i - que[qb]);
      if (i - k + 1 >= k) {
        for (;
             qb + 1 < qe &&
             dety(que[qe - 2], que[qe - 1]) * detx(que[qe - 1], i - k + 1) >=
                 dety(que[qe - 1], i - k + 1) * detx(que[qe - 2], que[qe - 1]);
             --qe)
          ;
        que[qe++] = i - k + 1;
      }
    }
    printf("%lld\n", f[n]);
  }
  return 0;
}
```
