B - Load Balancing
原题连接:i am an Acmer
special judge的题目,在这里介绍一种贪心的做法。先按Ti从大到小排序,然后从大到小循环,每次找最空闲的机器(在这台机器在执行的任务的总时间最小)放进去。这样得到的结果还是比较优的(找反例也不是那么容易的说)
1 #include <cstdio> 2 #include <algorithm> 3 #include <iostream> 4 #include <cstring> 5 using namespace std; 6 7 const int MAXN = 100010; 8 9 struct Node { 10 int t, id; 11 bool operator < (const Node &rhs) const { 12 return t > rhs.t; 13 } 14 }; 15 16 Node a[MAXN]; 17 int pos[MAXN], mach[110]; 18 int n, m; 19 20 int main() { 21 int T; 22 scanf("%d", &T); 23 while(T--) { 24 scanf("%d%d", &n, &m); 25 for(int i = 0; i < n; ++i) scanf("%d", &a[i].t), a[i].id = i; 26 sort(a, a + n); 27 memset(mach, 0, sizeof(mach)); 28 for(int i = 0; i < n; ++i) { 29 int x = 0; 30 for(int j = 0; j < m; ++j) 31 if(mach[j] < mach[x]) x = j; 32 mach[x] += a[i].t; 33 pos[a[i].id] = x; 34 } 35 printf("%d\n%d", n, pos[0]); 36 for(int i = 1; i < n; ++i) printf(" %d", pos[i]); 37 puts(""); 38 } 39 }