HDOJ 2850 Load Balancing

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2850

贪心题,唯一不明白的是为何我sort排序就AC, qsort排序就WA了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <cstdlib>
#include <algorithm>
using namespace std;
 
int nCase, M, N;
 
typedef struct TT{
    int num;
    int id;
}TT;
 
/*
int cmp(const void *arg1, const void *arg2)
{
    return ((TT*)arg1)->num - ((TT*)arg2)->num;
}
*/
 
bool cmp(TT arg1, TT arg2)
{
    return arg1.num > arg2.num;
}
 
TT work[1000005];
int out[1000005];
int server[105];
 
 
int main()
{
    //freopen("input.txt", "r", stdin);
    cin >> nCase;
    while(nCase--)
    {
        cin >> N >> M;
        memset(out, 0, N);
        memset(server, 0, M);
        for(int i=0; i<N; ++i)
        {
            cin >> work[i].num;
            work[i].id = i;
        }
        //qsort(work, N, sizeof(TT), cmp);
        sort(work, work+N, cmp);
 
        for(int i=0; i<N; ++i)
        {
            if(i< M)
            {
                server[i] = work[i].num;
                out[work[i].id] = i;
            }
            else
            {
                int t = -1;
                for(int j=0; j<M; ++j)
                    if(t==-1 || server[t]>server[j])
                        t = j;
                out[work[i].id] = t;
                server[t] += work[i].num;
            }
        }
        cout << N << endl;
        for(int i=0; i<N-1; ++i)
            printf("%d ", out[i]);
        printf("%d\n", out[N-1]);
    }
    return 0;
}

发布者

Tanky Woo

Tanky Woo,[个人主页:https://tankywoo.com] / [新博客:https://blog.tankywoo.com]

《HDOJ 2850 Load Balancing》有708个想法

发表评论

电子邮件地址不会被公开。 必填项已用*标注