Vì mình không có đủ thời gian để fix bug lại cho bạn nên bạn thông cảm
Code 1:
#include
#include
using namespace std;
struct Job {
int deadline, profit;
};
bool cmp(Job a, Job b) {
return a.profit > b.profit;
}
int main() {
int n;
cin >> n;
Job jobs[n];
for (int i = 0; i < n; i++) {
cin >> jobs[i].deadline >> jobs[i].profit;
}
sort(jobs, jobs+n, cmp);
bool slot[n] = {false};
int result[n];
for (int i = 0; i < n; i++) {
for (int j = min(n, jobs[i].deadline)-1; j >= 0; j--) {
if (slot[j] == false) {
result[j] = i;
slot[j] = true;
break;
}
}
}
int sum = 0;
for (int i = 0; i < n; i++) {
if (slot[i]) {
cout << result[i]+1 << endl;
sum += jobs[result[i]].profit;
}
}
cout << sum << endl;
return 0;
}
//Input:
4
1 15
3 10
5 100
1 27
//Output:
2
4
1
137
Code 2:
#include
#include
using namespace std;
struct Job {
int deadline, profit, id;
};
bool cmp(Job a, Job b) {
return a.profit > b.profit;
}
int main() {
int n;
cin >> n;
Job jobs[n];
for (int i = 0; i < n; i++) {
cin >> jobs[i].deadline >> jobs[i].profit;
jobs[i].id = i+1;
}
sort(jobs, jobs+n, cmp);
bool slot[n] = {false};
int result[n];
for (int i = 0; i < n; i++) {
for (int j = min(n, jobs[i].deadline)-1; j >= 0; j--) {
if (slot[j] == false) {
result[j] = i;
slot[j] = true;
break;
}
}
}
int sum = 0;
for (int i = 0; i < n; i++) {
if (slot[i]) {
cout << jobs[result[i]].id << endl;
sum += jobs[result[i]].profit;
}
}
cout << sum << endl;
return 0;
}
//input
4
1 15
3 10
5 100
1 27
//output
4
2
3
137