Main topic:
How to allocate n tasks to m servers makes the load as balanced as possible.
Ideas:
Sort tasks from large to small, and then into the server with the least load.
Because it is SPJ's sake, so can use this greedy.
For example, data
6 2
7 5 3 3 3 3
You will get the wrong answer.
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include < queue>using namespace std;struct node{int index,load; BOOL operator < (const node &CMP) Const {return load>cmp.load; }};struct foo{int index,v; BOOL operator < (const foo &CMP) Const {return v<cmp.v; }}SAVE[100005];p riority_queue <node> q;int n,m;int ans[100005];int main () {int T; scanf ("%d", &t); while (t--) {memset (ans,0,sizeof ans); while (! Q.empty ()) Q.pop (); scanf ("%d%d", &n,&m); for (int i=1;i<=n;i++) {save[i].index=i; scanf ("%d", &SAVE[I].V); } sort (save+1,save+1+n); for (int i=0;i<m;i++) {node T; T.index=i; T.load=0; Q.push (t); } for (int i=n;i>=1;i--) {node t=q.top (); Q.pop (); T.load+=save[I].V; printf ("---%d\n", t.load); Ans[save[i].index]=t.index; Q.push (t); } printf ("%d\n", N); for (int i=1;i<=n;i++) {printf ("%d%c", ans[i],i==n? ') \ n ': '); }} return 0;}
HDU 2850 Load Balancing (priority queue + greedy)