Guest User

BeforeAnExam.java

a guest
Apr 10th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.39 KB | None | 0 0
  1. import java.io.BufferedWriter;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.PrintWriter;
  5. import java.util.InputMismatchException;
  6. import java.io.OutputStreamWriter;
  7. import java.io.OutputStream;
  8. import java.io.Writer;
  9. import java.util.Arrays;
  10.  
  11. class BeforeAnExam {
  12.     static class Code {
  13.  
  14.         private void solve(InputReader in, OutputWriter out) {
  15.             int d = in.readInt();
  16.             int total = in.readInt();
  17.  
  18.             int[] min = new int[d];
  19.             int[] max = new int[d];
  20.  
  21.             int l = 0, r = 0;
  22.  
  23.             for(int i = 0; i < d; ++i) {
  24.                 min[i] = in.readInt();
  25.                 l += min[i];
  26.                 max[i] = in.readInt();
  27.                 r += max[i];
  28.             }
  29.  
  30.             /*l = sum of minimum number of hours
  31.             * r = sum of maximum number of hours
  32.             * total = total hours required*/
  33.             if(l <= total && r >= total) {
  34.                 out.printLine("YES");
  35.                 int i = 0;
  36.                 for(; i < d; ++i) {
  37.                     if(l + (max[i] - min[i]) <= total) {
  38.                         l += (max[i] - min[i]);
  39.                         out.print(max[i] +" ");
  40.                     }
  41.                     else {
  42.                         break;
  43.                     }
  44.                 }
  45.  
  46.                 if(i < d) {
  47.                     /*The l already contains the term min[i]
  48.                     * So subtracting from total we take away the min[i] form l
  49.                     * But we also have to do minimum amount of work on ith day
  50.                     * so to compensate the loss, we add min[i]*/
  51.                     out.print(total - l + min[i] +" ");
  52.                     i++;
  53.                 }
  54.  
  55.                 for (; i < d; ++i) {
  56.                     out.print(min[i] +" ");
  57.                 }
  58.  
  59.             }
  60.             else {
  61.                 out.printLine( "NO");
  62.             }
  63.  
  64.  
  65.         }
  66.     }
  67.  
  68.     public static void main(String[] args) throws IOException {
  69.         //initialize
  70.         InputStream inputStream = System.in;
  71.         OutputStream outputStream = System.out;
  72.         InputReader in = new InputReader(inputStream);
  73.         OutputWriter out = new OutputWriter(outputStream);
  74.         Code solver = new Code();
  75.         solver.solve(in, out);
  76.         out.flush();
  77.         out.close();
  78.     }
  79.  
  80.  
  81.     static class InputReader {
  82.         private InputStream stream;
  83.         private byte[] buf = new byte[1024];
  84.         private int curChar;
  85.         private int numChars;
  86.         private SpaceCharFilter filter;
  87.  
  88.         private InputReader(InputStream stream) {
  89.             this.stream = stream;
  90.         }
  91.  
  92.         private int read() {
  93.             if (numChars == -1)
  94.                 throw new InputMismatchException();
  95.             if (curChar >= numChars) {
  96.                 curChar = 0;
  97.                 try {
  98.                     numChars = stream.read(buf);
  99.                 } catch (IOException e) {
  100.                     throw new InputMismatchException();
  101.                 }
  102.                 if (numChars <= 0)
  103.                     return -1;
  104.             }
  105.             return buf[curChar++];
  106.         }
  107.  
  108.         private String readString() {
  109.             int c = read();
  110.             while (isSpaceChar(c))
  111.                 c = read();
  112.             StringBuilder res = new StringBuilder();
  113.             do {
  114.                 if (Character.isValidCodePoint(c))
  115.                     res.appendCodePoint(c);
  116.                 c = read();
  117.             } while (!isSpaceChar(c));
  118.             return res.toString();
  119.         }
  120.  
  121.         private int readInt() {
  122.             int c = read();
  123.             while (isSpaceChar(c))
  124.                 c = read();
  125.             int sgn = 1;
  126.             if (c == '-') {
  127.                 sgn = -1;
  128.                 c = read();
  129.             }
  130.             int res = 0;
  131.             do {
  132.                 if (c < '0' || c > '9')
  133.                     throw new InputMismatchException();
  134.                 res *= 10;
  135.                 res += c - '0';
  136.                 c = read();
  137.             } while (!isSpaceChar(c));
  138.             return res * sgn;
  139.         }
  140.  
  141.         private long readLong() {
  142.             int c = read();
  143.             while (isSpaceChar(c)) {
  144.                 c = read();
  145.             }
  146.             int sgn = 1;
  147.             if (c == '-') {
  148.                 sgn = -1;
  149.                 c = read();
  150.             }
  151.             long res = 0;
  152.             do {
  153.                 if (c < '0' || c > '9') {
  154.                     throw new InputMismatchException();
  155.                 }
  156.                 res *= 10;
  157.                 res += c - '0';
  158.                 c = read();
  159.             } while (!isSpaceChar(c));
  160.             return res * sgn;
  161.         }
  162.  
  163.         private double readDouble() {
  164.             int c = read();
  165.             while (isSpaceChar(c)) {
  166.                 c = read();
  167.             }
  168.             int sgn = 1;
  169.             if (c == '-') {
  170.                 sgn = -1;
  171.                 c = read();
  172.             }
  173.             double res = 0;
  174.             while (!isSpaceChar(c) && c != '.') {
  175.                 if (c == 'e' || c == 'E') {
  176.                     return res * Math.pow(10, readInt());
  177.                 }
  178.                 if (c < '0' || c > '9') {
  179.                     throw new InputMismatchException();
  180.                 }
  181.                 res *= 10;
  182.                 res += c - '0';
  183.                 c = read();
  184.             }
  185.             if (c == '.') {
  186.                 c = read();
  187.                 double m = 1;
  188.                 while (!isSpaceChar(c)) {
  189.                     if (c == 'e' || c == 'E') {
  190.                         return res * Math.pow(10, readInt());
  191.                     }
  192.                     if (c < '0' || c > '9') {
  193.                         throw new InputMismatchException();
  194.                     }
  195.                     m /= 10;
  196.                     res += (c - '0') * m;
  197.                     c = read();
  198.                 }
  199.             }
  200.             return res * sgn;
  201.         }
  202.  
  203.         private boolean isSpaceChar(int c) {
  204.             if (filter != null)
  205.                 return filter.isSpaceChar(c);
  206.             return isWhitespace(c);
  207.         }
  208.  
  209.         public String next() {
  210.             return readString();
  211.         }
  212.  
  213.  
  214.         private static boolean isWhitespace(int c) {
  215.             return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  216.         }
  217.  
  218.         private char readCharacter() {
  219.             int c = read();
  220.             while (isSpaceChar(c))
  221.                 c = read();
  222.             return (char) c;
  223.         }
  224.  
  225.         public interface SpaceCharFilter {
  226.             boolean isSpaceChar(int ch);
  227.         }
  228.     }
  229.  
  230.     static class OutputWriter {
  231.         private final PrintWriter writer;
  232.  
  233.         private OutputWriter(OutputStream outputStream) {
  234.             writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
  235.                     outputStream)));
  236.         }
  237.  
  238.         private OutputWriter(Writer writer) {
  239.             this.writer = new PrintWriter(writer);
  240.         }
  241.  
  242.         private void print(Object... objects) {
  243.             for (int i = 0; i < objects.length; i++) {
  244.                 if (i != 0) {
  245.                     writer.print(' ');
  246.                 }
  247.                 writer.print(objects[i]);
  248.             }
  249.             writer.flush();
  250.         }
  251.  
  252.         private void printLine(Object... objects) {
  253.             print(objects);
  254.             writer.println();
  255.             writer.flush();
  256.         }
  257.  
  258.         private void printLine(int i) {
  259.             writer.println(i);
  260.         }
  261.  
  262.  
  263.         private void close() {
  264.             writer.close();
  265.         }
  266.  
  267.         private void flush() {
  268.             writer.flush();
  269.         }
  270.  
  271.  
  272.     }
  273.  
  274.     static class IOUtils {
  275.  
  276.         private static int[] readIntArray(InputReader in, int size) {
  277.             int[] array = new int[size];
  278.             for (int i = 0; i < size; i++)
  279.                 array[i] = in.readInt();
  280.             return array;
  281.         }
  282.  
  283.         private static long[] readLongArray(InputReader in, int size) {
  284.             long[] array = new long[size];
  285.             for (int i = 0; i < size; i++)
  286.                 array[i] = in.readLong();
  287.             return array;
  288.         }
  289.  
  290.         private static char[] readCharArray(InputReader in, int size) {
  291.             char[] array = new char[size];
  292.             for (int i = 0; i < size; i++)
  293.                 array[i] = in.readCharacter();
  294.             return array;
  295.         }
  296.  
  297.         private static char[][] readTable(InputReader in, int rowCount,
  298.                                           int columnCount) {
  299.             char[][] table = new char[rowCount][];
  300.             for (int i = 0; i < rowCount; i++)
  301.                 table[i] = readCharArray(in, columnCount);
  302.             return table;
  303.         }
  304.  
  305.     }
  306.  
  307.     static class ArrayUtils {
  308.  
  309.         private static void fill(int[][] array, int value) {
  310.             for (int[] row : array)
  311.                 Arrays.fill(row, value);
  312.         }
  313.  
  314.     }
  315.  
  316.     static class MiscUtils {
  317.         private static final int[] DX4 = {1, 0, -1, 0};
  318.         private static final int[] DY4 = {0, -1, 0, 1};
  319.  
  320.         private static boolean isValidCell(int row, int column, int rowCount,
  321.                                            int columnCount) {
  322.             return row >= 0 && row < rowCount && column >= 0
  323.                     && column < columnCount;
  324.         }
  325.  
  326.     }
  327.  
  328.  
  329. }
Add Comment
Please, Sign In to add comment