服装店新进了a条领带,b条裤子,c个帽子,d件衬衫,现在要把这些搭配起来售卖。有三种搭配方式,一条领带和一件衬衫,一条裤子和一件衬衫,一个帽子和一件衬衫。卖出一套领带加衬衫可以得到e元,卖出一套裤子加衬衫可以得到f元,卖出一套帽子加衬衫可以得到g元。现在你需要输出最大的获利方式
服装店新进了a条领带,b条裤子,c个帽子,d件衬衫,现在要把这些搭配起来售卖。有三种搭配方式,一条领带和一件衬衫,一条裤子和一件衬衫,一个帽子和一件衬衫。卖出一套领带加衬衫可以得到e元,卖出一套裤子加衬衫可以得到f元,卖出一套帽子加衬衫可以得到g元。现在你需要输出最大的获利方式
一行7个整数分别表示a,b,c,d,e,f,g。
最大获利。
2 3 4 5 6 7 8
39
4个帽子加4件衬衫获利32元,1条裤子加1件衬衫获利7元,一共得到39元。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Comparator;
import java.util.PriorityQueue;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] params = br.readLine().trim().split(" ");
int a = Integer.parseInt(params[0]);
int b = Integer.parseInt(params[1]);
int c = Integer.parseInt(params[2]);
int d = Integer.parseInt(params[3]);
int e = Integer.parseInt(params[4]);
int f = Integer.parseInt(params[5]);
int g = Integer.parseInt(params[6]);
// 将三种搭配放入一个大根堆中,按照三种搭配的获利对搭配进行降序排列
PriorityQueue<int[]> maxHeap = new PriorityQueue<>(new Comparator<int[]>(){
@Override
public int compare(int[] o1, int[] o2) {
return o2[1] - o1[1];
}
});
maxHeap.offer(new int[]{a, e});
maxHeap.offer(new int[]{b, f});
maxHeap.offer(new int[]{c, g});
// 把衬衫分配给领带、裤子和帽子中赚钱多的
long money = 0;
while(!maxHeap.isEmpty() && d > 0){
int[] temp = maxHeap.poll();
money += (long)Math.min(temp[0], d) * temp[1];
d -= temp[0];
}
System.out.println(money);
}
} import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = sc.nextInt();
int e = sc.nextInt();
int f = sc.nextInt();
int g = sc.nextInt();
Queue<Mapping> mappings = new PriorityQueue<>(3);
mappings.offer(new Mapping(a,e));
mappings.offer(new Mapping(b,f));
mappings.offer(new Mapping(c,g));
int count = d;
long max = 0;
while(count > 0 && !mappings.isEmpty()){
Mapping mapping = mappings.poll();
if(count >= mapping.count){
max += mapping.count * mapping.cost;
count -= mapping.count;
}else {
max += count * mapping.cost;
break;
}
}
System.out.println(max);
}
private static class Mapping implements Comparable<Mapping>{
long cost;
int count;
public Mapping(int count, long cost) {
this.cost = cost;
this.count = count;
}
@Override
public int compareTo(Mapping o) {
return (int)(o.cost - this.cost);
}
}
} 贪心:在有限商品量的范围内,使用贪心选择最大价值的组合
a, b, c, d, e, f, g = map(int, input().split())
price = 0
for p, t in sorted([(e, 1), (f, 2), (g, 3)], reverse=True):
if 1 == t: # tie+shirt
n = min(a, d)
price += n * p
a, d = a - n, d - n # 剩余数量
# print('tie+shirt', n, n * p)
elif 2 == t: # trousers+shirt
n = min(b, d)
price += n * p
b, d = b - n, d - n # 剩余数量
# print('trousers+shirt', n, n * p)
elif 3 == t: # cap+shirt
n = min(c, d)
price += n * p
c, d = c - n, d - n # 剩余数量
# print('cap+shirt', n, n * p)
print(price)代码还可以简化,因为每个组合都是和衬衫,那么构建(price, type)组合就可以。
a, b, c, d, e, f, g = map(int, input().split())
price = 0
for p, t in sorted([(e, a), (f, b), (g, c)], reverse=True):
n = min(t, d)
price += n * p
d -= n # shirt剩余数量
print(price)
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<unordered_map>
using namespace std;
int main(){
int a,b,c,d,e,f,g;
cin>>a;
cin>>b;
cin>>c;
cin>>d;
cin>>e;
cin>>f;
cin>>g;
vector<int> price={e,f,g};
sort(price.begin(),price.end());
unordered_map<int,int> map={{e,a},{f,b},{g,c}};
//贪心策略,优先选择最值钱的搭配卖
long long profit=0;
int min_p=price[0];
int meadian_p=price[1];
int max_p=price[2];
while(d && map[max_p]){
profit+=max_p;
map[max_p]--;
d--;
}
while(d && map[meadian_p]){
profit+=meadian_p;
map[meadian_p]--;
d--;
}
while(d && map[min_p]){
profit+=min_p;
map[min_p]--;
d--;
}
cout<<profit;
} s = list(map(int, input().split())) a, b, c, d, e, f, g = s # a, b, c, d, e, f, g = 2,3,4,5,6,7,8 info = [(a,e),(b,f),(c,g)] info = sorted(info, key=lambda x: x[1], reverse=True) benefit = 0 remaining = d for i in range(3): if remaining==0: break num = min(info[i][0],remaining) benefit += num*info[i][1] remaining -= num print (benefit)
import java.util.*;
public class Main {
public static long cal(long d, long a, long e, long b, long f, long c, long g) {
long cnt = 0;
long minAd = Math.min(a, d);
cnt += (minAd * e);
a -= minAd;
d -= minAd;
long minBd = Math.min(b, d);
cnt += (minBd * f);
b -= minBd;
d -= minBd;
long minCd = Math.min(c, d);
cnt += (minCd * g);
c -= minCd;
d -= minCd;
return cnt;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = sc.nextInt();
int e = sc.nextInt();
int f = sc.nextInt();
int g = sc.nextInt();
long[] arr = new long[6];
arr[0] = cal(d, a, e, b, f, c, g);
arr[1] = cal(d, a, e, c, g, b, f);
arr[2] = cal(d, b, f, a, e, c, g);
arr[3] = cal(d, b, f, c, g, a, e);
arr[4] = cal(d, c, g, a, e, b, f);
arr[5] = cal(d, c, g, b, f, a, e);
Arrays.sort(arr);
System.out.println(arr[5]);
}
}
import java.util.Scanner;
import java.util.Vector;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long a, b, c, d, e, f, g;
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
d = sc.nextInt();
e = sc.nextInt();
f = sc.nextInt();
g = sc.nextInt();
long sum = 0;
if (d == 0) {
System.out.println(0);
} else {
long tem = d;
while (tem > 0) {
if (g == f && f == e || (g==0 && f==e) || (f==0 && g==e) || (e==0 && g==f)) {
if (a > b && a > c) {
if (a >= tem) {
a = tem;
sum += a * g;
tem = 0;
a=0;
} else {
sum += a * g;
tem -= a;
a=0;
}
} else if (b > a && b > c) {
if (b >= tem) {
b = tem;
sum += b * g;
tem = 0;
b=0;
} else {
sum += b * g;
tem -= b;
b=0;
}
} else if (c > a && c > b) {
if (c >= tem) {
c = tem;
sum += c * g;
tem = 0;
c=0;
} else {
sum += c * g;
tem -= c;
c=0;
}
}
} else if (g > f && g > e) {
if (c >= tem) {
c = tem;
sum += c * g;
tem = 0;
c=0;
g = 0;
} else {
sum += c * g;
tem -= c;
c=0;
g=0;
}
} else if (f > g && f > e) {
if (b >= tem) {
b = tem;
sum += b * f;
tem = 0;
b=0;
f = 0;
} else {
sum += b * f;
tem -= b;
b=0;
f=0;
}
} else if (e > g && e > f) {
if (a >= tem) {
a = tem;
sum += a * e;
tem = 0;
a=0;
e = 0;
} else {
sum += a * e;
tem -= a;
a=0;
e=0;
}
}
if(a==0 && b==0 && c==0){
break;
}
}
System.out.println(sum);
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int[][] arr = new int[3][2];
arr[0][0] = scanner.nextInt();
arr[1][0] = scanner.nextInt();
arr[2][0] = scanner.nextInt();
int d = scanner.nextInt();
arr[0][1] = scanner.nextInt();
arr[1][1] = scanner.nextInt();
arr[2][1] = scanner.nextInt();
long res = 0;
while (d>0){
int i = find(arr);
if (i!=-1){
if (arr[i][0]>d){
res += (long)arr[i][1]*d;
d=0;
}else {
res += (long)arr[i][1]*arr[i][0];
d = d-arr[i][0];
arr[i][0]=0;
}
}else {
break;
}
}
System.out.println(res);
}
private static int find(int[][] arr) {
int res = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[res][0]==0){
res = i;
}else if (arr[i][0]!=0&&arr[i][1]>=arr[res][1]){
res = i;
}
}
return arr[res][0]==0?-1:res;
}
} l=[int(i) for i in input().split()] ll=[] for i in range(3): ll.append([l[i],l[i+4]]) ll.append([l[3],0]) ll.sort(key=lambda x: x[1],reverse=True) sum=0 p=0 for i in range(3): if ll[i][0]>ll[3][0]: sum+=ll[3][0]*ll[i][1] print(sum) p=1 break else: sum+=ll[i][0]*ll[i][1] ll[3][0]-=ll[i][0] if p==0: print(sum)