AT_arc168_e [ARC168E] Subsegments with Large Sums 题解
套路地考虑 wqs
二分,记 表示分成 段, 的段数的最大值。
但是发现, 的段可以拼上旁边一个小段,构成一段坐标系上的横条,所以 其实并不是凸的。
由上的性质,可以知道,答案关于 具有单调性,我们尝试定义 表示 的段有 个的情况下,划分的最大段数。
但是最大段数的计算需要涉及到已选的段长,我们重新定义 即 的段的长度减一的和的最小值,若 ,则我们声称可以划分成 段。
感性理解起来, 特别容易凸,于是我们对 进行 wqs
二分,dp
记录前缀最小代价及对应下的最小个数,转移考虑从以当前点为右端点的最近合法点转移即可。
时间复杂度 。
// LUOGU_RID: 139532006
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
//#pragma GCC optimize("Ofast,fast-math")
//#pragma GCC target("avx,avx2")
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef double db;
#define F(i, a, b) for(int i = a; i <= (b); ++i)
#define F2(i, a, b) for(int i = a; i < (b); ++i)
#define dF(i, a, b) for(int i = a; i >= (b); --i)
template<typename T> void debug(string s, T x) {
cerr << "[" << s << "] = [" << x << "]\n";
}
template<typename T, typename... Args> void debug(string s, T x, Args... args) {
for (int i = 0, b = 0; i < (int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++;
else if (s[i] == ')' || s[i] == '}') b--;
else if (s[i] == ',' && b == 0) {
cerr << "[" << s.substr(0, i) << "] = [" << x << "] | ";
debug(s.substr(s.find_first_not_of(' ', i + 1)), args...);
break;
}
}
#ifdef ONLINE_JUDGE
#define Debug(...)
#else
#define Debug(...) debug(#__VA_ARGS__, __VA_ARGS__)
#endif
#define pb push_back
#define fi first
#define se second
#define Mry fprintf(stderr, "%.3lf MB\n", (&Med - &Mbe) / 1048576.0)
#define Try cerr << 1e3 * clock() / CLOCKS_PER_SEC << " ms\n";
typedef long long ll;
// namespace Fread {const int SIZE = 1 << 17; char buf[SIZE], *S, *T; inline char getchar() {if (S == T) {T = (S = buf) + fread(buf, 1, SIZE, stdin); if (S == T) return '\n';} return *S++;}}
// namespace Fwrite {const int SIZE = 1 << 17; char buf[SIZE], *S = buf, *T = buf + SIZE; inline void flush() {fwrite(buf, 1, S - buf, stdout), S = buf;} inline void putchar(char c) {*S++ = c;if (S == T) flush();} struct NTR {~NTR() {flush();}} ztr;}
// #ifdef ONLINE_JUDGE
// #define getchar Fread::getchar
// #define putchar Fwrite::putchar
// #endif
inline int ri() {
int x = 0;
bool t = 0;
char c = getchar();
while (c < '0' || c > '9') t |= c == '-', c = getchar();
while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return t ? -x : x;
}
inline void wi(int x) {
if (x < 0) {
putchar('-'), x = -x;
}
if (x > 9) wi(x / 10);
putchar(x % 10 + 48);
}
inline void wi(int x, char s) {
wi(x), putchar(s);
}
bool Mbe;
// mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3f;
const int _ = 3e5 + 5;
int n, k, s, a[_], sum[_], pre[_];
pii f[_];
void work(int x) {
F(i, 1, n) {
f[i] = f[i - 1];
if(pre[i]) f[i] = min(f[i], make_pair(f[pre[i] - 1].fi + (i - pre[i]) - x, f[pre[i] - 1].se + 1));
}
}
bool chk(int lim) {
int l = 1, r = n, mid, res = 0;
while(l <= r) {
mid = (l + r) >> 1;
work(mid);
if(f[n].se <= lim) l = mid + 1, res = mid;
else r = mid - 1;
}
work(res);
return f[n].fi + res * lim <= n - k;
}
bool Med;
signed main() {
// Mry;
n = ri(), k = ri(), s = ri();
F(i, 1, n) a[i] = ri(), sum[i] = sum[i - 1] + a[i];
int l = 0;
F(i, 1, n) {
while(sum[i] - sum[l] >= s) l++;
pre[i] = l;
}
l = 1;
int r = k, ans = 0;
while(l <= r) {
int mid = (l + r) >> 1;
if(chk(mid)) l = mid + 1, ans = mid;
else r = mid - 1;
}
cout << ans << '\n';
// Try;
return 0;
}
本文来自博客园,作者:蒟蒻orz,转载请注明原文链接:https://www.cnblogs.com/orzz/p/18121901