执行任务赚积分
题目描述
现有N个任务需要处理,同一时间只能处理一个任务,处理每个任务所需要的时间固定为1。
每个任务都有最晚处理时间限制和积分值,在最晚处理时间点之前处理完成任务才可获得对应的积分奖励。
可用于处理任务的时间有限,请问在有限的时间内,可获得的最多积分。
输入描述
第一行为一个数 N,表示有 N 个任务
- 1 ≤ N ≤ 100
第二行为一个数 T,表示可用于处理任务的时间
- 1 ≤ T ≤ 100
接下来 N 行,每行两个空格分隔的整数(SLA 和 V),SLA 表示任务的最晚处理时间,V 表示任务对应的积分。
- 1 ≤ SLA ≤ 100
- 0 ≤ V ≤ 100000
输出描述
可获得的最多积分
用例1
输入
4
3
1 2
1 3
1 4
1 5
输出
5
说明
虽然有3个单位的时间用于处理任务,可是所有任务在时刻1之后都无效。 所以在第1个时间单位内,选择处理有5个积分的任务。1-3时无任务处理。
用例2
输入
4
3
1 2
1 3
1 4
3 5
[Copy](javascript:😉
输出
9
说明
第1个时间单位内,处理任务3,获得4个积分
第2个时间单位内,处理任务4,获得5个积分
第3个时间单位内,无任务可处理
共获得9个积分
代码
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
//因为每个任务耗时 1,只要在最晚之前做完就可以,不需要提前,deadline 是第一生产力
//将任务按照sla分成不同的组,每次选组内最大的那个 就是最好的方案
struct Task {
int sla;
int score;
};
bool compareBySla(const Task &a, const Task &b) {
return a.sla < b.sla;
}
int getMaxScore(vector<Task> tasks, int T) {
int maxScore = 0;
set<int> St[T];
for (auto task: tasks) {
St[task.sla-1].insert(task.score);
}
for(auto st :St)
{
if(!st.empty())
{
int max = *st.rbegin();
maxScore = maxScore + max;
}
}
return maxScore;
}
int main() {
int N, T;
cin >> N >> T;
vector<Task> tasks(N);
for (int i = 0; i < N; ++i) {
cin >> tasks[i].sla >> tasks[i].score;
}
cout << getMaxScore(tasks, T) << endl;
return 0;
}