hdu 6012 Lotus and Horticulture(贪心)

来源:互联网 发布:mysql修复数据表 编辑:程序博客网 时间:2024/05/30 22:53

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 provideai units of research value; if the growth temperature exceeds the upper limit of the suitable temperature, it can provide thebi units of research value; temperatures below the lower limit of the appropriate temperature, can provideci units of research value.

Now, through experimentation, Lotus has known the appropriate growth temperature range for each plant, and the values ofa,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 integerT, 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
155 8 16 20 1210 16 3 13 138 11 13 1 117 9 6 17 52 11 20 8 5
 

Sample Output
83
 

Source
BestCoder Round #91
 
题目连接: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 0
原创粉丝点击