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

本文解析了一道经典的贪心算法题HDU 2850,介绍了使用C++实现的优先队列来解决任务分配问题的方法,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

这尼玛是一个错误的贪心

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

转载于:https://www.cnblogs.com/xiao-xin/articles/4167695.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值