HDU 2850 Load Balancing (priority queue + greedy)

Source: Internet
Author: User

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 &AMP;CMP) Const {return load>cmp.load;    }};struct foo{int index,v;    BOOL operator < (const foo &AMP;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", &AMP;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.