思路:贪心。我们记录所有的mintine之和sum_min和所有的maxtime之和sum_max。如果sumtime不在sum_min和sum_max之间。那么肯定无解,直接输出NO,如果有解,我们遍历每一天。我们判断在min[i]的基础上增加时间,是否可以选择到max[i]。
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int d = sc.nextInt();
int s = sc.nextInt();
int[] min = new int[d+5];
int[] max = new int[d+5];
int sum_min = 0;
int sum_max = 0;
for(int i=0;i<d;i++){
min[i] = sc.nextInt();
max[i] = sc.nextInt();
sum_min += min[i];
sum_max += max[i];
}
if(s<sum_min||s>sum_max){ //无解
System.out.println("NO");
}else{//有解
int[] res = new int[d+5];
int t = s - sum_min;
for(int i=0;i<d;i++){
if(t+min[i]<=max[i]){
res[i] = t+min[i];
t = 0;
}else{
res[i] = max[i];
t -= max[i]-min[i];
}
}
System.out.println("YES");
for(int i=0;i<d;i++) System.out.print(res[i]+" ");
}
}
}