Codeforces Round #608 (Div. 2) D. Portals(DP)
程序员文章站
2022-06-04 08:16:05
...
题目链接
思路:dp【i】【j】表示攻占了i个城堡,还剩j个士兵的最大价值。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e3+10;
vector<int>v[maxn];
int ans,a[maxn],b[maxn],c[maxn],u,V,n,m,k,flag=0,l[maxn],dp[maxn][maxn];
int main()
{
scanf("%d%d%d",&n,&m,&k);
int t=k;
for(int i=1;i<=n;++i) {
scanf("%d%d%d",&a[i],&b[i],&c[i]);
if(t<a[i]) flag=1;
t+=b[i];
}
for(int i=1;i<=n;++i) l[i]=i;
for(int i=1;i<=m;++i)
{
scanf("%d%d",&u,&V);
l[V]=max(l[V],u);
}
if(flag) {
puts("-1");return 0;
}
for(int i=1;i<=n;++i) v[l[i]].push_back(c[i]);
memset(dp,-1,sizeof(dp));
dp[0][k]=0;
for(int i=1;i<=n;++i)
{
for(int j=0;j<=5000;++j)if(j>=a[i]) dp[i][j+b[i]]=max(dp[i][j+b[i]],dp[i-1][j]);
for(auto x:v[i])
for(int j=1;j<=5000;++j)
if(dp[i][j]!=-1) dp[i][j-1]=max(dp[i][j-1],dp[i][j]+x);
}
for(int i=0;i<=5000;++i) ans=max(ans,dp[n][i]);
printf("%d\n",ans);
}
推荐阅读
-
Educational Codeforces Round 97 (Rated for Div. 2) D. Minimal Height Tree
-
Codeforces Round #665 (Div. 2) D. Maximum Distributed Tree(dfs)
-
Codeforces Round #619 (Div. 2) D. Time to Run
-
Codeforces Round #583 (Div. 1 + Div. 2,) D. Treasure Island(dfs+思维)
-
Codeforces Round #533 (Div. 2) D. Kilani and the Game(bfs+模拟)
-
Codeforces Round #643 (Div. 2) D. Game With Array
-
Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)
-
Codeforces Round #260 (Div. 2) D. A Lot of Games(树上博弈)
-
Codeforces Round #662 (Div. 2) D. Rarity and New Dress
-
Codeforces Round #658 (Div. 2) D. Unmerge(dp,01背包)