讨论/《美团 2021 届秋季校园招聘笔试真题》 - 搭配出售/
《美团 2021 届秋季校园招聘笔试真题》 - 搭配出售
共 4 个回复
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int a, b, c, d, e, f, g;
    cin >> a >> b >> c >> d >> e >> f >> g;
    
    vector<PII> v;
    v.push_back({e, a});
    v.push_back({f, b});
    v.push_back({g, c});
    sort(v.begin(), v.end());

    LL res = 0;
    for (int i = 2; i >= 0; i--) {
        int value = v[i].first, cnt = min(v[i].second, d);
        res += (LL)value * cnt;
        d -= cnt;
    }
    cout << res << endl;

    return 0;
}
import java.io.*;
import java.util.*;

class Solution {
    public static void main(String[] args) throws IOException {
        var reader = new BufferedReader(new InputStreamReader(System.in));
        var writer = new BufferedWriter(new OutputStreamWriter(System.out));

        var strings = reader.readLine().split(" ");
        int a = Integer.parseInt(strings[0]);
        int b = Integer.parseInt(strings[1]);
        int c = Integer.parseInt(strings[2]);
        int d = Integer.parseInt(strings[3]);
        int e = Integer.parseInt(strings[4]);
        int f = Integer.parseInt(strings[5]);
        int g = Integer.parseInt(strings[6]);

        List<int[]> v = new ArrayList<>();
        v.add(new int[]{e, a});
        v.add(new int[]{f, b});
        v.add(new int[]{g, c});
        v.sort(Comparator.comparingInt(o -> o[0]));

        long res = 0;
        for (int i = 2; i >= 0; i--) {
            int value = v.get(i)[0], cnt = Math.min(v.get(i)[1], d);
            res += (long) value * cnt;
            d -= cnt;
        }
        writer.write(res + "\n");

        writer.flush();
        reader.close();
        writer.close();
    }
}
1

直接可行的办法:Arrays.sort(sell,(s1,s2)->s2[2]-s1[2]);

在牛客上可以跑通 leetcode上Comparator无法识别

import java.util.*;
import java.lang.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        int[] arr = new int[4];
        arr[0] = sc.nextInt();
        arr[1] = sc.nextInt();
        arr[2] = sc.nextInt();
        arr[3] = sc.nextInt();

        Integer[][] sell = new Integer[3][3];
        sell[0] = new Integer[]{0,3,sc.nextInt()};
        sell[1] = new Integer[]{1,3,sc.nextInt()};
        sell[2] = new Integer[]{2,3,sc.nextInt()};

        Comparator<Integer[]> cmp = new Comparator<Integer[]>(){
            public int compare(Integer[] a,Integer[] b){
                return b[2].compareTo(a[2]);
            }
        };
        Arrays.sort(sell,cmp);

        long ans = 0;

        for(int i = 0;i < 3;i++){
            Integer[] se = sell[i];
            int a = se[0], b = se[1],val = se[2];
            
            long count = Math.min(arr[a],arr[b]);

            if(count > 0){
                ans += count * val;
                arr[a] -= count;
                arr[b] -= count;
            }
            
        }

        System.out.println(ans);
    }

}
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int[] num = new int[7];
        for (int i = 0; i < 7; i++) {
            num[i] = sc.nextInt();
        }

        PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> o2[0] - o1[0]);

        pq.offer(new int[]{num[4], 4});
        pq.offer(new int[]{num[5], 5});
        pq.offer(new int[]{num[6], 6});

        int ans = 0;
        while (num[3] > 0 && !pq.isEmpty()) {
            int[] a = pq.poll();
            ans += Math.min(num[3], num[a[1] - 4]) * a[0];
            num[3] -= num[a[1] - 4];
        }
        System.out.println(ans);

    }

到底有哪些例子通过不了??