HDU 6012 Lotus and Horticulture(离散化)
题目代号:HDU 6012
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6012
Lotus and Horticulture
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1231 Accepted Submission(s): 380
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 $a_i$ units of research value; if the growth temperature exceeds the upper limit of the suitable temperature, it can provide the $b_i$ units of research value; temperatures below the lower limit of the appropriate temperature, can provide $c_i$ 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.__
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 $a_i$ units of research value; if the growth temperature exceeds the upper limit of the suitable temperature, it can provide the $b_i$ units of research value; temperatures below the lower limit of the appropriate temperature, can provide $c_i$ 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\in[1,50000]$, the number of potted plants.
The next $n$ line, each line contains five integers $l_i,r_i,a_i,b_i,c_i\in[1, 10^9]$.
The first line of each test case contains a single integer $n\in[1,50000]$, the number of potted plants.
The next $n$ line, each line contains five integers $l_i,r_i,a_i,b_i,c_i\in[1, 10^9]$.
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
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
题目大意:有n种花,[l,r]之间的温度是最适温度,此时研究价值为a,高于r时研究价值为b,低于l时则研究价值为c。在某一温度能达到最大的研究价值是多少。
解题思路:离散化然后更新各个区间的最大值。
之前忘记输入数据量很大,所以使用的cin然后一直超时,,,我以为是map迭代器的原因后面没用map写了一次,然后发现依然超时,,,最后终于发现是cin的原因[气哭]
解法一:map暴力离散,然后迭代器遍历一遍
# include <stdio.h> # include <string.h> # include <stdlib.h> # include <iostream> # include <fstream> # include <vector> # include <queue> # include <stack> # include <map> # include <math.h> # include <algorithm> using namespace std; # define pi acos(-1.0) # define IOS ios::sync_with_stdio(false) # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define For(i,n,a) for(int i=n; i>=a; --i) # define FO(i,a,n) for(int i=a; i<n; ++i) # define Fo(i,n,a) for(int i=n; i>a ;--i) typedef long long LL; typedef unsigned long long ULL; map<int,LL>M; int main() { //freopen("in.txt", "r", stdin); int t; scanf("%d",&t); while(t--) { M.clear(); int n; LL ans=0,sum=0; scanf("%d",&n); int l,r,a,b,c; for(int i=1;i<=n;i++) { scanf("%d%d%d%d%d",&l,&r,&a,&b,&c); M[l<<1]+=a-c; M[(r<<1)+1]+=b-a; sum+=c; } ans=max(ans,sum); for(map<int,LL>::iterator p=M.begin();p!=M.end();p++) { sum+=p->second; ans=max(ans,sum); } printf("%lld\n",ans); } return 0; }
解法二:
# include <stdio.h> # include <string.h> # include <stdlib.h> # include <iostream> # include <fstream> # include <vector> # include <queue> # include <stack> # include <map> # include <math.h> # include <algorithm> using namespace std; # define pi acos(-1.0) # define IOS ios::sync_with_stdio(false) # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define For(i,n,a) for(int i=n; i>=a; --i) # define FO(i,a,n) for(int i=a; i<n; ++i) # define Fo(i,n,a) for(int i=n; i>a ;--i) typedef long long LL; typedef unsigned long long ULL; inline int Scan() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();} return x*f; } int const MAXM=50005; LL sum, f[2*MAXM], e[4*MAXM]; int l[MAXM], r[MAXM], a[MAXM], b[MAXM], c[MAXM]; int main() { //freopen("in.txt", "r", stdin); int t, n, ans, len; t=Scan(); while(t--) { n=Scan(), ans=sum=0; for(int i=1;i<=n;++i) { //l[i]=Scan(); r[i]=Scan(); a[i]=Scan(); b[i]=Scan(); c[i]=Scan(); scanf("%d%d%d%d%d",&l[i],&r[i],&a[i],&b[i],&c[i]); sum+=c[i]; f[++ans]=l[i]; f[++ans]=r[i]; } sort(f+1,f+ans+1); len=unique(f+1,f+ans+1)-(f+1); mem(e,0); FOR(i,1,n) { l[i]=lower_bound(f+1,f+len+1,l[i])-f; r[i]=lower_bound(f+1,f+len+1,r[i])-f; e[l[i]<<1]+=a[i]-c[i]; e[r[i]<<1|1]+=b[i]-a[i]; } LL num=sum; for(int i=1;i<=2*len+1;++i) { sum+=e[i]; num=max(num,sum); } printf("%lld\n",num); } return 0; }