hdu 2850 Load Balancing (优先队列 + 贪心)

题目大意:

怎么分配n个任务到m个服务器上使得负载尽量平衡。

思路:

将任务从大到小排序,依次放入负载最小的那个服务器中。

因为是spj 的缘故,所以可以使用这个贪心。

比如数据

6 2

7 5 3 3 3 3

就会得到错误答案。

#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];
priority_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 (优先队列 + 贪心),码迷,mamicode.com

时间: 2024-10-01 21:49:50

hdu 2850 Load Balancing (优先队列 + 贪心)的相关文章

hdu 4544 湫湫系列故事——消灭兔子 优先队列+贪心

将兔子的血量从小到大排序,箭的威力也从小到大排序, 对于每只兔子将威力大于血量的箭加入队列,写个优先队列使得出来数位价钱最少.. #include<stdio.h> #include<queue> #include<algorithm> #include<iostream> #include<functional> using namespace std; const int maxn=100010; struct tt { int d; int

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

hdu 4825 Xor Sum(trie+贪心)

hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #define CLR(a,b) memset((a)

【贪心专题】HDU 1009 FatMouse&#39; Trade (贪心选取)

链接:click here~~ 题意:老鼠准备了M磅猫食,准备拿这些猫食跟猫交换自己喜欢的食物.有N个房间,每个房间里面都有食物.你可以得到J[i]单位的食物,但你需要付出F[i]单位的的猫食. 计算M磅猫食可以获得最多食物的重量. [解题思路]贪心算法,求最优解.将J[i]/F[i]的值从大到小排列,每次取最大的,局部最优,达到全局最优,从而获得最大值. 代码: // 贪心策略,优先选择投资最大的房间,每选择一次,交换次数依次减少,最后的次数用于价值最小的 //注意精度转化:1.0*(int

Load Balancing 折半枚举大法好啊

Load Balancing 给出每个学生的学分.   将学生按学分分成四组,使得sigma (sumi-n/4)最小.         算法:   折半枚举 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <

HDU 4952 Poor Mitsui(贪心)

HDU 4957 Poor Mitsui 题目链接 思路:利用相邻交换法去贪心即可,注意容积为0的情况,这是个坑点 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 45; struct SB { int a, b; } sb[N]; bool cmp(SB x, SB y) { return x.b * y.a < x.

hdu 3650 Hot Expo(贪心)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3650 --------------------------------------------------------------------------------------------------------------------------------------------

HDU 4803 Poor Warehouse Keeper(贪心)

http://acm.hdu.edu.cn/showproblem.php?pid=4803 贪心的策略是,每次尽量加价格,加到能满足条件的最大值,然后加一下数量,这样反复直到到达答案. 然后加到满足条件最大值一步不能模拟,可以推一下公式就能直接算出来了 代码: #include <stdio.h> #include <string.h> const double eps = 1e-9; double x, y; int main() { while (~scanf("%

How Node.js Multiprocess Load Balancing Works

As of version 0.6.0 of node, load multiple process load balancing is available for node. The concept of forking and child processes isn't new to me. Yet, it wasn't obvious to me how this was implemented at first. It's quite easy to use however: var c