START202——Equalize Juice · znzryb
## 思路讲解
[START202——Min-Max Deque](/2025/09/04/20250904-5229ac3b)
和该问题一样,直接解决这个问题比较难,我们可以解决一些更简单的问题。
比如说就是让果汁恰好为X?
那么考虑我们要选 $i_1,i_2,...i_k$ 个,那么这些 i 要满足什么条件那?
这个问题其实也比较复杂,我们不妨只看 $...i_1...i_2...$ 这两个之间的情况。
我们发现,$A[1]+\cdots+A[i_1]+\cdots+A[i_2-1]≥X$,是必要条件。
而且 $A[i_1+1]+\cdots+A[i_2-1]$ 的加多了的部分还可以回馈给 $A[i_2]$
那么我们就会形成一种感觉,就是说前面的部分其实不重要,越少越好,后面的部分要越多越好,因为还可以回馈给下一个。(抱歉,官解也没有证明,所以说只能这样讲了)
当然,我们需要将这种思维拓展到每一个相邻元素之间,那么我们发现,前面一个元素的后面下面一个元素的前面,因此我们要恰好,就是需要删的元素不能多(这个还是很容易发现的)。
于是我们不难写出程序。
## AC代码
[https://www.codechef.com/viewsolution/1188472477](https://www.codechef.com/viewsolution/1188472477)
::: 源代码
```cpp
// by Gospel_rock
#include <algorithm>
#include <bits/stdc++.h>
#define all(vec) vec.begin(),vec.end()
#define PB push_back
#define PF push_front
#define EB emplace_back
#define EF emplace_front
#define EM emplace
#define FI first
#define SE second
#define pct __builtin_popcountll
#define ctz __builtin_ctzll
#define lson(o) (o<<1)
#define rson(o) (o<<1|1)
#define SZ(a) ((long long) a.size())
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define debug(var) cerr << #var <<":"<<var<<"\n";
#define cend cerr<<"\n-----------\n"
#define fsp(x) fixed<<setprecision(x)
#define minn(vec) *min_element(vec.begin(),vec.end())
#define maxx(vec) *max_element(vec.begin(),vec.end())
using namespace std;
#ifdef LOCAL
template<typename T> void _print(T x) { cerr << x; }
// 可以打印多层数组,但是格式不是很好
template<typename T> void _print(vector<T> v) { cerr << "[ "; for (T i : v) _print(i), cerr << " "; cerr << "]"; }
template<typename T> void _print(set<T> s) { cerr << "{ "; for (T i : s) _print(i), cerr << " "; cerr << "}"; }
template<typename T, typename U> void _print(pair<T, U> p) { cerr << "{"; _print(p.first); cerr << ", "; _print(p.second); cerr << "}"; }
template<typename T, typename U> void _print(map<T, U> m) { cerr << "{ "; for (auto &p : m) _print(p), cerr << " "; cerr << "}"; }
// 打印两层数组,格式好一些
template<typename T> void _print(vector<vector<T>> v){cerr<<"{\n";for (auto i : v) _print(i), cerr << "\n";cerr<<" }";}
#endif
using ll = long long;using ull = unsigned long long;
using ld=double;using LD=long double;
using i128 = __int128;
using pdd = pair<ld,ld>;using plb = pair<ll,bool>;
using pll = pair<ll,ll>;using vll=vector<ll>;using vb=vector<bool>;
using tll = tuple<ll,ll,ll>;using vii=vector<int>;
using vpll = vector<pll>;using vtll = vector<tll>;
using vdb = vector<ld>;using vc=vector<char>;
using arr3 = array<ll,3> ;using arr2 = array<ll,2>;
using CD=complex<double>;
static constexpr ll MAXN=(ll)1e6+10,INF=(ll)1e17+3; // 1e17+3是素数 1e18+9是素数
static constexpr ll mod=(ll)1e9+7; // 998244353;
static constexpr double eps=1e-8;const double pi=acos(-1.0);
ll N,M,Q,X,K,T,lT;
inline void __();
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>lT;
while(lT--)
__();
return 0;
}
/*
*/
inline void __(){
cin>>N;
vll A(N+5);
FOR(i,1,N){
cin>>A[i];
}
vll li(A.begin()+1,A.begin()+N+1);
sort(all(li));li.resize(unique(all(li))-li.begin());
ll ans=INF;
// #ifdef LOCAL
// cerr<<"OK\n";
// #endif
for(auto x:li){
auto findi1=[&](ll st)->pll {
ll sum=0;
FOR(i,st,N){
if(A[i]<=x){
return {i,sum};
}
sum+=A[i];
}
return {N+1,sum};
};
auto [st,sum]=findi1(1);
ll need=0;
ll lans=N;
while (st<=N) {
need=x-A[st]-sum;
ll ost=st;
if(need>0){
while (need>0 && st<=N) {
ll add=0;
if(st!=ost) add=A[st];
tie(st,sum)=findi1(st+1);
sum+=add;
need-=sum;
}
sum=abs(need);
}else{
tie(st,sum)=findi1(st+1);
}
if(need<=0) lans--;
}
ans=min(lans,ans);
#ifdef LOCAL
debug(x);
#endif
}
cout<<ans<<"\n";
}
/*
AC
https://www.codechef.com/viewsolution/1188472477
*/
```
:::
## 心路历程(WA,TLE,MLE……)