2019 cattle off scrimmage seventh C Governing sand weights segment tree greedy +

Governing sand

The meaning of problems

There are m forest species of trees, each tree has a certain height, and cut him to consume a certain price, and asked how much the cost of a minimum consumption can make the tallest trees in the forest more than half of all trees

analysis

Complexity Analysis: n 1e5 species of trees, and cutting down trees is certainly cut from cheap, there are intervals of, consider tree line, every enumerate a highly, highly above its first full cut, then cut low in his condition so as to satisfy more than half, cut below his definitely cut from low cost to start, it is a small problem k before the election, this is what a weight of the segment tree

Pit: different kinds of trees may be the same height

#include<bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
#define mkp make_pair
typedef long long ll;
#define int long long
using namespace std;
const int maxn =1e5+5;
ll tr[maxn<<2];
int sz,n;
int c[maxn],id[maxn];
ll sum[maxn];
struct Node{
    int h,p,c;
}a[maxn];
void build(int o,int l,int r){
    sum[o]=tr[o]=0;
    if(l==r)return ;
    int mid=l+r>>1;
    build(o<<1,l,mid);
    build(o<<1|1,mid+1,r);
}
 
void update(int o,int l,int r,int p,int v){
    if(l==r){
        tr[o]+=v;
        sum[o]+=v*c[p];
    }
    else {
        int mid=l+r>>1;
        if(p<=mid)update(o<<1,l,mid,p,v);
        else update(o<<1|1,mid+1,r,p,v);
        tr[o]=tr[o<<1|1]+tr[o<<1];
        sum[o]=sum[o<<1]+sum[o<<1|1];
    }
}
ll query(int o,int l,int r,int num){
    if(l==r){
        return min(1ll*num,tr[o])*1ll*c[l];
    }
    int mid=l+r>>1;
    if(tr[o<<1]>=num)return query(o<<1,l,mid,num);
    else return sum[o<<1]+query(o<<1|1,mid+1,r,num-tr[o<<1]);
}
int32_t main(){
        while(scanf("%lld",&n)!=EOF){
        long long sumcost=0;
        for(int i=1;i<=n;i++){
            scanf("%lld%lld%lld",&a[i].h,&a[i].c,&a[i].p);
            sumcost+=a[i].p*a[i].c;
            c[i]=a[i].c;
        }
        sort(c+1,c+1+n);
        sz=unique(c+1,c+1+n)-(c+1);
        build(1,1,sz);
        sort(a+1,a+1+n,[](Node a,Node b){
                return a.h<b.h;});
        long long qnum=0;
        long long ans=1e18;
        for(int i=1;i<=n;i++){
            id[i]=lower_bound(c+1,c+1+sz,a[i].c)-c;
        }
        for(int i=1;i<=n;){
            int p=i+1;
            ll nowhnum=a[i].p;
            int cnt=0;
            while(p<=n&&a[p].h==a[i].h){
                nowhnum+=a[p].p;
                cnt++;
                p++;
            }
                for(int j=i;j<=cnt+i;j++){
                    sumcost-=a[j].p*a[j].c;
                }
                if(nowhnum>qnum){
                    ans=min(sumcost,ans);
                }
                else {
                    ans=min(ans,sumcost+query(1,1,sz,qnum-nowhnum+1));
                }
                for(int j=i;j<=cnt+i;j++){
                    qnum+=a[j].p;
                    update(1,1,sz,id[j],a[j].p);
                }
                i=p;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/ttttttttrx/p/11407776.html