HDU6012 Lotus and Horticulture 扫描

Lotus and Horticulture
Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 42    Accepted Submission(s): 18


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
BestCoder Round #91 

样例是错的 答案是73


这里写图片描述
显然 一种植物可以表示为3个温度区间
假如把每个区间表示为一条线段 ,线段的值为研究价值
那问题就转化为当x=多少时 区间包括x的线段的值 的总和最大

将线段的2个端点取出来
定义结构体P表示端点

struct P{
    ll pos,key;
}

pos为端点的x坐标 ,左端点key为线段的值 ,右端点key为线段的值的负数
now表示当前x值下 区间包括x的线段的值 的总和
按pos,key排序 从左到右把点全部取出 依次将key加到now,
只需要记录now最大值 即可得到最高的研究价值

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

const int N = 150000 +5;

struct P{
    ll pos,key;
}p[2*N];

bool cmp(const P&a,const P&b){
    if(a.pos==b.pos)
        return a.key<b.key;
    return a.pos<b.pos;
}

int main()
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    //freopen("/home/lu/Documents/w.txt","w",stdout);
    int T,l,r,a,b,c,n;
    scanf("%d",&T);
    while(T--){
        ll ans=0;
        ll maxAns=0;
        scanf("%d",&n);
        for(int i=0;i<n;++i){
            scanf("%d%d%d%d%d",&l,&r,&a,&b,&c);
            ans+=c;
            p[2*i]={l,-c+a};
            p[2*i+1]={r+1,-a+b};
        }
        sort(p,p+2*n,cmp);
        maxAns=ans;
        ll lastPos=-1;
        for(int i=0;i<2*n;){
            while(p[i].pos==lastPos){//将相同位置的点全部取出
                ans+=p[i].key;
                ++i;
            }
            maxAns=max(maxAns,ans);
            ans+=p[i].key;
            lastPos=p[i].pos;
            ++i;
            maxAns=max(maxAns,ans);
        }
        printf("%lld\n",maxAns);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值