本题测试样例
4
6 2 4 9
1 1 1 2
输出:
3
4
6 2 8 9 16
1 1 1 2 2
输出:
3
3
1 1 5
1 1 2
输出:
3
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* @program: 个人demo
* @description: 每次都是pass
* @author: Mr.Hu
* @create: 2019-03-29 19:07
*/
public class OJ {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {//注意while处理多个case
int n = in.nextInt();
long wuli = 0, cost = 0;
long[] array = new long[n];
List<monster> list1 = new ArrayList<>(); //花费为1的怪兽
List<monster> list2 = new ArrayList<>(); //花费为2的怪兽
wuli = in.nextLong();
for (int i = 1; i < n; i++) {
array[i] = in.nextLong();
}
cost = in.nextLong();
for (int i = 1; i < n; i++) {
int nextCost = in.nextInt();
if (wuli >= array[i]) { //打得过记录下
if (nextCost == 1) list1.add(new monster(array[i], nextCost));
else list2.add(new monster(array[i], nextCost));
} else {
if (list1.size()>0 &&((nextCost == 1 && list1.get(0).wu >= array[i]) || (nextCost == 2 && list1.get(0).wu + wuli >= array[i]))) { //买以前的怪兽1实惠
cost += 1;
wuli += list1.get(0).wu;
list1.remove(0);
if (nextCost==1) list1.add(new monster(array[i], nextCost)); //不买这个 我记录下
else list2.add(new monster(array[i], nextCost));
} else if (list2.size()>0 && nextCost == 2 && list2.get(0).wu > array[i]) { //买以前的2怪兽实惠
cost += list2.get(0).money;
wuli += list2.get(0).wu;
list2.remove(0);
list2.add(new monster(array[i], nextCost)); //不买这个 我记录下
}else if (list1.size()>1 && nextCost == 2 && list1.get(1).wu +list1.get(0).wu + wuli >= array[i]){ //买俩1
cost += 2;
wuli += (list1.get(0).wu+list1.get(1).wu);
list1.remove(0);
list1.remove(0);
if (nextCost==1) list1.add(new monster(array[i], nextCost)); //不买这个 我记录下
else list2.add(new monster(array[i], nextCost));
}
else { //买这个怪兽实惠
cost += nextCost;
wuli += array[i];
}
}
}
System.out.println(cost);
}
}
static class monster implements Comparable<monster> {
long wu;
float money;
public monster(long wu, int money) {
this.wu = wu;
this.money = money;
}
@Override
public int compareTo(monster o) { //按武力值排序
if (o.wu == this.wu) return 0;
else return o.wu > this.wu ? 1 : -1;
}
}
}