Blog·Tanky WooABOUTRSS

HDOJ 2850 Load Balancing

12 Jan 2011
这篇博客是从旧博客 WordPress 迁移过来,内容可能存在转换异常。

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

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

#include 
#include 
#include 
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> work[i].num;
            work[i].id = i;
        }
        //qsort(work, N, sizeof(TT), cmp);
        sort(work, work+N, cmp);

        for(int i=0; iserver[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;
}