Lotus and Horticulture(贪心)

Lotus and Horticulture

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


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
 

Recommend
jiangzijing2015


贪心即可,不需要使用特别复杂的算法。我们假设一开始温度无穷小,其值就为所有c[i] 的和。随着温度上升,遇到一个区间的左端点,值加上(a[i]-c[i]),遇到右端点值加上(b[i]-a[i])。我们记录所有的端点,排序规则为,值从小到大,如果相等,左端点靠前(可以这样想,温度一点一点上升,到了一个温度,这个温度是一个区间的左端点和另一个区间的右端点,那么肯定左端点先有效),排完序遍历即可。还要注意,存在若干个点值相同且都是左端点或右端点的情况。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdio>
#include <vector>
#include <set>
using namespace std;
struct Node
{
    int val;
    int id;
    int dir;//1代表左端点
}N[50005*2];
int a[50005],b[50005],c[50005];
bool cmp(Node a,Node b)
{
    if(a.val!=b.val)
        return a.val<b.val;
    else
        return a.dir>b.dir;
}
int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        int n;
        scanf("%d",&n);
        long long ans=0,res=0;
        int num=0;
        int l,r;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d%d%d",&l,&r,&a[i],&b[i],&c[i]);  
            //cin>>N[num].val;
            //scanf("%lld",&N[num].val);
            N[num].val=l;
            N[num].id=i;
            N[num].dir=1;
            num++;
            //cin>>N[num].val;
            //scanf("%lld",&N[num].val);
            N[num].id=i;
            N[num].val=r;
            N[num].dir=-1;
            num++;
            //cin>>a[i]>>b[i]>>c[i];
            //scanf("%lld%lld%lld",&a[i],&b[i],&c[i]);
            res+=c[i];
        }
        //cout<<num<<endl;
        sort(N,N+num,cmp);
        ans=res;
        //cout<<ans<<endl;
        for(int i=0;i<num;i++)
        {
             l=i;
            while(N[i+1].val==N[i].val&&N[i+1].dir==N[i].dir)i++; 
            for(int j=l;j<=i;j++)
            {
                if(N[j].dir==1)
                    res=res+(a[N[j].id]-c[N[j].id]);
                else
                    res=res+(b[N[j].id]-a[N[j].id]);
            }
            //cout<<res<<" "<<N[i].val<<" "<<N[i].id<<endl;
            ans=max(res,ans);
        }
        //cout<<ans<<endl;
        printf("%I64d\n",ans);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值