PAT顶级 1003 Universal Travel Sites (35 分)(DP)
简单DP,设dp[i][j]表示前i个项目,截止到第j天所能获得的最大利润。注意必须先按照规则对输入进行排序才有可能找到最优的转移方案。同时数据范围没给,盲猜不会特别大,所以第二维取5000应该问题不大。细节见代码。
#include <bits/stdc++.h>
#define gcd(a, b) __gcd(a, b)
#define INF 0x3f3f3f3f3f
#define eps 1e-6
#define PI acos(-1.0)
#define pb push_back
#define fst first
#define sec second
#define eif else if
#define de(x) cout << x << ' '
#define en cout << '\n'
#define fuck cout << "fuck\n"
#define rep(i, x, y) for (int i = x; i < y; i++)
#define red(i, x, y) for (int i = x - 1; i >= y; i--)
#define mem(a, x) memset(a, x, sizeof(a))
#define IOS cin.tie(0), ios::sync_with_stdio(false)
#define maxn 200005
#define mod 1000000007
typedef long long ll;
#define pll pair<ll, ll>
using namespace std;
ll qpow(ll a, ll b) {
ll ans = 1;
for (; b; b >>= 1) {
if (b & 1)
ans = ans * a % mod;
a = a * a % mod;
}
return ans;
}
struct node {
int p, l, d;
bool operator < (const node& o) const {//必须要排序
if(d != o.d) return d < o.d;
else return l < o.l;
}
} nd[55];
int n, p[55], l[55], d[55], dp[55][5005];//dp[i][j]表示前i个项目,截止到第j天所能获得的最大利润
void solve() {
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> nd[i].p >> nd[i].l >> nd[i].d;
}
sort(nd + 1, nd + n + 1);
rep(i, 1, n + 1) {
p[i] = nd[i].p, l[i] = nd[i].l, d[i] = nd[i].d;
}
int ans = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= 5000; j++) {
dp[i][j] = max(dp[i][j], dp[i - 1][j]);
if(j > d[i]) continue;//不能break?
if(j < l[i]) continue;
dp[i][j] = max(dp[i][j], p[i]);
for(int k = 1; k < i; k++) {
if(j - l[i] >= 0) dp[i][j] = max(dp[i][j], dp[k][j - l[i]] + p[i]);
}
ans = max(ans, dp[i][j]);
}
}
cout << ans;
}
signed main() {
int T = 1;
//cin >> T;
while (T--) {
solve();
}
}