【HDU 6012 Lotus and Horticulture】+ 思维 + map

来源:互联网 发布:朱棣 知乎 编辑:程序博客网 时间:2024/05/21 23:00

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

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

题意 : 这几天Lotus对培养盆栽很感兴趣,于是她想搭建一个温室来满足她的研究欲望。Lotus将所有的nnn株盆栽都放在新建的温室里,所以所有盆栽都处于完全相同的环境中。每一株盆栽都有一个最佳生长温度区间[l,r][l,r][l,r],在这个范围的温度下生长会生长得最好,但是不一定会提供最佳的研究价值(Lotus认为研究发育不良的盆栽也是很有研究价值的)。Lotus进行了若干次试验,发现若第iii株盆栽的生长温度适宜,可以提供aia_ia​i​​的研究价值;若生长温度超过了适宜温度的上限,能提供bib_ib​i​​的研究价值;若生长温度低于适宜温度的下限,则能提供cic_ic​i​​的研究价值。现在通过试验,Lotus已经得知了每一株盆栽的适宜生长温度范围,也知道了它们的aaa、bbb、ccc的值。你需要根据这些信息,给温室选定一个温度(这个温度可以是任意实数),使得Lotus能获得的研究价值最大,输出最大研究价值

思路 : 首先考虑应该尝试选择哪些点:区间的左右端点、与区间左右端点距离0.50.50.5的点,这样就一定可以包括所有情况。 为了方便处理与区间左右端点距离0.50.50.5的点,只要将所有坐标扩大一倍,然后这些点就变成了“与区间左右端点距离111的点”了 考虑选出这些点后如何进行统计。显然先要将可以选的位置进行离散。假如我们选择的温度一开始是负无穷,这时答案是所有的ccc之和,考虑选择的温度不断升高,答案会如何变化。 每当选定的温度达到一个区间xxx的左端点时,答案加上ax−cxa_x-c_xa​x​​−c​x​​,每当选定温度超过xxx的右端点时,答案会加上bx−axb_x-a_xb​x​​−a​x​​。 维护一个数组vvv,初始全为000。我们在xxx的左端点处加上ax−cxa_x-c_xa​x​​−c​x​​,在xxx的右端点处加上bx−axb_x-a_xb​x​​−a​x​​,然后某个位置的前缀和就是选择这个位置作为最终温度的答案了 –> (这种方法是为了区分一些右端点和一些左端点重合的情况,把左端点扩大二倍,右端点二倍加一也是一样的)

AC代码:

#include<bits/stdc++.h>using namespace std;typedef long long LL;int main(){    int T,N,l,r,a,b,c;    scanf("%d",&T);    while(T--){        scanf("%d",&N);        map <int , LL> m;        while(N--){            scanf("%d %d %d %d %d",&l,&r,&a,&b,&c);            m[0] += c;            m[l * 2] += a - c;            m[r * 2 + 1] += b - a;        }        LL ans = 0,cut = 0;        for(map <int , LL> :: iterator o = m.begin() ; o != m.end(); o ++){            cut += o -> second;            ans = max(ans,cut);        }        printf("%lld\n",ans);    }    return 0;}
0 0