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

Load Balancing

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1129    Accepted Submission(s): 449
Special Judge


Problem Description
In the Wide Web World, Which web server was popular in web site? Apache, nginx, lighttpd? Baidu.com use Apache, and many web sites like 163.com use nginx. Why? Its configuration is very simple, and it has very powerful load balancing features.

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

He define the web site has M servers, and N jobs need be done. Each job has a processing time T; we seek to assign each job to 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 scheduling problem of finding an assignment of minimum gap is NP-hard. 

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

Input
The first integer C represents the number of 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 contains integers, meaning the time of job T1, T2Tn. (1<=Ti<=1000000)
 

Output
For each test case, first output the N, and follows N numbers mean the job i assign to which server. The servers number count 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 your assign plan (max {TMi} -min {TMj}) <= my answer + 1000, it be consider Accept.
从耗时最多的开始分配,每次分配给当前服务器任务最少的
#include<bits/stdc++.h>
int d[100003];
using namespace std;
struct node
{
    long long time;
    int loca;
}g[100003];
bool cmp1(node a,node b)
{
    return a.time>b.time;
}
struct node2
{
    long long time;
    int dec;
};
bool operator<(node2 a,node2 b)
{
    return a.time>b.time;
}
priority_queue<node2> P;
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        int n,m;scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            long long tt;scanf("%lld",&tt);
            g[i].loca=i,g[i].time=tt;
        }
        sort(g,g+n,cmp1);
        for(int i=0;i<m;i++)
        {
            node2 e;e.dec=i,e.time=0;
            P.push(e);
        }
        for(int i=0;i<n;i++)
        {
            node e;e=g[i];
            node2 t=P.top();P.pop();
            d[e.loca]=t.dec;
            t.time+=e.time;
            P.push(t);
        }
        while(!P.empty())P.pop();
        printf("%d\n",n);
        for(int i=0;i<n-1;i++)
            printf("%d ",d[i]);
        printf("%d\n",d[n-1]);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)加入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别加上a[i],然后再将加和的结果作为新的候选值加入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有加和结果作为新的候选值加入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值