1. 程式人生 > >codeforces 某套題s : surf(貪心 || 動態規劃)

codeforces 某套題s : surf(貪心 || 動態規劃)

blog 時間 with mis 動態規劃 oal 區間 could points

題目:

 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
*/

 

codeforces 某套題s : surf(貪心 || 動態規劃)