• Governing sand 贪心


    题目链接: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(1n105),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(1Hi109)-the height of each tree, Ci(1≤Ci≤200)C_i (1 leq C_i leq 200)Ci(1Ci200)-the cost of cutting down each tree, and Pi(1≤Pi≤109)P_i(1 leq P_ileq 10^9)Pi(1Pi109)-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



    对于最高的树我们有两种操作
    1.留下最高的树,依次砍掉剩下的树中花费最小的树,使其满足题意
    2.砍掉最高的树,维护剩下的树
    求出操作1和2的花费,取最小就是答案

    将n种树按高度从小到大排序,遍历n种树,对于第i高度的树,我们将把第i高的树全部砍掉的花费加上令第i-1高的树作为最高树所需的花费,与令第i高的树为最高所需的花费做比较,选取最小的花费。
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define ll long long
    #define maxn 100005
    struct node{
        ll h,p,c;
        bool operator <(const node &w)const{
            return h<w.h;
        }
    }a[maxn];
    int n;
    ll num[205];//存花费为i的树的数量 
    int main()
    {
        while(cin>>n)
        {
            ll cnt=0;
            for(int i=1;i<=n;i++)
            {
                cin>>a[i].h>>a[i].c>>a[i].p;
                cnt+=a[i].c*a[i].p;
            }
            for(int i=1;i<=205;i++)
            num[i]=0;
            ll ans=cnt,tot=0;
            sort(a+1,a+1+n);
            int pos=1;
            for(int i=1;i<=n;i=pos)
            {
                pos=i;
                while(a[i].h==a[pos].h&&pos<=n)pos++;
                ll sum=0,cost=0;//sum为目前最高树的数量 
                for(int j=i;j<pos;j++)
                {
                    cnt-=a[j].c*a[j].p;
                    sum+=a[j].p;
                }
                tot+=sum;//小于等于目前最高树的数量 
                sum=tot-sum*2+1;//还需砍掉多少颗树 
                for(int j=1;sum>0;j++)
                {
                    if(num[j]<=sum)
                    {
                        cost+=j*num[j];
                        sum-=num[j];
                    }
                    else
                    {
                        cost+=j*sum;
                        sum=0;
                    }
                }
                ans=min(ans,cost+cnt);
                for(int j=i;j<pos;j++)
                {
                    num[a[j].c]+=a[j].p;
                }
            }
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Eclipse Ctrl C卡的解决方法
    oracle函数 逗号分隔的字符串的每个字串两边加上单引号
    EXT自定义颜色表单域控件
    Ext JS开发经验累积
    mysql连接远程数据库
    Hibernate使用连接池
    oracle转mysql数据库 (hibernate+spring)
    解决html中img标签src中含中文出错的问题
    ckeditor自定义图片上传,结合EXT JS
    Web自动化框架搭建——前言
  • 原文地址:https://www.cnblogs.com/chen99/p/11329747.html
Copyright © 2020-2023  润新知