【题目来源】:Codeforces 4B.
Tomorrow Peter has a Biology exam. He does not like this subject much, but d days ago he learnt that he would have to take this exam. Peter's strict parents made him prepare for the exam immediately, for this purpose he has to study not less than minTimei and not more than maxTimei hours per each i-th day. Moreover, they warned Peter that a day before the exam they would check how he has followed their instructions.
So, today is the day when Peter's parents ask him to show the timetable of his preparatory studies. But the boy has counted only the sum of hours sumTime spent him on preparation, and now he wants to know if he can show his parents a timetable sсhedule with dnumbers, where each number sсhedulei stands for the time in hours spent by Peter each i-th day on biology studies, and satisfying the limitations imposed by his parents, and at the same time the sum total of all schedulei should equal to sumTime.
Input
The first input line contains two integer numbers d, sumTime (1 ≤ d ≤ 30, 0 ≤ sumTime ≤ 240) — the amount of days, during which Peter studied, and the total amount of hours, spent on preparation. Each of the following d lines contains two integer numbers minTimei, maxTimei (0 ≤ minTimei ≤ maxTimei ≤ 8), separated by a space — minimum and maximum amount of hours that Peter could spent in the i-th day.
Output
In the first line print YES, and in the second line print d numbers (separated by a space), each of the numbers — amount of hours, spent by Peter on preparation in the corresponding day, if he followed his parents' instructions; or print NO in the unique line. If there are many solutions, print any of them.
Examples
input
1 48 5 7
output
NO
input
2 5 0 1 3 5
output
YES 1 4
【题目大意】:先输入d,和sum再输入d行,每行两个数,判断在每行中两数范围之内取一个数相加最终结果能否为sum。
【AC代码】:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = sc.nextInt();
int a[] = new int[n];
int b[] = new int[n];
int a1 = 0;//最小值总和
int b1 = 0;//最大值总和
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
a1 += a[i];
b[i] = sc.nextInt();
b1 += b[i];
}
if (a1 > sum || b1 < sum)//输入的数若小于最小值之和或大于最大值之和都无法实现
System.out.println("NO");
else {
System.out.println("YES");
sum -= a1;
for (int i = 0; i < n - 1; i++) {
if (b[i] - a[i] < sum) {//按照顺序先依次分配最大的
System.out.print(b[i] + " ");
sum -= b[i] - a[i];
} else {
System.out.print((a[i] + sum) + " ");
sum = 0;
}
}
System.out.println(a[n - 1] + sum);
}
}
}