贪心
这道题C++可以过,JAVA超时了。
思路是贪心,洗的取最快的,再把这个最快的取甩干最慢的。这样最后一件出来的就是最快的。
只不过因为洗衣机不是用完就扔了,所以要使用优先队列进行存储。
洗衣机分basetime和nowtime就解决了重复使用的问题。
计划的优化方法是:
等等补充
/**
*
*/
/**
* @author Enron
*
*/
package _6000;
//超时
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
class Node implements Comparable<Node>{
long now,base;
Node(long a,long b){
now = a;base=b;
}
@Override
public int compareTo(Node o) {
return (int)(this.now - o.now);
}
}
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
int t=0;
while(++t <=T){
int l = sc.nextInt();
int n = sc.nextInt();
int m = sc.nextInt();
long res[] = new long[l];
Queue<Node> wash = new PriorityQueue<Node>();
Queue<Node> dry = new PriorityQueue<Node>();
for (int i = 0; i < n; i++) {
long w = sc.nextLong();
wash.add(new Node(w,w));
}
for (int i = 0; i < m; i++) {
long d = sc.nextLong();
dry.add(new Node(d,d));
}
long ans = 0;
for (int i = 0; i < l; i++) {
Node temp = wash.poll();
res[i] = temp.now;
temp.now += temp.base;
wash.add(temp);
}
for (int i = l-1; i >= 0; i--) {
Node temp = dry.poll();
long cmp = res[i]+temp.now;
ans = Math.max(ans, cmp);
temp.now += temp.base;
dry.add(temp);
}
System.out.println("Case #"+t+": "+ans);
}
}
}
Wash
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 64000/64000 K (Java/Others)Total Submission(s): 1511 Accepted Submission(s): 412
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!
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.
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.
∙1≤T≤100 .
∙1≤L≤106 .
∙1≤N,M≤105 .
∙1≤Wi,Di≤109 .
limits
∙1≤T≤100 .
∙1≤L≤106 .
∙1≤N,M≤105 .
∙1≤Wi,Di≤109 .
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
Recommend
jiangzijing2015