当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

Image cropper example

4PAM在高斯信道与瑞利信道下的基带仿真系统实验

matlab 优化工具 manopt 安装

为什么PrepareStatement性能更好更安全?

异步fifo的实现

MySQL installation and configuration super detailed tutorial and simple database and table building method

Spark SQL null value, Nan judgment and processing

SVD奇异值分解推导及应用与信号恢复

Spark SQL空值Null,NaN判断和处理

记一次Yarn Required executor memeory is above the max threshold(8192MB) of this cluster!
随机推荐
解决vender-base.66c6fc1c0b393478adf7.js:6 TypeError: Cannot read property ‘validate‘ of undefined问题
Spark获取DataFrame中列的方式--col,$,column,apply
《图书馆管理系统——“借书还书”模块》项目研发阶段性总结
matlab--CVX优化工具包安装
JVM-动态字节码技术详解
JVM dynamic bytecode technology details
ML - 图像 - 深度学习和卷积神经网络
Submarine cable detector tss350 (I)
ML - 语音 - 语音处理介绍
苹果内购和Apple Pay 的区别
图论及概念
二进制补码
Endnote 无法编辑range 解决
看到很多App出现闪烁的图片,特别是会员页面
使用cpolar建立一个商业网站(如何购买域名)
Delayed loading source code analysis:
Distributed principle - what is a distributed system
CGO is realy Cool!
UIDocumentInteractionController UIDocumentPickerViewController
My creation anniversary