当前位置:网站首页>2021上海市赛-B-排序后dp
2021上海市赛-B-排序后dp
2022-07-25 15:23:00 【塔子哥来了】
题目大意:
给你 n n n个三元组 ( a i , b i , c i ) (a_i,b_i,c_i) (ai,bi,ci).每个维度要分别选出 a , b , c ( a + b + c = n ) a,b,c(a+b+c=n) a,b,c(a+b+c=n)个使得价值最大。
题目思路:
从贪心入手:
如果是二元组的情况,我们只需要对 b − a b-a b−a排序后贪心选b个 b i b_i bi再剩下的选 a i a_i ai.
将排序后的序列挖去一个子序列后依然符合贪心的性质.所以我们可以对第三个维度进行 d p ( i , j ) dp(i,j) dp(i,j)代表前i个三元组,选了 j j j个 c i c_i ci的最大价值.
若第 i i i个不选,根据性质,当 i ≤ b i \leq b i≤b时贪心选 b b b,反之选 a a a.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vll vector<ll>
#define fi first
#define se second
const int maxn = 5000 + 5;
const int mod = 1e9 + 7;
struct Node{
int a , b , c;
bool operator < (const Node & g){
return b - a > g.b - g.a;
}
}a[maxn];
ll dp[maxn][maxn];
int main()
{
ios::sync_with_stdio(false);
int n , x , y , z; cin >> n >> x >> y >> z;
for (int i = 1 ; i <= n ; i++){
cin >> a[i].a >> a[i].b >> a[i].c;
}
sort(a + 1 , a + 1 + n);
for (int i = 0 ; i <= n ; i++)
for (int j = 0 ; j <= n ; j++)
dp[i][j] = -1e18;
dp[0][0] = 0;
for (int i = 1 ; i <= n ; i++){
for (int j = 0 ; j <= i ; j++){
if (j)
dp[i][j] = dp[i - 1][j - 1] + a[i].c;
int rest = i - j;
if (rest <= y) dp[i][j] = max(dp[i][j] , dp[i - 1][j] + a[i].b);
else dp[i][j] = max(dp[i][j] , dp[i - 1][j] + a[i].a);
}
}
cout << dp[n][z] << endl;
return 0;
}
边栏推荐
- MySQL transactions and mvcc
- How spark gets columns in dataframe --column, $, column, apply
- 2019陕西省省赛K-变种Dijstra
- Image cropper example
- Is it safe to open futures online? Which company has the lowest handling charge?
- CGO is realy Cool!
- JVM-垃圾收集器详解
- Object.prototype. Hasownproperty() and in
- Recommend 10 learning websites that can be called artifact
- 自定义注解校验API参数电话号
猜你喜欢
随机推荐
JVM-垃圾收集器详解
ML - Speech - advanced speech model
Ml speech depth neural network model
MySQL transactions and mvcc
Understanding the execution order of T-SQL query from the execution order of join on and where
The number of query results of maxcompute SQL is limited to 1W
Flex 布局
MATLAB读取显示图像时数据格式转换原因
二进制补码
HBCK fix problem
理解“平均负载”
Maxcompute SQL 的查询结果条数受限1W
Spark memory management mechanism new version
Hbck 修复问题
Object.prototype. Hasownproperty() and in
Spark AQE
Once spark reported an error: failed to allocate a page (67108864 bytes), try again
Node learning
ML - 自然语言处理 - 关键技术
异步fifo的实现









