hdu6000 wash (优先队列+贪心)

Wash

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 64000/64000 K (Java/Others)
Total Submission(s): 2045    Accepted Submission(s): 544


Problem Description
Mr.Panda is about to engage in his favourite activity doing laundry! He’s brought L indistinguishable loads of laundry to his local laundromat, which has N washing machines and M dryers.The  ith washing machine takes  Wi minutes to wash one load of laundry, and the  ith dryer takes Di minutes to dry a load of laundry.
At any point in time, each machine may only be processing at most one load of laundry.
As one might expect, Panda wants to wash and then dry each of his L loads of laundry. Each load of laundry will go through the following steps in order:
1. A non-negative amount of time after Panda arrives at the laundromat, Panda places the load in an unoccupied washing machine i.
2. Wi minutes later, he removes the load from the washing machine, placing it in a temporary holding basket (which has unlimited space)
3. A non-negative amount of time later, he places the load in an unoccupied dryer j 
4. Dj minutes later, he removes the load from the dryer Panda can instantaneously add laundry to or remove laundry from a machine. Help Panda minimize the amount of time (in minutes after he arrives at the laundromat) after which he can be done washing and drying all L loads of laundry!
 

Input
The first line of the input gives the number of test cases, T.
T test cases follow. Each test case consists of three lines. The first line contains three integer L, N, and M.
The second line contains N integers  W1,W2,...,WN representing the wash time of each wash machine.
The third line contains M integers  D1,D2,...,DM representing the dry time of each dryer.
 

Output
For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the minimum time it will take Panda to finish his laundry.

limits


1T100.
1L106.
1N,M105.
1Wi,Di109.
 

Sample Input
 
 
2 1 1 1 1200 34 2 3 2 100 10 1 10 10
 

Sample Output
 
 
Case #1: 1234 Case #2: 12
 

Source
 
题意:有l件衣服,n个洗衣机,m个烘干机,接下来会告诉你n个洗衣机的洗衣时间,m个烘干机的烘干时间,每个时间段只能处理一件衣服,问最小的时间洗完所有衣服。

思路:贪心的策略就是你先可以求出来洗完衣服的最早时间,那么我们拿最后洗好的衣服最早烘干。就是用最大值加最小值的方法来贪心,当然烘干要用最短时间那个烘干机。

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
struct node
{
    ll x,y;
    friend bool operator<(node a1,node b1)
    {
        return a1.x>b1.x;//小值优先级高
    }
}a;
priority_queue<node>q1,q2;
ll aa[1000005];
int main()
{
    int t;
    int casee=0;
    ll l,n,m;
    scanf("%d",&t);
    while(t--)
    {
        while(!q1.empty()) q1.pop();
        while(!q2.empty()) q2.pop();
        scanf("%lld%lld%lld",&l,&n,&m);
        for(int i=0;i<n;i++)//全放进优先队列自动排序
        {
            scanf("%lld",&a.x);
            a.y=a.x;
            q1.push(a);
        }
        for(int i=0;i<m;i++)
        {
            scanf("%lld",&a.x);
            a.y=a.x;
            q2.push(a);
        }
        for(int i=0;i<l;i++)
        {
            a=q1.top();
            q1.pop();
            aa[i]=a.x;//记录洗衣时间,求出来最晚洗完的那个
            a.x+=a.y;//因为每个时间段只能洗一件衣服,那么我们第一个洗衣机用过以后在用的时候要累加上次洗的时间
            q1.push(a);
        }
        ll ans=0;
        for(int i=l-1;i>=0;i--)
        {
            a=q2.top();
            q2.pop();
            ans=max(ans,a.x+aa[i]);
            a.x+=a.y;//烘干同洗衣一样
            q2.push(a);
        }
        printf("Case #%d: ",++casee);
        printf("%lld\n",ans);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值