P11702 [ROIR 2025] 不平衡划分

· · 题解

题目传送门

题目描述

给定一个非负整数数组 [a_1, a_2, \dots, a_n]。考虑将该数组划分成 k 个非空的连续子段。求出在所有划分方案中最大的子段和与最小的子段和的差的最大值。

思考过程

由于此题的数据范围较大,n 和 k 的大小均可能达到 3 \times 10^5。所以考虑使用 O(n) 或 O(n \log n) 的时间复杂度。

首先可以想到使用双指针对其进行计算,枚举每一个作为最大子段和的区间,当然,由于必须分为 k 个子段,所以最大的区间长度为 n-k+1。

代码如下:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 3e5 + 10, mod = 1e9 + 7;
char *p1, *p2, buf[N];
//#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
#define nc() getchar()
#define enter putchar('\n')
#define space putchar(' ')
inline int min(int a, int b){return a < b ? a : b;}
inline int max(int a, int b){return a > b ? a : b;}
inline int gcd(int a, int b){return b == 0 ? a : gcd(b, a % b);}
inline int lcm(int a, int b){return a / gcd(a, b) * b;}
inline int ksm(int a, int m){int ans = 1;while(m) {if (m & 1) ans *= a;a *= a;m >>= 1;}return ans;}
inline int ksm(int a, int m, int p){int ans = 1;while(m) { if(m & 1) ans = (ans * a) % p;a = (a * a) % p;m >>= 1;}return ans;}
inline void read(double& rdx) {double t = 0;int x = 0, s = 0, f = 1;char c;do c = nc();while (!isdigit(c) && c != '-' && c != '.');if (c == '-')f = -1, c = nc();while (isdigit(c) && c != '.') x = (c ^ 48) + (x << 1) + (x << 3), c = nc();if (c == '.') c = nc();else {rdx =  x * f;return;}while (c >= '0' && c <= '9') t = t * 10 + (c ^ 48), s = -~s, c = nc();while (s--) t /= 10;rdx = (x + t) * f;}
inline void read(char& ch) {ch = nc();while (ch == ' ' || ch == '\n') ch = nc();}
inline int read() {int x = 0, f = 1;char ch = nc();while (ch < 48 || ch > 57) {if (ch == '-')f = -1;ch = nc();}while (ch >= 48 && ch <= 57)x = x * 10 + ch - 48, ch = nc();return x * f;}
void write(int x) {if (x < 0)putchar('-'), x = -x;if (x > 9)write(x / 10);putchar(x % 10 + '0');return;}
//int e[N], ne[N], h[N];
//int idx;
//void add(int a, int b) {
//  e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
//}
int n = read(), k = read();
int a[N], s[N], ans;
signed main() 
{
    //ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //srand(time(NULL));
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
    for (int i = 1; i <= n; s[i] = s[i - 1] + a[i], i ++) a[i] = read();//运用前缀和来维护一段区间的和
    for (int i = 1; i <= n; i ++)
    {
        int j = i;
        while (j <= n && j - i + 1 <= n - k + 1)
        {
            int res = s[j] - s[i - 1];//从i到j的子段和大小
            int rr = j + 1, mn = INT_MAX, ll = i - 1;
            while (rr <= n) mn = min(mn, a[rr]), rr ++;//寻找这个区间右边的最小子段和
            while (ll >= 1) mn = min(mn, a[ll]), ll --;//寻找这个区间左边的最小子段和
            ans = max(ans, res - mn);
            j ++;
        }
    }
    write(ans), enter;
    return 0;
}

当然,这样的代码是绝对不可能 AC 这道绿题的,所以我们要对其进行优化!

首先想到的肯定是使用 ST 表维护区间最小值对最里面的两个 while 进行优化。

代码如下,打个模板就可以啦:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 3e5 + 10, mod = 1e9 + 7;
char *p1, *p2, buf[N];
//#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
#define nc() getchar()
#define enter putchar('\n')
#define space putchar(' ')
inline int min(int a, int b){return a < b ? a : b;}
inline int max(int a, int b){return a > b ? a : b;}
inline int gcd(int a, int b){return b == 0 ? a : gcd(b, a % b);}
inline int lcm(int a, int b){return a / gcd(a, b) * b;}
inline int ksm(int a, int m){int ans = 1;while(m) {if (m & 1) ans *= a;a *= a;m >>= 1;}return ans;}
inline int ksm(int a, int m, int p){int ans = 1;while(m) { if(m & 1) ans = (ans * a) % p;a = (a * a) % p;m >>= 1;}return ans;}
inline void read(double& rdx) {double t = 0;int x = 0, s = 0, f = 1;char c;do c = nc();while (!isdigit(c) && c != '-' && c != '.');if (c == '-')f = -1, c = nc();while (isdigit(c) && c != '.') x = (c ^ 48) + (x << 1) + (x << 3), c = nc();if (c == '.') c = nc();else {rdx =  x * f;return;}while (c >= '0' && c <= '9') t = t * 10 + (c ^ 48), s = -~s, c = nc();while (s--) t /= 10;rdx = (x + t) * f;}
inline void read(char& ch) {ch = nc();while (ch == ' ' || ch == '\n') ch = nc();}
inline int read() {int x = 0, f = 1;char ch = nc();while (ch < 48 || ch > 57) {if (ch == '-')f = -1;ch = nc();}while (ch >= 48 && ch <= 57)x = x * 10 + ch - 48, ch = nc();return x * f;}
void write(int x) {if (x < 0)putchar('-'), x = -x;if (x > 9)write(x / 10);putchar(x % 10 + '0');return;}
//int e[N], ne[N], h[N];
//int idx;
//void add(int a, int b) {
//  e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
//}
int n = read(), k = read();
int a[N], s[N], ans, t[22][N];
signed main() 
{
    //ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //srand(time(NULL));
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
    memset(t, 0x3f, sizeof t);//一定要初始化,因为求的是最小值。
    for (int i = 1; i <= n; s[i] = s[i - 1] + a[i], i ++) a[i] = read(), t[0][i] = a[i];
    int l = log2(n);
    for(int i = 1;i <= l; i ++)
    {
        for(int j = 1; j <= n; j ++)
        {
            t[i][j] = min(t[i - 1][j], t[i - 1][j + (1 << i - 1)]);
        }
    }//ST表
//  for (int i = 1; i <= n; i ++)
//  {
//      for (int j = 1; j <= n; j ++)
//      {
//          int x = i, y = j, len = log2(y - x + 1);
//          printf("x : %lld y : %lld val : %lld\n", i, j, min(t[len][x], t[len][y - (1 << len) + 1]));
//      }
//      enter;
//  }此处为调试ST表是否正确
    for (int i = 1; i <= n; i ++)
    {
        int j = i;
        while (j <= n && j - i <= n - k)
        {
            int res = s[j] - s[i - 1], x, y, len, mn1 = INT_MAX, mn2 = INT_MAX;
            x = 1, y = i - 1, len = log2(y - x + 1);
            mn1 = min(t[len][x], t[len][y - (1 << len) + 1]);
            x = j + 1, y = n, len = log2(y - x + 1);
            mn2 = min(t[len][x], t[len][y - (1 << len) + 1]);
            ans = max(ans, res - min(mn1, mn2));
            j ++;
        }
    }
    write(ans), enter;
    return 0;
}

但是,只是这样优化并不能 AC 此题,还需要对其进行优化。

通过观察可以发现如果最大的区间子段和在整个数列的中间,那么它的长度一定是 n-k+1。

证明如下:假设已经选好了一个区间从 i 到 j,且满足这个区间的长度小于 n-k+1,便会出现两种情况。

  1. 假设与这个区间相对的区间最小子段和在这个区间的左侧,那么 j 一定可以往右再挪 n-k+1-(j-i+1)=n-k+i-j 位,因为整个数列的值都是正的,所以选择的区间的子段和在变化后一定会变大,那么所求的答案值也会增加。
  2. 如果所求的区间相对的区间最小子段和在这个区间的右侧,那么 i 一定可以往左再挪 n-k+1-(j-i+1)=n-k+i-j 位,答案值也会变大。

但是如果这个选择的区间在整个数列的左端或右端,那么就不一定了,比如下面的样例:

5 3
3 3 1 2 8

假设我们仍然以上面的方法来计算,那么答案算出来就会是 8,而实际上答案是 9,这是因为相对于 i 等于 4, j 等于 5 这个区间,你的右侧并没有其他的值,从而导致你将左侧的相邻的元素给合并了,所以我们需要将选择的区间在整个数列的左端或右端时进行另外的讨论,一个一个累加直到区间的长度为 n-k+1 时为止。

代码如下:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 3e5 + 10, mod = 1e9 + 7, inf = 0x3f3f3f3f3f3f3f3f;
char *p1, *p2, buf[N];
//#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
#define nc() getchar()
#define enter putchar('\n')
#define space putchar(' ')
inline int min(int a, int b){return a < b ? a : b;}
inline int max(int a, int b){return a > b ? a : b;}
inline int gcd(int a, int b){return b == 0 ? a : gcd(b, a % b);}
inline int lcm(int a, int b){return a / gcd(a, b) * b;}
inline int ksm(int a, int m){int ans = 1;while(m) {if (m & 1) ans *= a;a *= a;m >>= 1;}return ans;}
inline int ksm(int a, int m, int p){int ans = 1;while(m) { if(m & 1) ans = (ans * a) % p;a = (a * a) % p;m >>= 1;}return ans;}
inline void read(double& rdx) {double t = 0;int x = 0, s = 0, f = 1;char c;do c = nc();while (!isdigit(c) && c != '-' && c != '.');if (c == '-')f = -1, c = nc();while (isdigit(c) && c != '.') x = (c ^ 48) + (x << 1) + (x << 3), c = nc();if (c == '.') c = nc();else {rdx =  x * f;return;}while (c >= '0' && c <= '9') t = t * 10 + (c ^ 48), s = -~s, c = nc();while (s--) t /= 10;rdx = (x + t) * f;}
inline void read(char& ch) {ch = nc();while (ch == ' ' || ch == '\n') ch = nc();}
inline int read() {int x = 0, f = 1;char ch = nc();while (ch < 48 || ch > 57) {if (ch == '-')f = -1;ch = nc();}while (ch >= 48 && ch <= 57)x = x * 10 + ch - 48, ch = nc();return x * f;}
void write(int x) {if (x < 0)putchar('-'), x = -x;if (x > 9)write(x / 10);putchar(x % 10 + '0');return;}
//int e[N], ne[N], h[N];
//int idx;
//void add(int a, int b) {
//  e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
//}
int n = read(), k = read();
int a[N], s[N], ans, t[22][N];
signed main() 
{
    //ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //srand(time(NULL));
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
    memset(t, inf, sizeof t);
    for (int i = 1; i <= n; s[i] = s[i - 1] + a[i], i ++) a[i] = read(), t[0][i] = a[i];
    if (k == 2)
    {
        for (int i = 2; i <= n; i ++)
        {
            ans = max(ans, abs(s[i - 1] - s[n] + s[i - 1]));
        }
        write(ans), enter;
        return 0;
    }//此处将k=2特判是因为像上面我提供的样例中,如果把k改成2,那么就无法将最小子段,最大子段和其余子段分为三份来计算了,所以在此直接对其进行左右分组,用前缀和维护,求出答案。
    int l = log2(n);
    for(int i = 1;i <= l; i ++)
    {
        for(int j = 1; j <= n; j ++)
        {
            t[i][j] = min(t[i - 1][j], t[i - 1][j + (1 << i - 1)]);
        }
    }
//  int totlen = n - k;
//  write(totlen), enter;
//  for (int i = 1; i <= n; i ++)
//  {
//      for (int j = 1; j <= n; j ++)
//      {
//          int x = i, y = j, len = log2(y - x + 1);
//          printf("x : %lld y : %lld val : %lld\n", i, j, min(t[len][x], t[len][y - (1 << len) + 1]));
//      }
//      enter;
//  }
    int tot = n - k;//区间长度应该是n-k+1,但是因为我比较懒,下面选择的区间长度我直接当j-i去算,将不等式两侧的+1直接约掉了。
    for (int j = 1; j <= tot; j ++)
    {
        int i = 1;
//      write(i), space, write(j), enter;
        int res = s[j] - s[i - 1], x, y, len, mn1 = INT_MAX, mn2 = INT_MAX;
        x = 1, y = i - 1, len = log2(y - x + 1);
        mn1 = min(t[len][x], t[len][y - (1 << len) + 1]);
        x = j + 1, y = n, len = log2(y - x + 1);
        mn2 = min(t[len][x], t[len][y - (1 << len) + 1]);
        ans = max(ans, res - min(mn1, mn2));
    }//计算左端的情况
    for (int i = 1; i <= n - tot; i ++)
    {
        int j = i + tot;
//      write(i), space, write(j), enter;
        int res = s[j] - s[i - 1], x, y, len, mn1 = INT_MAX, mn2 = INT_MAX;
        x = 1, y = i - 1, len = log2(y - x + 1);
        mn1 = min(t[len][x], t[len][y - (1 << len) + 1]);
        x = j + 1, y = n, len = log2(y - x + 1);
        mn2 = min(t[len][x], t[len][y - (1 << len) + 1]);
        ans = max(ans, res - min(mn1, mn2));
    }//计算中间的情况
    for (int i = n - tot + 1; i <= n; i ++)
    {
        int j = n;    
//      write(i), space, write(j), enter;
        int res = s[j] - s[i - 1], x, y, len, mn1 = INT_MAX, mn2 = INT_MAX;
        x = 1, y = i - 1, len = log2(y - x + 1);
        mn1 = min(t[len][x], t[len][y - (1 << len) + 1]);
        x = j + 1, y = n, len = log2(y - x + 1);
        mn2 = min(t[len][x], t[len][y - (1 << len) + 1]);
        ans = max(ans, res - min(mn1, mn2));
    }//计算右端的情况
    write(ans), enter;
    return 0;
}

那么这道题我们便可以愉快的 AC 啦!