HDU 6012【离散化+扫描线】

通过算法确定温室的最佳温度以获得最高的研究价值。面对多个植物种类及其各自适宜的生长温度范围,本算法采用离散化和扫描线的方法,在允许的温度范围内找到最佳温度点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Lotus and Horticulture

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


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
 

题意:

n个区间 [)前闭后开型,每个区间左中右分别三个花费,问哪个点花费最大。

题解:

因为点可以取任意实数,区间是整数,所以距离区间端点0.5的点都是可能的点,将区间左右端点*2,每个区间离散化成 l-1, l, r这三个点。然后扫描线去遍历,碰到左边边界 +a-c; 右边界 +b-a;

代码:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <bitset>
 6 #include <vector>
 7 #include <queue>
 8 #include <stack>
 9 #include <cmath>
10 #include <list>
11 #include <set>
12 #include <map>
13 #define rep(i,a,b) for(int i = a;i <= b;++ i)
14 #define per(i,a,b) for(int i = a;i >= b;-- i)
15 #define mem(a,b) memset((a),(b),sizeof((a)))
16 #define FIN freopen("in.txt","r",stdin)
17 #define FOUT freopen("out.txt","w",stdout)
18 #define IO ios_base::sync_with_stdio(0),cin.tie(0)
19 #define mid ((l+r)>>1)
20 #define ls (id<<1)
21 #define rs ((id<<1)|1)
22 #define N 50005
23 #define INF 0x3f3f3f3f
24 #define INFF ((1LL<<62)-1)
25 typedef long long LL;
26 using namespace std;
27 
28 int T, n, HASH[N*3], l[N], r[N], a[N], b[N], c[N];
29 LL cost[N*3];
30 int main()
31 {IO;
32     //FIN;
33     cin >> T;
34     while(T--){
35         cin >> n;
36         int len = 0;
37         LL tol = 0;
38         rep(i, 1, n){
39             cin >> l[i] >> r[i] >> a[i] >> b[i] >> c[i];
40             l[i] <<= 1; r[i] <<= 1;
41             HASH[len++] = l[i]; HASH[len++] = l[i]-1; HASH[len++] = r[i];
42             tol += c[i];
43         }
44         sort(HASH, HASH+len);
45         int size = unique(HASH, HASH+len)-HASH;
46         LL ans = tol;
47         mem(cost, 0);
48         
49         rep(i, 1, n){
50             l[i] = lower_bound(HASH, HASH+size, l[i])-HASH;
51             r[i] = lower_bound(HASH, HASH+size, r[i])-HASH;
52             cost[l[i]] += a[i]-c[i];
53             cost[r[i]+1] += b[i]-a[i];
54         }
55         rep(i, 0, size-1){
56             tol += cost[i];
57             ans = max(ans, tol);
58         }
59         cout << ans << endl;
60     }
61     return 0;
62 }
View Code

 

转载于:https://www.cnblogs.com/Jstyle-continue/p/6375642.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值