More than 2019 cattle off summer school camp (seventh) C-Governing sand

Questions face Description: a wood, requirements to cut some trees, so that the maximum number of trees more than half of the total number of trees each have a high degree, quantity, and cut spending needs. Seek minimum cost.

Idea: After sorting by height, from low to high enumeration height of the tallest trees per tree, and then determine the minimum cost. cost up to 200, up to two hundred per query, time enough.

All need to cut than the current tallest tree, the answer may be pretreated, real-time changes during enumeration. Note that different heights of the tree may be different, to consider when calculating the number of additional modifications to the existing number of trees in the tree after the current highly processed, or would interfere with the results.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(a) memset(a,0,sizeof a)
#define pii pair<int,int>
const int N=1e5+5;
const int INF=1e9+5;
ll read(ll &a){scanf("%lld",&a);return a;}
int read(int &a){scanf("%d",&a);return a;}
int read(){int a;scanf("%d",&a);return a;}
int read(int &a,int &b){scanf("%d %d",&a,&b);return a;}
int n;
struct node{
    ll h,c,p;
}a[N];
ll leftans;
//ll leftcnt;
ll cnt[210];
void scan(){
    leftans=0;
    //leftcnt=0;
    for(int i=0;i<n;i++){
            scanf("%lld%lld%lld",&a[i].h,&a[i].c,&a[i].p);
            leftans+=a[i].c*a[i].p;
            //leftcnt+=a[i].p;
    }
}
bool cmp(node x,node y){
    return x.h<y.h;
}
void solve(){
    sort(a,a+n,cmp);
    //a[n].h=0;
    mem(cnt);
    ll high=0;
    ll highcnt=0;
    ll needcut=0;
    ll ans=1e18+7;
    ll temp;
    ll all=0;
    queue<node>q;
    for(int i=0;i<n;i++){
        //leftcnt-=a[i].p;
        leftans-=a[i].p*a[i].c;
        all+=a[i].p;
        temp=leftans;
        if(a[i].h>high){
            highcnt=a[i].p;
            while(!q.empty()){
                cnt[q.front().c]+=q.front().p;
                q.pop();
            }
        }
        else highcnt+=a[i].p;
        high=a[i].h;
        needcut=all-(highcnt+highcnt-1);
        if(needcut>0){
            for(int j=1;j<=200&&needcut>0;j++){
                if(needcut>=cnt[j]){
                    needcut-=cnt[j];
                    temp+=cnt[j]*j;
                }
                else{
                    temp+=needcut*j;
                    needcut=0;
                    break;
                }
            }
        }
        ans=min(ans,temp);
        q.push(a[i]);
        //cnt[a[i].c]+=a[i].p;
    }
    printf("%lld\n",ans);
}
int main()
{
    while(~scanf("%d",&n)){
        scan();
        solve();
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/CGKirito/p/11327544.html