Picnic Cows

题目描述

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$没有意义。

代码

/*
 * 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;
}

Print Article

题目描述

Zero has an old printer that doesn’t work well sometimes. As it is antique, he still like to use it to print articles. But it is too old to work for a long time and it will certainly wear and tear, so Zero use a cost to evaluate this degree.
One day Zero want to print an article which has $N$ words, and each word $i$ has a cost $C_i$ to be printed. Also, Zero know that print $k$ words in one line will cost
$$\left(\sum_{i=1}^k C_i\right)^2+M$$
$M$ is a const number.
Now Zero want to know the minimum cost in order to arrange the article perfectly.

题意概述

一篇文章中有$N$个单词,输入第$i$个单词的代价为$C_i$。将连续$k$个单词排在一行的总代价为$\left(\sum_{i=1}^k C_i\right)^2+M$。求输入这篇文章的最小代价。
数据范围:$0 \le N \le 5 \times 10^5, \; 0 \le M \le 1000$。

算法分析

令$f_i$表示输入前$i$个单词的最小代价,$s_i=\sum_{j=1}^i C_j$。考虑第$i$个单词的所在行,可以得到转移方程
$$f_i=\min(f_j+(s_i-s_j)^2+M)$$
假设$k \lt j \lt i$,且$j$这个决策更优,即
$$f_j+(s_i-s_j)^2+M \le f_k+(s_i-s_k)^2+M$$
化简得
$$
\begin{align}
f_j-2s_is_j+s_j^2 &\le f_k-2s_is_k+s_k^2 \\
f_j+s_j^2-(f_k+s_k^2) &\le 2s_i(s_j-s_k) \\
{f_j+s_j^2-(f_k+s_k^2) \over 2s_j-2s_k} &\le s_i
\end{align}
$$
也就是说,在满足这个不等式时,对于状态$i$,决策$j$比决策$k$更优。
我们知道$s_i$单调不递减,即如果对于状态$i_0$,$k \lt j \lt i$且决策$j$更优,那么对于状态$i_1 \gt i_0$,决策$j$也一定更优,因此我们可以抛弃决策$k$。
对于每个$i$,假设平面中有点$(2s_i, f_i+s_i^2)$。对于不等式左侧这个东西,把它看成平面直角坐标系里直线的斜率。由于要最小化$f_i$,因此需要找到一个$k$使得不存在$k \lt j \lt i$满足不等式。可以发现我们只要用单调队列维护前$(i-1)$个点的下凸壳(斜率单调递增)。这样时间复杂度就是$O(N)$的了。

代码

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

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

int detx(int i, int j) { return s[j] - s[i] << 1; }

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

int main() {
  for (int n, m; ~scanf("%d%d", &n, &m);) {
    for (int i = 1; i <= n; ++i)
      scanf("%d", &a[i]);
    for (int i = 1; i <= n; ++i)
      s[i] = s[i - 1] + a[i];
    int qb = 0, qe = 1;
    for (int i = 1; i <= n; ++i) {
      for (; qb + 1 < qe &&
             dety(que[qb], que[qb + 1]) <= s[i] * detx(que[qb], que[qb + 1]);
           ++qb)
        ;
      f[i] = f[que[qb]] + (s[i] - s[que[qb]]) * (s[i] - s[que[qb]]) + m;
      for (; qb + 1 < qe &&
             dety(que[qe - 2], que[qe - 1]) * detx(que[qe - 1], i) >=
                 dety(que[qe - 1], i) * detx(que[qe - 2], que[qe - 1]);
           --qe)
        ;
      que[qe++] = i;
    }
    printf("%d\n", f[n]);
  }
  return 0;
}

Similarity of Necklaces 2

题意概述

给定四个数组$M, P, L, R$,要求构造一个数组$T$,使得
$$\sum_{i=1}^N M_iT_i=0 \; (L_i \le T_i \le R_i)$$
求$\sum_{i=1}^N P_iT_i$的最大值。
数据范围:$1 \le N \le 200, \; 1 \le M_i \le 20, \; 0 \le P_i \le 10^5, \; -25 \le L_i \lt R_i \le 25$。

算法分析

令$D_i=T_i-L_i$,那么限制条件变为$\sum_{i=1}^N M_iD_i=-\sum_{i=1}^N M_iL_i \; (0 \le D_i \le R_i-L_i)$,要求的值变为$\sum_{i=1}^N P_iD_i+\sum_{i=1}^N P_iL_i$。这就相当于有$N$种物品,第$i$种物品有$(R_i-L_i)$个,每一个的体积为$M_i$,价值为$P_i$,背包的体积为$-\sum_{i=1}^N M_iL_i$,求物品恰好装满背包时的最大价值。这是分组背包问题,用单调队列优化DP即可。

代码

#include <cstdio>
#include <cstring>

int min(int a, int b) {
  return a < b ? a : b;
}

static const int N = 205;
int p[N], m[N], u[N], l[N];
int f[N << 10], q1[N << 10], q2[N << 10];

int main() {
  int n;
  while (~ scanf("%d", &n)) {
    int v = 0, w = 0;
    for (int i = 0; i < n; ++ i) {
      scanf("%d%d%d%d", &p[i], &m[i], &l[i], &u[i]);
      if (l[i]) u[i] -= l[i], v -= l[i] * m[i], w -= l[i] * p[i];
    }
    memset(f, -0x3f, sizeof f), f[0] = 0;
    for (int i = 0; i < n; ++ i) {
      u[i] = min(u[i], v / m[i]);
      for (int d = 0; d < m[i]; ++ d) {
        int l = 1, r = 0;
        for (int j = 0; j <= (v - d) / m[i]; ++ j) {
          int val = f[j * m[i] + d] - j * p[i];
          while (l <= r && q1[r] < val) -- r;
          q2[++ r] = j, q1[r] = val;
          if (j - q2[l] > u[i]) ++ l;
          f[j * m[i] + d] = q1[l] + j * p[i];
        }
      }
    }
    printf("%d\n", f[v] - w);
  }
  return 0;
}