The meaning of problems
There are $ n $ the number of trees, trees have a high degree per $ H_i $, costs $ C_i $, the number of $ P_i $, now you want to cut down some trees, so that the rest of the tree is more than the tallest tree in general, for the Minimum cost of. ($ 1 \ leq n \ leq 10 ^ 5, \ 1 \ leq H_i \ leq 10 ^ 9, \ 1 \ leq C_i \ leq 200, \ 1 \ leq P_i \ leq 10 ^ 9 $)
analysis
First of all, easy to think we should enumerate the height of the tallest trees, cut down all the higher, then select from the low cost of the k-th smallest cut.
It focuses on how to obtain the current minimum cost of k.
$ C_i $ notes that the range is very small, so we can count the cost of the number $ i $ the tree, denoted by $ C [i] $ (equivalent to the establishment of 200 barrels).
From low to high by highly sorted, traversing it again, get the global minimum cost. Complexity is $ O (200n) $, if the binary search can do $ O (log200 \ cdot n) $.
#include<bits/stdc++.h> using namespace std; #define LL long long struct tree{ LL h,c,p; }a[100005]; bool cmp(tree a,tree b){ return a.h<b.h; } LL n; LL tmp=0,ans=0,tot=0; LL num[205]; int main(){ while(cin>>n){ memset(num,0,sizeof(num)); ans=tmp=tot=0; for(LL i=0;i<n;++i){ scanf("%lld%lld%lld",&a[i].h,&a[i].c,&a[i].p); tmp+=a[i].c*a[i].p; } ans=tmp; sort(a,a+n,cmp); for(LL i=0,j;i<n;i=j){ j=i;while(a[j].h==a[i].h&&j<n)++j; LL sum=0; LL cost=0; for(LL t=i;t<j;++t){ tmp-=a[t].c*a[t].p; tot+=a[t].p; sum+=a[t].p; } sum=tot-sum*2+1; for(LL w=1;sum>0;++w){ if(num[w]<=sum){ cost+=w*num[w]; sum-=num[w]; }else{ cost+=sum*w; break; } } ans=min(ans,cost+tmp); for(LL t=i;t<j;++t){ num[a[t].c]+=a[t].p; } } cout<<ans<<endl; } }
Reference Links: https://ac.nowcoder.com/acm/contest/view-submission?submissionId=41065020