Governing sand(权值线段树+离散化)

链接:https://ac.nowcoder.com/acm/contest/887/C
来源:牛客网
 

时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 65536K,其他语言131072K
64bit IO Format: %lld

题目描述

The Wow village is often hit by wind and sand,the sandstorm seriously hindered the economic development of the Wow village.

There is a forest in front of the Wowo village, this forest can prevent the invasion of wind and sand. But there is a rule that the number of tallest trees in the forest should be more than half of all trees, so that it can prevent the invasion of wind and sand. Cutting down a tree need to cost a certain amount of money. Different kinds of trees cost different amounts of money. Wow village is also poor.

There are n kinds of trees. The number of i-th kind of trees is PiP_iPi​, the height of i-th kind of trees is HiH_iHi​, the cost of cutting down one i-th kind of trees is CiC_iCi​.

 

(Note: "cutting down a tree" means removing the tree from the forest, you can not cut the tree into another height.)

 

输入描述:

The problem is multiple inputs (no more than 30 groups).
For each test case.
The first line contines one positive integers n(1≤n≤105)n (1 \leq n \leq 10^5)n(1≤n≤105),the kinds of trees.
Then followed n lines with each line three integers Hi(1≤Hi≤109)H_i (1 \leq H_i \leq 10^9)Hi​(1≤Hi​≤109)-the height of each tree, Ci(1≤Ci≤200)C_i (1 \leq C_i \leq 200)Ci​(1≤Ci​≤200)-the cost of cutting down each tree, and Pi(1≤Pi≤109)P_i(1 \leq P_i\leq 10^9)Pi​(1≤Pi​≤109)-the number of the tree.

输出描述:

For each test case, you should output the minimum cost.

示例1

输入

复制

2
5 1 1
1 10 1
2
5 1 2
3 2 3

输出

复制

1
2

 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+5;
struct node{
    ll H,C,P;
}tr[N];
bool cmpH(node a,node b){
    return a.H<b.H;
}
ll num[205],pre[N],pp[N];
struct TREE{
	ll num,sum;
}tree[205<<2];
inline void pushup(int p){
	tree[p].num=tree[p<<1].num+tree[p<<1|1].num;
	tree[p].sum=tree[p<<1].sum+tree[p<<1|1].sum;
}
void build(int l,int r,int p){
	if(l==r){
		tree[p].num=num[l];
		tree[p].sum=num[l]*l;
		return ;
	}
	int mid=(l+r)>>1;
	build(l,mid,p<<1);
	build(mid+1,r,p<<1|1);
	pushup(p);
}
void update(int l,int r,int p,int val,ll cnt){
	if(l==r){
		tree[p].num-=cnt;
		tree[p].sum-=cnt*val;
		return ;
	}
	int mid=(l+r)>>1;
	if(val<=mid) update(l,mid,p<<1,val,cnt);
	else update(mid+1,r,p<<1|1,val,cnt);
	pushup(p);
}
ll query(int l,int r,int p,ll cnt){
	if(cnt==0) return 0;
	if(tree[p].num==cnt) return tree[p].sum;
	if(l==r) return cnt*l;
	int mid=(l+r)>>1;
	if(tree[p<<1].num>=cnt) return query(l,mid,p<<1,cnt);
	else return tree[p<<1].sum+query(mid+1,r,p<<1|1,cnt-tree[p<<1].num);
}
int main(){
    int n;
    while(~scanf("%d",&n)) {
    	for(int i=1;i<=200;i++) num[i]=0;
        for(int i=1;i<=n;i++){
        	scanf("%lld%lld%lld",&tr[i].H,&tr[i].C,&tr[i].P);
        	num[tr[i].C]+=tr[i].P;
		}
		sort(tr+1,tr+1+n,cmpH);
		int idh=0;
		int lh=0;
		for(int i=1;i<=n;i++){
			if(tr[i].H>lh){
				lh=tr[i].H;
				tr[i].H=++idh;
			} 
			else{
				tr[i].H=idh;
			}
		}
		build(1,200,1);
		for(int i=0;i<=idh;i++) pre[i]=0,pp[i]=0;
		for(int i=1;i<=n;i++) pre[tr[i].H]+=tr[i].P;//pre[i]表示高为i的树的数目 
		for(int i=1;i<=idh;i++) pre[i]=pre[i-1]+pre[i];//pre[i]表示高<=i的树的数目 
		for(int i=1;i<=n;i++) pp[tr[i].H]+=tr[i].C*tr[i].P;//pp[i]表示高为i的树砍掉的花费
		for(int i=1;i<=idh;i++) pp[i]=pp[i-1]+pp[i];//pp[i]表示砍掉高度<=i的树的花费 
		ll ans=1e16;
		int pos=n;
		for(int i=idh;i>=1;i--){
			ll sum=0;
			while(pos&&tr[pos].H==i){
				update(1,200,1,tr[pos].C,tr[pos].P);
				pos--;
			}
			sum+=pp[idh]-pp[i];
			sum+=query(1,200,1,max(0LL,pre[i-1]-(pre[i]-pre[i-1]-1)));
			ans=min(ans,sum);
		}
		printf("%lld\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值