ac:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static Comparator2 com = new Comparator2();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] in = br.readLine().split(" ");
int kinds = Integer.parseInt(in[0]);
int demand = Integer.parseInt(in[1]);
String[] w = br.readLine().split(" "), p = br.readLine().split(" ");
double result = 0;
List<Mooncake> total_mooncake = new ArrayList<>();
for (int i = 0; i < kinds; i++) {
double weight = Double.parseDouble(w[i]);
double price = Double.parseDouble(p[i]);
double up = price / weight;
total_mooncake.add(new Mooncake(weight,price,up));
}
Collections.sort(total_mooncake, com);
double temp = demand;
while (temp > 0) {
if (total_mooncake.size() == 0) {
break;
}
Mooncake cur = total_mooncake.remove(0);
if (temp - cur.weight < 0) {
result += temp * cur.unit_price;
temp = 0;
} else {
result += cur.total_price;
temp -= cur.weight;
}
}
System.out.println(String.format("%.2f", result));
}
private static class Mooncake {
double weight;
double total_price;
double unit_price;
Mooncake(double weight, double total_price, double unit_price) {
this.weight = weight;
this.total_price = total_price;
this.unit_price = unit_price;
}
}
private static class Comparator2 implements Comparator<Mooncake> {
@Override
public int compare(Mooncake m1, Mooncake m2) {
if (m1.unit_price < m2.unit_price) return 1;
else return -1;
}
}
}
超时:
import java.util.*;
public class Main {
static Comparator2 com = new Comparator2();
public static void main(String[] args) {
double result = 0;
Scanner sc = new Scanner(System.in);
int kinds = sc.nextInt();
double demand = sc.nextDouble();
sc.nextLine();
List<Mooncake> total_mooncake = new ArrayList<>();
String[] w = sc.nextLine().split(" ");
String[] p = sc.nextLine().split(" ");
for (int i = 0; i < kinds; i++) {
double weight = Double.parseDouble(w[i]);
double price = Double.parseDouble(p[i]);
double up = price / weight;
total_mooncake.add(new Mooncake(weight,price,up));
}
Collections.sort(total_mooncake, com);
double temp = demand;
while (temp > 0) {
if (total_mooncake.size() == 0) {
break;
}
Mooncake cur = total_mooncake.remove(0);
if (temp - cur.weight < 0) {
result += temp * cur.unit_price;
temp = 0;
} else {
result += cur.total_price;
temp -= cur.weight;
}
}
System.out.println(String.format("%.2f", result));
}
private static class Mooncake {
double weight;
double total_price;
double unit_price;
Mooncake(double weight, double total_price, double unit_price) {
this.weight = weight;
this.total_price = total_price;
this.unit_price = unit_price;
}
}
private static class Comparator2 implements Comparator<Mooncake> {
@Override
public int compare(Mooncake m1, Mooncake m2) {
if (m1.unit_price < m2.unit_price) return 1;
else return -1;
}
}
}