Advertisement
j33vansh

Question 5 Rocky Bhai Java Solution

May 8th, 2022
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.87 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.PrintWriter;
  4. import java.math.BigInteger;
  5. import java.util.Arrays;
  6. import java.util.InputMismatchException;
  7.  
  8.  
  9. public class Main implements Runnable {
  10.     private InputReader in;
  11.     private PrintWriter out;
  12.  
  13.     public static void main(String[] args) {
  14. //      new Thread(new Template()).start();
  15.         new Main().run();
  16.     }
  17.  
  18.     public Main() {
  19.         String id = getClass().getName().toLowerCase();
  20. //      try {
  21. //          System.setIn(new FileInputStream(id + ".in"));
  22. //          System.setOut(new PrintStream(new FileOutputStream(id + ".out")));
  23. //      } catch (FileNotFoundException e) {
  24. //          throw new RuntimeException();
  25. //      }
  26.         in = new InputReader(System.in);
  27.         out = new PrintWriter(System.out);
  28.     }
  29.  
  30.     public void run() {
  31.         int numTests = in.readInt();
  32.         for (int testNumber = 0; testNumber < numTests; testNumber++) {
  33.             int m = in.readInt();
  34.             int n = in.readInt();
  35.             int k = in.readInt();
  36.             int[] t = new int[m];
  37.             int[] p = new int[m];
  38.             for (int i = 0; i < m; i++)
  39.                 t[i] = in.readInt();
  40.             for (int i = 0; i < m; i++)
  41.                 p[i] = in.readInt();
  42.             int l = 0;
  43.             int r = t[0] + (k - 1) * p[0];
  44.             int[] q = new int[m];
  45.             while (l < r) {
  46.                 int mm = (l + r) / 2;
  47.                 for (int i = 0; i < m; i++) {
  48.                     if (mm < t[i])
  49.                         q[i] = 0;
  50.                     else
  51.                         q[i] = (mm - t[i]) / p[i] + 1;
  52.                 }
  53.                 Arrays.sort(q);
  54.                 long cur = 0;
  55.                 for (int i = m - 1; i >= 0 && i >= m - n; i--)
  56.                     cur += q[i];
  57.                 if (cur >= k)
  58.                     r = mm;
  59.                 else
  60.                     l = mm + 1;
  61.             }
  62.             out.println(l);
  63.         }
  64.         out.close();
  65.     }
  66.  
  67.     private static class InputReader {
  68.         private InputStream stream;
  69.         private byte[] buf = new byte[1000];
  70.         private int curChar, numChars;
  71.  
  72.         public InputReader(InputStream stream) {
  73.             this.stream = stream;
  74.         }
  75.  
  76.         private int read() {
  77.             if (numChars == -1)
  78.                 throw new InputMismatchException();
  79.             if (curChar >= numChars) {
  80.                 curChar = 0;
  81.                 try {
  82.                     numChars = stream.read(buf);
  83.                 } catch (IOException e) {
  84.                     throw new InputMismatchException();
  85.                 }
  86.                 if (numChars <= 0)
  87.                     return -1;
  88.             }
  89.             return buf[curChar++];
  90.         }
  91.  
  92.         public int readInt() {
  93.             int c = read();
  94.             while (isSpaceChar(c))
  95.                 c = read();
  96.             int sgn = 1;
  97.             if (c == '-') {
  98.                 sgn = -1;
  99.                 c = read();
  100.             }
  101.             int res = 0;
  102.             do {
  103.                 if (c < '0' || c > '9')
  104.                     throw new InputMismatchException();
  105.                 res *= 10;
  106.                 res += c - '0';
  107.                 c = read();
  108.             } while (!isSpaceChar(c));
  109.             return res * sgn;
  110.         }
  111.  
  112.         public long readLong() {
  113.             int c = read();
  114.             while (isSpaceChar(c))
  115.                 c = read();
  116.             int sgn = 1;
  117.             if (c == '-') {
  118.                 sgn = -1;
  119.                 c = read();
  120.             }
  121.             long res = 0;
  122.             do {
  123.                 if (c < '0' || c > '9')
  124.                     throw new InputMismatchException();
  125.                 res *= 10;
  126.                 res += c - '0';
  127.                 c = read();
  128.             } while (!isSpaceChar(c));
  129.             return res * sgn;
  130.         }
  131.  
  132.         public String readString() {
  133.             int c = read();
  134.             while (isSpaceChar(c))
  135.                 c = read();
  136.             StringBuffer res = new StringBuffer();
  137.             do {
  138.                 res.appendCodePoint(c);
  139.                 c = read();
  140.             } while (!isSpaceChar(c));
  141.             return res.toString();
  142.         }
  143.  
  144.         private boolean isSpaceChar(int c) {
  145.             return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  146.         }
  147.  
  148.         private String readLine0() {
  149.             StringBuffer buf = new StringBuffer();
  150.             int c = read();
  151.             while (c != '\n' && c != -1) {
  152.                 buf.appendCodePoint(c);
  153.                 c = read();
  154.             }
  155.             return buf.toString();
  156.         }
  157.  
  158.         public String readLine() {
  159.             String s = readLine0();
  160.             while (s.trim().length() == 0)
  161.                 s = readLine0();
  162.             return s;
  163.         }
  164.  
  165.         public String readLine(boolean ignoreEmptyLines) {
  166.             if (ignoreEmptyLines)
  167.                 return readLine();
  168.             else
  169.                 return readLine0();
  170.         }
  171.  
  172.         public BigInteger readBigInteger() {
  173.             try {
  174.                 return new BigInteger(readString());
  175.             } catch (NumberFormatException e) {
  176.                 throw new InputMismatchException();
  177.             }
  178.         }
  179.  
  180.         public char readCharacter() {
  181.             int c = read();
  182.             while (isSpaceChar(c))
  183.                 c = read();
  184.             return (char) c;
  185.         }
  186.  
  187.         public double readDouble() {
  188.             int c = read();
  189.             while (isSpaceChar(c))
  190.                 c = read();
  191.             int sgn = 1;
  192.             if (c == '-') {
  193.                 sgn = -1;
  194.                 c = read();
  195.             }
  196.             double res = 0;
  197.             while (!isSpaceChar(c) && c != '.') {
  198.                 if (c < '0' || c > '9') {
  199.                     if (c == 'e' || c == 'E') {
  200.                         int e = readInt();
  201.                         return res * Math.pow(10, e);
  202.                     }
  203.                     throw new InputMismatchException();
  204.                 }
  205.                 res *= 10;
  206.                 res += c - '0';
  207.                 c = read();
  208.             }
  209.             if (c == '.') {
  210.                 c = read();
  211.                 double m = 1;
  212.                 while (!isSpaceChar(c)) {
  213.                     if (c == 'e' || c == 'E') {
  214.                         int e = readInt();
  215.                         return res * Math.pow(10, e);
  216.                     }
  217.                     if (c < '0' || c > '9')
  218.                         throw new InputMismatchException();
  219.                     m /= 10;
  220.                     res += (c - '0') * m;
  221.                     c = read();
  222.                 }
  223.             }
  224.             return res * sgn;
  225.         }
  226.     }
  227.  
  228. }
  229.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement