hdu 6012 Lotus and Horticulture(贪心)

Lotus and Horticulture

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 585    Accepted Submission(s): 188



Problem Description
These days Lotus is interested in cultivating potted plants, so she wants to build a greenhouse to meet her research desires.

Lotus placed all of the n pots in the new greenhouse, so all potted plants were in the same environment.

Each plant has an optimal growth temperature range of [l,r] , which grows best at this temperature range, but does not necessarily provide the best research value (Lotus thinks that researching poorly developed potted plants are also of great research value).

Lotus has carried out a number of experiments and found that if the growth temperature of the i-th plant is suitable, it can provide ai units of research value; if the growth temperature exceeds the upper limit of the suitable temperature, it can provide the bi units of research value; temperatures below the lower limit of the appropriate temperature, can provide ci units of research value.

Now, through experimentation, Lotus has known the appropriate growth temperature range for each plant, and the values of a , b , c are also known. You need to choose a temperature for the greenhouse based on these information, providing Lotus with the maximum research value.

__NOTICE: the temperature can be any real number.__
 

Input
The input includes multiple test cases. The first line contains a single integer T , the number of test cases.

The first line of each test case contains a single integer n[1,50000] , the number of potted plants.

The next n line, each line contains five integers li,ri,ai,bi,ci[1,109] .
 

Output
For each test case, print one line of one single integer presenting the answer.
 

Sample Input
  
  
1 5 5 8 16 20 12 10 16 3 13 13 8 11 13 1 11 7 9 6 17 5 2 11 20 8 5
 

Sample Output
  
  
83
 

Source
 
题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=6012

题目大意:一个温室种花,每种花都有一个适合生长的温度区间,低于提供ci的价值,等于提供ai价值,大于提供bi价值,问最大价值总和;

题目思路:这是一种很经典的贪心,把每个区间分成左右两个端点,记录每个端点的左右,所属区间,和大小。按大小排序(若大小相同,则左端点排前面)。如此排序,易得初始情况是所有花都在低于的温度下成长,然后遍历端点,左端点加ai-ci,右端点加bi-ai;
易错点: 这种贪心易错在端点相同时的考量;如这题:
              端点相同时,把同属左端点的的值优先考量,再考量同属右端点。

代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=50000+100;
int a[maxn],b[maxn],c[maxn];
struct node
{
    int l,id,f;
    bool operator<(const node&p)const
    {
        if(l!=p.l)
        return l<p.l;
        return f>p.f;
    }
} A[maxn*2];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        ll ans=0,res=0;
        int l,r;
        int len=0;
        for(int i=0; i<n; i++)
        {
            scanf("%d%d%d%d%d",&l,&r,&a[i],&b[i],&c[i]);
            A[len].l=l;
            A[len].id=i;
            A[len++].f=1;
            res+=c[i];
            A[len].l=r;
            A[len].id=i;
            A[len++].f=-1;
        }
        sort(A,A+len);
        ans=res;
        for(int i=0; i<len; i++)
        {
            l=i;
            while(A[i+1].l==A[i].l&&A[i+1].f==A[i].f)i++;
            for(int j=l;j<=i;j++)
            {
                int id=A[j].id;
                if(A[j].f==1)
                {
                    res+=a[id]-c[id];
                }
                else
                {
                    res+=b[id]-a[id];
                }
            }
            ans=max(ans,res);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值