思路讲解
https://ac.nowcoder.com/discuss/1452662
主要就是他的思路,赛时实际上就AC了,只不过赛后数据加强了,前面AI乱写的过不去了。
这个思路就是贪心,先把最小的吃掉,再吃次小的,再吃次次小的。。。。以此类推
AC代码
AC
https://ac.nowcoder.com/acm/contest/95323/M
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll,ll> pll; typedef array<ll,3> arr; const ll MAXN=static_cast<ll>(1e5)+10,maxval=static_cast<ll>(1e18)+3; ll N,T,A[MAXN]; multiset<ll> st; vector<pll> Apos;
int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>N; ll maxA=0,minA=maxval; for(int i=1;i<=N;++i) { cin>>A[i]; st.insert(A[i]); Apos.push_back({A[i],i}); } sort(Apos.begin(),Apos.end()); st.insert(*st.begin()*2); st.erase(st.begin()); ll ans=*st.rbegin()-*st.begin(); ll l=Apos[0].second,r=Apos[0].second; for(int i=1;i<N;++i) { ll pos=Apos[i].second,key=Apos[i].first; if(l<=pos && pos<=r) continue; if(pos<l) { ll l1=pos; for(int j=l1;j<l;++j) { auto it=st.lower_bound(A[j]); st.insert(*it*2); st.erase(it); } l=l1; }else { ll r1=pos; for(int j=r+1;j<=r1;++j) { auto it=st.lower_bound(A[j]); st.insert(*it*2); st.erase(it); } r=r1; } ans=min(ans,*st.rbegin()-*st.begin()); } cout<<ans<<endl; return 0; }
|
心路历程(WA,TLE,MLE……)