链接: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
#include
#include
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>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
#include
#include
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>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;
}