贪心+背包

这道题比较坑的就是我们的对于相同截止时间的需要排个序,因为我们这个工作是有时间前后顺序的,我们如果不排序的话我们一些截止时间晚的工作就无法得到最优报酬

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;

#define int long long
int t;
int n;
const int N = 5005;
struct node{
	int t,end,pr;
	int start;
	bool operator<(node b){
		if(end<b.end) return 1;
		return 0;
	}
}e[N];
int ans = 0;

int dp[N];

signed main(){
	cin >> t;
	while(t--){
		cin >> n;
		int tmax = 0;
		for(int i=1;i<=n;i++){
			cin >> e[i].t >> e[i].end >> e[i].pr;
			e[i].start = e[i].end - e[i].t;
			tmax = max(tmax,e[i].end);
		}
		sort(e+1,e+1+n);
		for(int i=0;i<=5004;i++) dp[i] = 0;
		for(int i=1;i<=n;i++){
			for(int j=e[i].end;j>=e[i].t;j--){
				dp[j] = max(dp[j],dp[j-e[i].t]+e[i].pr);
			}
		}
		int ans = 0;
		for(int i=1;i<=tmax;i++) ans = max(ans,dp[i]);
		cout << ans << endl;
	}
	return 0;
}
相关推荐
mit6.8241 小时前
bfs|栈
算法
CoderYanger2 小时前
优选算法-栈:67.基本计算器Ⅱ
java·开发语言·算法·leetcode·职场和发展·1024程序员节
jllllyuz2 小时前
Matlab实现基于Matrix Pencil算法实现声源信号角度和时间估计
开发语言·算法·matlab
稚辉君.MCA_P8_Java2 小时前
DeepSeek 插入排序
linux·后端·算法·架构·排序算法
多多*2 小时前
Java复习 操作系统原理 计算机网络相关 2025年11月23日
java·开发语言·网络·算法·spring·microsoft·maven
.YM.Z3 小时前
【数据结构】:排序(一)
数据结构·算法·排序算法
Chat_zhanggong3454 小时前
K4A8G165WC-BITD产品推荐
人工智能·嵌入式硬件·算法
百***48074 小时前
【Golang】slice切片
开发语言·算法·golang
墨染点香4 小时前
LeetCode 刷题【172. 阶乘后的零】
算法·leetcode·职场和发展
做怪小疯子4 小时前
LeetCode 热题 100——链表——反转链表
算法·leetcode·链表