hdu2850(2009多校第四场) n个数放在m个集合使(最大-最小)最小 优先队列贪心

从大到小排序,每次选择用时最少的集合==

这尼玛是一个错误的贪心

6 2

7000 5000 3000 3000 3000 3000

就跪了gg

不过网上也没有正解,就这样水过去吧2333

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<queue>
 4 #include<algorithm>
 5 using namespace std;
 6 int n,m,ans[100005];
 7 struct dian{
 8   friend bool operator<(dian n1,dian n2)
 9   {
10     return n1.time>n2.time;
11   }
12   int num,time;
13 };
14 struct A{
15   int id,time;
16 }a[100005];
17 int cmp(A n1,A n2)
18 {
19   return n1.time>n2.time;
20 }
21 void bfs()
22 {
23   priority_queue<dian>q;
24   dian n1;
25   while (!q.empty()) q.pop();
26   for (int i=0;i<m;i++)
27   {
28     n1.num=i;
29     n1.time=0;
30     q.push(n1);
31   }
32   for (int i=1;i<=n;i++)
33   {
34     n1=q.top(); q.pop();
35     ans[a[i].id]=n1.num;
36     n1.time+=a[i].time;
37     q.push(n1);
38   }
39   return;
40 }
41 int main()
42 {
43   int T,i;
44   scanf("%d",&T);
45   while (T--)
46   {
47     scanf("%d%d",&n,&m);
48     for (i=1;i<=n;i++)
49     {
50       scanf("%d",&a[i].time);
51       a[i].id=i;
52     }
53     sort(a+1,a+n+1,cmp);
54     bfs();
55     printf("%d\n",n);
56     for (i=1;i<n;i++)
57       printf("%d ",ans[i]);
58     printf("%d\n",ans[n]);
59   }
60   return 0;
61 }
View Code

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2850

posted on 2014-12-16 17:46  xiao_xin  阅读(134)  评论(0编辑  收藏  举报

导航