Advertisement
Guest User

Untitled

a guest
May 2nd, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.86 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.InputMismatchException;
  3. /**
  4.  * Built using CHelper plug-in
  5.  * Actual solution is at the top
  6.  */
  7. public class Main {
  8.     public static void main(String[] args) {
  9.         InputStream inputStream = System.in;
  10.         OutputStream outputStream = System.out;
  11.         InputReader in = new InputReader(inputStream);
  12.         OutputWriter out = new OutputWriter(outputStream);
  13.         AdaptiveBinarySearch solver = new AdaptiveBinarySearch();
  14.         solver.solve(1, in, out);
  15.         out.close();
  16.     }
  17.  
  18.     static class AdaptiveBinarySearch {
  19.         public void solve(int testNumber, InputReader in, OutputWriter out) {
  20.             int low = 1, high = in.readInt();
  21.             while (low < high) {
  22.                 int mid = (low + high) / 2;
  23.                 out.printLine("Q", mid);
  24.                 out.flush();
  25.                 if (in.readInt() == 0) {
  26.                     high = mid;
  27.                 } else {
  28.                     low = mid + 1;
  29.                 }
  30.             }
  31.             out.printLine("A", low);
  32.             out.close();
  33.         }
  34.     }
  35.     static class OutputWriter {
  36.         private final PrintWriter writer;
  37.  
  38.         public OutputWriter(OutputStream outputStream) {
  39.             writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
  40.         }
  41.  
  42.         public OutputWriter(Writer writer) {
  43.             this.writer = new PrintWriter(writer);
  44.         }
  45.  
  46.         public void print(Object... objects) {
  47.             for (int i = 0; i < objects.length; i++) {
  48.                 if (i != 0) {
  49.                     writer.print(' ');
  50.                 }
  51.                 writer.print(objects[i]);
  52.             }
  53.         }
  54.  
  55.         public void printLine(Object... objects) {
  56.             print(objects);
  57.             writer.println();
  58.         }
  59.  
  60.         public void close() {
  61.             writer.close();
  62.         }
  63.  
  64.         public void flush() {
  65.             writer.flush();
  66.         }
  67.     }
  68.     static class InputReader {
  69.         private InputStream stream;
  70.         private byte[] buf = new byte[1024];
  71.         private int curChar;
  72.         private int numChars;
  73.         private InputReader.SpaceCharFilter filter;
  74.  
  75.         public InputReader(InputStream stream) {
  76.             this.stream = stream;
  77.         }
  78.  
  79.         public int read() {
  80.             if (numChars == -1) {
  81.                 throw new InputMismatchException();
  82.             }
  83.             if (curChar >= numChars) {
  84.                 curChar = 0;
  85.                 try {
  86.                     numChars = stream.read(buf);
  87.                 } catch (IOException e) {
  88.                     throw new InputMismatchException();
  89.                 }
  90.                 if (numChars <= 0) {
  91.                     return -1;
  92.                 }
  93.             }
  94.             return buf[curChar++];
  95.         }
  96.  
  97.         public int readInt() {
  98.             int c = read();
  99.             while (isSpaceChar(c)) {
  100.                 c = read();
  101.             }
  102.             int sgn = 1;
  103.             if (c == '-') {
  104.                 sgn = -1;
  105.                 c = read();
  106.             }
  107.             int res = 0;
  108.             do {
  109.                 if (c < '0' || c > '9') {
  110.                     throw new InputMismatchException();
  111.                 }
  112.                 res *= 10;
  113.                 res += c - '0';
  114.                 c = read();
  115.             } while (!isSpaceChar(c));
  116.             return res * sgn;
  117.         }
  118.  
  119.         public boolean isSpaceChar(int c) {
  120.             if (filter != null) {
  121.                 return filter.isSpaceChar(c);
  122.             }
  123.             return isWhitespace(c);
  124.         }
  125.  
  126.         public static boolean isWhitespace(int c) {
  127.             return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  128.         }
  129.  
  130.         public interface SpaceCharFilter {
  131.             public boolean isSpaceChar(int ch);
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement