50 做项目的最大收益问题
ac
4 3 2
5 4 1 2
3 5 3 2
11
初始资金为3,最多做两个项目,每个项目的启动资金与利润见costs和profits。最优选择为:先做2号项目,做完之后资金增长到6,。然后做1号项目,做完之后资金增长到11。其他的任何选择都不会比这种选择好,所以返回11
import java.util.PriorityQueue;
import java.util.Scanner;
class Pro {
public int cost;
public int profit;
}
public class Main {
public static Scanner sc = new Scanner(System.in);
static int mod = 1_000_000_000 + 7;
/**
*
* @param args
*/
public static void main(String[] args) {
int n = sc.nextInt();
long w = sc.nextLong();
int k = sc.nextInt();
Pro[] pros = new Pro[n];
for(int i=0;i<n;i++){
pros[i] = new Pro();
int cost = sc.nextInt();
pros[i].cost = cost;
}
for(int i=0;i<n;i++){
int profit = sc.nextInt();
pros[i].profit = profit;
}
// profit 按从大到小排列
PriorityQueue<Pro> priorityQueueProfit = new PriorityQueue<>((a,b)->{
if(a.profit > b.profit){
return -1;
}else if(a.profit < b.profit){
return 1;
}
return 0;
});
// cost 小的按顺序拍起来
PriorityQueue<Pro> priorityQueueCost = new PriorityQueue<>((a,b)->{
if(a.cost < b.cost){
return -1;
}else if(a.cost > b.cost){
return 1;
}
return 0;
});
for(int i=0;i<n;i++){
if(pros[i].cost <= w){
priorityQueueProfit.add(pros[i]);
}else{
priorityQueueCost.add(pros[i]);
}
}
while (k > 0){
while (! priorityQueueCost.isEmpty() && priorityQueueCost.peek().cost <= w){
Pro pro = priorityQueueCost.poll();
priorityQueueProfit.add(pro);
}
if(priorityQueueProfit.isEmpty()){
break;
}
Pro pro = priorityQueueProfit.poll();
w += pro.profit;
k--;
}
System.out.println(w);
}
}
/*
4 3 2
5 4 1 2
3 5 3 2
11
*/
import java.util.PriorityQueue;
import java.util.Scanner;
class Pro {
public long cost;
public long profit;
}
public class Main {
public static Scanner sc = new Scanner(System.in);
static int mod = 1_000_000_000 + 7;
static int N = 100_000;
static int[] cost = new int[N];
static int[] profit = new int[N];
/**
*
* @param args
*/
public static void main(String[] args) {
int n = sc.nextInt();
long w = sc.nextLong();
int k = sc.nextInt();
for(int i=0;i<n;i++){
cost[i] = sc.nextInt();
}
for(int i=0;i<n;i++){
profit[i] = sc.nextInt();
}
// profit 按从大到小排列
PriorityQueue<Integer> priorityQueueProfit = new PriorityQueue<>((a,b)->{
if(profit[a] > profit[b]){
return -1;
}else if(profit[a] < profit[b]){
return 1;
}
return 0;
});
// cost 小的按顺序拍起来
PriorityQueue<Integer> priorityQueueCost = new PriorityQueue<>((a,b)->{
if(cost[a] < cost[b]){
return -1;
}else if(cost[a] > cost[b]){
return 1;
}
return 0;
});
for(int i=0;i<n;i++){
if(cost[i] <= w){
priorityQueueProfit.add(i);
}else{
priorityQueueCost.add(i);
}
}
while (k != 0){
while (! priorityQueueCost.isEmpty() && cost[priorityQueueCost.peek()] <= w){
int index = priorityQueueCost.poll();
priorityQueueProfit.add(index);
}
if(priorityQueueProfit.isEmpty()){
break;
}
w += profit[priorityQueueProfit.poll()];
k--;
}
System.out.println(w);
}
}
/*
4 3 2
5 4 1 2
3 5 3 2
11
*/