exec_outcome
stringclasses
1 value
code_uid
stringlengths
32
32
file_name
stringclasses
111 values
prob_desc_created_at
stringlengths
10
10
prob_desc_description
stringlengths
63
3.8k
prob_desc_memory_limit
stringclasses
18 values
source_code
stringlengths
117
65.5k
lang_cluster
stringclasses
1 value
prob_desc_sample_inputs
stringlengths
2
802
prob_desc_time_limit
stringclasses
27 values
prob_desc_sample_outputs
stringlengths
2
796
prob_desc_notes
stringlengths
4
3k
lang
stringclasses
5 values
prob_desc_input_from
stringclasses
3 values
tags
listlengths
0
11
src_uid
stringlengths
32
32
prob_desc_input_spec
stringlengths
28
2.37k
difficulty
int64
-1
3.5k
prob_desc_output_spec
stringlengths
17
1.47k
prob_desc_output_to
stringclasses
3 values
hidden_unit_tests
stringclasses
1 value
PASSED
505ce6b4a375255b73754f9cb43f102f
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /* @author kalZor */ public class TaskC1 { static class Solver { public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(); int low = 1,high = n; while (low<high) { int mid = low + (high-low)/2; out.println("? "+low+" "+high); out.flush(); int next = in.nextInt(); if(low==mid){ if(low==next) low = mid+1; else high = mid-1; continue; } int l =-1 , r = -1; if(mid<next){ l = mid; r = high; } else{ l = low; r = mid; } out.println("? "+l+" "+r); out.flush(); int nex = in.nextInt(); if(nex==next){ low = l; high = r; } else{ if(l==mid){ high = mid-1; } else low = mid+1; } } out.println("! "+low); out.flush(); } } public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solver solver = new Solver(); solver.solve(1, in, out); out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(InputStream stream){ br = new BufferedReader(new InputStreamReader(stream)); } public String next(){ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } public int nextInt(){ return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble(){ return Double.parseDouble(next()); } public String nextLine(){ String str = ""; try{ str = br.readLine(); } catch (IOException e){ e.printStackTrace(); } return str; } public int[] nextArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
0b55e003df17142a53f914b05c8c90e4
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class B { // 5 7 4 3 2 6 1 static boolean tests = false; static void solve(){ int left = 1, right = in.nextInt(); int kept = -1; while (true){ if (left == right){ out.println("! "+left); return; } if (kept == -1) out.println("? "+left+" "+right); out.flush(); int index = kept == -1 ? in.nextInt() : kept; if (left+1 == right){ out.println("! "+(index == right ? left : right)); return; } int mid = (left+right+1)/2; if (index <= mid){ out.println("? "+left+" "+mid); out.flush(); int j = in.nextInt(); if (j != index){ // this range does not contain max left = mid+1; kept = -1; }else { right = mid; kept = j; } }else{ out.println("? "+mid+" "+right); out.flush(); int j = in.nextInt(); if (j != index){ right = mid-1; kept = -1; }else{ left = mid; kept = j; } } } } // 5 1 4 2 3 static Reader in; static PrintWriter out; static int int_max = (int)1e9, mod = int_max+7; public static void main(String[] args){ in = new Reader(); out = new PrintWriter(System.out); int t = tests ? in.nextInt() : 1; while (t-- > 0) solve(); out.close(); } // Reader credited to SecondThread static class Reader{ BufferedReader b; StringTokenizer s; Reader(){ this.b = new BufferedReader(new InputStreamReader(System.in)); this.s = new StringTokenizer(""); } String next(){ while (!s.hasMoreTokens()) try{s = new StringTokenizer(b.readLine()); }catch(IOException ignored){} return s.nextToken(); } int nextInt(){return Integer.parseInt(next());} long nextLong(){return Long.parseLong(next());} double nextDouble(){return Double.parseDouble(next());} int[] int_array(int n){ int[] a = new int[n]; for (int i = 0; i < n; ++i) a[i] = nextInt(); return a; } long[] long_array(int n){ long[] a = new long[n]; for (int i = 0; i < n; ++i) a[i] = nextLong(); return a; } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
7e904f91297c1e08f2e2db746aad8a8f
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
// Working program with FastReader import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; // import jdk.internal.org.jline.terminal.impl.PosixSysTerminal; import java.util.*; import java.io.*; public class C1_Guessing_the_Greatest_easy_version_{ public static void main(String[] args) { FastReader sc=new FastReader(); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); int l = 1; int r = n; get(l,r); int position = sc.nextInt(); while(l<r-1){ int mid = l+(r-l)/2; get(l,mid); int leftside = sc.nextInt(); get(mid,r); int rightside = sc.nextInt(); if(leftside==position){ r = mid; } else if(rightside==position){ l = mid; } else{ if(position<=mid){ position = rightside; l = mid; } else{ position = leftside; r = mid; } } } if(l==r){ print(position); } else{ if(position==r)print(l); else print(r); } out.close(); } public static void get(int l,int r){ System.out.println("? "+l+" "+r); } public static void print(int position){ System.out.println("! "+position); } public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
10672322f1810dadf07dbd55a1aa6223
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; /** * # * * @author pttrung */ public class C_Round_703_Div2 { public static long MOD = 998244353; static long[] dp; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int n = in.nextInt(); System.out.println("? " + 1 + " " + n); int v = in.nextInt(); int st = 1; int ed = n; while (st < ed) { if (st + 1 == ed) { if (st == v) { out.println("! " + ed); } else { out.println("! " + st); } break; } int mid = (st + ed) / 2; int tmp; if (v <= mid) { if (st < mid) { System.out.println("? " + st + " " + mid); tmp = in.nextInt(); } else { tmp = -1; } } else { if ((mid + 1) < ed) { System.out.println("? " + (mid + 1) + " " + ed); tmp = in.nextInt(); } else { tmp = -1; } } if (tmp == v) { if (v <= mid) { ed = mid; } else { st = mid + 1; } } else { if (v <= mid) { st = mid + 1; } else { ed = mid; } if (st == ed) { out.println("! " + st); break; } System.out.println("? " + st + " " + ed); v = in.nextInt(); } } out.close(); } public static int[] KMP(String val) { int i = 0; int j = -1; int[] result = new int[val.length() + 1]; result[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(j) != val.charAt(i)) { j = result[j]; } j++; i++; result[i] = j; } return result; } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } public static int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point implements Comparable<Point> { int x; long y; public Point(int start, long end) { this.x = start; this.y = end; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Point point = (Point) o; return x == point.x && y == point.y; } @Override public int hashCode() { return Objects.hash(x, y); } @Override public int compareTo(Point o) { return Integer.compare(x, o.x); } } public static class FT { long[] data; FT(int n) { data = new long[n]; } public void update(int index, long value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public long get(int index) { long result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, long b) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2); if (b % 2 == 0) { return (val * val) % MOD; } else { return (val * ((val * a) % MOD)) % MOD; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); //br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
5bb8525736049b2eec0c4677f538c797
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.InputMismatchException; /** * Created by Wilson on * Feb. 08, 2021 */ public class InteractiveProgram { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); CSearchingLocalMinimum solver = new CSearchingLocalMinimum(); solver.solve(in, out); out.close(); } static class CSearchingLocalMinimum { OutputWriter out; InputReader in; int query(int l,int r) { out.printLine("?", l,r); out.flush(); return in.readInt(); } void writeOutput(int ans) { out.printLine("!", ans); out.flush(); } public void solve(InputReader in, OutputWriter out) { this.out = out; this.in = in; int n = in.readInt(); if (n == 1) { writeOutput(1); return; } int l,r,m,q1,q2,mid,ans; l =1; r = n; while(true){ mid = (l+r)/2; q1 = 0; q2=0; if(r != l) q1 = query(l,r); if(r -l <= 1){ if(r == l)ans = l; else if(r - l ==1 && q1 == r)ans = l; else ans = r; writeOutput(ans); break; } if(q1 < mid){ q2 = query(l,mid); if(q1 == q2){ r = mid; }else{ l = mid; } }else { q2 = query(mid,r); if(q1 == q2){ l = mid; }else { r = mid; } } } } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
6b3eff9e8af436e08052dfe8d306c66c
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; public class Solver { public static void main(String[] args) { FastReader in = new FastReader(); int t = 1; while (t-- > 0) { solve(in); } } public static void solve(FastReader in) { int n = in.nextInt(); System.out.println("! " + getMax(1, n, -1, in)); } public static int getMax(int l, int r, int idx, FastReader in) { if (r == l) { return r; } if (r - l == 1) { System.out.println("? " + l + " " + r); System.out.flush(); int n = in.nextInt(); return (n == r) ? l : r; } int mid = (l + r) / 2; int n = idx; if (idx == -1) { System.out.println("? " + l + " " + r); System.out.flush(); n = in.nextInt(); idx = n; } if (n <= mid) { if (l == mid) { return getMax(l, mid, -1, in); } System.out.println("? " + l + " " + mid); System.out.flush(); n = in.nextInt(); if (idx == n) { return getMax(l, mid, n, in); } else { return getMax(mid + 1, r, -1, in); } } else { if (mid + 1 == r) { return getMax(l, mid, -1, in); } System.out.println("? " + (mid + 1) + " " + r); System.out.flush(); n = in.nextInt(); if (idx == n) { return getMax(mid + 1, r, n, in); } else { return getMax(l, mid, -1, in); } } } public static class FastReader { BufferedReader br; StringTokenizer st; private FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static class FastWriter { private static final int BUF_SIZE = 1 << 13; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastWriter() { out = null; } public FastWriter(OutputStream os) { this.out = os; } public FastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public FastWriter write(byte b) { buf[ptr++] = b; if (ptr == BUF_SIZE) innerflush(); return this; } public FastWriter write(char c) { return write((byte) c); } public FastWriter write(char[] s) { for (char c : s) { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); } return this; } public FastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); }); return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } public FastWriter write(int x) { if (x == Integer.MIN_VALUE) { return write((long) x); } if (ptr + 12 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastWriter write(long x) { if (x == Long.MIN_VALUE) { return write("" + x); } if (ptr + 21 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } public FastWriter write(double x, int precision) { if (x < 0) { write('-'); x = -x; } x += Math.pow(10, -precision) / 2; // if(x < 0){ x = 0; } write((long) x).write("."); x -= (long) x; for (int i = 0; i < precision; i++) { x *= 10; write((char) ('0' + (int) x)); x -= (int) x; } return this; } public FastWriter writeln(char c) { return write(c).writeln(); } public FastWriter writeln(int x) { return write(x).writeln(); } public FastWriter writeln(long x) { return write(x).writeln(); } public FastWriter writeln(double x, int precision) { return write(x, precision).writeln(); } public FastWriter write(int... xs) { boolean first = true; for (int x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter write(long... xs) { boolean first = true; for (long x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter writeln() { return write((byte) '\n'); } public FastWriter writeln(int... xs) { return write(xs).writeln(); } public FastWriter writeln(long... xs) { return write(xs).writeln(); } public FastWriter writeln(char[] line) { return write(line).writeln(); } public FastWriter writeln(char[]... map) { for (char[] line : map) write(line).writeln(); return this; } public FastWriter writeln(String s) { return write(s).writeln(); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } public FastWriter print(byte b) { return write(b); } public FastWriter print(char c) { return write(c); } public FastWriter print(char[] s) { return write(s); } public FastWriter print(String s) { return write(s); } public FastWriter print(int x) { return write(x); } public FastWriter print(long x) { return write(x); } public FastWriter print(double x, int precision) { return write(x, precision); } public FastWriter println(char c) { return writeln(c); } public FastWriter println(int x) { return writeln(x); } public FastWriter println(long x) { return writeln(x); } public FastWriter println(double x, int precision) { return writeln(x, precision); } public FastWriter print(int... xs) { return write(xs); } public FastWriter print(long... xs) { return write(xs); } public FastWriter println(int... xs) { return writeln(xs); } public FastWriter println(long... xs) { return writeln(xs); } public FastWriter println(char[] line) { return writeln(line); } public FastWriter println(char[]... map) { return writeln(map); } public FastWriter println(String s) { return writeln(s); } public FastWriter println() { return writeln(); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
df154ed582f941299b76767607c1c64a
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.*; public class Question3 { static Scanner sc= new Scanner(System.in); static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); // static Reader sc = new Reader(); public static void main(String[] args) throws IOException { solve(); bw.close(); } public static void solve() throws IOException { int n = sc.nextInt(); int l = 1, r = n; int c = 0; int secmax = query(l, r); while(l < r) { //size = 1 if(l == r) { bw.write("! " + String.valueOf(l) + "\n"); bw.flush(); return; } //size = 2 if(l + 1 == r) { if(secmax == l) { bw.write("! " + String.valueOf(r) + "\n"); bw.flush(); return; } else { bw.write("! " + String.valueOf(l) + "\n"); bw.flush(); return; } } int mid = l + (r - l)/2; if(secmax <= mid) { if(query(l, mid) == secmax) { r = mid; } else { l = mid + 1; if(l != r) {//update value secmax = query(l, r); } } } else { //sec max in second half if(query(mid, r) == secmax) { l = mid; } else { r = mid - 1; if(r != l) { secmax = query(l, r); } } } } bw.write("! " + String.valueOf(l) + "\n"); bw.flush(); } public static int query(int l, int r) throws IOException { bw.write("? " + String.valueOf(l) + " " + String.valueOf(r) + "\n"); bw.flush(); int q = sc.nextInt(); return q; } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
c77a729e919e0b74b3c2bf9cf35c1043
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class _1486_C1 { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); int lbound = 1; int rbound = n; int res = 0; while(lbound < rbound) { int avg = (lbound + rbound) / 2; int sec = query(lbound, rbound, in); if(rbound == lbound + 1) { int sec2 = query(avg, rbound, in); if(sec2 == lbound) { res = rbound; }else { res = lbound; } break; } if(sec >= avg) { int sec2 = query(avg, rbound, in); if(sec == sec2) { lbound = avg; }else { rbound = avg - 1; } }else { int sec2 = query(lbound, avg, in); if(sec == sec2) { rbound = avg; }else { lbound = avg + 1; } } } if(res == 0) { System.out.println("! " + lbound); }else { System.out.println("! " + res); } in.close(); } static int query(int l, int r, BufferedReader in) throws IOException { System.out.println("? " + l + " " + r); System.out.flush(); return Integer.parseInt(in.readLine()); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
addbfe57e49592d25203eff6b8dc881e
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import com.sun.jdi.IntegerType; import com.sun.jdi.IntegerValue; import com.sun.jdi.connect.Connector; import javax.lang.model.type.IntersectionType; import javax.security.auth.login.AccountNotFoundException; import javax.swing.*; import java.sql.Array; import java.util.*; import java.lang.*; import java.io.*; public class Main { static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static FastReader sc = new FastReader(); static long mod = (int)1e9+7; public static void main (String[] args) throws java.lang.Exception { int t = 1;//sc.nextInt(); while(t-->0){ solve(); } } public static void solve() { int n = i(); int l=0; int r=n-1; while(l<r){ if(l+1==r){ out.println("? " + (l+1) + " " + (r+1)); out.flush(); int ind = i()-1; if(ind==l){ out.println("! " + (r+1));out.flush();return; }else{ out.println("! " + (l+1));out.flush();return; } } int ind=0; if(l==r){ ind=-1; }else{ out.println("? " + (l+1) + " " + (r+1)); out.flush(); ind = i()-1; } int mid = (l+r)/2; int indl=0; if(ind<=mid){ if(l==mid){ indl=-1; }else{ out.println("? " + (l+1) + " " + (mid+1));out.flush(); indl = i()-1; } if(ind==indl){ r=mid; }else{ l=mid+1; } }else{ if(mid+1==r){ indl=-1; }else{ out.println("? " + (mid+2) + " " + (r+1));out.flush(); indl = i()-1; } if(ind==indl){ l=mid+1; }else{ r=mid; } } // ind=3 mid=1 indl=1 l=2 } if(l==r){ out.print("! " + (l+1)); } out.println(); out.flush(); } static class Pair implements Comparable<Pair>{ int a; int b; Pair(int a, int b){ this.a=a; this.b=b; } public int compareTo(Pair o) { return this.a-o.a; } public String toString(){ return "{" + a + " " + b + "}" ; } } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static int csb(long n) { int count = 0; while (n > 0) { n &= (n - 1); count++; } return count; } static void mysort(int[] arr) { for(int i=0;i<arr.length;i++) { int rand = (int) (Math.random() * arr.length); int loc = arr[rand]; arr[rand] = arr[i]; arr[i] = loc; } Arrays.sort(arr); } static long power(long x, long y) { long res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res; } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static int[] ia(int n){ int[] arr = new int[n]; for(int i = 0;i<n;++i){ arr[i]=i(); } return arr; } static long[] la(int n){ long[] arr = new long[n]; for(int i = 0;i<n;++i){ arr[i]=l(); } return arr; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
fd4b519833fa6022a9d2dfb79b1794f6
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
/** * @author Nitin Bhakar * */ import java.io.*; import java.util.*; public class Codeforces{ static long mod = 1000000007L; static int ninf = Integer.MIN_VALUE; static int inf = Integer.MAX_VALUE; static void swap(int[] a,int i,int j) {int temp = a[i]; a[i] = a[j]; a[j] = temp;} static void priArr(int[] a) {for(int i=0;i<a.length;i++) out.print(a[i] + " ");out.println();} static long gcd(long a,long b){if(b==0) return a; return gcd(b,a%b);} static int gcd(int a,int b){ if(b==0) return a;return gcd(b,a%b);} static void sieve(boolean[] a, int n) {a[1] = false; for(int i=2;i*i<=n;i++) { if(a[i]) { for(int j=i*i;j<=n;j+=i) a[j] = false;}}} static int getRanNum(int lo, int hi) {return (int)Math.random()*(hi-lo+1)+lo;} //map.put(a[i],map.getOrDefault(a[i],0)+1); // map.putIfAbsent; static MyScanner sc = new MyScanner(); //<----------------------------------------------WRITE HERE-------------------------------------------> static void solve(){ int n = sc.nextInt(); int lo = 1; int hi = n; out.println("? "+lo+" "+hi); out.flush(); int secondMax = sc.nextInt(); while(lo<hi) { if(hi - lo == 1) { if(secondMax == lo) { lo = hi; } else hi = lo; break; } int mid = (lo+hi)>>1; out.println("? "+lo+" "+mid); out.flush(); int i1 = sc.nextInt(); int i2 = -1; if(mid+1 < hi) { out.println("? "+(mid+1)+" "+hi); out.flush(); i2 = sc.nextInt(); } if(i1 == secondMax) { hi = mid; } else if(i2 == secondMax) { lo = mid+1; } else { if(secondMax>mid) { hi = mid; secondMax = i1; } else { lo = mid+1; secondMax = i2; } } } out.println("! "+lo); out.flush(); } //<----------------------------------------------WRITE HERE-------------------------------------------> static class Pair implements Comparable<Pair> { int v1; int v2; Pair(int v,int f){ v1 = v; v2 = f; } public int compareTo(Pair p){ return this.v1-p.v1; } } public static void main(String[] args) { out = new PrintWriter(new BufferedOutputStream(System.out)); // int t = sc.nextInt(); int t= 1; while(t-- >0){ solve(); } // Stop writing your solution here. ------------------------------------- out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int[] readIntArray(int n){ int arr[] = new int[n]; for(int i = 0;i<n;i++){ arr[i] = Integer.parseInt(next()); } return arr; } int[] reverse(int arr[]){ int n= arr.length; int i = 0; int j = n-1; while(i<j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; j--;i++; } return arr; } long[] readLongArray(int n){ long arr[] = new long[n]; for(int i = 0;i<n;i++){ arr[i] = Long.parseLong(next()); } return arr; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } private static void sort(int[] arr) {List<Integer> list = new ArrayList<>();for (int i=0; i<arr.length; i++)list.add(arr[i]);Collections.sort(list);for (int i = 0; i < arr.length; i++)arr[i] = list.get(i);} }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
679f8978f063a2086ec0f7bcbf660426
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.math.BigInteger; import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Locale; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.Random; import java.util.Scanner; import java.util.TreeSet; public final class CF_703_D2_C1 { static boolean verb=true; static void log(Object X){if (verb) System.err.println(X);} static void log(Object[] X){if (verb) {for (Object U:X) System.err.print(U+" ");System.err.println("");}} static void log(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");System.err.println("");}} static void log(int[] X,int L){if (verb) {for (int i=0;i<L;i++) System.err.print(X[i]+" ");System.err.println("");}} static void log(long[] X){if (verb) {for (long U:X) System.err.print(U+" ");System.err.println("");}} static void logWln(Object X){if (verb) System.err.print(X);} static void info(Object o){ System.out.println(o);} static void output(Object o){outputWln(""+o+"\n"); } static void outputWln(Object o){try {out.write(""+ o);} catch (Exception e) {}} // Global vars static BufferedWriter out; static InputReader reader; static long powerMod(long b,long e,long m){ long x=1; while (e>0) { if (e%2==1) x=(b*x)%m; b=(b*b)%m; e=e/2; } return x; } static void test() { log("testing"); Random r=new Random(); int NTESTS=1000000000; int NMAX=100; //int MMAX=10000; int VMAX=100; for( int t=0;t<NTESTS;t++) { } log("done"); } static int sign(int x) { if (x>0) return 1; if (x==0) return 0; return -1; } static class Segment implements Comparable<Segment>{ int a; int b; public int compareTo(Segment X) { if (a!=X.a) return a-X.a; if (b!=X.b) return b-X.b; return 0; } public Segment(int a, int b) { this.a = a; this.b = b; } } static int order; static int BYA=0; static int BYB=1; static class Composite implements Comparable<Composite>{ int v; int idx; public int compareTo(Composite X) { if (v!=X.v) return X.v-v; return idx-X.idx; } public Composite(int v, int idx) { this.v = v; this.idx = idx; } public String toString() { return "["+v+","+idx+"]"; } } static class BIT { int[] tree; int N; BIT(int N){ tree=new int[N+1]; this.N=N; } void add(int idx,int val){ idx++; while (idx<=N){ tree[idx]+=val; idx+=idx & (-idx); } } int read(int idx){ idx++; int sum=0; while (idx>0){ sum+=tree[idx]; idx-=idx & (-idx); } return sum; } } static ArrayList<ArrayList<Integer>> generatePermutations(ArrayList<Integer> items){ ArrayList<ArrayList<Integer>> globalRes=new ArrayList<ArrayList<Integer>>(); if (items.size()>1) { for (Integer item:items){ ArrayList<Integer> itemsTmp=new ArrayList<Integer>(items); itemsTmp.remove(item); ArrayList<ArrayList<Integer>> res=generatePermutations(itemsTmp); for (ArrayList<Integer> list:res){ list.add(item); } globalRes.addAll(res); } } else { Integer item=items.get(0); ArrayList<Integer> list=new ArrayList<Integer>(); list.add(item); globalRes.add(list); } return globalRes; } static int ask(int l,int r) throws Exception { System.out.println("? "+(l+1)+" "+(r+1)); System.out.flush(); int x=reader.readInt()-1; return x; } static long mod=1000000007; static int get2ndMax(int[] a,int l,int r) { log("============ "+l+" "+r); Composite[] ar=new Composite[r-l+1]; for (int i=l;i<=r;i++) { ar[i-l]=new Composite(a[i],i); // logWln(a[i]+" "); } //log(""); Arrays.sort(ar); log(ar[1].idx); log("-------"); log("? "+(l+1)+" "+(r+1)+" --->"+(ar[1].idx+1)); return ar[1].idx; } static void testorBasic() { int n=3; //int[] a=new int[] {14,22,26}; int[] a=new int[] {12,1,36}; int l=0; int r=n-1; int x=get2ndMax(a,l,r); while (l+1<r) { int mid=(l+r)/2; if (x>mid) { if (mid+1==r) { // must pick other side r=mid; if (l+1<r) { x=get2ndMax(a,l,r); } } else { int y=get2ndMax(a,mid+1,r); if (y==x) { // good l=mid+1; } else { // bad r=mid; if (l+1<r) x=get2ndMax(a,l,r); } } } else { int y=get2ndMax(a,l,mid); log("case 2"); if (y==x) { r=mid; } else { l=mid+1; if (l+1<r) { log("case 3"); x=get2ndMax(a,l,r); } } } } int ans=0; /* if (x==l) ans=r; else if (x==r) ans=l; else */ { if (l==r) ans=r; else { x=get2ndMax(a,l,r); ans=(r+l)-x; } } int max=-1; int pos=-1; for (int i=0;i<n;i++) { if (a[i]>max) { max=a[i]; pos=i; } } if (pos!=ans) { log("Error"); log(n); log(a); log(ans+" vs "+pos); return; } } static void testor() { log("testoring"); Random rand=new Random(); int NTESTS=10000; int NMAX=4; int VMAX=50; for (int t=0;t<NTESTS;t++) { int n=rand.nextInt(NMAX)+2; HashSet<Integer> hs=new HashSet<Integer>(); int[] a=new int[n]; for (int i=0;i<n;i++) { int x=rand.nextInt(VMAX); while (hs.contains(x)) x=rand.nextInt(VMAX); a[i]=x; hs.add(x); } int l=0; int r=n-1; log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><"); log(n); log(a); int x=get2ndMax(a,l,r); while (l+1<r) { int mid=(l+r)/2; if (x>mid) { if (mid+1<r) { // must pick other side r=mid; if (l+1<r) { x=get2ndMax(a,l,r); } } else { int y=get2ndMax(a,mid+1,r); if (y==x) { // good l=mid+1; } else { // bad r=mid; if (l+1<r) x=x=get2ndMax(a,l,r); } } } else { int y=x=get2ndMax(a,l,r); if (y==x) { r=mid; } else { l=mid+1; if (l+1<r) { x=get2ndMax(a,l,r); } } } } int ans=0; /* if (x==l) ans=r; else if (x==r) ans=l; else */ { if (l==r) ans=r; else { x=get2ndMax(a,l,r); ans=(r+l)-x; } } int max=-1; int pos=-1; for (int i=0;i<n;i++) { if (a[i]>max) { max=a[i]; pos=i; } } if (pos!=ans) { log("Error"); log(n); log(a); log(ans+" vs "+pos); return; } } } static void process() throws Exception { //out = new BufferedWriter(new OutputStreamWriter(System.out)); reader=new InputReader(System.in); Locale.setDefault(Locale.US); //testor(); int n=reader.readInt(); int l=0; int r=n-1; int x=ask(l,r); while (l+1<r) { int mid=(l+r)/2; if (x>mid) { if (mid+1==r) { // must pick other side r=mid; if (l+1<r) { x=ask(l,r); } } else { int y=ask(mid+1,r); if (y==x) { // good l=mid+1; } else { // bad r=mid; if (l+1<r) x=ask(l,r); } } } else { int y=ask(l,mid); log("case 2"); if (y==x) { r=mid; } else { l=mid+1; if (l+1<r) { log("case 3"); x=ask(l,r); } } } } int ans=0; /* if (x==l) ans=r; else if (x==r) ans=l; else */ { if (l==r) ans=r; else { x=ask(l,r); ans=(r+l)-x; } } System.out.println("! "+(ans+1)); System.out.flush(); try { out.close(); } catch (Exception EX){} } public static void main(String[] args) throws Exception { process(); } static final class InputReader { private final InputStream stream; private final byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } private int read() throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read(buf); if (numChars <= 0) { return -1; } } return buf[curChar++]; } public final String readString() throws IOException { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res=new StringBuilder(); do { res.append((char)c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public final int readInt() throws IOException { int c = read(); boolean neg=false; while (isSpaceChar(c)) { c = read(); } char d=(char)c; //log("d:"+d); if (d=='-') { neg=true; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); //log("res:"+res); if (neg) return -res; return res; } public final long readLong() throws IOException { int c = read(); boolean neg=false; while (isSpaceChar(c)) { c = read(); } char d=(char)c; //log("d:"+d); if (d=='-') { neg=true; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); //log("res:"+res); if (neg) return -res; return res; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
7bc230573231f1799971cfc59fb07545
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class Guessing_the_greatest { static StringBuilder sb; static dsu dsu; static long fact[]; static long mod = (int) (1e9); static void solve() { int n = i(); int lo = 1; int hi = n+1; while(lo+1 < hi){ int mid = lo + (hi-lo)/2; int idx = index(lo,hi-1); if(idx < mid){ int left2max = index(lo,mid-1); if(left2max == idx){ hi = mid; }else{ lo = mid; } }else{ int right2max = index(mid,hi-1); if(right2max == idx){ lo = mid; }else{ hi = mid; } } } sb.append("! "+(hi-1)+"\n"); } static int index(int lo,int hi){ if(lo == hi){ return -1; } System.out.println("? "+lo+" "+hi); System.out.flush(); int idx = i(); return idx; } public static void main(String[] args) { sb = new StringBuilder(); int test = 1; // test = i(); while (test-- > 0) { solve(); } System.out.println(sb); } /* * fact=new long[(int)1e6+10]; fact[0]=fact[1]=1; for(int i=2;i<fact.length;i++) * { fact[i]=((long)(i%mod)1L(long)(fact[i-1]%mod))%mod; } */ //**************NCR%P****************** static long ncr(int n, int r) { if (r > n) return (long) 0; long res = fact[n] % mod; // System.out.println(res); res = ((long) (res % mod) * (long) (p(fact[r], mod - 2) % mod)) % mod; res = ((long) (res % mod) * (long) (p(fact[n - r], mod - 2) % mod)) % mod; // System.out.println(res); return res; } static long p(long x, long y)// POWER FXN // { if (y == 0) return 1; long res = 1; while (y > 0) { if (y % 2 == 1) { res = (res * x) % mod; y--; } x = (x * x) % mod; y = y / 2; } return res; } //**************END****************** // *************Disjoint set // union*********// static class dsu { int parent[]; dsu(int n) { parent = new int[n]; for (int i = 0; i < n; i++) parent[i] = -1; } int find(int a) { if (parent[a] < 0) return a; else { int x = find(parent[a]); parent[a] = x; return x; } } void merge(int a, int b) { a = find(a); b = find(b); if (a == b) return; parent[b] = a; } } //**************PRIME FACTORIZE **********************************// static TreeMap<Integer, Integer> prime(long n) { TreeMap<Integer, Integer> h = new TreeMap<>(); long num = n; for (int i = 2; i <= Math.sqrt(num); i++) { if (n % i == 0) { int nt = 0; while (n % i == 0) { n = n / i; nt++; } h.put(i, nt); } } if (n != 1) h.put((int) n, 1); return h; } //****CLASS PAIR ************************************************ static class Pair implements Comparable<Pair> { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair o) { return (int) (this.y - o.y); } } //****CLASS PAIR ************************************************** static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int Int() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String String() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return String(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } static InputReader in = new InputReader(System.in); static OutputWriter out = new OutputWriter(System.out); public static long[] sort(long[] a2) { int n = a2.length; ArrayList<Long> l = new ArrayList<>(); for (long i : a2) l.add(i); Collections.sort(l); for (int i = 0; i < l.size(); i++) a2[i] = l.get(i); return a2; } public static int[] sort(int[] a2) { int n = a2.length; ArrayList<Integer> l = new ArrayList<>(); for (int i : a2) l.add(i); Collections.sort(l); for (int i = 0; i < l.size(); i++) a2[i] = l.get(i); return a2; } public static long pow(long x, long y) { long res = 1; while (y > 0) { if (y % 2 != 0) { res = (res * x);// % modulus; y--; } x = (x * x);// % modulus; y = y / 2; } return res; } //GCD___+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public static long gcd(long x, long y) { if (x == 0) return y; else return gcd(y % x, x); } // ******LOWEST COMMON MULTIPLE // ********************************************* public static long lcm(long x, long y) { return (x * (y / gcd(x, y))); } //INPUT PATTERN******************************************************** public static int i() { return in.Int(); } public static long l() { String s = in.String(); return Long.parseLong(s); } public static String s() { return in.String(); } public static int[] readArrayi(int n) { int A[] = new int[n]; for (int i = 0; i < n; i++) { A[i] = i(); } return A; } public static long[] readArray(long n) { long A[] = new long[(int) n]; for (int i = 0; i < n; i++) { A[i] = l(); } return A; } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
fbeef33793871812d3d4eb50ec31ac8d
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.*; public class Codeforces { public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } } static FastReader f = new FastReader(); static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static StringBuilder sb = new StringBuilder(""); private static int m = (int) 1e9 + 7; static int MAX = 500005; static long[] fact; static int[] inputArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = f.nextInt(); } return a; } private static class SegmentTree { int[] array; int[] tree; int n; int max; SegmentTree(int a[], int n) { this.tree = new int[4 * n + 1]; this.array = a; this.n = n; buildTree(); } void buildTree() { buildTreeHelper(0, 0, n - 1); } void buildTreeHelper(int i, int start, int end) { if (start > end) { return; } if (start == end) { tree[i] = array[start]; return; } int mid = (start + end) / 2; buildTreeHelper(2 * i + 1, start, mid); buildTreeHelper(2 * i + 2, mid + 1, end); tree[i] = Math.max(tree[2 * i + 1] , tree[2 * i + 2]); } char overlap(int start, int end, int qs, int qe) { if (qe < start || qs > end || start > end) { return 0; } if (qs <= start && qe >= end) { return 2; } return 1; } int query(int start, int end) { return andQueryHelper(0, 0, n - 1, start, end); } int andQueryHelper(int i, int start, int end, int qs, int qe) { if (overlap(start, end, qs, qe) == 0) { return 0; } if (overlap(start, end, qs, qe) == 1) { int mid = (start + end) / 2; return Math.max(andQueryHelper(2 * i + 1, start, mid, qs, qe) , andQueryHelper(2 * i + 2, mid + 1, end, qs, qe)); } else { return tree[i]; } } } static int query(int l , int r) { System.out.println("? " + l + " " + r); System.out.flush(); int res = f.nextInt(); System.out.flush(); return res; } public static void main(String[] args) throws IOException { int l = 1, r = f.nextInt(); boolean need = true; int d = 0; while (l < r) { if (r == l + 1) { d = query(l, r); if (d == l) { l = r; } break; } if (need) d = query(l, r); int mid = (l + r) / 2; int x; if (d <= mid) { x = query(l, mid); if (x != d) { need = true; l = mid + 1; } else { need = false; r = mid; } } else { if (mid + 1 == r) { r = mid; need = true; continue; } x = query(mid + 1, r); if (x != d) { r = mid; need = true; } else { l = mid + 1; need = false; } } } System.out.println("! " + l); } } /* 5 2 1 1 1 500 4 217871987498122 10 100000000000000001 1 */
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
b3569ba4ce8fa9371f767a547c7b663a
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.text.DecimalFormat; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; public class Main { static long mod = (long) 1e9 + 7; static long mod1 = 998244353; public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); int n = in.nextInt(); int l = 0; int r = n; while (r-l>1) { int mid = l + (r - l) / 2; int smax = query(l,r-1,out,in); if(smax<mid){ int m=query(l,mid-1,out,in); if(m==smax) r=mid; else l=mid; } else { int m=query(mid,r-1,out,in); if (m==smax) l=mid; else r=mid; } } out.println("! " + r); out.close(); } static int query(int l,int r,PrintWriter out,InputReader in){ if(l>=r) return -1; out.println("? "+(l+1)+" "+(r+1)); out.flush(); return in.nextInt()-1; } static final Random random = new Random(); static void ruffleSort(int[] a) { int n = a.length;//shuffle, then sort for (int i = 0; i < n; i++) { int oi = random.nextInt(n), temp = a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } static long gcd(long x, long y) { if (x == 0) return y; if (y == 0) return x; long r = 0, a, b; a = Math.max(x, y); b = Math.min(x, y); r = b; while (a % b != 0) { r = a % b; a = b; b = r; } return r; } static long modulo(long a, long b, long c) { long x = 1, y = a % c; while (b > 0) { if (b % 2 == 1) x = (x * y) % c; y = (y * y) % c; b = b >> 1; } return x % c; } public static void debug(Object... o) { System.err.println(Arrays.deepToString(o)); } static String printPrecision(double d) { DecimalFormat ft = new DecimalFormat("0.00000000000"); return String.valueOf(ft.format(d)); } static int countBit(long mask) { int ans = 0; while (mask != 0) { mask &= (mask - 1); ans++; } return ans; } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] readArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
56d282c2c34e58d44b6af32ab25907f2
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; public class Codeforces { static Scanner sc; public static void main(String[] args){ sc = new Scanner(System.in); int n = sc.nextInt(); int l = 1, r = n; int temp = get2ndMax(l, r); if(get2ndMax(l, temp) == temp){ l = 1; r = temp; while(l<r){ int mid = (l+r+1)/2; if(get2ndMax(mid, n) == temp) l = mid; else r = mid-1; } System.out.println("! " + l); } else{ l = temp; r = n; while(l < r){ int mid = (l+r)/2; if(get2ndMax(1, mid) == temp) r = mid; else l = mid+1; } System.out.println("! " + l ); } sc.close(); } public static int get2ndMax(int l, int r){ if(l >= r) return -1; System.out.println("? " + l + " " + r); int temp = sc.nextInt(); return temp; } public static void sort2dArray (int[][] arr) { Arrays.sort(arr, (a, b) -> { if (a[0] == b[0]) return a[1] - b[1]; return a[0] - b[0]; }); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
03a1f700dc462f5978560b7c4e46f9df
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.Optional; import java.util.Scanner; public class ProblemC { final Scanner scanner; public static void main(String[] args) { new ProblemC().solve(); } ProblemC() { scanner = new Scanner(System.in); } void solve() { int n = scanner.nextInt(); System.out.printf("! %d", findMax(1, n, null)); scanner.close(); } // sMax can be null int findMax(int l, int r, Integer maybeSMax) { if (l == r) { return l; } int sMax = maybeSMax == null ? getSMax(l, r).get() : maybeSMax; if (l + 1 == r) { return sMax == l ? r : l; } int withSMaxL = l; int withSMaxR = (l + r) / 2; int withoutSMaxL = withSMaxR + 1; int withoutSMaxR = r; if (sMax >= withoutSMaxL && sMax <= withoutSMaxR) { int tempL = withSMaxL; withSMaxL = withoutSMaxL; withoutSMaxL = tempL; int tempR = withSMaxR; withSMaxR = withoutSMaxR; withoutSMaxR = tempR; } Optional<Integer> sMaxOfSMaxNeighborhood = getSMax(withSMaxL, withSMaxR); if (sMaxOfSMaxNeighborhood.isPresent() && sMax == sMaxOfSMaxNeighborhood.get()) { return findMax(withSMaxL, withSMaxR, sMax); } return findMax(withoutSMaxL, withoutSMaxR, null); } Optional<Integer> getSMax(int l, int r) { if (l == r) { return Optional.empty(); } System.out.printf("? %d %d\n\n", l, r); System.out.flush(); return Optional.of(scanner.nextInt()); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
28590d075ecd2ed0d94f5bdd3d16df26
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { static Scanner sc=new Scanner(System.in); static int ask(int l,int r) { if(l==r) return -1; System.out.println("? "+" "+l+" "+r); int x=sc.nextInt(); return x; } public static void main (String[] args) throws java.lang.Exception { // your code goes here int n=sc.nextInt(); int secmaxpos=ask(1,n); if(ask(1,secmaxpos)==secmaxpos) { int low=1; int high=secmaxpos; while(low<high) { int mid=(low+high+1)/2; if(ask(mid,n)==secmaxpos) low=mid; else high=mid-1; } System.out.println("! "+" "+low); } else { int low=secmaxpos; int high=n; while(low<high) { int mid=(low+high)/2; if(ask(1,mid)==secmaxpos) high=mid; else low=mid+1; } System.out.println("! "+" "+low); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
28c080175e0fc58d62d289cef9d25448
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.util.concurrent.LinkedBlockingDeque; import javax.sql.rowset.spi.SyncResolver; import java.io.*; import java.nio.channels.NonReadableChannelException; import java.text.DateFormatSymbols; import static java.lang.System.out; public class CpTemp{ static long a[]; static long b[]; static int count=0; static char c[]; static int dp[]; static int n; static int m; static long h[]; static long p[]; static FastScanner fs = null; static ArrayList<Integer> al[]; static HashMap<Long,Long> hm; public static void main(String[] args) { fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = 1; outer: while (t-- > 0) { int n = fs.nextInt(); int idsm=ask(1, n); int l=1,h=n; while(h>l) { int mid=(l+h)/2; if(idsm<=mid) { int left=ask(l, mid); if(left==(idsm)) { h=mid; } else { l=mid+1; idsm=ask(l, h); } } else { int right=ask(mid+1, h); if(right==(idsm)) { l=mid+1; } else { h=mid; idsm=ask(l, h); } } } if(l==h) { out.println("! "+l); } else out.println("! "+(ask(l, h)==l?h:l)); } out.close(); } public static int ask(int l,int r) { if(l==r)return -1; out.println("? "+l+" "+r); out.flush(); return fs.nextInt(); } static long bit[]; // '1' index based array public static void update(long bit[],int i,int x){ for(;i<bit.length;i+=(i&(-i))){ bit[i] += x; } } public static long sum(int i){ long sum=0; for(;i>0 ;i -= (i&(-i))){ sum += bit[i]; } return sum; } static class Pair implements Comparable<Pair> { long x; int y; Pair(long x, int y) { this.x = x; this.y = y; } public int compareTo(Pair o){ return this.y-o.y; } } static int power(int x, int y, int p) { if (y == 0) return 1; if (x == 0) return 0; int res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long power(long x, long y) { if (y == 0) return 1; if (x == 0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readlongArray(int n){ long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } static boolean prime[]; static void sieveOfEratosthenes(int n) { prime = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } } public static int log2(int x) { return (int) (Math.log(x) / Math.log(2)); } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static long nCk(int n, int k) { long res = 1; for (int i = n - k + 1; i <= n; ++i) res *= i; for (int i = 2; i <= k; ++i) res /= i; return res; } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
c082a59d8269a1b08c908bad3b79e0d3
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class C { static FastScanner sc = new FastScanner(); public static void main(String[] args) { PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); out.println("! " + solve(n)); out.close(); } static int query(int l, int r) { if(l == r) return -1; System.out.println("? " + l + " " + r); System.out.flush(); int q = sc.nextInt(); return q; } static int solve(int n) { int l = 1; int h = n; while(h-l > 1) { int m = l + (h-l)/2; int sg = query(l, h); if(sg < m) { int lsg = query(l, m-1); if(lsg == sg) h = m-1; else l = m; } else { int rsg = query(m, h); if(rsg == sg) l = m; else h = m-1; } } return query(l, h) == l ? h : l; } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); } return null; } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
5321f651e582f83ee7d0fc16b9111a72
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class C { static FastScanner sc = new FastScanner(); public static void main(String[] args) { PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); out.println("! " + solve(n)); out.close(); } static int query(int l, int r) { if(l==r) return -1; System.out.println("? " + l + " " + r); System.out.flush(); int q = sc.nextInt(); return q; } static int solve(int n) { int l = 1; int h = n; while(h-l > 1) { int m = l + (h-l)/2; int sg = query(l, h); if(sg < m) { int lsg = query(l, m-1); if(sg == lsg) h = m - 1; else l = m; } else { int rsg = query(m, h); if(sg == rsg) l = m; else h = m - 1; } } return query(l, h) == l ? h : l; } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); } return null; } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
189622baecb671413ffb4374780993fe
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class C { static FastScanner sc = new FastScanner(); public static void main(String[] args) { PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); out.println("! " + solve(n)); out.close(); } static int query(int l, int r) { if(l == r) return -1; System.out.println("? " + l + " " + r); System.out.flush(); int q = sc.nextInt(); return q; } static int solve(int n) { int l = 1; int h = n; while(h-l > 1) { int m = l + (h-l)/2; int sg = query(l, h); if(sg < m) { int lsg = query(l, m-1); if(sg == lsg) h = m-1; else l = m; } else { int rsg = query(m, h); if(sg == rsg) l = m; else h = m-1; } } return query(l,h) == l ? h : l; } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); } return null; } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
220cd95493f24a2a459f18a1ca05c46f
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class C { static FastScanner sc = new FastScanner(); public static void main(String[] args) { PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); out.println("! " + solve(n)); out.close(); } static int query(int l, int r) { if(l == r) return -1; System.out.println("? " + l + " " + r); System.out.flush(); int q = sc.nextInt(); return q; } static int solve(int n) { int l = 1; int h = n+1; while(h-l > 1) { int m = l + (h-l)/2; int sg = query(l, h-1); if(sg < m) { int lsg = query(l, m-1); if(sg == lsg) h = m; else l = m; } else { int rsg = query(m, h-1); if(sg == rsg) l = m; else h = m; } } return h-1; } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); } return null; } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
3d9573ba85f8dfc82f6eab7aa95ad2c4
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class C { static FastScanner sc = new FastScanner(); public static void main(String[] args) { PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); out.println("! " + solve(n)); out.close(); } static int query(int l, int r) { if(l == r) return -1; System.out.println("? " + l + " " + r); System.out.flush(); int q = sc.nextInt(); return q; } static int solve(int n) { int l = 1; int h = n+1; while(h-l > 1) { int m = l + (h-l)/2; int sg = query(l, h-1); if(sg < m) { int lsg = query(l, m-1); if(sg == lsg) h = m; else l = m; } else { int rsg = query(m, h-1); if(sg == rsg) l = m; else h = m; } } return l; } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); } return null; } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
137f15ee12457e1a09248295cc73e4ae
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.math.BigInteger; //import javafx.util.*; public final class B { static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static ArrayList<ArrayList<Integer>> g; static long mod=1000000007; static int D1[],D2[],par[]; static boolean set[]; static long INF=Long.MAX_VALUE; public static void main(String args[])throws IOException { int N=i(); int index=ask(1,N); if(index==N || index==1) { if(index==1)System.out.println("! "+f2(1,N,index)); else System.out.println("! "+f1(1,N,index)); } else if(ask(1,index)==index) { System.out.println("! "+f1(1,index,index)); } else System.out.println("! "+f2(index,N,index)); } static int f1(int l,int r,int index) { while(r-l>1) { int m=(l+r)/2; if(ask(m,index)==index)l=m; else r=m; } return l; } static int f2(int l,int r,int index) { while(r-l>1) { int m=(l+r)/2; if(ask(index,m)==index)r=m; else l=m; } return r; } static long fact(long N) { long num=1L; while(N>=1) { num=((num%mod)*(N%mod))%mod; N--; } return num; } static boolean reverse(long A[],int l,int r) { while(l<r) { long t=A[l]; A[l]=A[r]; A[r]=t; l++; r--; } if(isSorted(A))return true; else return false; } static boolean isSorted(long A[]) { for(int i=1; i<A.length; i++)if(A[i]<A[i-1])return false; return true; } static boolean isPalindrome(char X[],int l,int r) { while(l<r) { if(X[l]!=X[r])return false; l++; r--; } return true; } static long min(long a,long b,long c) { return Math.min(a, Math.min(c, b)); } static void print(int a) { System.out.println("! "+a); } static int ask(int a,int b) { System.out.println("? "+a+" "+b); return i(); } static int find(int a) { if(par[a]<0)return a; return par[a]=find(par[a]); } static void union(int a,int b) { a=find(a); b=find(b); if(a!=b) { par[a]+=par[b]; //transfers the size par[b]=a; //changes the parent } } static void swap(char A[],int a,int b) { char ch=A[a]; A[a]=A[b]; A[b]=ch; } static void sort(long[] a) //check for long { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void setGraph(int N) { g=new ArrayList<ArrayList<Integer>>(); for(int i=0; i<=N; i++) { g.add(new ArrayList<Integer>()); } } static long pow(long a,long b) { long mod=1000000007; long pow=1; long x=a; while(b!=0) { if((b&1)!=0)pow=(pow*x)%mod; x=(x*x)%mod; b/=2; } return pow; } static long toggleBits(long x)//one's complement || Toggle bits { int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; return ((1<<n)-1)^x; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static boolean isPrime(long N) { if (N<=1) return false; if (N<=3) return true; if (N%2 == 0 || N%3 == 0) return false; for (int i=5; i*i<=N; i=i+6) if (N%i == 0 || N%(i+2) == 0) return false; return true; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } //Debugging Functions Starts static void print(char A[]) { for(char c:A)System.out.print(c+" "); System.out.println(); } static void print(boolean A[]) { for(boolean c:A)System.out.print(c+" "); System.out.println(); } static void print(int A[]) { for(int a:A)System.out.print(a+" "); System.out.println(); } static void print(long A[]) { for(long i:A)System.out.print(i+ " "); System.out.println(); } static void print(ArrayList<Integer> A) { for(int a:A)System.out.print(a+" "); System.out.println(); } //Debugging Functions END //---------------------- //IO FUNCTIONS STARTS static HashMap<Integer,Integer> Hash(int A[]) { HashMap<Integer,Integer> mp=new HashMap<>(); for(int a:A) { int f=mp.getOrDefault(a,0)+1; mp.put(a, f); } return mp; } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] inputLong(int N) { long A[]=new long[N]; for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } //IO FUNCTIONS END } //class pair implements Comparable<pair>{ // int index; long a; // pair(long a,int index) // { // this.a=a; // this.index=index; // } // public int compareTo(pair X) // { // if(this.a>X.a)return 1; // if(this.a==X.a)return this.index-X.index; // return -1; // } //} //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()) { try { st=new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } //gey double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
cc7709722ca9e29d071fdcbec8bd3384
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.math.BigInteger; //import javafx.util.*; public final class B { static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static ArrayList<ArrayList<Integer>> g; static long mod=1000000007; static int D1[],D2[],par[]; static boolean set[]; static long INF=Long.MAX_VALUE; public static void main(String args[])throws IOException { int N=i(); System.out.println("! "+f(1,N)); } static int f(int l,int r) { if(r-l>1) { int index=ask(l,r); // System.out.println("INDEX HAS BEEN--> "+index); int m=(l+r)/2; int a=0; if(index<=m) {a=ask(l,m); if(a==index) return f(l,m); else return f(m,r); } else { a=ask(m, r); if(a!=index) return f(l,m); else return f(m,r); } } if(ask(l,r)==l)return r; else return l; } static long fact(long N) { long num=1L; while(N>=1) { num=((num%mod)*(N%mod))%mod; N--; } return num; } static boolean reverse(long A[],int l,int r) { while(l<r) { long t=A[l]; A[l]=A[r]; A[r]=t; l++; r--; } if(isSorted(A))return true; else return false; } static boolean isSorted(long A[]) { for(int i=1; i<A.length; i++)if(A[i]<A[i-1])return false; return true; } static boolean isPalindrome(char X[],int l,int r) { while(l<r) { if(X[l]!=X[r])return false; l++; r--; } return true; } static long min(long a,long b,long c) { return Math.min(a, Math.min(c, b)); } static void print(int a) { System.out.println("! "+a); } static int ask(int a,int b) { System.out.println("? "+a+" "+b); return i(); } static int find(int a) { if(par[a]<0)return a; return par[a]=find(par[a]); } static void union(int a,int b) { a=find(a); b=find(b); if(a!=b) { par[a]+=par[b]; //transfers the size par[b]=a; //changes the parent } } static void swap(char A[],int a,int b) { char ch=A[a]; A[a]=A[b]; A[b]=ch; } static void sort(long[] a) //check for long { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void setGraph(int N) { g=new ArrayList<ArrayList<Integer>>(); for(int i=0; i<=N; i++) { g.add(new ArrayList<Integer>()); } } static long pow(long a,long b) { long mod=1000000007; long pow=1; long x=a; while(b!=0) { if((b&1)!=0)pow=(pow*x)%mod; x=(x*x)%mod; b/=2; } return pow; } static long toggleBits(long x)//one's complement || Toggle bits { int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; return ((1<<n)-1)^x; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static boolean isPrime(long N) { if (N<=1) return false; if (N<=3) return true; if (N%2 == 0 || N%3 == 0) return false; for (int i=5; i*i<=N; i=i+6) if (N%i == 0 || N%(i+2) == 0) return false; return true; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } //Debugging Functions Starts static void print(char A[]) { for(char c:A)System.out.print(c+" "); System.out.println(); } static void print(boolean A[]) { for(boolean c:A)System.out.print(c+" "); System.out.println(); } static void print(int A[]) { for(int a:A)System.out.print(a+" "); System.out.println(); } static void print(long A[]) { for(long i:A)System.out.print(i+ " "); System.out.println(); } static void print(ArrayList<Integer> A) { for(int a:A)System.out.print(a+" "); System.out.println(); } //Debugging Functions END //---------------------- //IO FUNCTIONS STARTS static HashMap<Integer,Integer> Hash(int A[]) { HashMap<Integer,Integer> mp=new HashMap<>(); for(int a:A) { int f=mp.getOrDefault(a,0)+1; mp.put(a, f); } return mp; } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] inputLong(int N) { long A[]=new long[N]; for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } //IO FUNCTIONS END } //class pair implements Comparable<pair>{ // int index; long a; // pair(long a,int index) // { // this.a=a; // this.index=index; // } // public int compareTo(pair X) // { // if(this.a>X.a)return 1; // if(this.a==X.a)return this.index-X.index; // return -1; // } //} //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()) { try { st=new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } //gey double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
d7266bedb4160c29750536bf04c50a7d
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.*; public class Prb2 { static PrintWriter pw; static Scanner sc; static long mod = 1000000000+7; static void print(final String arg) { pw.write(arg); pw.flush(); } static int ni(){ return sc.nextInt(); } static long nl(){ return sc.nextLong(); } static void runFile() throws Exception { sc = new Scanner(new FileReader("input.txt")); pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); } static void runIo() throws Exception { pw =new PrintWriter(System.out); sc = new Scanner(System.in); } static boolean sv[] = new boolean[1000002]; static void seive() { //true -> not prime // false->prime sv[0] = sv[1] = true; sv[2] = false; for(int i = 0; i< sv.length; i++) { if( !sv[i] && (long)i*(long)i < sv.length ) { for ( int j = i*i; j<sv.length ; j += i ) { sv[j] = true; } } } } static class Pair { int a ,b; Pair(int a, int b){ this.a = a; this.b = b; } } static int query(int l, int r){ if( l >= r ) return -1; print("? " +l+" "+r+"\n"); return ni(); } public static void main(String[] args) throws Exception { // runFile(); runIo(); //int t = sc.nextInt(); int t = 1; while( t-- > 0 ) { int n = ni(); int l = 1 , r = n; while ( l+1 < r ) { int idx = query(l, r); int m = l + (r-l)/2; if( idx <= m) { if( idx == query(l, m) ) r = m; else l = m; } else{ if( idx == query(m, r) ) l = m; else r = m; } } int idx = query(l, r); int ans = l; if( l == idx) ans = r; print("! "+ans) ; } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
98f4c16a56f051873cd745b1837280e9
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; // import java.util.concurrent.CountDownLatch; // import javax.lang.model.element.QualifiedNameable; // import javax.swing.ViewportLayout; // import javax.swing.plaf.basic.BasicTreeUI.TreeCancelEditingAction; // import javax.swing.plaf.metal.MetalComboBoxUI.MetalPropertyChangeListener; // import javax.swing.text.html.HTMLDocument.HTMLReader.CharacterAction; // import org.w3c.dom.Node; import java.lang.*; import java.math.BigInteger; // import java.net.CookieHandler; // import java.security.SecureRandomParameters; // import java.security.cert.CollectionCertStoreParameters; // import java.text.BreakIterator; import java.io.*; @SuppressWarnings("unchecked") public class Main { static FastReader in; static PrintWriter out; static int bit(long n) { return (n == 0) ? 0 : (1 + bit(n & (n - 1))); } static void p(Object o) { out.print(o); } static void pn(Object o) { out.println(o); } static void pni(Object o) { out.println(o); out.flush(); } static String n() throws Exception { return in.next(); } static String nln() throws Exception { return in.nextLine(); } static int ni() throws Exception { return Integer.parseInt(in.next()); } static long nl() throws Exception { return Long.parseLong(in.next()); } static double nd() throws Exception { return Double.parseDouble(in.next()); } static class FastReader { static BufferedReader br; static StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception { br = new BufferedReader(new FileReader(s)); } String next() throws Exception { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception { String str = ""; try { str = br.readLine(); } catch (IOException e) { throw new Exception(e.toString()); } return str; } } static long power(long a, long b) { if (a == 0L) return 0L; if (b == 0) return 1; long val = power(a, b / 2); val = val * val; if ((b % 2) != 0) val = val * a; return val; } static long power(long a, long b, long mod) { if (a == 0L) return 0L; if (b == 0) return 1; long val = power(a, b / 2L, mod) % mod; val = (val * val) % mod; if ((b % 2) != 0) val = (val * a) % mod; return val; } static ArrayList<Long> prime_factors(long n) { ArrayList<Long> ans = new ArrayList<Long>(); while (n % 2 == 0) { ans.add(2L); n /= 2L; } for (long i = 3; i <= Math.sqrt(n); i++) { while (n % i == 0) { ans.add(i); n /= i; } } if (n > 2) { ans.add(n); } return ans; } static void sort(ArrayList<Long> a) { Collections.sort(a); } static void reverse_sort(ArrayList<Long> a) { Collections.sort(a, Collections.reverseOrder()); } static void swap(long[] a, int i, int j) { long temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(List<Long> a, int i, int j) { long temp = a.get(i); a.set(j, a.get(i)); a.set(j, temp); } static void sieve(boolean[] prime) { int n = prime.length - 1; Arrays.fill(prime, true); for (int i = 2; i * i <= n; i++) { if (prime[i]) { for (int j = i * i; j <= n; j += i) { prime[j] = false; } } } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static void sort(long[] arr, int l, int r) { if (l >= r) return; int mid = (l + r) / 2; sort(arr, l, mid); sort(arr, mid + 1, r); merge(arr, l, mid, r); } public static void sort(int[] arr, int l, int r) { if (l >= r) return; int mid = (l + r) / 2; sort(arr, l, mid); sort(arr, mid + 1, r); merge(arr, l, mid, r); } static void merge(int[] arr, int l, int mid, int r) { int[] left = new int[mid - l + 1]; int[] right = new int[r - mid]; for (int i = l; i <= mid; i++) { left[i - l] = arr[i]; } for (int i = mid + 1; i <= r; i++) { right[i - (mid + 1)] = arr[i]; } int left_start = 0; int right_start = 0; int left_length = mid - l + 1; int right_length = r - mid; int temp = l; while (left_start < left_length && right_start < right_length) { if (left[left_start] < right[right_start]) { arr[temp] = left[left_start++]; } else { arr[temp] = right[right_start++]; } temp++; } while (left_start < left_length) { arr[temp++] = left[left_start++]; } while (right_start < right_length) { arr[temp++] = right[right_start++]; } } static void merge(long[] arr, int l, int mid, int r) { long[] left = new long[mid - l + 1]; long[] right = new long[r - mid]; for (int i = l; i <= mid; i++) { left[i - l] = arr[i]; } for (int i = mid + 1; i <= r; i++) { right[i - (mid + 1)] = arr[i]; } int left_start = 0; int right_start = 0; int left_length = mid - l + 1; int right_length = r - mid; int temp = l; while (left_start < left_length && right_start < right_length) { if (left[left_start] < right[right_start]) { arr[temp] = left[left_start++]; } else { arr[temp] = right[right_start++]; } temp++; } while (left_start < left_length) { arr[temp++] = left[left_start++]; } while (right_start < right_length) { arr[temp++] = right[right_start++]; } } static HashMap<Long, Integer> map_prime_factors(long n) { HashMap<Long, Integer> map = new HashMap<>(); while (n % 2 == 0) { map.put(2L, map.getOrDefault(2L, 0) + 1); n /= 2L; } for (long i = 3; i <= Math.sqrt(n); i++) { while (n % i == 0) { map.put(i, map.getOrDefault(i, 0) + 1); n /= i; } } if (n > 2) { map.put(n, map.getOrDefault(n, 0) + 1); } return map; } static long divisor(long n) { long count = 0; for (long i = 1L; i * i <= n; i++) { if (n % i == 0) { if (i == n / i) count += i; else { count += i; count += n / i; } } } return count; } // static void smallest_prime_factor(int n) { // smallest_prime_factor[1] = 1; // for (int i = 2; i <= n; i++) { // if (smallest_prime_factor[i] == 0) { // smallest_prime_factor[i] = i; // for (int j = i * i; j <= n; j += i) { // if (smallest_prime_factor[j] == 0) { // smallest_prime_factor[j] = i; // } // } // } // } // } // static int[] smallest_prime_factor; // static int count = 1; // static int[] p = new int[100002]; // static long[] flat_tree = new long[300002]; // static int[] in_time = new int[1000002]; // static int[] out_time = new int[1000002]; // static long[] subtree_gcd = new long[100002]; // static int w = 0; // static boolean poss = true; /* * (a^b^c)%mod * Using fermats Little theorem * x^(mod-1)=1(mod) * so b^c can be written as b^c=x*(mod-1)+y * then (a^(x*(mod-1)+y))%mod=(a^(x*(mod-1))*a^(y))mod * the term (a^(x*(mod-1)))%mod=a^(mod-1)*a^(mod-1) * */ // ---------------------------------------------------Segment_Tree----------------------------------------------------------------// static class comparator implements Comparator<node> { public int compare(node a, node b) { return a.value - b.value > 0 ? 1 : -1; } } static class Segment_Tree { private long[] segment_tree; public Segment_Tree(int n) { this.segment_tree = new long[4 * n + 1]; } void build(int index, int left, int right, int[] a) { if (left == right) { segment_tree[index] = a[left]; return; } int mid = (left + right) / 2; build(2 * index + 1, left, mid, a); build(2 * index + 2, mid + 1, right, a); segment_tree[index] = segment_tree[2 * index + 1] + segment_tree[2 * index + 2]; } long query(int index, int left, int right, int l, int r) { if (left > right) return 0; if (left >= l && r >= right) { return segment_tree[index]; } if (l > right || left > r) return 0; int mid = (left + right) / 2; return query(2 * index + 1, left, mid, l, r) + query(2 * index + 2, mid + 1, right, l, r); } void update(int index, int left, int right, int node, int val) { if (left == right) { segment_tree[index] += val; return; } int mid = (left + right) / 2; if (node <= mid) update(2 * index + 1, left, mid, node, val); else update(2 * index + 2, mid + 1, right, node, val); segment_tree[index] = segment_tree[2 * index + 1] + segment_tree[2 * index + 2]; } } // ------------------------------------------------------ DSU // --------------------------------------------------------------------// class dsu { private int[] parent; private int[] rank; private int[] size; public dsu(int n) { this.parent = new int[n + 1]; this.rank = new int[n + 1]; this.size = new int[n + 1]; for (int i = 0; i <= n; i++) { parent[i] = i; rank[i] = 1; size[i] = 1; } } int findParent(int a) { if (parent[a] == a) return a; else return parent[a] = findParent(parent[a]); } void join(int a, int b) { int parent_a = findParent(a); int parent_b = findParent(b); if (parent_a == parent_b) return; if (rank[parent_a] > rank[parent_b]) { parent[parent_b] = parent_a; size[parent_a] += size[parent_b]; } else if (rank[parent_a] < rank[parent_b]) { parent[parent_a] = parent_b; size[parent_b] += size[parent_a]; } else { parent[parent_a] = parent_b; size[parent_b] += size[parent_a]; rank[parent_b]++; } } } // ------------------------------------------------Comparable---------------------------------------------------------------------// static class pair implements Comparable<pair> { int a; int b; int dir; public pair(int a, int b, int dir) { this.a = a; this.b = b; this.dir = dir; } public int compareTo(pair p) { // if (this.b == Integer.MIN_VALUE || p.b == Integer.MIN_VALUE) // return (int) (this.index - p.index); return (int) (this.a - p.a); } } static class pair2 implements Comparable<pair2> { long a; int index; public pair2(long a, int index) { this.a = a; this.index = index; } public int compareTo(pair2 p) { return (int) (this.a - p.a); } } static class node { long value; int index; public node(long value, int index) { this.value = value; this.index = index; } } // static int ans = 0; static int ans = 0; static int leaf = 0; static boolean poss = true; static int bottom = Integer.MAX_VALUE; static int right = Integer.MAX_VALUE; static int max_right; static int max_bottom; static long mod = 1000000007L; int glo = 0; static int[] dx = { 0, 0, -1, 1 }; static int[] dy = { 1, -1, 0, 0 }; public static void main(String[] args) throws Exception { in = new FastReader(); out = new PrintWriter(System.out); int tc = 1; while (tc-- > 0) { int n=ni(); pn("? 1 "+n); out.flush(); int second=ni(); if(second!=1)pn("? 1 "+second); if(second!=1)out.flush(); boolean left=((second!=1) &&(ni()==second) )?true:false; if(left){ int l=1; int r=second; while(l+1<r){ int mid=l+(r-l)/2; pn("? "+mid+" "+second); out.flush(); int val=ni(); if(val==second) { l=mid; }else r=mid; } pn("! "+l); }else{ int l=second; int r=n; while(l+1<r){ int mid=l+(r-l)/2; pn("? "+second+" "+mid); out.flush(); int val=ni(); if(val==second) { r=mid; }else l=mid; } pn("! "+r); } } out.flush(); out.close(); } static void dfs(int i, int j, int[][] c, boolean[][] visited) { int n = c.length; visited[i][j] = true; for (int k = 0; k < 4; k++) { int a = i + dx[k]; int b = j + dy[k]; if (a >= 0 && b >= 0 && a < n && b < n && !visited[a][b]) { dfs(a, b, c, visited); } } } static void factorial(long[] fact, long[] fact_inv, int n, long mod) { fact[0] = 1; for (int i = 1; i < n; i++) { fact[i] = (i * fact[i - 1]) % mod; } for (int i = 0; i < n; i++) { fact_inv[i] = power(fact[i], mod - 2, mod);// (1/x)%m can be calculated by fermat's little theoram which is // (x**(m-2))%m when m is prime } //(a^(b^c))%m is equal to, let res=(b^c)%(m-1) then (a^res)%m //https://www.geeksforgeeks.org/find-power-power-mod-prime/?ref=rp } static void find(int i, int n, int[] row, int[] col, int[] d1, int[] d2) { if (i >= n) { ans++; return; } for (int j = 0; j < n; j++) { if (col[j] == 0 && d1[i - j + n - 1] == 0 && d2[i + j] == 0) { col[j] = 1; d1[i - j + n - 1] = 1; d2[i + j] = 1; find(i + 1, n, row, col, d1, d2); col[j] = 0; d1[i - j + n - 1] = 0; d2[i + j] = 0; } } } static int answer(int l, int r, int[][] dp) { if (l > r) return 0; if (l == r) { dp[l][r] = 1; return 1; } if (dp[l][r] != -1) return dp[l][r]; int val = Integer.MIN_VALUE; int mid = l + (r - l) / 2; val = 1 + Math.max(answer(l, mid - 1, dp), answer(mid + 1, r, dp)); return dp[l][r] = val; } static TreeSet<Integer> ans(int n) { TreeSet<Integer> set = new TreeSet<>(); for (int i = 1; i * i <= n; i++) { if (n % i == 0) { set.add(i); set.add(n / i); } } set.remove(1); return set; } // static long find(String s, int i, int n, long[] dp) { // // pn(i); // if (i >= n) // return 1L; // if (s.charAt(i) == '0') // return 0; // if (i == n - 1) // return 1L; // if (dp[i] != -1) // return dp[i]; // if (s.substring(i, i + 2).equals("10") || s.substring(i, i + 2).equals("20")) // { // return dp[i] = (find(s, i + 2, n, dp)) % mod; // } // if ((s.charAt(i) == '1' || (s.charAt(i) == '2' && s.charAt(i + 1) - '0' <= // 6)) // && ((i + 2 < n ? (s.charAt(i + 2) != '0' ? true : false) : (i + 2 == n ? true // : false)))) { // return dp[i] = (find(s, i + 1, n, dp) + find(s, i + 2, n, dp)) % mod; // } else // return dp[i] = (find(s, i + 1, n, dp)) % mod; static void print(int[] a) { for (int i = 0; i < a.length; i++) p(a[i] + " "); pn(""); } static long count(long n) { long count = 0; while (n != 0) { count += n % 10; n /= 10; } return count; } static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static int LcsOfPrefix(String a, String b) { int i = 0; int j = 0; int count = 0; while (i < a.length() && j < b.length()) { if (a.charAt(i) == b.charAt(j)) { j++; count++; } i++; } return a.length() + b.length() - 2 * count; } static void reverse(int[] a, int n) { for (int i = 0; i < n / 2; i++) { int temp = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = temp; } } static char get_char(int a) { return (char) (a + 'a'); } static int find1(int[] a, int val) { int ans = -1; int l = 0; int r = a.length - 1; while (l <= r) { int mid = l + (r - l) / 2; if (a[mid] <= val) { l = mid + 1; ans = mid; } else r = mid - 1; } return ans; } static int find2(int[] a, int val) { int l = 0; int r = a.length - 1; int ans = -1; while (l <= r) { int mid = l + (r - l) / 2; if (a[mid] <= val) { ans = mid; l = mid + 1; } else r = mid - 1; } return ans; } // static void dfs(List<List<Integer>> arr, int node, int parent, long[] val) { // p[node] = parent; // in_time[node] = count; // flat_tree[count] = val[node]; // subtree_gcd[node] = val[node]; // count++; // for (int adj : arr.get(node)) { // if (adj == parent) // continue; // dfs(arr, adj, node, val); // subtree_gcd[node] = gcd(subtree_gcd[adj], subtree_gcd[node]); // } // out_time[node] = count; // flat_tree[count] = val[node]; // count++; // } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
db67b69317cb3e0ed78fc2fc26f55bc4
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.util.PriorityQueue; import java.util.StringTokenizer; public class C { public static void main(String[] args){ MyScanner sc = new MyScanner(); PrintWriter printWriter = new PrintWriter(System.out); int l = 1; int n = sc.nextInt(); int r = n; HashMap<String,Integer> mp = new HashMap<>(); while(r-l>=2){ int mid = l + (r-l)/2; int secondHalfGlobal; printWriter.println("? "+l+" "+r); printWriter.flush(); secondHalfGlobal = sc.nextInt(); if(r==l+2){ if(secondHalfGlobal == l){ l++; continue; }else if(secondHalfGlobal == r){ r--; continue; } } int secondHalf1; if(secondHalfGlobal > mid){ printWriter.println("? "+(mid+1)+" "+r); printWriter.flush(); secondHalf1 = sc.nextInt(); if(secondHalf1 == secondHalfGlobal){ l = mid+1; }else{ r = mid; } }else{ printWriter.println("? "+l+" "+mid); printWriter.flush(); secondHalf1 = sc.nextInt(); if(secondHalf1 == secondHalfGlobal){ r = mid; }else{ l = mid+1; } } } if(l==r){ printWriter.println("! "+l); printWriter.flush(); } if(l==r-1){ int res; printWriter.println("? "+l+" "+r); printWriter.flush(); res = sc.nextInt(); if(res == l){ printWriter.println("! "+r); printWriter.flush(); }else{ printWriter.println("! "+l); printWriter.flush(); } } } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
481740478a374bd2a0b53cdb5c38236a
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.util.PriorityQueue; import java.util.StringTokenizer; public class C { public static void main(String[] args){ MyScanner sc = new MyScanner(); PrintWriter printWriter = new PrintWriter(System.out); int l = 1; int n = sc.nextInt(); int r = n; while(true){ if(l==r){ printWriter.println("! "+l); printWriter.flush(); break; }else if(l==r-1){ int res; printWriter.println("? "+l+" "+r); printWriter.flush(); res = sc.nextInt(); if(res == l){ printWriter.println("! "+r); printWriter.flush(); }else{ printWriter.println("! "+l); printWriter.flush(); } break; }else{ int mid = (r+l)/2; int secondHalfGlobal; printWriter.println("? "+l+" "+r); printWriter.flush(); secondHalfGlobal = sc.nextInt(); if(r==l+2){ if(secondHalfGlobal == l){ l++; continue; }else if(secondHalfGlobal == r){ r--; continue; } } int secondHalf1; if(secondHalfGlobal > mid){ printWriter.println("? "+mid+" "+r); printWriter.flush(); secondHalf1 = sc.nextInt(); if(secondHalf1 == secondHalfGlobal){ l = mid; }else{ r = mid; } }else{ printWriter.println("? "+l+" "+mid); printWriter.flush(); secondHalf1 = sc.nextInt(); if(secondHalf1 == secondHalfGlobal){ r = mid; }else{ l = mid; } } } } } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
d2e8401f92468bb925f588a1e69ab1ee
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.util.PriorityQueue; import java.util.StringTokenizer; public class C { public static void main(String[] args){ MyScanner sc = new MyScanner(); PrintWriter printWriter = new PrintWriter(System.out); int l = 1; int n = sc.nextInt(); int r = n; while(true){ if(l==r){ printWriter.println("! "+l); printWriter.flush(); break; }else if(l==r-1){ int res; printWriter.println("? "+l+" "+r); printWriter.flush(); res = sc.nextInt(); if(res == l){ printWriter.println("! "+r); printWriter.flush(); }else{ printWriter.println("! "+l); printWriter.flush(); } break; }else{ int mid = (r+l)/2; int secondHalfGlobal; printWriter.println("? "+l+" "+r); printWriter.flush(); secondHalfGlobal = sc.nextInt(); int secondHalf1; if(secondHalfGlobal > mid){ printWriter.println("? "+mid+" "+r); printWriter.flush(); secondHalf1 = sc.nextInt(); if(secondHalf1 == secondHalfGlobal){ l = mid; }else{ r = mid; } }else{ printWriter.println("? "+l+" "+mid); printWriter.flush(); secondHalf1 = sc.nextInt(); if(secondHalf1 == secondHalfGlobal){ r = mid; }else{ l = mid; } } } } } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
76d445053b02f16ac0440c38850b0ec1
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.util.PriorityQueue; import java.util.StringTokenizer; public class C { public static void main(String[] args){ MyScanner sc = new MyScanner(); PrintWriter printWriter = new PrintWriter(System.out); int l = 1; int n = sc.nextInt(); int r = n; while(true){ if(l==r){ printWriter.println("! "+l); printWriter.flush(); break; }else if(l==r-1){ int res; printWriter.println("? "+l+" "+r); printWriter.flush(); res = sc.nextInt(); if(res == l){ printWriter.println("! "+r); printWriter.flush(); }else{ printWriter.println("! "+l); printWriter.flush(); } break; }else{ int mid = l + (r-l)/2; int secondHalfGlobal; printWriter.println("? "+l+" "+r); printWriter.flush(); secondHalfGlobal = sc.nextInt(); int secondHalf1; if(secondHalfGlobal > mid){ printWriter.println("? "+mid+" "+r); printWriter.flush(); secondHalf1 = sc.nextInt(); if(secondHalf1 == secondHalfGlobal){ l = mid; }else{ r = mid; } }else{ printWriter.println("? "+l+" "+mid); printWriter.flush(); secondHalf1 = sc.nextInt(); if(secondHalf1 == secondHalfGlobal){ r = mid; }else{ l = mid; } } } } } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
e876677f41943a39447700a0fc2791db
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.*; public class C_1_Guessing_the_Greatest_easy_version{ static Scanner sc = new Scanner(System.in); // @Harshit Maurya public static void reverse(int[] arr, int l, int r) { int d = (r - l + 1) / 2; for (int i = 0; i < d; i++) { int t = arr[l + i]; arr[l + i] = arr[r - i]; arr[r - i] = t; } } // QUICK MATHS private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } public static void main(String[] args) { if (System.getProperty("ONLINE_JUDGE") == null) { try { System.setOut(new PrintStream( new FileOutputStream("convention.out"))); sc = new Scanner(new File("convention.in")); } catch (Exception e) { } } solve(); } // BIT MAGIC public int getMSB(int b) { return (int)(Math.log(b) / Math.log(2)); } //HELPER FUNCTIONS private static int[] nextIntArray(int n){ int arr[]=new int[n]; for(int i=0; i<n; i++){ arr[i]=sc.nextInt(); } return arr; } private static int[][] nextInt2DArray(int m,int n){ int arr[][]=new int[m][n]; for(int i=0; i<m; i++){ for(int j=0; j<n; j++){ arr[i][j]=sc.nextInt(); } } return arr; } private static long[] nextLongArray(int n){ long arr[]=new long[n]; for(int i=0; i<n; i++){ arr[i]=sc.nextLong(); } return arr; } private static double[] nextDoubleArray(int n){ double arr[]=new double[n]; for(int i=0; i<n; i++){ arr[i]=sc.nextDouble(); } return arr; } static int[] copy(int A[]) { int B[]=new int[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static long[] copy(long A[]) { long B[]=new long[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static void print(int A[]) { for(int i : A) { System.out.print(i+" "); } System.out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static void solve() { int r=sc.nextInt(); int l=1; while(l<r){ int second_last1=query(l, r); int mid=l+(r-l)/2; if(second_last1<=mid){ if(query(l, mid)==second_last1) r=mid; else l=mid+1; }else{ if(query((mid+1), r)==second_last1) l=mid+1; else r=mid; } } System.out.println("! "+l); } public static int query(int l,int r) { if(l==r)return -1; System.out.flush(); System.out.println("? "+l+" "+r); System.out.flush(); return sc.nextInt(); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
be8bed18486a21a00fb540c004c5a8a4
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.StringTokenizer; import static java.lang.Double.parseDouble; import static java.lang.Integer.compare; import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.System.in; public class Main { private static final int MOD = (int) (1E9 + 7); static FastScanner scanner = new FastScanner(in); public static void main(String[] args) throws IOException { // Write your solution here int t = 1; //t = parseInt(scanner.nextLine()); while (t-- > 0) { solve(); } } private static void solve() throws IOException { int n = scanner.nextInt(); int secMax = query(1, n); int ans = 0; if (query(secMax, n) == secMax) { int l = secMax + 1, r = n; while (l <= r) { int mid = (l + r) / 2; if (query(secMax, mid) == secMax) { ans = mid; r = mid - 1; } else { l = mid + 1; } } } else { int l = 1, r = secMax - 1; while (l <= r) { int mid = (l + r) / 2; if (query(mid, secMax) == secMax) { ans = mid; l = mid + 1; } else { r = mid - 1; } } } System.out.println("! " + ans); } private static int query(int l, int h) { if(l == h) return -1; System.out.println("? " + l + " " + h); System.out.flush(); return scanner.nextInt(); } private static class Pair implements Comparable<Pair> { int index, value; public Pair(int index, int value) { this.index = index; this.value = value; } public int compareTo(Pair o) { if (value != o.value) return compare(value, o.value); return compare(index, o.index); } @Override public String toString() { return "Pair{" + "index=" + index + ", value=" + value + '}'; } } static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return parseInt(next()); } long nextLong() { return parseLong(next()); } double nextDouble() { return parseDouble(next()); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
0f51081858c985ac1c8bd1a8b925d769
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; public class cf1{ public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static long d[]; public static int ar[]; public static void main(String args[]){ FastReader sc=new FastReader(); StringBuilder sb=new StringBuilder(); int n=sc.nextInt(); int a=0; int l=1; int r=n; boolean flag=true; while(true){ if(r-l<=1){break;} if(flag){ System.out.println("? "+l+" "+r); a=sc.nextInt();flag=false;} if(a>=l && a<=(r+l+1)/2){ System.out.println("? "+l+" "+(r+l+1)/2); int x=sc.nextInt(); if(a==x){r=(r+l+1)/2;} if(a!=x ){l=(r+l+1)/2;flag=true;} }else{ if(a<=r && a>(r+l+1)/2){ System.out.println("? "+(r+l+1)/2+" "+r); int x=sc.nextInt(); if(a==x){l=(r+l+1)/2;} if(a!=x ){r=(r+l+1)/2;flag=true;} } } } System.out.println("? "+l+" "+r); a=sc.nextInt(); if(l==a){System.out.println("! "+r);} else{System.out.println("! "+l);} System.out.flush(); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
616741c0b2fa549dbb503e547a5bf4db
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; public class cf1{ public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static long d[]; public static int ar[]; public static void main(String args[]){ FastReader sc=new FastReader(); StringBuilder sb=new StringBuilder(); int n=sc.nextInt(); int a=0; int l=1; int r=n; boolean flag=true; while(true){ if(r-l<=1){break;} if(flag){ System.out.println("? "+l+" "+r); a=sc.nextInt();flag=false;} if(a>=l && a<=(r+l+1)/2){ System.out.println("? "+l+" "+(r+l+1)/2); int x=sc.nextInt(); if(a==x){r=(r+l+1)/2;} if(a!=x ){l=(r+l+1)/2;flag=true;} }else{ if(a<=r && a>(r+l+1)/2){ System.out.println("? "+(r+l+1)/2+" "+r); int x=sc.nextInt(); if(a==x){l=(r+l+1)/2;} if(a!=x ){r=(r+l+1)/2;flag=true;} } } } System.out.println("? "+l+" "+r); a=sc.nextInt(); if(l==a){System.out.println("! "+r);} else{System.out.println("! "+l);} System.out.flush(); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
6b0c5aeba6db9d66b1a6f75f472abdb8
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; public class cf1{ public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static long d[]; public static int ar[]; public static void main(String args[]){ FastReader sc=new FastReader(); StringBuilder sb=new StringBuilder(); int n=sc.nextInt(); int a=0; int l=1; int r=n; boolean flag=true; while(true){ if(r-l<=1){break;} if(flag){ System.out.println("? "+l+" "+r); a=sc.nextInt();flag=false;} if(a>=l && a<=(r+l+1)/2){ System.out.println("? "+l+" "+(r+l+1)/2); int x=sc.nextInt(); if(a==x){r=(r+l+1)/2;} if(a!=x ){l=(r+l+1)/2;flag=true;} }else{ if(a<=r && a>(r+l+1)/2){ System.out.println("? "+(r+l+1)/2+" "+r); int x=sc.nextInt(); if(a==x){l=(r+l+1)/2;} if(a!=x ){r=(r+l+1)/2;flag=true;} } } } System.out.println("? "+l+" "+r); a=sc.nextInt(); if(l==a){System.out.println("! "+r);} else{System.out.println("! "+l);} System.out.flush(); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
1639efb32aa6bc62e4047f6ccfda6382
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; public class cf1{ public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static long d[]; public static int ar[]; public static void main(String args[]){ FastReader sc=new FastReader(); StringBuilder sb=new StringBuilder(); int n=sc.nextInt(); int a=0; int l=1; int r=n; boolean flag=true; while(true){ if(r-l<=1){break;} System.out.println("? "+l+" "+r); a=sc.nextInt(); if(a>=l && a<=(r+l+1)/2){ System.out.println("? "+l+" "+(r+l+1)/2); int x=sc.nextInt(); if(a==x){r=(r+l+1)/2;} if(a!=x ){l=(r+l+1)/2;} }else{ if(a<=r && a>(r+l+1)/2){ System.out.println("? "+(r+l+1)/2+" "+r); int x=sc.nextInt(); if(a==x){l=(r+l+1)/2;} if(a!=x ){r=(r+l+1)/2;} } } } System.out.println("? "+l+" "+r); a=sc.nextInt(); if(l==a){System.out.println("! "+r);} else{System.out.println("! "+l);} System.out.flush(); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
9073d6c95fcdd73deff69f0b357c550e
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; public class cf1{ public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static long d[]; public static int ar[]; public static void main(String args[]){ FastReader sc=new FastReader(); StringBuilder sb=new StringBuilder(); int n=sc.nextInt(); int a=0; int l=1; int r=n; boolean flag=true; while(true){ if(r-l<=1){break;} System.out.println("? "+l+" "+r); a=sc.nextInt(); if(a>=l && a<=(r+l+1)/2){ System.out.println("? "+l+" "+(r+l+1)/2); int x=sc.nextInt(); if(a==x){r=(r+l+1)/2;} if(a!=x ){l=(r+l+1)/2;} }else{ if(a<=r && a>(r+l+1)/2){ System.out.println("? "+(r+l+1)/2+" "+r); int x=sc.nextInt(); if(a==x){l=(r+l+1)/2;} if(a!=x ){r=(r+l+1)/2;} } } } System.out.println("? "+l+" "+r); a=sc.nextInt(); if(l==a){System.out.println("! "+r);} else{System.out.println("! "+l);} System.out.flush(); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
befc3f962731aeaf8dcd25171f8c69b5
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class Main { static Scanner sc; static PrintWriter pw; public static int query(int l, int r) throws IOException { pw.println("? " + l + " " + r); pw.flush(); return sc.nextInt(); } public static void main(String[] args) throws IOException { sc = new Scanner(System.in); pw = new PrintWriter(System.out); int n = sc.nextInt(); int l = 1, r = n; while (r - l > 1) { int mid = l + r >> 1; int x = query(l, r); if (x < mid) { int y = query(l, mid); if (y == x) { r = mid; } else { l = mid + 1; } } else { int y = query(mid, r); if (y == x) { l = mid; } else { r = mid - 1; } } } if (l != r) { int x = query(l, r); if (r == x) r = l; } pw.println("! " + r); pw.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String readAllLines(BufferedReader reader) throws IOException { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line); content.append(System.lineSeparator()); } return content.toString(); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String[] nextArray(int n) throws IOException { String[] a = new String[n]; for (int i = 0; i < n; i++) a[i] = next(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
f7c66260e2138120653719b205335c53
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class Main { static Scanner sc; static PrintWriter pw; public static int query(int l, int r) throws IOException { pw.println("? " + l + " " + r); pw.flush(); return sc.nextInt(); } public static void main(String[] args) throws IOException { sc = new Scanner(System.in); pw = new PrintWriter(System.out); int n = sc.nextInt(); int l = 1, r = n; while (r - l > 1) { int mid = l + r >> 1; int x = query(l, r); if (x < mid) { int y = query(l, mid); if (y == x) { r = mid; } else { l = mid; } } else { int y = query(mid, r); if (y == x) { l = mid; } else { r = mid - 1; } } } if (l != r) { int x = query(l, r); if (r == x) r = l; } pw.println("! " + r); pw.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String readAllLines(BufferedReader reader) throws IOException { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line); content.append(System.lineSeparator()); } return content.toString(); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String[] nextArray(int n) throws IOException { String[] a = new String[n]; for (int i = 0; i < n; i++) a[i] = next(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
1c4a9ccef680ebb9214860a068280d83
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class Main { static Scanner sc; static PrintWriter pw; public static int query(int l, int r) throws IOException { pw.println("? " + l + " " + r); pw.flush(); return sc.nextInt(); } public static void main(String[] args) throws IOException { sc = new Scanner(System.in); pw = new PrintWriter(System.out); int n = sc.nextInt(); int l = 1, r = n; while (l != r - 1) { int mid = l + r >> 1; int x = query(l, r); if (x < mid) { int y = query(l, mid); if (y == x) { r = mid; } else { l = mid; } } else { int y = query(mid, r); if (y == x) { l = mid; } else { r = mid; } } } int x = query(l, r); pw.println("! " + (x == r ? l : r)); pw.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String readAllLines(BufferedReader reader) throws IOException { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line); content.append(System.lineSeparator()); } return content.toString(); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String[] nextArray(int n) throws IOException { String[] a = new String[n]; for (int i = 0; i < n; i++) a[i] = next(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
7ebf6f1a131b47d90d040388246eece6
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Practice { public static void main(String[] args) throws Exception { Scanner s = new Scanner(System.in); int n = s.nextInt(); System.out.flush(); getAns(n, s); } private static void getAns(int n, Scanner s) { // TODO Auto-generated method stub int sec=call(0,n-1,s); if(sec==0||call(0,sec,s)!=sec) { int l=sec,r=n-1; while(r-l>1) { int m=(l+r)/2; if(call(sec,m,s)!=sec) { l=m; }else { r=m; } }System.out.println("! "+(r+1)); }else { int l=0,r=sec; while(r-l>1) { int m=(l+r)/2; if(call(m,sec,s)!=sec) { r=m; }else { l=m; } }System.out.println("! "+(l+1)); } } private static int call(int l, int r,Scanner s) { // TODO Auto-generated method stub System.out.println("? "+(l+1)+" "+(r+1)); int sec=s.nextInt(); System.out.flush(); return sec-1; } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
05bf15e4fa17c100f78b210634611b8c
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { /* HashMap<> map=new HashMap<>(); TreeMap<> map=new TreeMap<>(); map.put(p,map.getOrDefault(p,0)+1); for(Map.Entry<> mx:map.entrySet()){ int v=mx.getValue(),k=mx.getKey(); } ArrayList<Pair<Character,Integer>> l=new ArrayList<>(); ArrayList<> l=new ArrayList<>(); HashSet<> has=new HashSet<>();*/ PrintWriter out; FastReader sc; int mod=(int)(1e9+7); int maxint= Integer.MAX_VALUE; int minint= Integer.MIN_VALUE; long maxlong=Long.MAX_VALUE; long minlong=Long.MIN_VALUE; public void sol(){ int testCase=1; while(testCase-->0){ int n=ni(); int l=1,r=n; int p=0,a=0; boolean f=true,x=true,y=true; while(r-l>1){ pl("? "+l+" "+r); out.flush(); p=ni(); int mid=l+(r-l)/2; if(p<=mid){ pl("? "+l+" "+mid); out.flush(); a=ni(); if(a==p){ r=mid; }else{ l=mid+1; } }else{ pl("? "+mid+" "+r); out.flush(); a=ni(); if(a==p){ l=mid; }else{ r=mid-1; } } }if(r>l){ pl("? "+l+" "+r); out.flush(); a=ni(); if(a==r){ pl("! "+l); out.flush(); }else{ pl("! "+r); out.flush(); } }else { pl("! "+l); out.flush(); } } } public static void main(String[] args) { Main g=new Main(); g.out=new PrintWriter(System.out); g.sc=new FastReader(); g.sol(); g.out.flush(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public int ni(){ return sc.nextInt(); }public long nl(){ return sc.nextLong(); }public double nd(){ return sc.nextDouble(); }public char[] rl(){ return sc.nextLine().toCharArray(); }public String rl1(){ return sc.nextLine(); } public void pl(Object s){ out.println(s); }public void ex(){ out.println(); } public void pr(Object s){ out.print(s); }public String next(){ return sc.next(); }public long abs(long x){ return Math.abs(x); } public int abs(int x){ return Math.abs(x); } public double abs(double x){ return Math.abs(x); } public long pow(long x,long y){ return (long)Math.pow(x,y); } public int pow(int x,int y){ return (int)Math.pow(x,y); } public double pow(double x,double y){ return Math.pow(x,y); }public long min(long x,long y){ return (long)Math.min(x,y); } public int min(int x,int y){ return (int)Math.min(x,y); } public double min(double x,double y){ return Math.min(x,y); }public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); }static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }void sort(double[] a) { ArrayList<Double> l = new ArrayList<>(); for (double i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }int swap(int a,int b){ return a; }long swap(long a,long b){ return a; }double swap(double a,double b){ return a; } boolean isPowerOfTwo (int x) { return x!=0 && ((x&(x-1)) == 0); }boolean isPowerOfTwo (long x) { return x!=0 && ((x&(x-1)) == 0); }public long max(long x,long y){ return (long)Math.max(x,y); } public int max(int x,int y){ return (int)Math.max(x,y); } public double max(double x,double y){ return Math.max(x,y); }long sqrt(long x){ return (long)Math.sqrt(x); }int sqrt(int x){ return (int)Math.sqrt(x); }void input(int[] ar,int n){ for(int i=0;i<n;i++)ar[i]=ni(); }void input(long[] ar,int n){ for(int i=0;i<n;i++)ar[i]=nl(); }void fill(int[] ar,int k){ Arrays.fill(ar,k); }void yes(){ pl("YES"); }void no(){ pl("NO"); } int[] sieve(int n) { boolean prime[] = new boolean[n+1]; int[] k=new int[n+1]; for(int i=0;i<=n;i++) { prime[i] = true; k[i]=i; } for(int p = 2; p <=n; p++) { if(prime[p] == true) { // sieve[p]=p; for(int i = p*2; i <= n; i += p) { prime[i] = false; // sieve[i]=p; while(k[i]%(p*p)==0){ k[i]/=(p*p); } } } }return k; }int strSmall(int[] arr, int target) { int start = 0, end = arr.length-1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr[mid] >= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } int strSmall(ArrayList<Integer> arr, int target) { int start = 0, end = arr.size()-1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr.get(mid) > target) { start = mid + 1; ans=start; } else { end = mid - 1; } } return ans; } // public static class Pair implements Comparable<Pair> { // int x; // int y; // public Pair(int x, int y) { // this.x = x; // this.y = y; // } // public String toString() { // return x + "," + y; // } // public boolean equals(Object o) { // if (o instanceof Pair) { // Pair p = (Pair) o; // return p.x == x && p.y == y; // } // return false; // } // public int hashCode() { // return new Double(x).hashCode() * 31 + new Double(y).hashCode(); // } // public int compareTo(Pair other) { // if (this.x == other.x) { // return Long.compare(this.y, other.y); // } // return Long.compare(this.x, other.x); // } // } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
56252476080ee72608a1c15e9a5f389b
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; import javafx.util.Pair; public class Main { public static void main(String[] args) { FastScanner input =new FastScanner(); int n = input.nextInt(); int l = 1; int r = n; while(l<r) { int mid = (l+r)/2; int now = query(l, r); if(now<=mid) { if(query(l, mid)==now) { r = mid; }else { l = mid+1; } } else { if(query(mid+1, r)==now) { l = mid+1; } else r = mid; } } System.out.println("! "+l); } public static int query(int l , int r) { FastScanner input = new FastScanner(); if(l==r) return -1; System.out.println("? "+l+" "+r); return input.nextInt(); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
0ba9388cce931473f5e8d09d52a87cb3
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; import javafx.util.Pair; public class Main { public static void main(String[] args) { FastScanner input = new FastScanner(); int n = input.nextInt(); int r = n; int l = 1; System.out.println("? " + l + " " + r); int smax = input.nextInt(); while (l <= r) { if (r - l == 1) { System.out.println("? " + l + " " + r); int notans = input.nextInt(); if (notans == l) { System.out.println("! " + r); } else { System.out.println("! " + l); } return; } int mid = (l + r) / 2; if (smax < mid) { int nowsmax = -1; if (l < mid - 1) { System.out.println("? " + l + " " + (mid - 1)); nowsmax = input.nextInt(); } if (nowsmax == smax) { l = l; r = mid - 1; smax = nowsmax; } else { if(mid==r) { System.out.println("! "+r); return; } System.out.println("? " + mid + " " + r); smax = input.nextInt(); l = mid; r = r; if (r - l == 1) { if (smax == l) { System.out.println("! " + r); } else { System.out.println("! " + l); } return; } } } else { int nowsmax = -1; if (mid < r) { System.out.println("? " + mid + " " + r); nowsmax = input.nextInt(); } if (nowsmax == smax) { l = mid; r = r; smax = nowsmax; } else { if(l==mid-1) { System.out.println("! "+l); return; } System.out.println("? " + l + " " + (mid - 1)); smax = input.nextInt(); l = l; r = mid - 1; if (r - l == 1) { if (smax == l) { System.out.println("! " + r); } else { System.out.println("! " + l); } return; } } } } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
ab667e4e4fefb40c32e3aaaa8da2d309
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Scanner; public class Main { static Scanner in = new Scanner(System.in); // static Scanner in = new Scanner( new File("javain.txt")); public static void main(String[] args) throws FileNotFoundException { if(args.length > 0){ //in = new Scanner( new File("javain.txt")); } int t = 1; for(int i = 0; i < t; i++){ solve(); } } public static void solve(){ int n = in.nextInt(); int l = 1, r = n; System.out.println("? " + l + " " + r + "\n"); System.out.flush(); int second = in.nextInt(); while(l < r - 1){ int mid = (l + r) / 2; if(mid >= second){ System.out.println("? " + l + " " + mid + "\n"); System.out.flush(); int t = in.nextInt(); if(t == second){ r = mid; }else{ l = mid + 1; if(l < r){ System.out.println("? " + l + " " + r + "\n"); System.out.flush(); second = in.nextInt(); } } }else{ System.out.println("? " + mid + " " + r + "\n"); System.out.flush(); int t = in.nextInt(); if(t == second){ l = mid; }else{ r = mid - 1; if(l < r ){ System.out.println("? " + l + " " + r + "\n"); System.out.flush(); second = in.nextInt(); } } } } //System.out.println("? " + l + " " + r + "\n"); //System.out.flush(); if(l == r || second == l){ System.out.println("! " + r + "\n"); }else{ System.out.println("! " + l + "\n"); } System.out.flush(); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
dc226d060e0217c6d52073c8ddbfa850
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Scanner; public class Main { static Scanner in = new Scanner(System.in); // static Scanner in = new Scanner( new File("javain.txt")); public static void main(String[] args) throws FileNotFoundException { if(args.length > 0){ //in = new Scanner( new File("javain.txt")); } int t = 1; for(int i = 0; i < t; i++){ solve(); } } public static void solve(){ int n = in.nextInt(); int l = 1, r = n; System.out.println("? " + l + " " + r + "\n"); System.out.flush(); int second = in.nextInt(); while(l < r - 1){ int mid = (l + r) / 2; if(mid >= second){ System.out.println("? " + l + " " + mid + "\n"); System.out.flush(); int t = in.nextInt(); if(t == second){ r = mid; }else{ l = mid; if(l < r){ System.out.println("? " + l + " " + r + "\n"); System.out.flush(); second = in.nextInt(); } } }else{ System.out.println("? " + mid + " " + r + "\n"); System.out.flush(); int t = in.nextInt(); if(t == second){ l = mid; }else{ r = mid; if(l < r ){ System.out.println("? " + l + " " + r + "\n"); System.out.flush(); second = in.nextInt(); } } } } //System.out.println("? " + l + " " + r + "\n"); //System.out.flush(); if(l == r || second == l){ System.out.println("! " + r + "\n"); }else{ System.out.println("! " + l + "\n"); } System.out.flush(); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
0b0d4e22bec990825782353f35efda73
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class C { static int ans = Integer.MAX_VALUE; static String str = ""; static int[] dp; public static void main(String[] args) throws IOException { FastScanner fs=new FastScanner(); PrintWriter out = new PrintWriter(System.out); // int T = 1; // int T=fs.nextInt(); // for (int tt=0; tt<T; tt++) { // } int n = fs.nextInt(); System.out.println("? 1 "+n); System.out.flush(); int last = fs.nextInt(); if (n==2) { if (last==1) { System.out.println("! 2"); } else { System.out.println("! 1"); } } else { int cur = 0; if (last==1) { cur = last; } else { System.out.println("? 1 "+last); System.out.flush(); cur = fs.nextInt(); } if (cur==last && cur!=1) { int left = 1, right = cur-1, ans=-1; while (left<=right) { int mid = (left+right)/2; System.out.println("? "+mid+" "+last); System.out.flush(); int res = fs.nextInt(); if (res==last) { ans=mid; left=mid+1; } else { right=mid-1; } } System.out.println("! "+ans); } else { int left = last+1, right = n, ans=-1; while (left<=right) { int mid = (left+right)/2; System.out.println("? "+last+" "+mid); System.out.flush(); int res = fs.nextInt(); if (res==last) { ans=mid; right=mid-1; } else { left=mid+1; } } System.out.println("! "+ans); } } } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
da8de0750f708ddd464cba52c7a1ce40
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.math.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; //--------------->>>>IF YOU ARE HERE FOR QUICKSORT HACK THEN SORRY NO HACK FOR YOU<<<------------------- public class a{ static long[] count,count1,count2; static boolean[] prime; static Node[] nodes,nodes1,nodes2; static long[] arr; static long[][] cost; static int[] arrInt,darrInt,farrInt; static long[][] dp; static char[] ch,ch1; static long[] darr,farr; static long[][] mat,mat1; static boolean[] vis; static long x,h; static long maxl; static double dec; static long mx = (long)1e7; static long inf = (long)1e15; static String s,s1,s2,s3,s4; static long minl; static long mod = ((long)(1e9))+7; // static int minl = -1; // static long n; static int n,n1,n2,q,r1,c1,r2,c2; static long a; static long b; static long c; static long d; static long y,z; static int m; static int ans; static long k; static FastScanner sc; static String[] str,str1; static Set<Integer> set,set1,set2; static SortedSet<Long> ss; static List<Long> list,list1,list2,list3; static PriorityQueue<Node> pq,pq1; static LinkedList<Node> ll,ll1,ll2; static Map<Integer,List<Integer>> map1; static Map<Long,Integer> map; static StringBuilder sb,sb1,sb2; static int index; static long[] sum; static int[] dx = {0,-1,0,1,-1,1,-1,1}; static int[] dy = {-1,0,1,0,-1,-1,1,1}; // public static void solve(){ // FastScanner sc = new FastScanner(); // // int t = sc.nextInt(); // int t = 1; // for(int tt = 0 ; tt < t ; tt++){ // // s = sc.next(); // // s1 = sc.next(); // n = sc.nextInt(); // m = sc.nextInt(); // // int k = sc.nextInt(); // // sb = new StringBuilder(); // map = new HashMap<>(); // map1 = new HashMap<>(); // // q = sc.nextInt(); // // sb = new StringBuilder(); // // long k = sc.nextLong(); // // ch = sc.next().toCharArray(); // // count = new int[200002]; // // m = sc.nextInt(); // for(int o = 0 ; o < m ; o++){ // int l = sc.nextInt()-1; // int r = sc.nextInt()-1; // } // } // } //--------------->>>>IF YOU ARE HERE FOR QUICKSORT HACK THEN SORRY NO HACK FOR YOU<<<------------------- public static void solve(){ int l = 1; int r = n; while(l < r){ int mid = (l+r)/2; // System.out.println(l+" "+r); System.out.println("?"+ " " + l + " "+ r); System.out.flush(); int index = sc.nextInt(); int index1 = -1; if(index >= l && index <= mid){ if(l == mid){ l = mid+1; continue; } System.out.println("?"+ " " + l + " "+ mid); System.out.flush(); index1 = sc.nextInt(); if(index == index1) r = mid; else l = mid+1; } else{ if(mid+1 == r){ r = mid; continue; } System.out.println("?"+ " " + (mid+1) + " "+ r); System.out.flush(); index1 = sc.nextInt(); if(index == index1) l = mid+1; else r = mid; } } System.out.println("! "+r); } public static void main(String[] args) { sc = new FastScanner(); // Scanner sc = new Scanner(System.in); // int t = sc.nextInt(); int t = 1; // int l = 1; while(t > 0){ // n = sc.nextInt(); // n = sc.nextLong(); // k = sc.nextLong(); // x = sc.nextLong(); // y = sc.nextLong(); // z = sc.nextLong(); // a = sc.nextLong(); // b = sc.nextLong(); // c = sc.nextLong(); // d = sc.nextLong(); // x = sc.nextLong(); // y = sc.nextLong(); // z = sc.nextLong(); // d = sc.nextLong(); n = sc.nextInt(); // n1 = sc.nextInt(); // m = sc.nextInt(); // q = sc.nextInt(); // k = sc.nextLong(); // x = sc.nextLong(); // d = sc.nextLong(); // s = sc.next(); // ch = sc.next().toCharArray(); // ch1 = sc.next().toCharArray(); // n = 6; // arr = new long[n]; // for(int i = 0 ; i < n ; i++){ // arr[i] = sc.nextLong(); // } // arrInt = new int[n]; // for(int i = 0 ; i < n ; i++){ // arrInt[i] = sc.nextInt(); // } // x = sc.nextLong(); // y = sc.nextLong(); // ch = sc.next().toCharArray(); // m = n; // m = sc.nextInt(); // darr = new long[m]; // for(int i = 0 ; i < m ; i++){ // darr[i] = sc.nextLong(); // } // k = sc.nextLong(); // m = n; // darrInt = new int[n]; // for(int i = 0 ; i < n ; i++){ // darrInt[i] = sc.nextInt(); // } // farrInt = new int[m]; // for(int i = 0; i < m ; i++){ // farrInt[i] = sc.nextInt(); // } // m = n; // mat = new long[n][m]; // for(int i = 0 ; i < n ; i++){ // for(int j = 0 ; j < m ; j++){ // mat[i][j] = sc.nextLong(); // } // } // m = n; // mat = new char[n][m]; // for(int i = 0 ; i < n ; i++){ // String s = sc.next(); // for(int j = 0 ; j < m ; j++){ // mat[i][j] = s.charAt(j); // } // } // str = new String[n]; // for(int i = 0 ; i < n ; i++) // str[i] = sc.next(); // nodes = new Node[n]; // for(int i = 0 ; i < n ;i++) // nodes[i] = new Node(sc.nextLong(),sc.nextLong()); solve(); t -= 1; } } public static int log(long n,long base){ if(n == 0 || n == 1) return 0; if(n == base) return 1; double num = Math.log(n); double den = Math.log(base); if(den == 0) return 0; return (int)(num/den); } public static boolean isPrime(long n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n%2 == 0 || n%3 == 0) return false; for (int i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false; return true; } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static long lcm(long a, long b) { return (b/gcd(b, a % b)) * a; } public static long mod_inverse(long a,long mod){ long x1=1,x2=0; long p=mod,q,t; while(a%p!=0){ q = a/p; t = x1-q*x2; x1=x2; x2=t; t=a%p; a=p; p=t; } return x2<0 ? x2+mod : x2; } public static void swap(long[] curr,int i,int j){ long temp = curr[j]; curr[j] = curr[i]; curr[i] = temp; } static final Random random=new Random(); static void ruffleSortLong(long[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); long temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static void ruffleSortInt(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); int temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static void ruffleSortChar(char[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); char temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static long binomialCoeff(long n, long k){ long res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1] for (long i = 0; i < k; ++i) { res = (res*(n - i)); res = (res/(i + 1)); } return res; } static long[] fact; public static long inv(long n){ return power(n, mod-2); } public static void fact(int n){ fact = new long[n+1]; fact[0] = 1; for(int j = 1;j<=n;j++) fact[j] = (fact[j-1]*(long)j)%mod; } public static long binom(int n, int k){ fact(n+1); long prod = fact[n]; prod*=inv(fact[n-k]); prod%=mod; prod*=inv(fact[k]); prod%=mod; return prod; } static long power(long x, long y){ if (y == 0) return 1; if (y%2 == 1) return (x*power(x, y-1))%mod; return power((x*x)%mod, y/2)%mod; } static void sieve(int n){ prime = new boolean[n+1]; for(int i=2;i<n;i++) prime[i] = true; for(int p = 2; p*p <=n; p++) { if(prime[p] == true) { for(int i = p*p; i <= n; i += p) prime[i] = false; } } } static class Node{ long first; long second; Node(long f,long s){ this.first = f; this.second = s; } } static long sq(long num){ return num*num; } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
672f8411a4982a1d1fc50d04430ef232
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.io.PrintStream; //import java.util.*; public class Solution { public static final boolean LOCAL = System.getProperty("ONLINE_JUDGE")==null; static class FastScanner { BufferedReader br; StringTokenizer st ; FastScanner(){ br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } FastScanner(String file) { try { br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); st = new StringTokenizer(""); } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("file not found"); e.printStackTrace(); } } String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } String readLine() throws IOException{ return br.readLine(); } } static class Pair<T,X>{ T first; X second; Pair(T first,X second){ this.first = first; this.second = second; } @Override public int hashCode(){ return Objects.hash(first,second); } @Override public boolean equals(Object obj){ return obj.hashCode() == this.hashCode(); } } static PrintStream debug = null; static long mod = (long)(Math.pow(10,9) + 7); public static void main(String[] args) throws Exception { FastScanner s = new FastScanner(); if(LOCAL){ // s = new FastScanner("src/input.txt"); // PrintStream o = new PrintStream("src/sampleout.txt"); // debug = new PrintStream("src/debug.txt"); // System.setOut(o); } long mod = 1000000007; int tcr = 1;//s.nextInt(); //StringBuilder sb = new StringBuilder(); for(int tc=0;tc<tcr;tc++){ int n = s.nextInt(); int st = 0; int e = n-1; while(st != e){ println("? "+(st+1)+" "+(e+1)); int index = s.nextInt(); index--; int mid = (st + e)/2; if((e - st + 1) == 3){ if(index == e){ println("? "+(st+1)+" "+(st+2)); int smax = s.nextInt(); smax--; st = (smax == (st)?(st+1):(st)); break; }else if(index == st){ println("? "+(st+2)+" "+(st+3)); int smax = s.nextInt(); smax--; st = (smax == (st+1)?(st+2):(st+1)); break; }else{ println("? "+(st+1)+" "+(st+2)); int smax = s.nextInt(); smax--; if(smax == index){ break; }else{ st = st + 2; break; } } }else if((e - st + 1) == 2){ println("? "+(st+1)+" "+(st+2)); int smax = s.nextInt(); smax--; if(smax == st){ st = st+1;break; } else{ break; } } if(index <= mid){ println("? "+(st+1)+" "+(mid+1)); int smax = s.nextInt(); smax--; if(smax == index){ e = mid; }else{ st = mid + 1; } }else{ println("? "+(mid+2)+" "+(e+1)); int smax = s.nextInt(); smax--; if(smax == index){ st = mid + 1; }else{ e = mid; } } } println("! "+(st+1)); } //print(sb.toString()); } static int len(long num){ return Long.toString(num).length(); } static long mulmod(long a, long b,long mod) { long ans = 0l; while(b > 0){ long curr = (b & 1l); if(curr == 1l){ ans = ((ans % mod) + a) % mod; } a = (a + a) % mod; b = b >> 1; } return ans; } public static void dbg(PrintStream ps,Object... o) throws Exception{ if(ps == null){ return; } Debug.dbg(ps,o); } public static long modpow(long num,long pow,long mod){ long val = num; long ans = 1l; while(pow > 0l){ long bit = pow & 1l; if(bit == 1){ ans = (ans * (val%mod))%mod; } val = (val * val) % mod; pow = pow >> 1; } return ans; } public static char get(int n){ return (char)('a' + n); } public static long[] sort(long arr[]){ List<Long> list = new ArrayList<>(); for(long n : arr){list.add(n);} Collections.sort(list); for(int i=0;i<arr.length;i++){ arr[i] = list.get(i); } return arr; } public static int[] sort(int arr[]){ List<Integer> list = new ArrayList<>(); for(int n : arr){list.add(n);} Collections.sort(list); for(int i=0;i<arr.length;i++){ arr[i] = list.get(i); } return arr; } // return the (index + 1) // where index is the pos of just smaller element // i.e count of elemets strictly less than num public static int justSmaller(long arr[],long num){ // System.out.println(num+"@"); int st = 0; int e = arr.length - 1; int ans = -1; while(st <= e){ int mid = (st + e)/2; if(arr[mid] >= num){ e = mid - 1; }else{ ans = mid; st = mid + 1; } } return ans + 1; } public static int justSmaller(int arr[],int num){ // System.out.println(num+"@"); int st = 0; int e = arr.length - 1; int ans = -1; while(st <= e){ int mid = (st + e)/2; if(arr[mid] >= num){ e = mid - 1; }else{ ans = mid; st = mid + 1; } } return ans + 1; } //return (index of just greater element) //count of elements smaller than or equal to num public static int justGreater(long arr[],long num){ int st = 0; int e = arr.length - 1; int ans = arr.length; while(st <= e){ int mid = (st + e)/2; if(arr[mid] <= num){ st = mid + 1; }else{ ans = mid; e = mid - 1; } } return ans; } public static int justGreater(int arr[],int num){ int st = 0; int e = arr.length - 1; int ans = arr.length; while(st <= e){ int mid = (st + e)/2; if(arr[mid] <= num){ st = mid + 1; }else{ ans = mid; e = mid - 1; } } return ans; } public static void println(Object obj){ System.out.println(obj.toString()); } public static void print(Object obj){ System.out.print(obj.toString()); } public static int gcd(int a,int b){ if(b == 0){return a;} return gcd(b,a%b); } public static long gcd(long a,long b){ if(b == 0l){ return a; } return gcd(b,a%b); } public static int find(int parent[],int v){ if(parent[v] == v){ return v; } return parent[v] = find(parent, parent[v]); } public static List<Integer> sieve(){ List<Integer> prime = new ArrayList<>(); int arr[] = new int[100001]; Arrays.fill(arr,1); arr[1] = 0; arr[2] = 1; for(int i=2;i<=100000;i++){ if(arr[i] == 1){ prime.add(i); for(long j = (i*1l*i);j<100001;j+=i){ arr[(int)j] = 0; } } } return prime; } static boolean isPower(long n,long a){ long log = (long)(Math.log(n)/Math.log(a)); long power = (long)Math.pow(a,log); if(power == n){return true;} return false; } private static int mergeAndCount(int[] arr, int l,int m, int r) { // Left subarray int[] left = Arrays.copyOfRange(arr, l, m + 1); // Right subarray int[] right = Arrays.copyOfRange(arr, m + 1, r + 1); int i = 0, j = 0, k = l, swaps = 0; while (i < left.length && j < right.length) { if (left[i] <= right[j]) arr[k++] = left[i++]; else { arr[k++] = right[j++]; swaps += (m + 1) - (l + i); } } while (i < left.length) arr[k++] = left[i++]; while (j < right.length) arr[k++] = right[j++]; return swaps; } // Merge sort function private static int mergeSortAndCount(int[] arr, int l,int r) { // Keeps track of the inversion count at a // particular node of the recursion tree int count = 0; if (l < r) { int m = (l + r) / 2; // Total inversion count = left subarray count // + right subarray count + merge count // Left subarray count count += mergeSortAndCount(arr, l, m); // Right subarray count count += mergeSortAndCount(arr, m + 1, r); // Merge count count += mergeAndCount(arr, l, m, r); } return count; } static class Debug{ //change to System.getProperty("ONLINE_JUDGE")==null; for CodeForces public static final boolean LOCAL = System.getProperty("ONLINE_JUDGE")==null; private static <T> String ts(T t) { if(t==null) { return "null"; } try { return ts((Iterable) t); }catch(ClassCastException e) { if(t instanceof int[]) { String s = Arrays.toString((int[]) t); return "{"+s.substring(1, s.length()-1)+"}\n"; }else if(t instanceof long[]) { String s = Arrays.toString((long[]) t); return "{"+s.substring(1, s.length()-1)+"}\n"; }else if(t instanceof char[]) { String s = Arrays.toString((char[]) t); return "{"+s.substring(1, s.length()-1)+"}\n"; }else if(t instanceof double[]) { String s = Arrays.toString((double[]) t); return "{"+s.substring(1, s.length()-1)+"}\n"; }else if(t instanceof boolean[]) { String s = Arrays.toString((boolean[]) t); return "{"+s.substring(1, s.length()-1)+"}\n"; } try { return ts((Object[]) t); }catch(ClassCastException e1) { return t.toString(); } } } private static <T> String ts(T[] arr) { StringBuilder ret = new StringBuilder(); ret.append("{"); boolean first = true; for(T t: arr) { if(!first) { ret.append(", "); } first = false; ret.append(ts(t)); } ret.append("}"); return ret.toString(); } private static <T> String ts(Iterable<T> iter) { StringBuilder ret = new StringBuilder(); ret.append("{"); boolean first = true; for(T t: iter) { if(!first) { ret.append(", "); } first = false; ret.append(ts(t)); } ret.append("}"); return ret.toString(); } public static void dbg(PrintStream ps,Object... o) throws Exception { if(LOCAL) { System.setErr(ps); System.err.print("Line #"+Thread.currentThread().getStackTrace()[2].getLineNumber()+": ["); for(int i = 0; i<o.length; i++) { if(i!=0) { System.err.print(", "); } System.err.print(ts(o[i])); } System.err.println("]"); } } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
e865e7cef2fbf7dd26030098b4eec7db
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
//WHEN IN DOUBT , USE BRUTE FORCE !!!!!!!!! import java.lang.String; import java.io.*; import java.util.*; import java.util.StringTokenizer; //class Main //AtCoder() //class Solution // Codechef public class Solution2 //Codeforces { static Scanner sc=new Scanner(System.in); public static void main(String args[]) { try { //sc=new Scanner(System.in); int TT = 1;//sc.nextInt(); for(int hehe=1 ; hehe <= TT ; hehe++){ int N=sc.nextInt(); int l=0,r=N-1; while(l+1<r){ int mid=l + (r-l)/2; int secPos = ask(l,r); if(secPos > mid){ int secPosInt=ask(mid , r); if(secPosInt == secPos){ l=mid; } else{ r=mid; } } else{ int secPosInt=ask(l , mid); if(secPosInt == secPos){ r=mid; } else{ l=mid; } } } int a=ask(l,r); System.out.println("! "+(1+(l+r)-a)); System.out.flush(); } //out.flush(); }catch (Exception e) { sopln(e.toString()); } } private static int ask(int l, int r) { System.out.println("? "+(l+1)+" "+(r+1)); System.out.flush(); int ret=sc.nextInt(); return ret-1; } public final static int d = 256; static int MOD = 1000000007; static final double PI = Math.PI; private static BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); private static PrintWriter out = new PrintWriter (new OutputStreamWriter (System.out)); static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } char nextChar() { try { return (char) (br.read()); }catch (IOException e){ return '~'; } } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void sop(Object o){System.out.print(o);} static double ceil(double d){ return Math.ceil(d); } static double floor(double d){ return Math.floor(d); } static double round(double d){ return Math.round(d); } static void sopln(Object o){System.out.println(o);} static void printArray(int[] arr){ for(int i=0;i<arr.length;i++){ sop(arr[i]+" "); } sopln(""); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
e607cd3f3aa0303a64a5ad9512d2651f
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; /** * * @Har_Har_Mahadev */ public class C { private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353; private static int N = 0; public static void process() throws IOException { int n = sc.nextInt(); int smax = ask(1, n); int ans = 0; if(smax == ask(1, smax)) { int L = 1, U = smax - 1; while(L <= U){ int m = (L + U) / 2; if(ask(m,smax) == smax){ ans = m; L = m + 1; } else{ U = m - 1; } } } else { int L = smax + 1, U = n; while(L <= U){ int m = (L + U) / 2; if(ask(smax,m) == smax){ ans = m; U = m - 1; } else{ L = m + 1; } } } pp(ans); } private static void pp(int i) { println("! "+i); } private static int ask(int i, int n) throws IOException { if(i == n)return -1; pflush("? "+i+" "+n); int val = sc.nextInt(); return val; } //============================================================================= //--------------------------The End--------------------------------- //============================================================================= static FastScanner sc; static PrintWriter out; public static void main(String[] args) throws IOException { boolean oj = true; if (oj) { sc = new FastScanner(); out = new PrintWriter(System.out); } else { sc = new FastScanner(100); out = new PrintWriter("output.txt"); } int t = 1; // t = sc.nextInt(); while (t-- > 0) { process(); } out.flush(); out.close(); } static class Pair implements Comparable<Pair> { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { return Integer.compare(this.x, o.x); } // @Override // public boolean equals(Object o) { // if (this == o) return true; // if (!(o instanceof Pair)) return false; // Pair key = (Pair) o; // return x == key.x && y == key.y; // } // // @Override // public int hashCode() { // int result = x; // result = 31 * result + y; // return result; // } } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static void println(Object o) { out.println(o); } static void println() { out.println(); } static void print(Object o) { out.print(o); } static void pflush(Object o) { out.println(o); out.flush(); } static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } static int max(int x, int y) { return Math.max(x, y); } static int min(int x, int y) { return Math.min(x, y); } static int abs(int x) { return Math.abs(x); } static long abs(long x) { return Math.abs(x); } static int log2(int N) { int result = (int) (Math.log(N) / Math.log(2)); return result; } static long max(long x, long y) { return Math.max(x, y); } static long min(long x, long y) { return Math.min(x, y); } public static int gcd(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } public static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } public static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static int lcm(int a, int b) { return (a * b) / gcd(a, b); } public static int lower_bound(int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) high = mid; else low = mid + 1; } return low; } public static int upper_bound(int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > x) high = mid; else low = mid + 1; } return low; } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() throws FileNotFoundException { br = new BufferedReader(new InputStreamReader(System.in)); } FastScanner(int a) throws FileNotFoundException { br = new BufferedReader(new FileReader("input.txt")); } String next() throws IOException { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) throws IOException { int[] A = new int[n]; for (int i = 0; i != n; i++) { A[i] = sc.nextInt(); } return A; } long[] readArrayLong(int n) throws IOException { long[] A = new long[n]; for (int i = 0; i != n; i++) { A[i] = sc.nextLong(); } return A; } } static void ruffleSort(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
2c1e73f2840518f375fbcadfb3f3b613
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
/******************************************************************************* * author : dante1 * created : 18/02/2021 23:12 *******************************************************************************/ import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; // import java.math.BigInteger; import java.util.*; public class c1 { public static void main(String[] args) { int n = in.nextInt(), t = query(1, n), max_idx = 0; if (t == query(1, t)) { int lo = 1, hi = t-1; while (lo <= hi) { int mid = (lo + hi) >> 1; if (t == query(mid, t)) { max_idx = mid; lo = mid+1; } else { hi = mid-1; } } } else { int lo = t+1, hi = n; while (lo <= hi) { int mid = (lo + hi) >> 1; if (t == query(t, mid)) { max_idx = mid; hi = mid-1; } else { lo = mid+1; } } } out.println("! " + max_idx); out.flush(); } static int query(int l, int r) { if (l == r) { return 0; } out.println("? " + l + " " + r); out.flush(); return in.nextInt(); } // Handle I/O static int MOD = (int) (1e9 + 7); static FastScanner in = new FastScanner(); static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); st = null; } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readIntArray(int size) { int[] arr = new int[size]; for (int i = 0; i < size; i++) { arr[i] = nextInt(); } return arr; } long nextLong() { return Long.parseLong(next()); } long[] readLongArray(int size) { long[] arr = new long[size]; for (int i = 0; i < size; i++) { arr[i] = nextLong(); } return arr; } double nextDouble() { return Double.parseDouble(next()); } double[] readDoubleArray(int size) { double[] arr = new double[size]; for (int i = 0; i < size; i++) { arr[i] = nextDouble(); } return arr; } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
19050d3e17280145a1cfc73a684650a1
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Random; import java.util.StringTokenizer; public class TaskC { static long mod = (long) (1000000000+7); public static void main(String[] args) { // TODO Auto-generated method stub FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); int test = 1; while(test-->0) { int n = sc.nextInt(); out.println("? "+ 1 + " " + n); out.flush(); int maxpos = sc.nextInt(); int currpos = 0; if(n==2) { out.print(maxpos== 1 ? "! "+2 : "! "+1); continue; } if(maxpos!=1) { out.println("? "+ 1 + " " + maxpos); out.flush(); currpos = sc.nextInt(); } else { currpos = -1; } if(currpos==maxpos) { int k = 0; for(int b = maxpos/2;b>=1;b/=2) { if(k+b<maxpos && isAns(k+b, maxpos, maxpos, out, sc))k+=b; } while(k+1<maxpos && isAns(k+1, maxpos, maxpos, out, sc))k++; out.println("! "+k); } else { int k = maxpos; for(int b = (n-maxpos+1)/2;b>=1;b/=2) { if(k+b<=n && !isAns(maxpos, k+b, maxpos, out, sc))k+=b; } while(k+1<=n && !isAns(maxpos, k+1, maxpos, out, sc))k++; k++; out.println("! "+k); } } out.close(); } static boolean isAns(int l, int r, int maxpos, PrintWriter out, FastReader sc) { out.println("?"+" "+(l)+" "+r); out.flush(); int t = sc.nextInt(); return t==maxpos; } static void shuffleArray(long[] arr){ int n = arr.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = arr[i]; int randomPos = i + rnd.nextInt(n-i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static class FastReader { StringTokenizer st; BufferedReader br; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String s = ""; while(st==null || st.hasMoreElements()) { try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } } return s; } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
b4e155002977230d7f26ca5fbb56d02c
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Random; import java.util.StringTokenizer; public class TaskC { static long mod = (long) (1000000000+7); public static void main(String[] args) { // TODO Auto-generated method stub FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); int test = 1; while(test-->0) { int n = sc.nextInt(); out.println("? "+ 1 + " " + n); out.flush(); int maxpos = sc.nextInt(); int currpos = 0; if(n==2) { out.print(maxpos== 1 ? "! "+2 : "! "+1); continue; } if(maxpos!=1) { out.println("? "+ 1 + " " + maxpos); out.flush(); currpos = sc.nextInt(); } else { currpos = -1; } if(currpos==maxpos) { int k = 0; for(int b = maxpos/2;b>=1;b/=2) { while(k+b<maxpos && isAns(k+b, maxpos, maxpos, out, sc))k+=b; } out.println("! "+k); } else { int k = maxpos; for(int b = (n-maxpos+1)/2;b>=1;b/=2) { while(k+b<=n && !isAns(maxpos, k+b, maxpos, out, sc))k+=b; } k++; out.println("! "+k); } } out.close(); } static boolean isAns(int l, int r, int maxpos, PrintWriter out, FastReader sc) { out.println("?"+" "+(l)+" "+r); out.flush(); int t = sc.nextInt(); return t==maxpos; } static void shuffleArray(long[] arr){ int n = arr.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = arr[i]; int randomPos = i + rnd.nextInt(n-i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static class FastReader { StringTokenizer st; BufferedReader br; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String s = ""; while(st==null || st.hasMoreElements()) { try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } } return s; } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
f21b5bf97755fc753957af4d93061736
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.lang.reflect.Array; import java.text.DecimalFormat; import java.util.*; import java.io.*; public class Equal { static class pair implements Comparable<pair> { int x; int y; public pair(int u, int v) { this.x = u; this.y = v; } @Override public int compareTo(pair o) { return o.x-x; } } static int[][]mem=new int[1001][1001]; // static int dp(int n,int k){ // if(n>=mem.length) // return 0; // if(k<0||k>=mem.length) // return 0; // if(mem[n][k]!=-1) // return mem[n][k]; // } static String timeInc(String time,int h,int m){ String []s =time.split(":"); int i=0; int hh=0; int mm=0; while (i<s[0].length()&&s[0].charAt(i)=='0'){ i++; } if(i<s[0].length()) hh=Integer.parseInt(s[0].substring(i)); i=0; while (i<s[1].length()&&s[1].charAt(i)=='0'){ i++; } if(i<s[1].length()) mm=Integer.parseInt(s[1].substring(i)); mm++; if(mm%m==0){ mm=0; hh++; if(hh%h==0) hh=0; } String hs=""+hh; while(hs.length()<2) hs="0"+hs; String ms=""+mm; while (ms.length()<2) ms="0"+ms; return hs+":"+ms; } static boolean validMirror(String s,int h,int m){ HashSet<Character>hs=new HashSet<>(); hs.add('3'); hs.add('4'); hs.add('6'); hs.add('7'); hs.add('9'); for (int i = 0; i <s.length() ; i++) { if(hs.contains(s.charAt(i))){ return false; } } String mirrored=""; for (int i = s.length()-1; i >=0 ; i--) { if(s.charAt(i)=='5'){ mirrored+="2"; continue; } if(s.charAt(i)=='2'){ mirrored+="5"; continue; } mirrored+=s.charAt(i); } int i=0; int hh=0; int mm=0; String []s2 =mirrored.split(":"); while (i<s2[0].length()&&s2[0].charAt(i)=='0'){ i++; } if(i<s2[0].length()) hh=Integer.parseInt(s2[0].substring(i)); i=0; while (i<s2[1].length()&&s2[1].charAt(i)=='0'){ i++; } if(i<s2[1].length()) mm=Integer.parseInt(s2[1].substring(i)); if(hh>=h||mm>=m) return false; return true; } public static void main(String[] args) throws IOException { //BufferedReader br = new BufferedReader(new FileReader("name.in")); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; PrintWriter out = new PrintWriter(System.out); int n=Integer.parseInt(br.readLine()); boolean found=false; int p=-1; int q=0; System.out.println("? 1 "+n); int second=Integer.parseInt(br.readLine()); int beg=-1; int end=-1; if(second>1&&second!=n) { System.out.println("? 1 " + second); int before = Integer.parseInt(br.readLine()); if (before == second) { beg = 1; end = second; } else { beg = second; end = n; } } else{ beg=1; end=n; } while(!found&&q<40){ int mid=(beg+end)>>1; if(end-beg==1){ if(end<=second) mid=end; else mid=beg; if(end==second||beg==second){ found=true; p=end+beg-second; break; } } if(mid>second){ System.out.println("? "+second+" "+mid); } else { System.out.println("? "+mid+" "+second); } int x=Integer.parseInt(br.readLine()); if(end-beg==1){ found=true; if(x==second) p=mid; else { if(end<=second) p=beg; else p=end; } } else { if (x == second) { if (mid < second) { if(second-mid==1){ found=true; p=mid; } beg = mid; } else { end = mid; if(mid-second==1){ found=true; p=mid; } } } else { if (mid < second) { if(mid-beg==1){ found=true; p=beg; } end = mid; } else { if(end-mid==1){ found=true; p=end; } beg = mid; } } } q++; } System.out.println("! "+p); out.flush(); out.close(); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
c299b02dcb70835618e3e1279f054d6b
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class C2 { public static boolean check(int l, int r, int v, BufferedReader in) throws IOException { System.out.printf("? %d %d\n", l, r); System.out.flush(); int ans = Integer.parseInt(in.readLine()); return ans == v; } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); System.out.printf("? %d %d\n", 1, n); System.out.flush(); int pos = Integer.parseInt(in.readLine()); int l, r; if(pos > 1 && check(1, pos, pos, in)){ l = 1; r = pos; while(l < r){ int mid = (l + r + 1) >> 1; if(mid < pos && check(mid, pos, pos, in)) l = mid; else r = mid - 1; } System.out.printf("! %d\n", l); System.out.flush(); } else{ l = pos; r = n; while (l < r) { int mid = (l + r) >> 1; if (pos < mid && check(pos, mid, pos, in)) r = mid; else l = mid + 1; } System.out.printf("! %d\n", l); System.out.flush(); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
df52029960f0eb031ad3a4bef3c9a3fc
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class C { public static boolean check(int l, int r, int v, BufferedReader in) throws IOException { System.out.printf("? %d %d\n", l, r); System.out.flush(); int ans = Integer.parseInt(in.readLine()); return ans == v; } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); System.out.printf("? %d %d\n", 1, n); int pos = Integer.parseInt(in.readLine()); int l = 1, r = pos; while(l < r){ int mid = (l + r + 1) >> 1; if(mid < pos && check(mid, pos, pos, in)) l = mid; else r = mid - 1; } if (l < pos && check(l, pos, pos, in)) { System.out.printf("! %d\n", l); System.out.flush(); } else{ l = pos; r = n; while (l < r) { int mid = (l + r) >> 1; if (pos < mid && check(pos, mid, pos, in)) r = mid; else l = mid + 1; } System.out.printf("! %d\n", l); System.out.flush(); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
fef597ecea2dc0a9f3cee712d183141c
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
// 18-Feb-2021 import java.util.*; import java.io.*; public class C { static class FastReader { BufferedReader br; StringTokenizer st; private FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayOne(int n) { int[] a = new int[n + 1]; for (int i = 1; i < n + 1; i++) a[i] = nextInt(); return a; } long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayOne(int n) { long[] a = new long[n + 1]; for (int i = 1; i < n + 1; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static PrintWriter w = new PrintWriter(System.out); static FastReader s = new FastReader(); public static void main(String[] args) { int n = s.nextInt(); int ans = -1; int maxInd = query(1,n); if(maxInd == query(1,maxInd)) { int l = 1, r = maxInd - 1; while(l <= r) { int mid = (l + r) / 2; if(query(mid, maxInd) == maxInd) { ans = mid; l =mid + 1; }else { r = mid - 1; } } }else { int l = maxInd + 1, r = n; while(l <= r) { int mid = (l + r) / 2; if(query(maxInd,mid) == maxInd) { ans = mid; r =mid - 1; }else { l = mid + 1; } } } w.println("! " + ans); w.flush(); } private static int query(int l, int r) { if(l == r) return Integer.MIN_VALUE; w.println("? "+ l +" " + r); w.flush(); return s.nextInt(); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
634b84e2653c9b41807c742d4fe803f7
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class GuessingTheGreatestHard { MyScanner scanner = new MyScanner(); private int solve(int n) { int start = 0, end = n - 1; int secMax = ask(start, end); // Guess Right while (start <= end) { int mid = start + (end - start) / 2; if (mid == n - 1) break; int val = ask(mid, n - 1); if (val == secMax) { start = mid + 1; } else { end = mid - 1; } } int right = end; if (right != secMax) { return right; } start = 0; end = n - 1; while (start <= end) { int mid = start + (end - start) / 2; if (mid == 0) break; int val = ask(0, mid); if (val == secMax) { end = mid - 1; } else { start = mid + 1; } } int left = start; return left; } // int[] arr = {1, 2}; // private int ask(int l, int r) { // int first = -1, second = -1; // for (int i = l; i <= r; i++) { // if (first == -1 || arr[i] > arr[first]) { // second = first; // first = i; // } // else if (second == -1 || arr[i] > arr[second]) { // second = i; // } // } // return second; // } private int ask(int l, int r) { System.out.println("? " + (l + 1) + " " + (r + 1)); System.out.flush(); int ans = scanner.nextInt(); return ans - 1; } private void print(int n) { System.out.println("! " + (n + 1)); System.out.flush(); } public static void main(String[] args) { GuessingTheGreatestHard test = new GuessingTheGreatestHard(); // int t = test.scanner.nextInt(); // for (int i = 0; i < t; i++) { int n = test.scanner.nextInt(); int ans = test.solve(n); test.print(ans); // } } class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int len) { int[] arr = new int[len]; for (int i = 0; i < len; i++) { arr[i] = nextInt(); } return arr; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
af116e53e5068bb67177e3d3deb779d2
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class GuessingTheGreatest { MyScanner scanner = new MyScanner(); private int solve(int n) { int start = 0, end = n - 1; int secMax = ask(start, end); // Guess Right while (start <= end) { int mid = start + (end - start) / 2; if (mid == n - 1) break; int val = ask(mid, n - 1); if (val == secMax) { start = mid + 1; } else { end = mid - 1; } } int right = end; if (right != secMax) { return right; } start = 0; end = n - 1; while (start <= end) { int mid = start + (end - start) / 2; if (mid == 0) break; int val = ask(0, mid); if (val == secMax) { end = mid - 1; } else { start = mid + 1; } } int left = start; return left; } // int[] arr = {1, 2}; // private int ask(int l, int r) { // int first = -1, second = -1; // for (int i = l; i <= r; i++) { // if (first == -1 || arr[i] > arr[first]) { // second = first; // first = i; // } // else if (second == -1 || arr[i] > arr[second]) { // second = i; // } // } // return second; // } private int ask(int l, int r) { System.out.println("? " + (l + 1) + " " + (r + 1)); System.out.flush(); int ans = scanner.nextInt(); return ans - 1; } private void print(int n) { System.out.println("! " + (n + 1)); System.out.flush(); } public static void main(String[] args) { GuessingTheGreatest test = new GuessingTheGreatest(); // int t = test.scanner.nextInt(); // for (int i = 0; i < t; i++) { int n = test.scanner.nextInt(); int ans = test.solve(n); test.print(ans); // } } class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int len) { int[] arr = new int[len]; for (int i = 0; i < len; i++) { arr[i] = nextInt(); } return arr; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
2b1da4d40ce2837d3b97076e03534da2
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; /** * @author Mubtasim Shahriar */ public class Cr703A { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader sc = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solver solver = new Solver(); // int t = sc.nextInt(); int t = 1; while (t-- != 0) { solver.solve(sc, out); } out.close(); } static class Solver { public void solve(InputReader sc, PrintWriter out) { int n = sc.nextInt(); int l = 1; int r = n; int at = -1; HashMap<Integer,HashMap<Integer,Integer>> map = new HashMap<>(); boolean[] taken = new boolean[n+1]; while(l<=r) { if(taken[l]) { l++; continue; } if(taken[r]) { r--; continue; } if(l==r) { at= l; break; } int mid = (l+r)>>1; if(r-l==1) { int secMax =gett(map,l,r); if(secMax==-1) { secMax = query(sc,l,r); putt(map,l,r,secMax); } // int secMax = query(sc,l,r); taken[secMax] = true; if(secMax==l) { at = r; break; } else { at = l; break; } } else { int wholeAt = gett(map,l,r); if(wholeAt==-1) { wholeAt = query(sc,l,r); putt(map,l,r,wholeAt); } taken[wholeAt] = true; if(wholeAt==mid) { int sec = gett(map,mid,r); if(sec==-1) { sec = query(sc,mid,r); putt(map,mid,r,sec); } taken[sec] = true; if(sec!=wholeAt) { r = mid-1; } else { l = mid; } } else if(wholeAt>mid) { if(mid+1==r) { r = mid; continue; } int sec = gett(map,mid+1,r); if(sec==-1) { sec = query(sc,mid+1,r); putt(map,mid+1,r,sec); } taken[sec] = true; if(sec!=wholeAt) { r = mid; } else { l = mid; } } else { if(mid-1==l) { l = mid; continue; } int sec = gett(map,l,mid-1); if(sec==-1) { sec = query(sc,l,mid-1); putt(map,l,mid-1,sec); } if(sec!=wholeAt) { l = mid; } else { r = mid; if(mid==wholeAt) r--; } } } } if(at==-1) { throw new RuntimeException(); } System.out.println("! " + at); System.out.flush(); } private void putt(HashMap<Integer, HashMap<Integer, Integer>> map, int f, int s, int val) { if(!map.containsKey(f))map.put(f,new HashMap<>()); HashMap<Integer,Integer> sec = map.get(f); if(!sec.containsKey(s)) sec.put(s,val); } private int gett(HashMap<Integer, HashMap<Integer, Integer>> map, int l, int r) { if(!map.containsKey(l)) return -1; HashMap<Integer,Integer> sec = map.get(l); if(!sec.containsKey(r)) return -1; return sec.get(r); } private int query(InputReader sc, int ql, int qr) { System.out.println("? " + ql + " " + qr); System.out.flush(); return sc.nextInt(); } } static class SegMax { int leftMost, rightMost, mid; boolean leaf; int max; int maxIdx; SegMax lchild, rchild; SegMax(int l, int r, int[] arr) { leftMost = l; rightMost = r; mid = (l + r) >> 1; leaf = l == r; if (leaf) { max = arr[l]; maxIdx = l; } else { lchild = new SegMax(l, mid, arr); rchild = new SegMax(mid + 1, r, arr); max = Math.max(lchild.max, rchild.max); } } int query(int l, int r) { if (l > rightMost || r < leftMost) return Integer.MIN_VALUE; if (l <= leftMost && rightMost <= r) return max; return Math.max(lchild.query(l, r), rchild.query(l, r)); } } static void sort(int[] arr) { Random rand = new Random(); int n = arr.length; for (int i = 0; i < n; i++) { int idx = rand.nextInt(n); if (idx == i) continue; arr[i] ^= arr[idx]; arr[idx] ^= arr[i]; arr[i] ^= arr[idx]; } Arrays.sort(arr); } static void sort(long[] arr) { Random rand = new Random(); int n = arr.length; for (int i = 0; i < n; i++) { int idx = rand.nextInt(n); if (idx == i) continue; arr[i] ^= arr[idx]; arr[idx] ^= arr[i]; arr[i] ^= arr[idx]; } Arrays.sort(arr); } static void sortDec(int[] arr) { Random rand = new Random(); int n = arr.length; for (int i = 0; i < n; i++) { int idx = rand.nextInt(n); if (idx == i) continue; arr[i] ^= arr[idx]; arr[idx] ^= arr[i]; arr[i] ^= arr[idx]; } Arrays.sort(arr); int l = 0; int r = n - 1; while (l < r) { arr[l] ^= arr[r]; arr[r] ^= arr[l]; arr[l] ^= arr[r]; l++; r--; } } static void sortDec(long[] arr) { Random rand = new Random(); int n = arr.length; for (int i = 0; i < n; i++) { int idx = rand.nextInt(n); if (idx == i) continue; arr[i] ^= arr[idx]; arr[idx] ^= arr[i]; arr[i] ^= arr[idx]; } Arrays.sort(arr); int l = 0; int r = n - 1; while (l < r) { arr[l] ^= arr[r]; arr[r] ^= arr[l]; arr[l] ^= arr[r]; l++; r--; } } static class InputReader { private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int peek() { if (numChars == -1) { return -1; } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) { return -1; } } return buf[curChar]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') { buf.appendCodePoint(c); } c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) { s = readLine0(); } return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) { return readLine(); } else { return readLine0(); } } public BigInteger readBigInteger() { try { return new BigInteger(nextString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char nextCharacter() { int c = read(); while (isSpaceChar(c)) { c = read(); } return (char) c; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) { read(); } return value == -1; } public String next() { return nextString(); } public SpaceCharFilter getFilter() { return filter; } public void setFilter(SpaceCharFilter filter) { this.filter = filter; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public int[] nextSortedIntArray(int n) { int array[] = nextIntArray(n); Arrays.sort(array); return array; } public int[] nextSumIntArray(int n) { int[] array = new int[n]; array[0] = nextInt(); for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt(); return array; } public long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; ++i) array[i] = nextLong(); return array; } public long[] nextSumLongArray(int n) { long[] array = new long[n]; array[0] = nextInt(); for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt(); return array; } public long[] nextSortedLongArray(int n) { long array[] = nextLongArray(n); Arrays.sort(array); return array; } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
da37fa6de7b24d892b5bc684b3230e6c
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class D{ static FastScanner fs = null; public static void main(String[] args) { fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = 1; while (t-->0) { int n = fs.nextInt(); System.out.println("? 1 "+n); System.out.flush(); int x = fs.nextInt(); if(x>1){ System.out.println("? 1 "+x); System.out.flush(); int xx = fs.nextInt(); if(x==xx){ findleft(1,x); } else{ findright(x,n); } } else{ findright(1,n); } } out.close(); } static void findright(int a,int b){ int st = a; int end =b; while (end>st+1) { int md = (st+end)/2; System.out.println("? "+a+" "+md); System.out.flush(); int yy = fs.nextInt(); if(yy==a){ end = md; } else{ st = md; } } System.out.println("! "+end); } static void findleft(int a,int b){ int st = a; int end = b; while (end>st+1) { int md = (st+end)/2; System.out.println("? "+md+" "+b); System.out.flush(); int yy = fs.nextInt(); if(yy==b){ st = md; } else{ end = md; } } System.out.println("! "+st); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
15413e20a3e58521714dc2a25251a7b7
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.PrintWriter; public class T1698C2 { private static final T1698C2FastScanner in = new T1698C2FastScanner(); private static final PrintWriter out = new PrintWriter(System.out); public static void main(String args[]) { int n = in.nextInt(); int posSecMax = query(0, n-1); int posMax = search(0, n-1, posSecMax); answer(posMax); out.flush(); } private static int search(int leftMax, int rightMax, int posSecMax) { int offsetFromLeft = (rightMax - leftMax) / 2; int mid = leftMax + offsetFromLeft; if (rightMax - leftMax == 1) { int newPosSecMax = query(leftMax, rightMax); if (newPosSecMax == leftMax) return rightMax; else return leftMax; } else if (rightMax - leftMax == 2) { if (posSecMax >= leftMax && posSecMax <= rightMax) { //и max и secondMax среди этих трёх if (posSecMax == leftMax) { return search(leftMax + 1, rightMax, posSecMax); } else if (posSecMax == rightMax) { return search(leftMax, leftMax + 1, posSecMax); } else { int newPosSecMax = query(leftMax, leftMax + 1); if (newPosSecMax == posSecMax) { return leftMax; } else { return rightMax; } } } else if (posSecMax < leftMax) { int posSecMaxLeftHalf = query(posSecMax, leftMax); if (posSecMaxLeftHalf == posSecMax) { return leftMax; } else { return search(leftMax+1, rightMax, posSecMax); } } else { int posSecMaxRightHalf = query(rightMax, posSecMax); if (posSecMaxRightHalf == posSecMax) { return rightMax; } else { return search(leftMax, leftMax+1, posSecMax); } } } boolean isLeftHalf; if (posSecMax >= leftMax && posSecMax <= mid) { // posSecMax внутри первой половины int posNewSecMax = query(leftMax, mid); isLeftHalf = (posNewSecMax == posSecMax) ? true : false; //max внутри первой половины } else if (posSecMax >= mid + 1 && posSecMax <= rightMax) { // posSecMax внутри второй половины int posNewSecMax = query(mid+1, rightMax); isLeftHalf = (posNewSecMax == posSecMax) ? false : true; //max внутри второй половины } else if (posSecMax < leftMax) { //posSecMax находится "левее" первой половины, поэтому её и рассматриваем на наличие max int posNewSecMax = query(posSecMax, mid); isLeftHalf = (posNewSecMax == posSecMax) ? true : false; //max внутри второй половины } else { //posSecMax находится "правее" второй половины, поэтому её и рассматриваем на наличие max int posNewSecMax = query(mid + 1, posSecMax); isLeftHalf = (posNewSecMax == posSecMax) ? false : true; //max внутри второй половины } if (isLeftHalf) { return search(leftMax, mid, posSecMax); } else { return search(mid+1, rightMax, posSecMax); } } static int query(int l, int r) { out.println("? " + (l + 1) + " " + (r + 1)); out.flush(); return (in.nextInt() - 1); } static void answer(int position) { out.println("! " + (position + 1)); } } class T1698C2FastScanner { private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public T1698C2FastScanner() { in = new BufferedInputStream(System.in, BS); } public T1698C2FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
87db2acf0bfeeeb23aea249e66ad3e34
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.Scanner; public class T1486C { static Scanner in = new Scanner(System.in); public static void main(String[] args) { int n = in.nextInt(); int left = 0; int right = n - 1; int secondMaximum = query(left, right); int positionMaximum = search(left, right, secondMaximum); answer(positionMaximum); } static int search(int left, int right, int positionSecondMaximum) { if (right - left == 0) { return left; } if (positionSecondMaximum == -1) { positionSecondMaximum = query(left, right); } if (right - left == 1) { if (left == positionSecondMaximum) { return right; } else { return left; } } else if (right - left == 2) { int positionSecondMaxLeftAndMiddle = query(left, left + 1); if (positionSecondMaximum == right) { if (positionSecondMaxLeftAndMiddle == left) { return left + 1; } else { return left; } } else { if (positionSecondMaxLeftAndMiddle == positionSecondMaximum) { if (positionSecondMaxLeftAndMiddle == left) { return left + 1; } else { return left; } } else { return right; } } } int length = (right - left) / 2; int rightForLeft = left + length; int leftForRight = rightForLeft + 1; if (positionSecondMaximum <= rightForLeft) { //если позиция второго максимума слева int newPositionSecondMaximum = query(left, rightForLeft); if (newPositionSecondMaximum == positionSecondMaximum) { return search(left, rightForLeft, positionSecondMaximum); } else { return search(leftForRight, right, -1); } } else { int newPositionSecondMaximum = query(leftForRight, right); if (newPositionSecondMaximum == positionSecondMaximum) { return search(leftForRight, right, positionSecondMaximum); } else { return search(left, rightForLeft, -1); } } } static int query(int l, int r) { System.out.println("? " + (l + 1) + " " + (r + 1)); System.out.flush(); return (in.nextInt() - 1); } static void answer(int position) { System.out.println("! " + (position + 1)); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
9fc705b026d8212c9a3142ba1ce586f8
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.Scanner; public class T1486C { static Scanner in = new Scanner(System.in); public static void main(String[] args) { int n = in.nextInt(); int left = 0; int right = n - 1; int secondMaximum = query(left, right); int positionMaximum = search(left, right, secondMaximum); answer(positionMaximum); } static int search(int left, int right, int positionSecondMaximum) { if (right - left == 0) { return left; } if (positionSecondMaximum == -1) { positionSecondMaximum = query(left, right); } if (right - left == 1) { if (left == positionSecondMaximum) { return right; } else { return left; } } int length = (right - left) / 2; int middle = left + length; if (positionSecondMaximum < middle) { int newPositionSecondMaximum = query(left, middle); if (newPositionSecondMaximum == positionSecondMaximum) { return search(left, middle, positionSecondMaximum); } else { return search(middle, right, -1); } } else { int newPositionSecondMaximum = query(middle, right); if (newPositionSecondMaximum == positionSecondMaximum) { return search(middle, right, positionSecondMaximum); } else { return search(left, middle, -1); } } } static int query(int l, int r) { System.out.println("? " + (l + 1) + " " + (r + 1)); System.out.flush(); return (in.nextInt() - 1); } static void answer(int position) { System.out.println("! " + (position + 1)); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
7596be8d7832a2b52e1b80fd896f3b3f
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Stack; import java.util.StringTokenizer; import static java.lang.Math.*; public class C { static FastScanner sc; static PrintWriter pw; public static void main(String[] args) { //45 sc = new FastScanner(); pw = new PrintWriter(System.out); int T = 1; while (T-- > 0) { solve(); } pw.close(); } static HashMap<Integer, HashMap<Integer, Integer>> rangeMap = new HashMap<>(); static void solve() { int n = sc.nextInt(); int low = 1, high = n; rangeMap.clear(); while(high - low > 1) { int mid = (low + high) / 2; int secondMax = getQuery(low, high); if(secondMax <= mid) { int secondMax2 = getQuery(low, mid); if(secondMax == secondMax2) { high = mid; } else { low = mid; } } else { int secondMax2 = getQuery(mid, high); if(secondMax == secondMax2) { low = mid; } else { high = mid; } } } int notAnswer = getQuery(low, high); int ans = notAnswer == low ? high : low; System.out.println("! "+ans); } static int getQuery(int low, int high) { if(rangeMap.containsKey(low)) { if(rangeMap.get(low).containsKey(high)) { return rangeMap.get(low).get(high); } } System.out.println("? "+low+" "+high); int num = sc.nextInt(); HashMap<Integer, Integer> map = rangeMap.getOrDefault(low, new HashMap<>()); map.put(high, num); rangeMap.put(low, map); return num; } static int getMove(int low, int ind, int high) { if(ind <= low) { return 1; } int left = getQuery(low, ind); if(high <= ind) { return -1; } int right = getQuery(ind, high); if(left == ind) { return -1; } return 1; } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
68b144bbae0d8e28df0f91a1a30e31c6
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Stack; import java.util.StringTokenizer; import static java.lang.Math.*; public class C { static FastScanner sc; static PrintWriter pw; public static void main(String[] args) { sc = new FastScanner(); pw = new PrintWriter(System.out); int T = 1; while (T-- > 0) { solve(); } pw.close(); } static HashMap<Integer, HashMap<Integer, Integer>> rangeMap = new HashMap<>(); static void solve() { int n = sc.nextInt(); int low = 1, high = n; rangeMap.clear(); while(high - low > 1) { int mid = (low + high) / 2; int secondMax = getQuery(low, high); if(secondMax <= mid) { int secondMax2 = getQuery(low, mid); if(secondMax == secondMax2) { high = mid; } else { low = mid; } } else { int secondMax2 = getQuery(mid, high); if(secondMax == secondMax2) { low = mid; } else { high = mid; } } } int notAnswer = getQuery(low, high); int ans = notAnswer == low ? high : low; System.out.println("! "+ans); } static int getQuery(int low, int high) { if(rangeMap.containsKey(low)) { if(rangeMap.get(low).containsKey(high)) { return rangeMap.get(low).get(high); } } System.out.println("? "+low+" "+high); int num = sc.nextInt(); HashMap<Integer, Integer> map = rangeMap.getOrDefault(low, new HashMap<>()); map.put(high, num); rangeMap.put(low, map); return num; } static int getMove(int low, int ind, int high) { if(ind <= low) { return 1; } int left = getQuery(low, ind); if(high <= ind) { return -1; } int right = getQuery(ind, high); if(left == ind) { return -1; } return 1; } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
1391bf373a8ebf9ef2291835cbfca0f8
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.lang.reflect.Type; import java.math.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; import java.text.DecimalFormat; import java.util.*; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; import java.util.concurrent.locks.LockSupport; public class Main { static class Task { public static String roundS(double result, int scale) { String fmt = String.format("%%.%df", scale); return String.format(fmt, result); // DecimalFormat df = new DecimalFormat("0.000000"); // double result = Double.parseDouble(df.format(result)); } int rt(int x) { if (x != fa[x]) { int to = rt(fa[x]); dp[x] ^= dp[fa[x]]; fa[x] = to; return to; } return x; } void combine(int x, int y, int val) { int rt1 = rt(x); int rt2 = rt(y); if (rt1 == rt2) return; fa[rt1] = rt2; dp[rt1] = dp[x] ^ dp[y] ^ val; g--; } int fa[], dp[]; int g; static int MAXN = 10000; static Random rd = new Random(348957438574659L); static int[] ch[],val,size,rnd,cnt; static int len=0,rt=0; // return new node, the node below s static int rotate(int s,int d){ // child int x=ch[s][d^1]; // give me grandson ch[s][d^1] = ch[x][d]; // child become father ch[x][d]=s; // update size, update new son first update(s); update(x); return x; } static void update(int s){ size[s] = size[ch[s][0]] + size[ch[s][1]] + cnt[s]; } // 0 for left, 1 for right static int cmp(int x,int num) { if (val[x] == num) return -1; return num < val[x] ? 0 : 1; } static int insert(int s,int num) { if(s==0){ s=++len; val[s]=num;size[s]=1;rnd[s]=rd.nextInt();cnt[s] = 1; }else{ int d=cmp(s,num); if(d!=-1) { ch[s][d]=insert(ch[s][d],num); // father's random should be greater if(rnd[s]<rnd[ch[s][d]]) { s=rotate(s,d^1); }else { update(s); } }else{ ++cnt[s];++size[s]; } } return s; } static int del(int s,int num) { int d = cmp(s, num); if (d != -1) { ch[s][d] = del(ch[s][d], num); update(s); }else if (ch[s][0] * ch[s][1] == 0) { if(--cnt[s]==0){ s = ch[s][0] + ch[s][1]; } } else { int k = rnd[ch[s][0]] < rnd[ch[s][1]] ? 0 : 1; // k points to smaller random value,then bigger one up s = rotate(s, k); // now the node with value num become the child ch[s][k] = del(ch[s][k], num); update(s); } return s; } static int getKth(int s, int k){ int lz = size[ch[s][0]]; if(k>=lz+1&&k<=lz+cnt[s]){ return val[s]; }else if(k<=lz){ return getKth(ch[s][0], k); }else{ return getKth(ch[s][1], k-lz-cnt[s]); } } static int getRank(int s,int value){ if(s==0)return 1; if(value==val[s])return size[ch[s][0]] + 1; if(value<val[s])return getRank(ch[s][0],value); return getRank(ch[s][1],value)+size[ch[s][0]] + cnt[s]; } static int getPre(int data){ int ans= -1; int p=rt; while(p>0){ if(data>val[p]){ if(ans==-1 || val[p]>val[ans]) ans=p; p=ch[p][1]; } else p=ch[p][0]; } return ans!=-1?val[ans]:(-2147483647); } static int getNext(int data){ int ans= -1; int p=rt; while(p>0){ if(data<val[p]){ if(ans==-1 || val[p]<val[ans]) ans=p; p=ch[p][0]; } else p=ch[p][1]; } return ans!=-1?val[ans]:2147483647; } static boolean find(int s,int num){ while(s!=0){ int d=cmp(s,num); if(d==-1) return true; else s=ch[s][d]; } return false; } static int ans = -10000000; static boolean findX(int s,int num){ while(s!=0){ if(val[s]<=num){ ans = num; } int d=cmp(s,num); if(d==-1) return true; else { s = ch[s][d]; } } return false; } long gcd(long a,long b){ if(b==0) return a; return gcd(b,a%b); } void linear_sort(int arr[]){ int d = 65536; ArrayDeque bucket[] = new ArrayDeque[d]; for(int j=0;j<d;++j){ bucket[j] = new ArrayDeque(); } for(int u:arr){ bucket[u%d].offer(u); } int pos = 0; for(int j=0;j<d;++j){ while(bucket[j].size()>0) { arr[pos++] = (int)bucket[j].pollFirst(); } } for(int u:arr){ bucket[u/d].offer(u); } pos = 0; for(int j=0;j<d;++j){ while(bucket[j].size()>0) { arr[pos++] = (int)bucket[j].pollFirst(); } } } public void solve(int testNumber, InputReader in, PrintWriter out) { // int a[] = {4,3,2,45,1,2,3,4,56,7,2,1,2}; // linear_sort(a); // for(int u:a){ // out.println(u); // } int t = 1; // // outer:for(int j=0;j<t;++j){ int n =in.nextInt(); int l = 1; int r = n; while(r>=l){ if(l==r){ out.println("! " + l); out.flush(); break; } out.println("? "+l +" "+r); out.flush(); int pos = in.nextInt(); if(r-l==1){ out.println("! " + (r+l-pos)); out.flush(); break; } int mid = (l+r)/2; if(pos>=mid) { out.println("? " + mid + " " + r); out.flush(); int pos1 = in.nextInt(); if (pos1==pos){ l = mid; }else{ r = mid-1; } }else{ out.println("? " + l + " " + mid); out.flush(); int pos1 = in.nextInt(); if (pos==pos1){ r = mid; }else{ l = mid+1; } } } } // while(true) { // int n = in.nextInt(); // // int m =in.nextInt(); // // fa = new int[n]; // dp = new int[n]; // for(int i=0;i<n;++i){ // fa[i] = i; // } // g = n; // int c = 0; // int as[] = new int[n]; // int bs[] = new int[n]; // char xs[] = new char[n]; // // int at = -1; // Set<Integer> st = new HashSet<>(); // // for (int i = 0; i < n; ++i) { // String line = in.next(); // int p = 0; // int a = 0; // while(Character.isDigit(line.charAt(p))){ // a = a*10 + (line.charAt(p)-'0'); p++; // } // char x = line.charAt(p++); // // int b = 0; // while(p<line.length()){ // b = b*10 + (line.charAt(p)-'0'); p++; // } // // as[i] = a; // xs[i] = x; // bs[i] = b; // // if(x=='='){ // int r1 = rt(a); int r2 = rt(b); // if(r1==r2){ // if(dp[a]!=dp[b]){ // c++; // at = i; // } // }else { // combine(a, b, 0); // } // }else if(x=='<'){ // int r1 = rt(a); int r2 = rt(b); // if(r1==r2){ // if(dp[a]>=dp[b]){ // c++; // at = i; // } // }else { // combine(a, b, -1); // } // }else{ // int r1 = rt(a); int r2 = rt(b); // if(r1==r2){ // if(dp[a]<=dp[b]){ // c++; // at = i; // } // }else { // combine(a, b, 1); // } // } // // // } // if(g==1||c>=2){ // out.println("Impossible"); // continue; // } // // // for(int xuan: st){ // // // // // } // // // // // // // } } static long mul(long a, long b, long p) { long res=0,base=a; while(b>0) { if((b&1L)>0) res=(res+base)%p; base=(base+base)%p; b>>=1; } return res; } static long mod_pow(long k,long n,long p){ long res = 1L; long temp = k%p; while(n!=0L){ if((n&1L)==1L){ res = mul(res,temp,p); } temp = mul(temp,temp,p); n = n>>1L; } return res%p; } public static double roundD(double result, int scale){ BigDecimal bg = new BigDecimal(result).setScale(scale, RoundingMode.UP); return bg.doubleValue(); } } private static void solve() { InputStream inputStream = System.in; // InputStream inputStream = null; // try { // inputStream = new FileInputStream(new File("D:\\chrome_download\\travel_restrictions_input.txt")); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } OutputStream outputStream = System.out; // OutputStream outputStream = null; // File f = new File("D:\\chrome_download\\travel_restrictions_output.txt"); // try { // f.createNewFile(); // } catch (IOException e) { // e.printStackTrace(); // } // try { // outputStream = new FileOutputStream(f); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task task = new Task(); task.solve(1, in, out); out.close(); } public static void main(String[] args) { // new Thread(null, () -> solve(), "1", (1 << 10 ) ).start(); solve(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String nextLine() { String line = null; try { line = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return line; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public char nextChar() { return next().charAt(0); } public int[] nextArray(int n) { int res[] = new int[n]; for (int i = 0; i < n; ++i) { res[i] = nextInt(); } return res; } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
161772fd6c96718d4e44ea7389a554ea
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
//package R703; import java.io.*; import java.math.*; import java.util.*; public class Q3 { static int INF = (int)(1e9); static long mod = (long)(1e9)+7; static long mod2 = 998244353; static long[] segtree; static char[] curans; static long ans; static String S; static ArrayList<Integer>[] graph; static boolean[] vis; static int[] a; static int N; static long K; static long[] fact; static ArrayList<Long> pos; static ArrayList<Long> neg; static long[] max; static int[] dp; public static void main(String[] args) { //Cash out /**/ FastScanner I = new FastScanner(); //Input OutPut O = new OutPut(); //Output int N = I.nextInt(); O.pln("? " + 1 + " "+ N); int index = I.nextInt(); int lo = index; int hi = N; int ans = 0; if (index == N) { lo = 1; hi = index - 1; while (lo <= hi) { int mid = (lo + hi) / 2; O.pln("? " + mid + " " + index); int new_index = I.nextInt(); if (new_index == index) { lo = mid + 1; ans = max(ans,mid); }else { hi= mid - 1; } } }else if (index == 1){ ans = N; lo = index + 1; while (lo <= hi) { int mid = (lo + hi) / 2; O.pln("? " + index + " " + mid); int new_index = I.nextInt(); if (new_index == index) { ans = min(ans,mid); hi = mid - 1; }else { lo = mid + 1; } } }else { O.pln("? " + 1 + " "+ index); int id2 = I.nextInt(); if (id2 == index) { lo = 1; hi = index - 1; while (lo <= hi) { int mid = (lo + hi) / 2; O.pln("? " + mid + " " + index); int new_index = I.nextInt(); if (new_index == index) { ans = max(ans, mid); lo = mid + 1; }else { hi= mid - 1; } } }else { lo = index + 1; while (lo <= hi) { ans = N; int mid = (lo + hi) / 2; O.pln("? " + index + " " + mid); int new_index = I.nextInt(); if (new_index == index) { ans = min(ans, mid); hi = mid - 1; }else { lo = mid + 1; } } } } O.pln("! " + ans); } public static double[][] matrix_exp(double[][] a, long exp, long mod) { int R = a.length; int C = a[0].length; double[][] ans = new double[R][C]; boolean mult_yet = false; while (exp > 0) { if (exp % 2 == 1) { if (!mult_yet) { mult_yet = true; for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { ans[i][j] = a[i][j]; } } }else { double[][] new_ans = mult(ans, a, mod); for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { ans[i][j] = new_ans[i][j]; } } } } double[][] new_a = mult(a, a, mod); // a = a^2 (binary exponentiation on matrices) for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { a[i][j] = new_a[i][j]; } } exp /= 2; } return ans; } public static double[][] mult(double[][] a, double[][] b, long mod) { int r1 = a.length; int c1 = a[0].length; int r2 = b.length; int c2 = b[0].length; //Requires: c1 = r2 double[][] ans = new double[r1][c2]; for (int r = 0; r < r1; r++) { for (int c = 0; c < c2; c++) { //Dot product of (a[r])^T and b[c] (as vectors in R^n (n = c1 = r2)) double[] col_vector = new double[r2]; double[] row_vector = new double[c1]; for (int i = 0; i < r2; i++) { col_vector[i] = b[i][c]; } for (int i = 0; i < c1; i++) { row_vector[i] = a[r][i]; } ans[r][c] = dot_product(row_vector, col_vector, mod); } } return ans; } public static double dot_product(double[] a, double[] b, long mod) { double ans = 0; int N = a.length; //Requires: a and b are both vectors in R^n for (int i = 0; i < N; i++) { ans += a[i] * b[i]; } return ans; } public static double max(double a, double b) {return Math.max(a, b);} public static double min(double a, double b) {return Math.min(a, b);} public static long min(long a, long b) {return Math.min(a,b);} public static long max(long a, long b) {return Math.max(a,b);} public static int min(int a, int b) {return Math.min(a,b);} public static int max(int a, int b) {return Math.max(a,b);} public static long abs(long x) {return Math.abs(x);} public static long abs(int x) {return Math.abs(x);} public static long ceil(long num, long den) {long ans = num/den; if (num%den!=0) ans++; return ans;} public static long GCD(long a, long b) { if (a==0||b==0) return max(a,b); return GCD(min(a,b),max(a,b)%min(a,b)); } public static long FastExp(long base, long exp, long mod) { long ans=1; while (exp>0) { if (exp%2==1) ans*=base; exp/=2; base*=base; base%=mod; ans%=mod; } return ans; } public static long ModInv(long num,long mod) {return FastExp(num,mod-2,mod);} public static int pop(long x) { //Returns number of bits within a number int cnt = 0; while (x>0) { if (x%2==1) cnt++; x/=2; } return cnt; } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() {return Integer.parseInt(next());} long nextLong() {return Long.parseLong(next());}; double nextDouble() {return Double.parseDouble(next());} } static class OutPut{ PrintWriter w = new PrintWriter(System.out); void pln(double x) {w.println(x);w.flush();} void pln(boolean x) {w.println(x);w.flush();} void pln(int x) {w.println(x);w.flush();} void pln(long x) {w.println(x);w.flush();} void pln(String x) {w.println(x);w.flush();} void pln(char x) {w.println(x);w.flush();} void pln(StringBuilder x) {w.println(x);w.flush();} void pln(BigInteger x) {w.println(x);w.flush();} void p(int x) {w.print(x);w.flush();} void p(long x) {w.print(x);w.flush();} void p(String x) {w.print(x);w.flush();} void p(char x) {w.print(x);w.flush();} void p(StringBuilder x) {w.print(x);w.flush();} void p(BigInteger x) {w.print(x);w.flush();} void p(double x) {w.print(x);w.flush();} void p(boolean x) {w.print(x);w.flush();} } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
80bba7ce8929777af3c057591d1b8e2b
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; import java.util.List; import java.util.*; public class realfast implements Runnable { private static final int INF = (int) 1e9; // long in= 1000000007; long fac[]= new long[1000001]; long inv[]=new long[1000001]; public void solve() throws IOException { Scanner in = new Scanner(System.in); //int t = readInt(); int n = in.nextInt(); System.out.println("? "+1+" "+n); System.out.flush(); int val = in.nextInt(); int pal=-1; int nal=-1; if(val!=1) { System.out.println("? "+1+" "+val); System.out.flush(); pal = in.nextInt(); } if(val!=n){ System.out.println("? "+(val)+" "+n); System.out.flush(); nal = in.nextInt(); } int left =0; int right =0; int ans =0; if(nal==val) { left=val+1; right=n; ans=right; while(left<=right) { int mid = left+(right-left)/2; System.out.println("? "+val+" "+mid); System.out.flush(); int pal1 = in.nextInt(); if(pal1==val) { ans= mid; right=mid-1; } else left=mid+1; } } else { left=1; right=val-1; ans= left; while(left<=right) { int mid = left+(right-left)/2; System.out.println("? "+mid+" "+val); System.out.flush(); int pal1=in.nextInt(); if(pal1==val) { ans=mid; left=mid+1; } else right=mid-1; } } System.out.println("! "+ans); //int ans =right; //System.out. } public int value (int seg[], int left , int right ,int index, int l, int r) { if(left>right) { return -100000000; } if(right<l||left>r) return -100000000; if(left>=l&&right<=r) return seg[index]; int mid = left+(right-left)/2; int val = value(seg,left,mid,2*index+1,l,r); int val2 = value(seg,mid+1,right,2*index+2,l,r); return Math.max(val,val2); } public int gcd(int a , int b ) { if(a<b) { int t =a; a=b; b=t; } if(a%b==0) return b ; return gcd(b,a%b); } public long pow(long n , long p,long m) { if(p==0) return 1; long val = pow(n,p/2,m);; val= (val*val)%m; if(p%2==0) return val; else return (val*n)%m; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) { new Thread(null, new realfast(), "", 128 * (1L << 20)).start(); } private static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; private BufferedReader reader; private StringTokenizer tokenizer; private PrintWriter out; @Override public void run() { try { if (ONLINE_JUDGE || !new File("input.txt").exists()) { reader = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { reader = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } solve(); } catch (IOException e) { throw new RuntimeException(e); } finally { try { reader.close(); } catch (IOException e) { // nothing } out.close(); } } private String readString() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } @SuppressWarnings("unused") private int readInt() throws IOException { return Integer.parseInt(readString()); } @SuppressWarnings("unused") private long readLong() throws IOException { return Long.parseLong(readString()); } @SuppressWarnings("unused") private double readDouble() throws IOException { return Double.parseDouble(readString()); } } class edge implements Comparable<edge>{ int u ; int v; edge(int u, int v) { this.u=u; this.v=v; } public int compareTo(edge e) { return this.v-e.v; } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
2bfeebe1509f2caec0c25c93e5ed5500
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.awt.*; import java.util.Scanner; public class Main { Scanner scan = new Scanner(System.in); public static void main(String[] args) { Main m = new Main(); // int q = scan.nextInt(); while (q-- > 0) m.solve(); } void solve() { int n = scan.nextInt(); System.out.println("? 1 " + n); int secondMaxI = scan.nextInt(); int l, cur, r; if (secondMaxI == n) { cur = 0; l = 1; r = n - 1; } else { System.out.println("? " + secondMaxI + " " + n); cur = scan.nextInt(); l = cur == secondMaxI ? secondMaxI + 1 : 1; r = cur == secondMaxI ? n : secondMaxI - 1; } int res = 0; while (l <= r) { int mid = l + r >> 1; System.out.println("? " + Math.min(mid, secondMaxI) + " " + Math.max(mid, secondMaxI)); cur = scan.nextInt(); if (cur == secondMaxI) { res = mid; if (mid > secondMaxI) { r = mid - 1; } else { l = mid + 1; } } else { if (mid > secondMaxI) { l = mid + 1; } else { r = mid - 1; } } } System.out.flush(); System.out.println("! " + res); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
6513500c5d11a6b16e302307f976c52f
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.Scanner; public class GuessGreatest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int l = 1; int r = n; int ans = 0; while (l < r) { if (r - l == 1) { System.out.println("? " + l + " " + r); System.out.flush(); int sm = sc.nextInt(); if (l == sm) ans = r; else ans = l; break; } int mid = (r + l) / 2; System.out.println("? " + l + " " + r); System.out.flush(); int sm = sc.nextInt(); if (sm >= mid) { System.out.println("? " + mid + " " + r); System.out.flush(); int rr = sc.nextInt(); if (sm == rr) { l = mid; } else { r = mid; } } else { System.out.println("? " + l + " " + mid); System.out.flush(); int ll = sc.nextInt(); if (sm == ll) { r = mid; } else { l = mid; } } } System.out.println("! " + ans); sc.close(); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
7c3b2dac6707969a22fbf5447dfdb145
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.Scanner; public class Main { public static Scanner scammer = new Scanner(System.in); public static int querry(int left, int right, boolean rite){ if(left == right) return rite ? right + 1 : left - 1; System.out.print("? "); System.out.print(left); System.out.print(" "); System.out.println(right); System.out.flush(); return scammer.nextInt(); } public static void solve(boolean right, int smax, int n){ int p = right ? smax : 1, k = right ? n : smax, mid = (p + k) >> 1; while(p <= k){ mid = (p + k) >> 1; int outcome = right ? querry(smax, mid, true) : querry(mid, smax, false); if(outcome == smax){ if(right) k = mid - 1; else p = mid + 1; } else{ if(right) p = ++mid; else k = --mid; } } System.out.print("! "); System.out.println(mid); System.out.flush(); } public static void solve_small(int n){ int x = querry(1, n, false); System.out.printf("! %d\n", x == 1 ? 2 : 1); System.out.flush(); } public static void main(String[] args) { int n = scammer.nextInt(); int smax = querry(1, n, false); if(n < 3) { solve_small(n); return; } boolean right = false; int drugi_max = querry(1, smax, false); if(drugi_max != smax) right = true; solve(right, smax, n); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
18da4f0b1b151119f373b1f8e448e3d3
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.*; public class guessingthegreatesteasy { public static void main(String[] args) throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); System.out.println("? " + 1 + " " + n); int secondMax = 0; System.out.flush(); secondMax = Integer.parseInt(f.readLine()); boolean left = true; if(secondMax > 1) { System.out.println("? " + 1 + " " + secondMax); System.out.flush(); left = Integer.parseInt(f.readLine()) == secondMax; } if(secondMax == 1 || !left) { int low = secondMax; int high = n; while(high - low > 1) { int mid = (low - high) / 2 + high; System.out.println("? " + secondMax + " " + mid); System.out.flush(); int secondSecondMax = Integer.parseInt(f.readLine()); if(secondSecondMax != secondMax) low = mid; else high = mid; } System.out.println("! " + high); } else{ int high = secondMax; int low = 1; while(high - low > 1) { int mid = (low - high) / 2 + high; System.out.println("? " + mid + " " + secondMax); System.out.flush(); int secondSecondMax = Integer.parseInt(f.readLine()); if(secondSecondMax != secondMax) high = mid; else low = mid; } System.out.println("! " + low); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
6ad606f135c562af5e739838d464f809
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
//package codeforce.div2.r703; import java.util.HashMap; import java.util.Map; import java.util.Scanner; /** * @author pribic (Priyank Doshi) * @since 18/02/21 */ public class C { public static void main(String[] args) { try (Scanner sc = new Scanner(System.in)) { int T = 1;//sc.nextInt(); for (int tt = 1; tt <= T; tt++) { int n = sc.nextInt(); Map<String, Integer> memo = new HashMap<>(); int l = 1; int r = n; while (true) { if (l == r) { printAns(l); break; } else if (r == l + 1) { int ans = getAns(sc, memo, l, r); printAns(l == ans ? r : l); break; } else { int mid = l + (r - l) / 2; int indexOfSecondMax = getAns(sc, memo, l, r); int indexOfSecondMaxAgain; if (indexOfSecondMax > mid) { if(mid + 1 == r) { r = mid; } else { indexOfSecondMaxAgain = getAns(sc, memo, mid + 1, r); if (indexOfSecondMax == indexOfSecondMaxAgain) { l = mid; } else { r = mid; } } } else { indexOfSecondMaxAgain = getAns(sc, memo, l , mid); if (indexOfSecondMax == indexOfSecondMaxAgain) { r = mid; } else { l = mid; } } } } System.out.flush(); } } } private static int getAns(Scanner sc, Map<String, Integer> memo, int l, int r) { int ans; if (memo.containsKey(key(l, r))) ans = memo.get(key(l, r)); else { query(l, r); ans = sc.nextInt(); memo.put(key(l, r), ans); } return ans; } private static String key(int a, int b) { return a + ":" + b; } private static void printAns(int ans) { System.out.println("! " + ans); } private static void query(int l, int r) { System.out.println("?" + " " + l + " " + r); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
97e2e19c6f27019ced2ff15e75a0ec58
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
//package codeforce.div2.r703; import java.util.HashMap; import java.util.Map; import java.util.Scanner; /** * @author pribic (Priyank Doshi) * @since 18/02/21 */ public class C { public static void main(String[] args) { try (Scanner sc = new Scanner(System.in)) { int T = 1;//sc.nextInt(); for (int tt = 1; tt <= T; tt++) { int n = sc.nextInt(); int l = 1; int r = n; while (true) { if (l == r) { printAns(l); break; } else if (r == l + 1) { //we got the ans int ans; query(l, r); ans = sc.nextInt(); printAns(l == ans ? r : l); break; } else { int mid = l + (r - l) / 2; query(l, r); int indexOfSecondMax = sc.nextInt(); if (indexOfSecondMax > mid) { query(mid, r); int indexOfSecondMaxAgain = sc.nextInt(); if (indexOfSecondMax == indexOfSecondMaxAgain) { l = mid; } else { r = mid; } } else { query(l, mid); int indexOfSecondMaxAgain = sc.nextInt(); if (indexOfSecondMax == indexOfSecondMaxAgain) { r = mid; } else { l = mid; } } } } System.out.flush(); } } } private static void printAns(int ans) { System.out.println("! " + ans); } private static void query(int l, int r) { System.out.println("?" + " " + l + " " + r); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
ce29622244b81add15ede8cefa9aab62
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class Main { static FastScanner sc = new FastScanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(); public static void main(String[] args) throws Exception { int n = sc.nextInt(); System.out.println("? 1 " + n); int center = sc.nextInt(); if (center == 1) { int left = 1; int right = n; while (right - left > 1) { int mid = (left + right) / 2; System.out.println("? " + center + " " + mid); int now = sc.nextInt(); if (center == now) { right = mid; } else { left = mid; } } System.out.println("! " + right); } else if (center == n) { int left = 1; int right = n; while (right - left > 1) { int mid = (left + right) / 2; System.out.println("? " + mid + " " + center); int now = sc.nextInt(); if (center == now) { left = mid; } else { right = mid; } } System.out.println("! " + left); } else { System.out.println("? 1 " + center); int now = sc.nextInt(); if (center == now) { int left = 1; int right = center; while (right - left > 1) { int mid = (left + right) / 2; System.out.println("? " + mid + " " + center); now = sc.nextInt(); if (center == now) { left = mid; } else { right = mid; } } System.out.println("! " + left); } else { int left = center; int right = n; while (right - left > 1) { int mid = (left + right) / 2; System.out.println("? " + center + " " + mid); now = sc.nextInt(); if (center == now) { right = mid; } else { left = mid; } } System.out.println("! " + right); } } } static class GeekInteger { public static void save_sort(int[] array) { shuffle(array); Arrays.sort(array); } public static int[] shuffle(int[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); int randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } public static void save_sort(long[] array) { shuffle(array); Arrays.sort(array); } public static long[] shuffle(long[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); long randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } } } class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String[] nextArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) a[i] = next(); return a; } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
840460089ecf99e5beac21430f2a947c
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.*; public class MainC { static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public boolean hasNext() { try { String string = reader.readLine(); if (string == null) { return false; } tokenizer = new StringTokenizer(string); return tokenizer.hasMoreTokens(); } catch (IOException e) { return false; } } } static InputReader in = new InputReader(System.in); static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) { solve(); } static int answer; static void solve() { int n = in.nextInt(); dg1(1, n); a(answer); } static void dg1 (int start, int end) { int secIndex = query(start, end); if (start + 1 == end) { answer = secIndex == start ? end : start; return; } if (secIndex == start) { dg2(secIndex, end, false); return; } if (secIndex == end) { dg2(start, secIndex, true); return; } int temp = query(start, secIndex); if (temp == secIndex) { if (secIndex == start + 1) { answer = start; return; } dg2(start, secIndex, true); } else { if (secIndex == end - 1) { answer = end; return; } dg2(secIndex, end, false); } } static void dg2 (int start, int end, boolean flag) { int mid = (start + end)/2; int temp; if (start + 1 == end) { answer = flag ? start : end; return; } if (flag) { temp = query(mid, end); if (temp == end) { dg2(mid, end, true); } else { dg1(start, mid); } } else { temp = query(start, mid); if (temp == start) { dg2(start, mid, false); } else { dg1(mid, end); } } } static int query (int s, int e) { System.out.println("? " + s + " " + e); System.out.flush(); return in.nextInt(); } static void a (int index) { System.out.println("! " + index); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
7f3e6d93a49d372c7bb8f688dc66d2f3
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.*; public class C { public static void main(String args[]) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = 1; while (t-- > 0) { int n = sc.nextInt(); int l = 1; int r = n; int x = -1; while (true) { if (l == r) { pw.println("! " + l); break; } if (r - l == 1 && x != -1) { pw.println("! " + (l == x ? r : l)); break; } int mid = l + r >> 1; String s = "? " + l + " " + " " + r; if (x == -1) { pw.println(s); pw.flush(); x = sc.nextInt(); } else { if (x <= mid) { s = "? " + l + " " + " " + mid; pw.println(s); pw.flush(); int y = sc.nextInt(); if (y == x) { r = mid; } else { l = mid; x = -1; } } else { s = "? " + mid + " " + " " + r; pw.println(s); pw.flush(); int y = sc.nextInt(); if (y == x) { l = mid; } else { r = mid; x = -1; } } } } } pw.flush(); } /////////////////////////////////////////////////////////////////////////////////////////// static class tri { int x, y, z; public tri(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } } static class pair implements Comparable<pair> { int x, y; boolean w, l; PriorityQueue<Long> pq; public pair(boolean a, boolean b) { w = a; l = b; } pair(int s, int d) { x = s; y = d; } @Override public int compareTo(pair p) { return Long.compare(x, p.x); } @Override public String toString() { return x + " " + y; } } static long mod(long ans, int mod) { return (ans % mod + mod) % mod; } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static int log(int n, int base) { int ans = 0; while (n + 1 > base) { ans++; n /= base; } return ans; } static long pow(long b, long e) { long ans = 1; while (e > 0) { if ((e & 1) == 1) ans = ((ans * 1l * b)); e >>= 1; { } b = ((b * 1l * b)); } return ans; } static long powmod(long r, long e, int mod) { long ans = 1; r %= mod; while (e > 0) { if ((e & 1) == 1) ans = (int) ((ans * 1l * r) % mod); e >>= 1; r = (int) ((r * 1l * r) % mod); } return ans; } static int ceil(int a, int b) { int ans = a / b; return a % b == 0 ? ans : ans + 1; } static long ceil(long a, long b) { long ans = a / b; return a % b == 0 ? ans : ans + 1; } static HashMap<Integer, Integer> compress(int a[]) { TreeSet<Integer> ts = new TreeSet<>(); HashMap<Integer, Integer> hm = new HashMap<>(); for (int x : a) ts.add(x); for (int x : ts) { hm.put(x, hm.size() + 1); } return hm; } // Returns nCr % p static int C[]; static int nCrModp(int n, int r, int p) { if (r > n - r) r = n - r; if (C[r] != 0) return C[r]; // The array C is going to store last // row of pascal triangle at the end. // And last entry of last row is nCr C[0] = 1; // Top row of Pascal Triangle // One by constructs remaining rows of Pascal // Triangle from top to bottom for (int i = 1; i <= n; i++) { // Fill entries of current row using previous // row values for (int j = Math.min(i, r); j > 0; j--) // nCj = (n-1)Cj + (n-1)C(j-1); C[j] = (C[j] + C[j - 1]) % p; } return C[r]; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public int[] intArr(int n) throws IOException { int a[] = new int[n]; for (int i = 0; i < a.length; i++) { a[i] = nextInt(); } return a; } public long[] longArr(int n) throws IOException { long a[] = new long[n]; for (int i = 0; i < a.length; i++) { a[i] = nextLong(); } return a; } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } public static void shuffle(int[] times2) { int n = times2.length; for (int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); int tmp = times2[i]; times2[i] = times2[r]; times2[r] = tmp; } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
490877d18b416193996376d3f4a0a516
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.*; public class C { public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int low = 1, high = n; out.println("? "+low+" "+high); out.flush(); int index = in.nextInt(); int left = 0; if(index!=1){ out.println("? "+low+" "+index); out.flush(); left = in.nextInt(); } if(n==2){ if(left==0) out.println("! "+n); else out.println("! "+1); } else if(left==index){ low = 1; high = index-1; while(low<=high){ int mid = (low+high)/2; out.println("? "+mid+" "+index); out.flush(); int x = in.nextInt(); if(x==index) low = mid+1; else high = mid-1; } out.println("! "+high); } else{ low = index+1; high = n; while(low<=high){ int mid = (low+high)/2; out.println("? "+index+" "+mid); out.flush(); int x = in.nextInt(); if(x==index) high = mid-1; else low = mid+1; } out.println("! "+low); } out.flush(); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while(!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch(IOException e) {} return st.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } } static final Random random = new Random(); static void ruffleSort(int[] a){ int n = a.length; for(int i=0;i<n;i++){ int j = random.nextInt(n), temp = a[j]; a[j] = a[i]; a[i] = temp; } Arrays.sort(a); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
fe467e62a3b406278d24af0e292fdd5b
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.*; public class guessing { //--------------------------INPUT READER--------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os, true); } public void p(int i) {w.println(i);}; public void p(long l) {w.println(l);}; public void p(double d) {w.println(d);}; public void p(String s) { w.println(s);}; public void pr(int i) {w.print(i);}; public void pr(long l) {w.print(l);}; public void pr(double d) {w.print(d);}; public void pr(String s) { w.print(s);}; public void pl() {w.println();}; public void close() {w.close();}; } //------------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { // ADMIN_MODE(); //int t = sc.ni();while(t-->0) solve(); w.close(); } static void solve() throws IOException { int n = sc.ni(); int l = 1, r = n; while(true) { if(l == r) { w.p("! "+l); return; } w.p("? "+l+" "+r); int id = sc.ni(); if(r-l==1) { w.p("! "+(id==l?r:l)); return; } int mid = (l+r)/2; if(mid >= id) { w.p("? "+l+" "+mid); int idd = sc.ni(); if(idd == id) { r = mid; } else l = mid+1; } else { w.p("? "+(mid)+" "+r); int idd = sc.ni(); if(idd == id) { l = mid; } else { r = mid-1; } } } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
d7bdf2b120e186a49ce275ba0d493f98
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; // import java.lang.*; // import java.math.*; public class Codeforces { static FastReader sc=new FastReader(); static PrintWriter out=new PrintWriter(System.out); static long mod=1000000007; // static long mod=998244353; static int MAX=Integer.MAX_VALUE; static int MIN=Integer.MIN_VALUE; static long MAXL=Long.MAX_VALUE; static long MINL=Long.MIN_VALUE; static ArrayList<Integer> graph[]; static boolean flag=false; public static void main (String[] args) throws java.lang.Exception { // code goes here // int t=I(); // outer:while(t-->0) // { int n=I(); int l=0,r=n; while(r-l>1){ int mid=(l+r)/2; int smax=myFun(l,r-1); if(smax<mid){ if(myFun(l,mid-1)==smax){ r=mid; }else{ l=mid; } }else{ if(myFun(mid,r-1)==smax){ l=mid; }else{ r=mid; } } } out.println("! "+r); out.flush(); // } out.close(); } public static int myFun(int st,int end) { if(st>=end)return -1; out.flush(); out.println("? "+(st+1)+" "+(end+1)); out.flush(); int p=I(); return p-1; } public static class pair { long a; long b; public pair(long val,long index) { a=val; b=index; } } public static class myComp implements Comparator<pair> { //sort in ascending order. public int compare(pair p1,pair p2) { if(p1.a==p2.a) return 0; else if(p1.a<p2.a) return -1; else return 1; } //sort in descending order. // public int compare(pair p1,pair p2) // { // if(p1.a==p2.a) // return 0; // else if(p1.a<p2.a) // return 1; // else // return -1; // } } public static long kadane(long a[],int n) { long max_sum=Long.MIN_VALUE,max_end=0; for(int i=0;i<n;i++){ max_end+=a[i]; if(max_sum<max_end){max_sum=max_end;} if(max_end<0){max_end=0;} } return max_sum; } public static void DFS(int s,boolean visited[]) { visited[s]=true; for(int i:graph[s]){ if(!visited[i]){ DFS(i,visited); } } } public static void setGraph(int n,int m) { graph=new ArrayList[n+1]; for(int i=0;i<=n;i++){ graph[i]=new ArrayList<>(); } for(int i=0;i<m;i++){ int u=I(),v=I(); graph[u].add(v); graph[v].add(u); } } public static int BS(long a[],long x,int ii,int jj) { // int n=a.length; int mid=0; int i=ii,j=jj; while(i<=j) { mid=(i+j)/2; if(a[mid]<x){ i=mid+1; }else if(a[mid]>x){ j=mid-1; }else{ return mid; } } return -1; } public static int lower_bound(int arr[],int s, int N, int X) { if(arr[N]<X)return N; if(arr[0]>X)return -1; int left=s,right=N; while(left<right){ int mid=(left+right)/2; if(arr[mid]==X) return mid; else if(arr[mid]>X){ if(mid>0 && arr[mid]>X && arr[mid-1]<=X){ return mid-1; }else{ right=mid-1; } }else{ if(mid<N && arr[mid+1]>X && arr[mid]<=X){ return mid; }else{ left=mid+1; } } } return left; } public static ArrayList<Integer> primeSieve(int n) { ArrayList<Integer> arr=new ArrayList<>(); boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int i = 2; i <= n; i++) { if (prime[i] == true) arr.add(i); } return arr; } // Fenwick / BinaryIndexed Tree USE IT - FenwickTree ft1=new FenwickTree(n); public static class FenwickTree { int farr[]; int n; public FenwickTree(int c) { n=c+1; farr=new int[n]; } // public void update_range(int l,int r,long p) // { // update(l,p); // update(r+1,(-1)*p); // } public void update(int x,int p) { for(;x<=n;x+=x&(-x)) { farr[x]+=p; } } public long get(int x) { long ans=0; for(;x>0;x-=x&(-x)) { ans=ans+farr[x]; } return ans; } } //Disjoint Set Union public static class DSU { int par[],rank[]; public DSU(int c) { par=new int[c+1]; rank=new int[c+1]; for(int i=0;i<=c;i++) { par[i]=i; rank[i]=0; } } public int find(int a) { if(a==par[a]) return a; return par[a]=find(par[a]); } public void union(int a,int b) { int a_rep=find(a),b_rep=find(b); if(a_rep==b_rep) return; if(rank[a_rep]<rank[b_rep]) par[a_rep]=b_rep; else if(rank[a_rep]>rank[b_rep]) par[b_rep]=a_rep; else { par[b_rep]=a_rep; rank[a_rep]++; } } } //SEGMENT TREE CODE // public static void segmentUpdate(int si,int ss,int se,int qs,int qe,long x) // { // if(ss>qe || se<qs)return; // if(qs<=ss && qe>=se) // { // seg[si][0]+=1L; // seg[si][1]+=x*x; // seg[si][2]+=2*x; // return; // } // int mid=(ss+se)/2; // segmentUpdate(2*si+1,ss,mid,qs,qe,x); // segmentUpdate(2*si+2,mid+1,se,qs,qe,x); // } // public static long segmentGet(int si,int ss,int se,int x,long f,long s,long t,long a[]) // { // if(ss==se && ss==x) // { // f+=seg[si][0]; // s+=seg[si][1]; // t+=seg[si][2]; // long ans=a[x]+(f*((long)x+1L)*((long)x+1L))+s+(t*((long)x+1L)); // return ans; // } // int mid=(ss+se)/2; // if(x>mid){ // return segmentGet(2*si+2,mid+1,se,x,f+seg[si][0],s+seg[si][1],t+seg[si][2],a); // }else{ // return segmentGet(2*si+1,ss,mid,x,f+seg[si][0],s+seg[si][1],t+seg[si][2],a); // } // } public static class myComp1 implements Comparator<pair1> { //sort in ascending order. public int compare(pair1 p1,pair1 p2) { if(p1.a==p2.a) return 0; else if(p1.a<p2.a) return -1; else return 1; } //sort in descending order. // public int compare(pair p1,pair p2) // { // if(p1.a==p2.a) // return 0; // else if(p1.a<p2.a) // return 1; // else // return -1; // } } public static class pair1 { long a; long b; public pair1(long val,long index) { a=val; b=index; } } public static ArrayList<pair1> mergeIntervals(ArrayList<pair1> arr) { //****************use this in main function-Collections.sort(arr,new myComp1()); ArrayList<pair1> a1=new ArrayList<>(); if(arr.size()<=1) return arr; a1.add(arr.get(0)); int i=1,j=0; while(i<arr.size()) { if(a1.get(j).b<arr.get(i).a) { a1.add(arr.get(i)); i++; j++; } else if(a1.get(j).b>arr.get(i).a && a1.get(j).b>=arr.get(i).b) { i++; } else if(a1.get(j).b>=arr.get(i).a) { long a=a1.get(j).a; long b=arr.get(i).b; a1.remove(j); a1.add(new pair1(a,b)); i++; } } return a1; } public static boolean isPalindrome(String s,int n) { for(int i=0;i<=n/2;i++){ if(s.charAt(i)!=s.charAt(n-i-1)){ return false; } } return true; } public static int gcd(int a,int b) { if(b==0) return a; else return gcd(b,a%b); } public static long gcd(long a,long b) { if(b==0) return a; else return gcd(b,a%b); } public static long fact(long n) { long fact=1; for(long i=2;i<=n;i++){ fact=((fact%mod)*(i%mod))%mod; } return fact; } public static long fact(int n) { long fact=1; for(int i=2;i<=n;i++){ fact=((fact%mod)*(i%mod))%mod; } return fact; } public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static void printArray(long a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(int a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(pair a[]) { for(int i=0;i<a.length;i++){ out.print(a[i].a+"->"+a[i].b+" "); } out.println(); } public static void printArray(char a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]); } out.println(); } public static void printArray(String a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(boolean a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(int a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(long a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(char a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArrayL(ArrayList<Long> arr) { for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); } out.println(); } public static void printArrayI(ArrayList<Integer> arr) { for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); } out.println(); } public static void printArrayS(ArrayList<String> arr) { for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); } out.println(); } public static void printMapInt(HashMap<Integer,Integer> hm){ for(Map.Entry<Integer,Integer> e:hm.entrySet()){ out.println(e.getKey()+"->"+e.getValue()); }out.println(); } public static void printMapLong(HashMap<Long,Long> hm){ for(Map.Entry<Long,Long> e:hm.entrySet()){ out.println(e.getKey()+"->"+e.getValue()); }out.println(); } //Modular Arithmetic public static long add(long a,long b) { a+=b; if(a>=mod)a-=mod; return a; } public static long sub(long a,long b) { a-=b; if(a<0)a+=mod; return a; } public static long mul(long a,long b) { return ((a%mod)*(b%mod))%mod; } public static long divide(long a,long b,long m) { a=mul(a,modInverse(b,m)); return a; } public static long modInverse(long a,long m) { int x=0,y=0; own p=new own(x,y); long g=gcdExt(a,m,p); if(g!=1){ out.println("inverse does not exists"); return -1; }else{ long res=((p.a%m)+m)%m; return res; } } public static long gcdExt(long a,long b,own p) { if(b==0){ p.a=1; p.b=0; return a; } int x1=0,y1=0; own p1=new own(x1,y1); long gcd=gcdExt(b,a%b,p1); p.b=p1.a - (a/b) * p1.b; p.a=p1.b; return gcd; } public static long pwr(long m,long n) { long res=1; m=m%mod; if(m==0) return 0; while(n>0) { if((n&1)!=0) { res=(res*m)%mod; } n=n>>1; m=(m*m)%mod; } return res; } public static class own { long a; long b; public own(long val,long index) { a=val; b=index; } } //Modular Airthmetic public static void sort(int[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public static void sort(char[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { char tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public static void sort(long[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public static int i(char ch){return Integer.parseInt(String.valueOf(ch));} public static int I(){return sc.I();} public static long L(){return sc.L();} public static String S(){return sc.S();} public static double D(){return sc.D();} } class FastReader { BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (st == null || !st.hasMoreElements()){ try { st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int I(){ return Integer.parseInt(next()); } long L(){ return Long.parseLong(next()); } double D(){ return Double.parseDouble(next()); } String S(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
07820147e1e4038aa24867e3223d0868
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class CodeForces { static BufferedReader br; public static void main(String[] args) throws Exception { br=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(br.readLine()); int l=1,r=n; while(l<r) { int ind=query(l,r); int mid=(l+r)/2; if(ind<=mid) { if(query(l,mid)==ind) r=mid; else l=mid+1; } else { if(query(mid+1,r)==ind) l=mid+1; else r=mid; } } System.out.println("! "+l); } public static int query(int l,int r) throws NumberFormatException, IOException { if(l==r)return -1; System.out.flush(); System.out.println("? "+l+" "+r); //System.out.flush(); return Integer.parseInt(br.readLine()); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
ef3103e0c4ce9c730d54e320fac11c91
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class CodeForces { static BufferedReader br; public static void main(String[] args) throws Exception { br=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(br.readLine()); int l=1,r=n; while(l<r) { int ind=query(l,r); int mid=(l+r)/2; if(ind<=mid) { if(query(l,mid)==ind) r=mid; else l=mid+1; } else { if(query(mid+1,r)==ind) l=mid+1; else r=mid; } } System.out.println("! "+l); } public static int query(int l,int r) throws NumberFormatException, IOException { if(l==r)return -1; System.out.flush(); System.out.println("? "+l+" "+r); System.out.flush(); return Integer.parseInt(br.readLine()); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
c4fc00f592076f623d091b9c49ee1f09
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.*; public class B { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int left = 1; int right = n; while(right - left + 1 > 2) { System.out.println("? "+ left + " "+right); int sMax = sc.nextInt(); int mid = left + (right - left) / 2; if(sMax < mid) { System.out.println("? "+ left + " "+mid); int sMax1 = sc.nextInt(); if(sMax == sMax1) { right = mid; } else { left = mid; } } else { System.out.println("? "+ mid + " "+right); int sMax2 = sc.nextInt(); if(sMax == sMax2) { left = mid; } else { right = mid; } } } System.out.println("? "+ left + " "+right); int sMax = sc.nextInt(); if(sMax == left) { System.out.println("! " + right); } else { System.out.println("! " + left); } System.out.flush(); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } // Use this instead of Arrays.sort() on an array of ints. Arrays.sort() is n^2 // worst case since it uses a version of quicksort. Although this would never // actually show up in the real world, in codeforces, people can hack, so // this is needed. static void ruffleSort(int[] a) { //ruffle int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n), temp=a[i]; a[i]=a[oi]; a[oi]=temp; } //then sort Arrays.sort(a); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
fec521848829b5eec1a8c9543f26053b
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.*; public class Main { static class point { long val, time, t3; point(long val, long time, int t3) { this.val = val; this.time = time; this.t3 = t3; } } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } static class comp implements Comparator<point> { public int compare(point a, point b) { if (a.val == b.val) { return Long.compare(b.time, a.time); } return Long.compare(a.val, b.val); } } static class TreeNode { int val; TreeNode left,right; TreeNode(int val){ this.val=val;left=null;right=null; } } static TreeNode maxx(int start,int end,int[] arr){ if(start>end){ return null; } int ind=0; int max=-1; for(int j=start;j<=end;j++){ if(arr[j]>max){ max=arr[j]; ind=j; } } TreeNode jj=new TreeNode(arr[ind]); jj.left=maxx(start,ind-1,arr); jj.right=maxx(ind+1,end,arr); return jj; } static void dfs(TreeNode root,int dep){ if(root==null){ return; } ans[hashMap.get(root.val)]=dep; dfs(root.left,dep+1); dfs(root.right,dep+1); } static int[] ans; static HashMap<Integer,Integer> hashMap; static class pont{ int val,index; pont(int val,int index){ this.val=val; this.index=index; } } static class compr implements Comparator<pont>{ public int compare(pont a,pont b){ return a.val-b.val; } } static class poin{ int src; long val; poin(int src,long val){ this.src=src; this.val=val; } } public static void main(String[] args) throws IOException { Scanner s = new Scanner(System.in); int n=s.nextInt(); int low=1,high=n; System.out.println("? "+low+" "+high ); System.out.println(); System.out.flush(); int secondmaxindex=s.nextInt(); if(low==secondmaxindex){ System.out.println("? "+secondmaxindex+" "+high ); System.out.println(); System.out.flush(); int secondmax2=s.nextInt(); if(secondmax2!=secondmaxindex){ high=secondmaxindex-1; low=1; int ans=low; while(low<=high){ int mid=(low)+(high-low)/2; if(mid==secondmaxindex){ break; } System.out.println("? "+mid+" "+secondmaxindex ); System.out.println(); System.out.flush(); int inpu=s.nextInt(); if(inpu==secondmaxindex){ ans=mid; low=mid+1; }else{ high=mid-1; } } System.out.println("! "+" "+ans); System.out.flush(); }else{ low=secondmaxindex+1; high=n; int ans=high; while(low<=high){ int mid=(low)+(high-low)/2; if(mid==secondmaxindex){ break; } System.out.println("? "+secondmaxindex+" "+mid); System.out.println(); System.out.flush(); int inpu=s.nextInt(); if(inpu==secondmaxindex){ ans=mid; high=mid-1; }else{ low=mid+1; } } System.out.println("! "+" "+ans); System.out.flush(); } } else{ System.out.println("? "+low+" "+secondmaxindex ); System.out.println(); System.out.flush(); int secondmax2=s.nextInt(); if(secondmax2==secondmaxindex){ high=secondmaxindex-1; low=1; int ans=low; while(low<=high){ int mid=(low)+(high-low)/2; if(mid==secondmaxindex){ break; } System.out.println("? "+mid+" "+secondmaxindex ); System.out.println(); System.out.flush(); int inpu=s.nextInt(); if(inpu==secondmaxindex){ ans=mid; low=mid+1; }else{ high=mid-1; } } System.out.println("! "+" "+ans); System.out.flush(); }else{ low=secondmaxindex+1; high=n; int ans=high; while(low<=high){ int mid=(low)+(high-low)/2; if(mid==secondmaxindex){ break; } System.out.println("? "+secondmaxindex+" "+mid); System.out.println(); System.out.flush(); int inpu=s.nextInt(); if(inpu==secondmaxindex){ ans=mid; high=mid-1; }else{ low=mid+1; } } System.out.println("! "+" "+ans); System.out.flush(); } } // int t=s.nextInt(); // int t=1; // for(int jj=0;jj<t;jj++){ // int n=s.nextInt(); // HashMap<Integer,HashMap<Integer,Integer>> has=new HashMap<>(); // int m=s.nextInt(); // int x=s.nextInt(); // int y=s.nextInt(); // long[] ti=new long[m]; // long[] ki=new long[m]; // HashMap<Integer,HashSet<Integer>> hash=new HashMap<>(); // for(int i=0;i<=n;i++){ // has.put(i,new HashMap<>()); // hash.put(i,new HashSet<>()); // } // for(int i=0;i<m;i++){ // int a=s.nextInt(); // int b=s.nextInt(); //// if(has.get(a)==null){ //// has.put(a,new HashMap<>()); //// } //// if(has.get(b)==null){ //// has.put(b,new HashMap<>()); //// } // has.get(a).put(b,i); // has.get(b).put(a,i); // ti[i]=s.nextLong(); // ki[i]=s.nextLong(); // } // // long[] vis=new long[n+1]; // Arrays.fill(vis,Long.MAX_VALUE); // vis[x]=0; // Queue<poin> qu=new LinkedList<>(); // qu.add(new poin(x,0)); // long ans=Long.MAX_VALUE; // while(!qu.isEmpty()){ // poin te=qu.poll(); // if(te.src==y){ // ans=Math.min(ans,te.val);continue; // } // for(Integer v:has.get(te.src).keySet()){ // long ll=(ki[has.get(te.src).get(v)]+(te.val%ki[has.get(te.src).get(v)]))%ki[has.get(te.src).get(v)]+ti[has.get(te.src).get(v)]; //// if(te.val>ki[has.get(te.src).get(v)]){ //// long ij=(long)Math.ceil((double)te.val/(double)ki[has.get(te.src).get(v)]); //// ll=(long)((long)ij*(long)ki[has.get(te.src).get(v)]); //// }else{ //// // long ij=(long)Math.ceil((double)ki[has.get(te.src).get(v)]/(double)te.val); //// if(te.val==0){ //// ll=0; //// }else{ //// ll=(long)((long)(long)ki[has.get(te.src).get(v)]);} //// } //// ll=(long)((long)ll+(long)ti[has.get(te.src).get(v)]); //// long ll= (long)Math.max((long)((long)te.val+(long)ti[has.get(te.src).get(v)]), (long)((long)ki[has.get(te.src).get(v)]*(long)Math.floor((double)ki[has.get(te.src).get(v)]/(double)(te.val+ti[has.get(te.src).get(v)])))); //// if( !hash.get(v).contains(te.src) ){ //// vis[v]=ll; //// hash.get(v).add(te.src); //// qu.add(new poin( v,vis[v])); //// } // if(vis[v]>=ll){ // vis[v]=ll; //// hash.get(v).add(te.src); // qu.add(new poin( v,vis[v])); // // } // } // } // if(ans==Long.MAX_VALUE){ // System.out.println(-1); // }else{ // System.out.println(ans);} // } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
66e0e49e2129c36d4a4041b39c735f8e
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class Guessing_The_Greatest { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void shuffle(int[] a) { Random r = new Random(); for (int i = 0; i <= a.length - 2; i++) { int j = i + r.nextInt(a.length - i); swap(a, i, j); } Arrays.sort(a); } public static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static FastReader t = new FastReader(); static PrintWriter o = new PrintWriter(System.out); public static void main(String[] args) { // TODO Auto-generated method stub int n = t.nextInt(); int left = 0, right = n; while (right - left > 1) { int mid = (left + right) >> 1; int sMax = query(left, right - 1); if (sMax < mid) { if (query(left, mid - 1) == sMax) { right = mid; } else { left = mid; } } else { if (query(mid, right - 1) == sMax) { left = mid; } else { right = mid; } } } o.println("! " + right); o.flush(); o.close(); } private static int query(int left, int right) { if (left >= right) return -1; o.println("? " + (left + 1) + " " + (right + 1)); o.flush(); return t.nextInt() - 1; } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
93bac2d924e68a723d057eef4b4f00f3
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.*; import java.awt.*; public class C { BufferedReader in; PrintWriter ob; StringTokenizer st; public static void main(String[] args) throws IOException { new C().run(); } void run() throws IOException { in=new BufferedReader(new InputStreamReader(System.in)); ob=new PrintWriter(System.out); solve(); System.out.flush(); } void solve() throws IOException { int n = ni(); System.out.println("? 1 "+n); int s_max_pos = ni(); int left_sec_max = 0; if( s_max_pos != 1 ) { System.out.println("? 1"+" "+s_max_pos); left_sec_max = ni(); } if( s_max_pos != 1 && left_sec_max == s_max_pos ) { int left = 1, right = s_max_pos; while( right - left > 1 ) { int mid = left + (right - left)/2; System.out.println("? "+mid+" "+s_max_pos); int res = ni(); if ( res == s_max_pos ) left = mid; else right = mid - 1; } if( right == s_max_pos ) { System.out.println("! "+left); return; } System.out.println("? "+right+" "+s_max_pos); int f = ni(); if( f == s_max_pos ) System.out.println("! "+right); else System.out.println("! "+left); } else { int left = s_max_pos, right = n; while( right - left > 1 ) { int mid = left + (right - left)/2; System.out.println("? "+s_max_pos+" "+mid); int res = ni(); if ( res == s_max_pos ) right = mid; else left = mid + 1; } if( left == s_max_pos ) { System.out.println("! "+right); return; } System.out.println("? "+s_max_pos+" "+left); int f = ni(); if( f == s_max_pos ) System.out.println("! "+left); else System.out.println("! "+right); } } String ns() throws IOException { return nextToken(); } long nl() throws IOException { return Long.parseLong(nextToken()); } int ni() throws IOException { return Integer.parseInt(nextToken()); } double nd() throws IOException { return Double.parseDouble(nextToken()); } String nextToken() throws IOException { if(st==null || !st.hasMoreTokens()) st=new StringTokenizer(in.readLine()); return st.nextToken(); } int[] nia(int start,int b) throws IOException { int a[]=new int[b]; for(int i=start;i<b;i++) a[i]=ni(); return a; } long[] nla(int start,int n) throws IOException { long a[]=new long[n]; for (int i=start; i<n ;i++ ) { a[i]=nl(); } return a; } public void tr(Object... o) { ob.println(Arrays.deepToString(o)); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
e360ccc296c1f6d16abbfde05e2fb3bd
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.*; import java.awt.*; public class C { BufferedReader in; PrintWriter ob; StringTokenizer st; public static void main(String[] args) throws IOException { new C().run(); } void run() throws IOException { in=new BufferedReader(new InputStreamReader(System.in)); ob=new PrintWriter(System.out); solve(); System.out.flush(); } void solve() throws IOException { int n = ni(); System.out.println("? 1 "+n); int s_max_pos = ni(); int left_sec_max = 0; if( s_max_pos != 1 ) { System.out.println("? 1"+" "+s_max_pos); left_sec_max = ni(); } if( s_max_pos != 1 && left_sec_max == s_max_pos ) { int left = 1, right = s_max_pos; while( right - left > 1 ) { int mid = left + (right - left)/2; System.out.println("? "+mid+" "+s_max_pos); int res = ni(); if ( res == s_max_pos ) left = mid; else right = mid - 1; } if( right == s_max_pos ) { System.out.println("! "+left); return; } System.out.println("? "+right+" "+s_max_pos); int f = ni(); if( f == s_max_pos ) System.out.println("! "+right); else System.out.println("! "+left); } else { int left = s_max_pos, right = n; while( right - left > 1 ) { int mid = left + (right - left)/2; System.out.println("? "+s_max_pos+" "+mid); int res = ni(); if ( res == s_max_pos ) right = mid; else left = mid + 1; } if( left == s_max_pos ) { System.out.println("! "+right); return; } System.out.println("? "+s_max_pos+" "+left); int f = ni(); if( f == s_max_pos ) System.out.println("! "+left); else System.out.println("! "+right); } } String ns() throws IOException { return nextToken(); } long nl() throws IOException { return Long.parseLong(nextToken()); } int ni() throws IOException { return Integer.parseInt(nextToken()); } double nd() throws IOException { return Double.parseDouble(nextToken()); } String nextToken() throws IOException { if(st==null || !st.hasMoreTokens()) st=new StringTokenizer(in.readLine()); return st.nextToken(); } int[] nia(int start,int b) throws IOException { int a[]=new int[b]; for(int i=start;i<b;i++) a[i]=ni(); return a; } long[] nla(int start,int n) throws IOException { long a[]=new long[n]; for (int i=start; i<n ;i++ ) { a[i]=nl(); } return a; } public void tr(Object... o) { ob.println(Arrays.deepToString(o)); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
af076ed9303a75a7e29b5186eaec3a7d
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.Scanner; public class C { static Scanner sc = new Scanner(System.in); public static long ask(long l, long r) { if(l >= r) return -1; System.out.println("? " + l + " " + r); long x = sc.nextLong(); return x; } public static void main(String[] args) { long n = sc.nextLong(); long pos = ask(1, n); if(ask(1, pos) == pos) { long ini = 1, fin = pos; while(ini < fin) { long mid = (ini + fin + 1)/2; if(ask(mid, n) == pos) { ini = mid; } else { fin = mid - 1; } } System.out.println("! " + ini); } else { long ini = pos, fin = n; while(ini < fin) { long mid = (ini + fin)/2; if(ask(1, mid) == pos) { fin = mid; } else { ini = mid+1; } } System.out.println("! " + ini); } sc.close(); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
5f67a419f74b148f88801dea22f46b37
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class C { public static void main(String[] args) { FastScanner fs = new FastScanner(); int t = 1;//fs.nextInt(); while (t-- > 0) { int n = fs.nextInt(); int overall = query( 1, n, fs ); boolean left = false; if (overall != 1) { if (query( 1, overall, fs ) == overall) left = true; } if (left) { int low = 1, high = overall - 1; while (low <= high) { int mid = (low + high) >> 1; if (query( mid, overall, fs ) == overall) low = mid + 1; else high = mid - 1; } System.out.println( "! " + high ); System.out.flush(); } else { int low = overall + 1, high = n; while (low <= high) { int mid = (low + high) >> 1; if (query( overall, mid, fs ) == overall) high = mid - 1; else low = mid + 1; } System.out.println( "! " + low ); System.out.flush(); } } } static int query(int low, int high, FastScanner fs) { System.out.println( "? " + low + " " + high ); System.out.flush(); return fs.nextInt(); } private static class FastScanner { BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) ); StringTokenizer st = new StringTokenizer( "" ); public String next() { while (!st.hasMoreElements()) try { st = new StringTokenizer( br.readLine() ); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt( next() ); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong( next() ); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
2a6293c7e926e67f4adc6deff9a9e4f8
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
// Main Code at the Bottom import java.util.*; import java.io.*; public class Main{ //Fast IO class static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { boolean env=System.getProperty("ONLINE_JUDGE") != null; //env=true; if(!env) { try { br=new BufferedReader(new FileReader("src\\input.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } } else br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long MOD=(long)1e9+7; //debug static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } // static FastReader sc=new FastReader(); static Scanner sc=new Scanner(System.in); static PrintWriter out=new PrintWriter(System.out); //Global variables and functions static int query(int l,int r) { System.out.println("? "+(l+1)+" "+(r+1)); out.flush(); int x=sc.nextInt()-1; out.flush(); return x; } //Main function(The main code starts from here) public static void main (String[] args) throws java.lang.Exception { int test=1,t=0; //test=sc.nextInt(); while(test-->0) { int n=sc.nextInt(); int l=0,r=n-1; while(l<r) { int mid=l+(r-l)/2; int x=query(l,r); if(r-l==1) { if(x==l) l=r; break; } if(x<=mid) { int y=query(l,mid); if(y==x) r=mid; else l=mid+1; } else { int y=query(mid,r); if(y==x) l=mid; else r=mid-1; } } out.println("! "+(l+1)); } out.flush(); out.close(); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
b86bec9bcb2b309cdf191819e22870f9
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class GuessingTheGreatest implements Closeable { private InputReader in = new InputReader(System.in); private PrintWriter out = new PrintWriter(System.out); public void solve() { int n = in.ni(); int pivot = ask(0, n - 1); int left, right; if (pivot > 0 && pivot < n - 1) { int leftHalf = ask(0, pivot); if (leftHalf == pivot) { left = 0; right = pivot - 1; } else { left = pivot + 1; right = n - 1; } } else if (pivot == n - 1) { left = 0; right = n - 2; } else { left = 1; right = n - 1; } int max; if (pivot < left) { max = n + 5; while (left <= right) { int mid = left + (right - left) / 2; int response = ask(pivot, mid); if (response == pivot) { max = Math.min(max, mid); right = mid - 1; } else { left = mid + 1; } } } else { max = -1; while (left <= right) { int mid = left + (right - left) / 2; int response = ask(mid, pivot); if (response == pivot) { max = Math.max(mid, max); left = mid + 1; } else { right = mid - 1; } } } answer(max); } private int ask(int left, int right) { out.printf("? %d %d\n", left + 1, right + 1); out.flush(); return in.ni() - 1; } private void answer(int idx) { out.printf("! %d\n", idx + 1); out.flush(); } @Override public void close() throws IOException { in.close(); out.close(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int ni() { return Integer.parseInt(next()); } public long nl() { return Long.parseLong(next()); } public void close() throws IOException { reader.close(); } } public static void main(String[] args) throws IOException { try (GuessingTheGreatest instance = new GuessingTheGreatest()) { instance.solve(); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
bfcfd360d6ae0364f1d1c0c1c1abf6a1
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.*; public class C { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static FastReader s = new FastReader(); static PrintWriter out = new PrintWriter(System.out); private static int[] rai(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextInt(); } return arr; } private static int[][] rai(int n, int m) { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = s.nextInt(); } } return arr; } private static long[] ral(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextLong(); } return arr; } private static long[][] ral(int n, int m) { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = s.nextLong(); } } return arr; } private static int ri() { return s.nextInt(); } private static long rl() { return s.nextLong(); } private static String rs() { return s.next(); } static long gcd(long a,long b) { if(b==0) { return a; } return gcd(b,a%b); } static boolean isPrime(int n) { //check if n is a multiple of 2 if (n % 2 == 0) return false; //if not, then just check the odds for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static int MOD=1000000007; static long mpow(long base,long pow) { long res=1; while(pow>0) { if(pow%2==1) { res=(res*base)%MOD; } pow>>=1; base=(base*base)%MOD; } return res; } static public int minimumSize(int[] nums, int m) { PriorityQueue<Integer> pq=new PriorityQueue<>(Collections.reverseOrder()); for(int i:nums) { pq.add(i); } System.out.println(pq); for(int i=0;i<m;i++) { int val=pq.remove(); if(val==1) { return 1; } int f=val/2; int s=val-f; pq.add(f); pq.add(s); System.out.println(pq); } return pq.remove(); } public static void main(String[] args) { // StringBuilder ans = new StringBuilder(); // int t = ri(); int t = 1; while (t-- > 0) { int n=ri(); int l=1,r=n; boolean f=false; while(l<r) { out.println("? "+l+" "+r); out.flush(); int ind=ri(); if(l+1==r) { if(ind==r) { out.println("! " + l); out.flush(); f = true; }else { out.println("! " + r); out.flush(); f = true; } break; } int mid=(l+r)/2; // System.out.println("mid="+mid); if(ind<=mid) { out.println("? "+l+" "+mid); out.flush(); int index=ri(); if(index==ind) { r=mid; } else { l=mid+1; } } else { if(mid+1==r) { r=mid; continue; } out.println("? "+(mid+1)+" "+r); out.flush(); int index=ri(); if(ind==index) { l=mid+1; } else { r=mid; } } // if(ind==l) // { // l+=1; // continue; // } // else if(ind==r) // { // r-=1; // continue; // } // out.println("? "+l+" "+ind); // out.flush(); // int index=ri(); // if(index==ind) // { // r=ind-1; // } // else // { // l=ind+1; // } } if(!f) { out.println("! " + l); out.flush(); } } // out.print(ans.toString()); // out.flush(); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
10de2140c70b33e8f22c144bab520c79
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
//created by Whiplash99 import java.io.*; import java.util.*; public class C { private static int count; private static void flush(){System.out.flush();} private static void ask(int l, int r) throws Exception { count++; if(count>40) throw new Exception(); System.out.println("? "+l+" "+r); flush(); } private static void answer(int x) { System.out.println("! "+x); flush(); } public static void main(String[] args) throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int i,N; count=0; N=Integer.parseInt(br.readLine().trim()); ask(1,N); int p=Integer.parseInt(br.readLine().trim()); boolean flag=true; int ans=0; if(p>1) { ask(1,p); ans=p-1; int tmp=Integer.parseInt(br.readLine().trim()); if(tmp==p) { flag=false; int l=1,r=p-1,mid; while (l<=r) { mid=(l+r)/2; ask(mid,p); tmp=Integer.parseInt(br.readLine().trim()); if(tmp==p) { ans=mid; l=mid+1; } else r=mid-1; } } } if(flag) { int l=p+1,r=N,mid; ans=p+1; while (l<=r) { mid=(l+r)/2; ask(p,mid); int tmp=Integer.parseInt(br.readLine().trim()); if(tmp==p) { ans=mid; r=mid-1; } else l=mid+1; } } answer(ans); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
d24231f56378789b792d114026a09957
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.FileReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Khater */ public class HH { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); C1GuessingTheGreatestEasyVersion solver = new C1GuessingTheGreatestEasyVersion(); solver.solve(1, in, out); out.close(); } static class C1GuessingTheGreatestEasyVersion { int idx; public void solve(int testNumber, Scanner sc, PrintWriter pw) { int t = 1; // t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); idx = ask(1, n, sc, pw); int b = ask(1,idx,sc,pw); int a = ask(idx,n,sc,pw); int ans =1; if(b==idx) { ans = bbs(1,idx,sc,pw); }else { ans = abs(idx,n,sc,pw); } pw.println("! "+ans); } } int abs(int l, int r, Scanner sc, PrintWriter pw) { if (l >= r-1) { if(ask(idx, l, sc, pw)!=idx) return r; else return l; } int mid = (l+r)/2; int b = ask(idx, mid, sc, pw); if(b==idx)return abs(l,mid,sc,pw); else return abs(mid+1,r,sc,pw); } int bbs(int l, int r, Scanner sc, PrintWriter pw) { if (l >= r-1) { if(ask(r, idx, sc, pw)==idx) return r; else return l; } int mid = (l+r)/2; int a = ask(mid, idx, sc, pw); if(a==idx)return bbs(mid,r,sc,pw); else return bbs(l,mid-1,sc,pw); } int ask(int l, int r, Scanner sc, PrintWriter pw) { if(l==r) return -1; pw.println("? " + l + " " + r); pw.flush(); return sc.nextInt(); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
e66d64880205fedf17c4833eafa97b36
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class EdA { static long[] mods = {1000000007, 998244353, 1000000009}; static long mod = mods[0]; public static MyScanner sc; public static PrintWriter out; public static void main(String[] omkar) throws Exception{ // TODO Auto-generated method stub sc = new MyScanner(); out = new PrintWriter(System.out); // String input1 = bf.readLine().trim(); // String input2 = bf.readLine().trim(); // COMPARING INTEGER OBJECTS U DO DOT EQUALS NOT == int lr = 0;//-1 = left, 1= right; int n = sc.nextInt(); out.println("? 1 " + n); out.flush(); int pos = sc.nextInt(); if (pos == 1){ lr = 1; } else if (pos == n){ lr = -1; } else{ out.println("? 1 " + pos); out.flush(); int left = sc.nextInt(); if (left == pos){ lr = -1; } else{ lr = 1; } } if (lr == -1){ int l = 1; int r = pos-1; while(l < r){ int mid = (l+r+1)/2; out.println("? " + mid + " " + pos); out.flush(); int v = sc.nextInt(); if (v == pos){ l = mid; } else{ r = mid-1; } } out.println("! " + l); out.close(); } else{ int l = pos+1; int r = n; while(l < r){ int mid = (l+r)/2; out.println("? " + pos + " " + mid); out.flush(); int v = sc.nextInt(); if (v == pos){ r = mid; } else{ l = mid+1; } } out.println("! " + l); out.close(); } // for(int j = 0;j<array.length;j++){ // out.print(array[j] + " "); // } // out.println(); } static class Pair implements Comparable<Pair>{ private int x; private int y; public Pair(int x, int y){ this.x = x; this.y = y; } public int compareTo(Pair other){ return Integer.compare(x, other.x); } } static class Pair2 implements Comparable<Pair2>{ private int x; private int y; public Pair2(int x, int y){ this.x = x; this.y = y; } public int compareTo(Pair2 other){ return Integer.compare(y, other.y); } } public static void sort(int[] array){ ArrayList<Integer> copy = new ArrayList<>(); for (int i : array) copy.add(i); Collections.sort(copy); for(int i = 0;i<array.length;i++) array[i] = copy.get(i); } static String[] readArrayString(int n){ String[] array = new String[n]; for(int j =0 ;j<n;j++) array[j] = sc.next(); return array; } static int[] readArrayInt(int n){ int[] array = new int[n]; for(int j = 0;j<n;j++) array[j] = sc.nextInt(); return array; } static int[] readArrayInt1(int n){ int[] array = new int[n+1]; for(int j = 1;j<=n;j++){ array[j] = sc.nextInt(); } return array; } static long[] readArrayLong(int n){ long[] array = new long[n]; for(int j =0 ;j<n;j++) array[j] = sc.nextLong(); return array; } static double[] readArrayDouble(int n){ double[] array = new double[n]; for(int j =0 ;j<n;j++) array[j] = sc.nextDouble(); return array; } static int minIndex(int[] array){ int minValue = Integer.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static int minIndex(long[] array){ long minValue = Long.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static int minIndex(double[] array){ double minValue = Double.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static long power(long x, long y){ if (y == 0) return 1; if (y%2 == 1) return (x*power(x, y-1))%mod; return power((x*x)%mod, y/2)%mod; } static void verdict(boolean a){ out.println(a ? "YES" : "NO"); } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } //StringJoiner sj = new StringJoiner(" "); //sj.add(strings) //sj.toString() gives string of those stuff w spaces or whatever that sequence is
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
273d96f94f76317c35ffef67b9acc9c4
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.*; public class Main{ static int ask(int l,int r) throws IOException { if(l==r)return -1; pw.println("? "+l+" "+r); pw.flush(); return sc.nextInt(); } static void main() throws Exception{ int n=sc.nextInt(); int scnd=ask(1, n); int lo=1,hi=n; while((hi-lo+1)>2) { int mid=(lo+hi)>>1; if(scnd<=mid) { int left=ask(lo, mid); if(left==scnd) { hi=mid; } else { lo=mid+1; scnd=ask(lo, hi); } } else { int right=ask(mid+1, hi); if(right==scnd) { lo=mid+1; } else { hi=mid; scnd=ask(lo, hi); } } } if(lo==hi) { pw.println("! "+lo); } else pw.println("! "+(ask(lo, hi)==lo?hi:lo)); } public static void main(String[] args) throws Exception{ sc=new MScanner(System.in); pw = new PrintWriter(System.out); int tc=1; // tc=sc.nextInt(); for(int i=1;i<=tc;i++) { // pw.printf("Case %d:\n", i); main(); } pw.flush(); } static PrintWriter pw; static MScanner sc; static class MScanner { StringTokenizer st; BufferedReader br; public MScanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public MScanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int[] intArr(int n) throws IOException { int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public long[] longArr(int n) throws IOException { long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public int[] intSortedArr(int n) throws IOException { int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt(); shuffle(in); Arrays.sort(in); return in; } public long[] longSortedArr(int n) throws IOException { long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong(); shuffle(in); Arrays.sort(in); return in; } public Integer[] IntegerArr(int n) throws IOException { Integer[]in=new Integer[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public Long[] LongArr(int n) throws IOException { Long[]in=new Long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } static void shuffle(int[]in) { for(int i=0;i<in.length;i++) { int idx=(int)(Math.random()*in.length); int tmp=in[i]; in[i]=in[idx]; in[idx]=tmp; } } static void shuffle(long[]in) { for(int i=0;i<in.length;i++) { int idx=(int)(Math.random()*in.length); long tmp=in[i]; in[i]=in[idx]; in[idx]=tmp; } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output
PASSED
0a726a4df6728001b289c0bcc70e1beb
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
//package Div2.C; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class GuessingTheGreatest { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int u = 0; int v = n - 1; int res = -1; while (u < v) { if (res == -1) { System.out.format("? %d %d\n", u + 1, v + 1); res = Integer.parseInt(br.readLine()); res--; } if (v - u == 1) { if (res == u) u++; else v--; } else { int mid = (u + v) / 2; if (res <= mid) { System.out.format("? %d %d\n", u + 1, mid + 1); int res1 = Integer.parseInt(br.readLine()); res1--; if (res == res1) { v = mid; res = res1; } else { u = mid + 1; res = -1; } } else { System.out.format("? %d %d\n", mid + 1, v + 1); int res1 = Integer.parseInt(br.readLine()); res1--; if (res == res1) { u = mid; res = res1; } else { v = mid - 1; res = -1; } } } } System.out.format("! %d\n", u + 1); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 8
standard input
[ "binary search", "interactive" ]
b3e8fe76706ba17cb285c75459508334
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,600
null
standard output