2019牛客暑期多校訓練營(第七場) 權值線段樹或主席樹

鏈接:https://ac.nowcoder.com/acm/contest/887/C

題目描述

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

題意:給你一些種類的樹,每種樹都有高度、花費以及高度,要求我們刪掉一些樹(也可以不刪),使得最高的樹的數量大於其他剩餘的樹數量的總和,刪掉一棵樹會有花費,問最小的花費。

思路:對高度從小到大排序,枚舉每一個高度,當做最高的樹,顯然大於該高度的樹都刪掉,如果此時還需要刪除樹x顆,那麼可以考慮建立一棵權值線段樹,權值是花費,那麼我們就可以查詢前x個花費最小的樹的總花費,最後加和即可。我們也可以用主席樹。

權值線段樹:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+100;
ll pre[maxn],suf[maxn],ans;
int st[maxn],ed[maxn],n;
struct node{
	ll h;
	ll c;
	ll p;
	bool operator < (const node &a) const
	{
		return h<a.h;
	}
}a[maxn];
struct Node{
	int l;
	int r;
	ll num;
	ll cost;
}tree[maxn<<2];
void pushup(int cur)
{
    tree[cur].num=tree[cur<<1].num+tree[cur<<1|1].num;
    tree[cur].cost=tree[cur<<1].cost+tree[cur<<1|1].cost;
}
void build(int l,int r,int cur)
{
    tree[cur].num=0;
    tree[cur].cost=0;
    tree[cur].l=l;
    tree[cur].r=r;
    if(l==r) return ;
    int m=(l+r)>>1;
    build(l,m,cur<<1);
    build(m+1,r,cur<<1|1);
}
void update(ll cost,ll num,int cur)
{
    if(tree[cur].l==tree[cur].r)
    {
        tree[cur].num+=num;
        tree[cur].cost+=1LL*tree[cur].l*num;
        return;
    }
    if(cost<=tree[cur<<1].r) update(cost,num,cur<<1);
    else update(cost,num,cur<<1|1);
    pushup(cur);
}
ll query(ll num,int cur)
{
	if(num<=0) return 0;
    if(tree[cur].l==tree[cur].r) return tree[cur].l*num;
    if(tree[cur<<1].num>=num) return query(num,cur<<1);
    else return tree[cur<<1].cost+query(num-tree[cur<<1].num,cur<<1|1);
}
int main()
{
	while(~scanf("%d",&n))
	{
		ans=1e18;
		for(int i=1;i<=n;i++) scanf("%lld%lld%lld",&a[i].h,&a[i].c,&a[i].p);
		sort(a+1,a+1+n);
		pre[0]=suf[n+1]=0;
		for(int i=1;i<=n;i++) pre[i]=pre[i-1]+a[i].p;
		for(int i=n;i>=1;i--) suf[i]=suf[i+1]+a[i].c*a[i].p;
		st[1]=1;ed[n]=n;
		for(int i=2;i<=n;i++) a[i].h==a[i-1].h?st[i]=st[i-1]:st[i]=i;
		for(int i=n-1;i>=1;i--) a[i].h==a[i+1].h?ed[i]=ed[i+1]:ed[i]=i;
		build(1,200,1);
		for(int i=1;i<=n;i++)
		{
			ll tmp=suf[ed[i]+1];
			if(pre[ed[i]]-pre[st[i]-1]<=pre[st[i]-1]) tmp+=query(2*pre[st[i]-1]-pre[ed[i]]+1,1);
			ans=min(ans,tmp);
			if(ed[i]==i) for(int j=st[i];j<=ed[i];j++) update(a[j].c,a[j].p,1);
		}
		printf("%lld\n",ans);
	}
	return 0;
}

主席樹:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+100;
ll pre[maxn],suf[maxn],ans;
int st[maxn],ed[maxn],root[maxn],cnt,n;
struct node{
    ll h;
    ll c;
    ll p;
    bool operator < (const node &a) const
    {
        return h<a.h;
    }
}a[maxn];
struct Node{
    int l;
    int r;
    ll num;
    ll cost;
}tree[maxn*40];
void pushup(int cur)
{
    tree[cur].cost=tree[tree[cur].l].cost+tree[tree[cur].r].cost;
    tree[cur].num=tree[tree[cur].l].num+tree[tree[cur].r].num;
}
void build(int &cur,int l,int r)
{
    cur=++cnt;
    tree[cur].cost=0;
    tree[cur].num=0;
    if(l==r) return ;
    int m=(l+r)>>1;
    build(tree[cur].l,l,m);
    build(tree[cur].r,m+1,r);
}
void update(int &now,int last,int l,int r,ll cost,ll num)
{
    now=++cnt;
    tree[now]=tree[last];
    if(l==r)
    {
        tree[now].num+=num;
        tree[now].cost+=l*1LL*num;
        return ;
    }
    int m=(l+r)>>1;
    if(cost<=m) update(tree[now].l,tree[last].l,l,m,cost,num);
    else update(tree[now].r,tree[last].r,m+1,r,cost,num);
    pushup(now);
}
ll query(int now,int l,int r,ll num)
{
    if(num<=0) return 0;
    if(l==r) return l*num;
    int m=(l+r)>>1;
    if(tree[tree[now].l].num>=num) return query(tree[now].l,l,m,num);
    else return tree[tree[now].l].cost+query(tree[now].r,m+1,r,num-tree[tree[now].l].num);
}
int main()
{
    while(~scanf("%d",&n))
    {
        cnt=0;ans=1e18;
        for(int i=1;i<=n;i++) scanf("%lld%lld%lld",&a[i].h,&a[i].c,&a[i].p);
        sort(a+1,a+1+n);
        pre[0]=suf[n+1]=0;
        for(int i=1;i<=n;i++) pre[i]=pre[i-1]+a[i].p;
        for(int i=n;i>=1;i--) suf[i]=suf[i+1]+a[i].c*a[i].p;
        st[1]=1;ed[n]=n;
        for(int i=2;i<=n;i++) a[i].h==a[i-1].h?st[i]=st[i-1]:st[i]=i;
        for(int i=n-1;i>=1;i--) a[i].h==a[i+1].h?ed[i]=ed[i+1]:ed[i]=i;
        build(root[0],1,210);
        for(int i=1;i<=n;i++) update(root[i],root[i-1],1,200,a[i].c,a[i].p);
        for(int i=1;i<=n;i++)
        {
            ll tmp=suf[ed[i]+1];
            if(pre[ed[i]]-pre[st[i]-1]<=pre[st[i]-1]) tmp+=query(root[st[i]-1],1,200,2*pre[st[i]-1]-pre[ed[i]]+1);
            ans=min(ans,tmp);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章