2019牛客暑期多校训练营(第七场) Governing sand


时间限制: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≤10^5),the kinds of trees.
Then followed n lines with each line three integers Hi(1≤Hi≤10^9)-the height of each tree, Ci(1≤Ci≤200)-the cost of cutting down each tree, and Pi(1≤Pi≤10^9)-the number of the tree.

输出描述:

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

输入

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

输出

1
2

题意:每种树具有P-数量,H-高度,C-除掉的费用三个属性,求清除一些树后,使森林中最高的树木数量应该超过所有树木的一半的最小费用。

题解:按照高度排序,枚举高度,大于当前高度的树应全部除掉,小于当前高度的树应留下(此高度的树的数量-1)棵树,其他的除掉(优先处理花费小的树,才能使最小费用最小)。令w[i]为清除第i种树之后所有树的花费,sum[i]为第i种树之前所有树的数量,value[i]为需要花费i才能清除的树的数量。

代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=100005;
struct node
{
  long long p,h,c;
  node(){}
  node(long long a,long long b,long long d)
  {p=a;h=b;c=d;}
  bool operator <(const node&n) const
  { return h<n.h; }
}v[maxn];
long long w[maxn],num[maxn],value[205];
int main()
{
  int i,j,n;
  long long h,c,p,t,cnt,ans,sum;
  while(~scanf("%d",&n))
  {
    ans=0x3f3f3f3f3f3f3f3f; w[n]=num[0]=0;
    fill(value,value+205,0);
    for(i=1;i<=n;i++) 
     {
      scanf("%lld%lld%lld",&h,&c,&p);
      v[i]=node(p,h,c);
     }
    sort(v+1,v+1+n);
    
    for(i=n-1;i>=1;i--) w[i]=w[i+1]+v[i+1].p*v[i+1].c;
    
    for(i=1;i<=n;i++)
    {
      t=i;cnt=v[i].p;
      num[i]=num[i-1]+v[i-1].p;
      while(i+1<n&&v[i].h==v[i+1].h) i++,num[i]=num[i-1]+v[i-1].p,cnt+=v[i].p;
      sum=w[i];cnt=num[t]-(cnt-1);
      if(cnt<0) cnt=0;
      for(j=1;j<=200;j++)
      {
        if(!value[j]) continue;
        if(cnt>=value[j])
          {cnt-=value[j];sum+=j*value[j];}
        else
          {sum+=j*cnt;break;}
      }
     for(j=t;j<=i;j++) value[v[j].c]+=v[j].p; //value[]总是存储比当前树高度小的树的花费和树的数量
     ans=min(ans,sum);
    }
    printf("%lld\n",ans);
  }
  system("pause");
  return 0;
}

猜你喜欢

转载自www.cnblogs.com/VividBinGo/p/11331601.html