zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Xcode修改新建项目注释模板(作者和公司名等)
    Xcode全局替换内容,一键Replace
    The type javax.ws.rs.core.MediaType cannot be resolved. It is indirectly referenced from required .class files
    IT公司的女流之辈
    论公司的核心竞争力
    人性
    如何使用yum 下载 一个 package ?如何使用 yum install package 但是保留 rpm 格式的 package ? 或者又 如何通过yum 中已经安装的package 导出它,即yum导出rpm?
    The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files.
    maven repo plugin archiver
    Error connecting to database [Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (13)]
  • 原文地址:https://www.cnblogs.com/chen99/p/11329747.html
Copyright © 2011-2022 走看看