+ Enumeration array of arrays (classical)

Meaning of the questions: There are n trees, gives a high number for each, removing the cost and quantity, is seeking a minimum spend much so that the number of the remaining trees in the tallest trees accounted for more than half.

Analysis: The highly ordered enumerate every height, to the current enumeration of this highly regarded maximum height, the number of num trees, then be sure to cut back because of higher than his, then consider smaller than his, because to constitute a two-fold relationship, so retain up to num-1 Ke.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=205;
const int M=1e5+5;
ll tree[N][2];
struct node{
    ll h,p,c;
}a[M];
ll sufc[M],sufp[M];
bool cmp(node p,node q){
    return p.h<q.h;
}
void add(int u,ll x,int i){
    while(u<=200){
        tree[u][i]+=x;
        u+=-u&u;
    }
}
ll SUM(int u,int i){
    ll ans=0ll;
    while(u){
        ans+=tree[u][i];
        u-=-u&u;
    }
    return ans;
}
int main(){
    int n;
    while(~scanf("%d",&n)){
        memset(tree,0ll,sizeof(tree));
        ll sum=0,maxc=0,minc=M;
        for(int i=1;i<=n;i++){
            scanf("%lld%lld%lld",&a[i].h,&a[i].c,&a[i].p);
            sum+=a[i].p;
            maxc=max(maxc,a[i].c);
            minc=min(minc,a[i].c);
        }
        sort(a+1,a+1+n,cmp);
        for(int i=n;i>=1;i--){
            sufc[i]=sufc[i+1]+a[i].p*a[i].c;//后缀代价总和
            sufp[i]=sufp[i+1]+a[i].p;//后缀树木总数和 
        }
        ll ans=sufc[1];
        int i=1;
        /*for(int i=1;i<=n;i++)
            cout<<sufc[i]<<" ";
        cout<<endl;*/
        while(i<=n){
            int l=i,r=i;
            ll num=0ll,needcost=0ll;
            while(r<=n&&a[r].h==a[l].h)
                num+=a[r++].p;
            
            needcost+=sufc [R & lt]; 
            LL need = SUM-NUM-SUFP [R & lt] - (num- . 1 ); // Enumeration identified as the current maximum height, will certainly be higher than the current cut off, and then consider both the front due to the times, so a maximum cut tree num-1 
            IF (need <= 0 ) 
                ANS = min (ANS, needcost);
             the else { 
                LL L = MiNC, R & lt MAXC =, S = 0LL;
                 the while (L <= R & lt) { 
                    MIDD LL = (L + R & lt) >> . 1 ;
                     IF (the SUM (MIDD, 0 )> = need) 
                        S = MIDD, = R & lt midd- . 1 ;
                     the else
                        L = + MIDD . 1 ; 
                } 
                need - the SUM = (S, 0 ); 
                needcost + the SUM = (S, . 1 );
                 // as to maintain the greedy, and so there is greater than the prefix tree to cut time to lose extra consideration 
                needcost- = S * ABS (need); 
            } 
            ANS = min (ANS, needcost); 
             the while (L < R & lt) { 
                the Add (A [L] .c, A [L] .p, 0 ); 
                the Add (A [L] .c, A [L] .c * A [L] .p, . 1 ); 
                L ++ ; 
            } 
            I=r;//cout<<r<<endl;
        }
        printf("%lld\n",ans);
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/starve/p/11330365.html