题解 | #餐馆#贪心 最后一个用例在超时的边缘徘徊
餐馆
https://www.nowcoder.com/practice/d2cced737eb54a3aa550f53bb3cc19d0
import java.util.*; public class Main { static boolean[] used; static long res; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); // 桌子数量 int m = sc.nextInt(); // 顾客数量 used = new boolean[n]; int[] tables = new int[n]; for (int i = 0; i < n; i++) { tables[i] = sc.nextInt(); } int[][] nums = new int[m][2]; // 顾客人数和消费 for (int i = 0; i < m; i++) { nums[i][0] = sc.nextInt(); nums[i][1] = sc.nextInt(); } //消费高的人少的先来 Arrays.sort(nums, (x, y)-> { return x[1] != y[1] ? y[1] - x[1] : x[0] - y[0]; }); //桌子小的先使用 Arrays.sort(tables); for (int j = 0; j < m; j++) { int people = nums[j][0]; int money = nums[j][1]; dfs(tables,n,people,money); } System.out.println(res); } private static void dfs(int[] tables,int n,int people,int money) { for (int i=0; i<n; i++) { if (!used[i]) { if (tables[i] >= people) { res +=(long)money; used[i] = true; break; } } } } }