题目大意:

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

思路:

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

由于是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 (优先队列 + 贪心)的更多相关文章

  1. Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心

    C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...

  2. 【架构】How To Use HAProxy to Set Up MySQL Load Balancing

    How To Use HAProxy to Set Up MySQL Load Balancing Dec  2, 2013 MySQL, Scaling, Server Optimization U ...

  3. CF# Educational Codeforces Round 3 C. Load Balancing

    C. Load Balancing time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  4. UVA 12904 Load Balancing 暴力

    Load Balancing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/vi ...

  5. Load Balancing 折半枚举大法好啊

    Load Balancing 给出每个学生的学分.   将学生按学分分成四组,使得sigma (sumi-n/4)最小.         算法:   折半枚举 #include <iostrea ...

  6. [zz] pgpool-II load balancing from FAQ

    It seems my pgpool-II does not do load balancing. Why? First of all, pgpool-II' load balancing is &q ...

  7. How Network Load Balancing Technology Works--reference

    http://technet.microsoft.com/en-us/library/cc756878(v=ws.10).aspx In this section Network Load Balan ...

  8. Network Load Balancing Technical Overview--reference

    http://technet.microsoft.com/en-us/library/bb742455.aspx Abstract Network Load Balancing, a clusteri ...

  9. 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 ...

随机推荐

  1. js题

    function newStr(){ return "hi";}function turn(str){ str.toString = newStr;}var str1 = &quo ...

  2. mysl lock table read

    <pre name="code" class="html">Session 1: mysql> use zjzc; Reading table ...

  3. redis.conf配置详解

    http://www.2cto.com/database/201307/225113.html

  4. MSSQL 日期操作函数 总结

    set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go ALTER FUNCTION [dbo].[ufn_getDateOfWeek] (@Date Dateti ...

  5. iterm快捷键及操作技巧(附Linux快捷键)

    标签操作 新建标签:command + t 关闭标签:command + w 切换标签:command + 数字 command + 左右方向键 切换全屏:command + enter 查找:com ...

  6. aix6.1 openssh安装

    环境: IBM AIX6.1 1.下载(可以直接从附件中下载): openssl IBM官方网站下载:https://www14.software.ibm.com/webapp/iwm/web/reg ...

  7. 在centos中添加开机自启动服务

    将服务的shell脚本添加到/etc/rc.d的rc.local文件的最后面,需要在服务名称的前面加上其路径. 例如我要将httpd添加到开机自启动中,需要在rc.local添加如下代码 /usr/s ...

  8. Pods 更新后提示Bundle资源找不到

    http://www.oschina.net/question/101347_2159145

  9. java中文乱码解决之道(四)—–java编码转换过程

    原文出处:http://cmsblogs.com/?p=1475 前面三篇博客侧重介绍字符.编码问题,通过这三篇博客各位博友对各种字符编码有了一个初步的了解,要了解java的中文问题这是必须要了解的. ...

  10. exit函数的妙用

    写了一个程序,用来推断一个文件是否存在: #include<stdio.h> main() {  FILE *fp;  fp = fopen ("/home/wang/my/ct ...