题目链接
题解
有一个贪心策略:同样的干草集合,底长小的一定不比底长大的矮
设(f[i])表示(i...N)形成的干草堆的最小底长,同时用(g[i])记录此时的高度
那么
[f[i] = min{f[j]} quad [sum[j - 1] - sum[i - 1] ge f[j]]
]
变形一下
[sum[i - 1] le sum[j - 1] - f[j]
]
我们知道(j)越小一定不比(j)更大的劣
所以我们只需要在满足该式条件下尽量往前选(j)
只需单调队列优化
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define cls(s) memset(s,0,sizeof(s))
#define mp(a,b) make_pair<int,int>(a,b)
#define cp pair<int,int>
using namespace std;
const int maxn = 100005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int sum[maxn],f[maxn],g[maxn],head,tail,n;
cp q[maxn];
int main(){
n = read();
REP(i,n) sum[i] = sum[i - 1] + read();
q[head = tail = 1] = mp(n + 1,sum[n] - f[n]);
for (int i = n; i; i--){
while (head < tail && q[head + 1].second >= sum[i - 1]) head++;
int j = q[head].first;
f[i] = sum[j - 1] - sum[i - 1];
g[i] = g[j] + 1;
while (head <= tail && sum[i - 1] - f[i] > q[tail].second) tail--;
q[++tail] = mp(i,sum[i - 1] - f[i]);
}
printf("%d
",g[1]);
return 0;
}