标签:PAT int Universal nd Sites ans ll dp define
简单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();
}
}
标签:PAT,int,Universal,nd,Sites,ans,ll,dp,define 来源: https://www.cnblogs.com/lipoicyclic/p/15953442.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。