hdu2850 2010.3.6

hdu2850 2010.3.6

赤果果的贪心,被我们都想复杂了

看到很长恨长的题,它不一定是难题,有可能是最简单的题

 

hdu 2850

 

Load Balancing

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 394    Accepted Submission(s): 147

Special Judge

 

Problem Description

In the Wide Web World, Which web server waspopular in web site? Apache, nginx, lighttpd? Baidu.com use Apache, and manyweb sites like 163.com use nginx. Why? Its configuration is very simple, and ithas very powerful load balancing features.

 

How does load balancing work? Loadbalancing is a technique to spread work between two or more computers in orderto get optimal resource utilization, maximize throughput, and minimize responsetime. Wiskey very curious about this technology and he wants to build a simpleload balancing model.

 

He define the web site has M servers, and Njobs need be done. Each job has a processing time T; we seek to assign each jobto one of the servers so that the loads placed on all servers are as"balanced" as possible. The "balanced" mean the Minimize it(max {TMi} - min {TMj}). TMi is the server i total time cost.

 

But, as Wiskey Know, this schedulingproblem of finding an assignment of minimum gap is NP-hard.

 

Can you write a better load balancing thanWiskey? Challenge it~!

 

 

 

Input

The first integer C represents the numberof cases, And C cases followed.

 

Each test case contains a single integer N(1<=N<=100000) and M (1<=M<=100). The next N line containsintegers, meaning the time of job T1, T2Tn. (1<=Ti<=1000000)

 

 

 

Output

For each test case, first output the N, andfollows N numbers mean the job i assign to which server. The servers numbercount from 0.

 

 

Sample Input

2

6 3

2 3 4 6 2 2

6 3

6 4 3 2 2 2

 

 

Sample Output

6

0 1 2 0 1 2

6

0 1 1 2 2 2

Hint

Hint

This problem is special judge, if yourassign plan (max {TMi} -min {TMj}) <= my answer + 1000, it be considerAccept.

 

 

 

 

Source

2009 Multi-University Training Contest 4 -Host by HDU

 

 

Recommend

gaojie

 

 

1.暴搜

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

#define MAXN 100000+10
#define MAXM 100+10

struct node
{
	int key,value;
}f[MAXN];

int n,m,c,ans[MAXN];
__int64 num[MAXM];

bool cmp(struct node a,struct node b)
{
	return (a.value>b.value);
}

int main()
{
	int k,i,t,j;
	scanf("%d",&c);
	for(k=1;k<=c;k++)
	{
		scanf("%d %d",&n,&m);
		memset(ans,0,sizeof(ans));
		memset(num,0,sizeof(num));
		for(i=1;i<=n;i++)
		{
			scanf("%d",&f[i].value);
			f[i].key=i;
		}
		sort(f+1,f+n+1,cmp);
		for(i=1;i<=n;i++)
		{
			t=0;
			for(j=1;j<m;j++)
				if(num[j]<num[t]) t=j;
			ans[f[i].key]=t;
			num[t]+=f[i].value;
		}
		printf("%d\n",n);
		for(i=1;i<n;i++)
			printf("%d ",ans[i]);
		printf("%d\n",ans[n]);
	}
	return 0;
}

2.优先队列

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <functional>

using namespace std;

#define MAXN 100000+10
#define MAXM 100+10

struct node
{
    friend bool operator< (node n1, node n2)
    {
        return n1.val > n2.val;
    }
    int num;
    __int64 val;
};

struct node2
{
	int  value,key;
}f[MAXN];

struct node mid;

int n,m,c,ans[MAXN];

bool cmp(struct node2 a,struct node2 b)
{
	return (a.value>b.value);
}

int main()
{
	int k,i;
	scanf("%d",&c);
	for(k=1;k<=c;k++)
	{
		scanf("%d %d",&n,&m);
		memset(ans,0,sizeof(ans));
		for(i=1;i<=n;i++)
		{
			scanf("%d",&f[i].value);
			f[i].key=i;
		}
		sort(f+1,f+n+1,cmp);
		priority_queue<node> q;
		for(i=0;i<m;i++)
		{
			mid.val=0;
			mid.num=i;
			q.push(mid);
		}
		for(i=1;i<=n;i++)
		{
			mid=q.top();
			q.pop();
			mid.val=f[i].value+mid.val;
			q.push(mid);
			ans[f[i].key]=mid.num;
		}
		printf("%d\n",n);
		for(i=1;i<n;i++)
			printf("%d ",ans[i]);
		printf("%d\n",ans[n]);
	}
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值