多机调度问题贪心算法c++代码
时间: 2023-11-19 15:56:41 浏览: 106
多机调度问题 -贪心法——C++代码
以下是多机调度问题的贪心算法C++代码:
```c++
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
const int MAXN = 1005;
struct Job {
int id, time;
bool operator < (const Job &rhs) const {
return time > rhs.time;
}
} job[MAXN];
int n, m, ans, sum[MAXN];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &job[i].time);
job[i].id = i;
}
sort(job + 1, job + n + 1);
for (int i = 1; i <= n; i++) {
int p = 1;
for (int j = 2; j <= m; j++) {
if (sum[j] < sum[p]) {
p = j;
}
}
sum[p] += job[i].time;
ans = max(ans, sum[p]);
}
printf("%d\n", ans);
return 0;
}
```
阅读全文