题目:
Now that you’ve come to Florida and taken up surfing, you love it! Of course, you’ve realized that if you take a particular wave, even if it’s very fun, you may miss another wave that’s just about to come that’s even more fun. Luckily, you’ve gotten excellent data for each wave that is going to come: you’ll know exactly when it will come, how many fun points you’ll earn if you take it, and how much time you’ll have to wait before taking another wave. (The wait is due to the fact that the wave itself takes some time to ride and then you have to paddle back out to where the waves are crashing.) Obviously, given a list of waves, your goal will be to maximize the amount of fun you could have.
题意:
大体上就是给你n组数据,区间开始,权值的大小,区间持续时间(即区间的结束时间),选择其中的区间,使其最终权值最大。
详细题意自己翻吧。
思路:
贪心的思想,也有点像背包,首先将n个区间按结束时间排序,然后背包每个区间选与不选使其最终权值最大。
具体看代码。
代码:
#include <iostream> #include <cstdio> #include <algorithm> #include <stdio.h> #include <cmath> #include <cstring> #include <vector> #include <map> #include <set> #include <bitset> #include <queue> #include <cstdlib> using namespace std; #define is_lower(c) (c>=‘a‘ && c<=‘z‘) #define is_upper(c) (c>=‘A‘ && c<=‘Z‘) #define is_alpha(c) (is_lower(c) || is_upper(c)) #define is_digit(c) (c>=‘0‘ && c<=‘9‘) #define min(a,b) ((a)<(b)?(a):(b)) #define max(a,b) ((a)>(b)?(a):(b)) #define IO ios::sync_with_stdio(0);\ cin.tie(0); cout.tie(0); #define For(i,a,b) for(int i = a; i <= b; i++) typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef vector<int> vi; const ll inf=0x3f3f3f3f; const double EPS=1e-10; const ll inf_ll=(ll)1e18; const ll maxn=100005LL; const ll mod=1000000007LL; const int N = 1e6+5; struct node { int s,e; int score; }r[300005]; bool cmp(node a,node b) { return a.e <b.e; } long long dp[N*2]; int main() { int T; scanf("%d",&T); for(int i = 1; i <= T; i++){ int x ; scanf("%d%d%d",&r[i].s,&r[i].score,&x); r[i].e = r[i].s + x; } sort(r+1,r+T+1,cmp); for(int i = 1; i <= T;i++){ dp[r[i].e] = max(dp[r[i].s]+r[i].score,dp[r[i].e]); for(int j = r[i].e+1; j <= r[i+1].e;j++) //此循环代表若不选它的下一区间权值的最大值。 dp[j] = dp[r[i].e]; } printf("%lld\n",dp[r[T].e]); }
/*
4
8 50 2
10 40 2
2 80 9
13 20 5
10
2079 809484 180
8347 336421 2509
3732 560423 483
2619 958859 712
7659 699612 3960
7856 831372 3673
5333 170775 1393
2133 989250 2036
2731 875483 10
7850 669453 842
*/
原文地址:https://www.cnblogs.com/GHzz/p/8724023.html