import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // 面额的种类数
int m = sc.nextInt(); // 需要发放的工资总额
ArrayList<Integer> money = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
int x = sc.nextInt(); // 面额、单位:元
int y = sc.nextInt(); // 数目、单位:张
for (int j = 0; j < y; j++) {
money.add(x); // 向金库中添加y张x元的钞票
}
}
Collections.sort(money); // 将金额按照从小到大的顺序排列
// 开始发工资
int months = pay(money, m);
System.out.println(months);
}
static int pay(ArrayList<Integer> money, int m) {
int month = 0;
if (money.get(0) >= m) {
month = money.size(); // 最小金额能够发放的话,直接发放
return month;
} else {
// 最小金额不能发放,找到次小金额的下标
int index = 1;
while(money.get(0) == money.get(index)) index += 1;
while ((money.get(0) + money.get(index)) >= m) {
month += 1;
money.remove(0);
money.remove(index);
if (money.get(index) >= m) {
return month + money.size() - index + 1;
}
}
}
return month;
}
}
2020百度春招编程第二题
最新推荐文章于 2021-04-16 15:03:56 发布