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
sequencelengths
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
7e3ee5c2909d60edc7995ce13ff28e96
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) > n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws Exception { for (int i = 2; i < 25; i++) { System.out.println("? 1 "+i); long n = sc.nextLong(); System.out.println("? "+i+" 1"); long m = sc.nextLong(); if(n==-1) { System.out.println("! "+(i-1)); break; } if(n!=m) { System.out.println("! "+(n+m)); break; } } pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws FileNotFoundException { 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 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 { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } public int[] nextArrint(int size) throws IOException { int[] a = new int[size]; for (int i = 0; i < a.length; i++) { a[i] = sc.nextInt(); } return a; } public long[] nextArrlong(int size) throws IOException { long[] a = new long[size]; for (int i = 0; i < a.length; i++) { a[i] = sc.nextLong(); } return a; } public int[][] next2dArrint(int rows, int columns) throws IOException { int[][] a = new int[rows][columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { a[i][j] = sc.nextInt(); } } return a; } public long[][] next2dArrlong(int rows, int columns) throws IOException { long[][] a = new long[rows][columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { a[i][j] = sc.nextLong(); } } return a; } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
a71a2ff5d75f9fc517594326a8c7122a
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.text.DecimalFormat; import java.util.*; import javax.swing.plaf.basic.BasicInternalFrameTitlePane.RestoreAction; /* */ public class Codeforces { static long mod= 10000_0000_7; static class Node{ int val, ind; Node(int val, int ind){ this.val=val; this.ind=ind; } } public static void main(String[] args) throws Exception { PrintWriter out=new PrintWriter(System.out); FastScanner fs=new FastScanner(); // DecimalFormat formatter= new DecimalFormat("#0.000000"); // int t=fs.nextInt(); int t=1; outer:for(int time=1;time<=t;time++) { long ans=2; int cnt=0; long l=2, r = (long)1e+7; while(l<r&&cnt<50) { long mid = (l+r+1)/2; System.out.println("? "+(mid-1)+" "+mid); long a=fs.nextLong(); if(a==-1) { cnt++; r=mid-1; continue; } else { l=mid; } System.out.println("? "+mid+" "+(mid-1)); long b= fs.nextLong(); cnt++; if(a!=b) ans= Math.max(ans, a+b); } if(l>ans) ans=l; System.out.println("! "+ans); } out.close(); } static long pow(long a,long b) { long res=1; while(b!=0) { if((b&1)!=0) { res*=a; res%=mod; } a*=a; a%=mod; b=b>>1; } return res; } static long gcd(long a,long b) { if(b==0) return a; return gcd(b,a%b); } static long nck(int n,int k) { if(k>n) return 0; long res=1; res*=fact(n); res%=mod; res*=modInv(fact(k)); res%=mod; res*=modInv(fact(n-k)); res%=mod; return res; } static long fact(long n) { // return fact[(int)n]; long res=1; for(int i=2;i<=n;i++) { res*=i; res%=mod; } return res; } static long modInv(long n) { return pow(n,mod-2); } static void sort(int[] a) { //suffle int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n); int temp=a[i]; a[i]=a[oi]; a[oi]=temp; } //then sort Arrays.sort(a); } static void sort(long[] a) { //suffle int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n); long temp=a[i]; a[i]=a[oi]; a[oi]=temp; } //then sort 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(); } String nextLine() { String str=""; try { str= (br.readLine()); } catch (IOException e) { e.printStackTrace(); } return str; } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } long[] readArrayL(int n) { long a[]=new long[n]; for(int i=0;i<n;i++) a[i]=nextLong(); return a; } 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
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
624424d146ffc0e618d26787fd0c3bc8
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.util.Scanner; public class ProblemE { /** * @param args */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int attempts = 0; int current = 2; while (attempts < 50) { attempts++; System.out.println("? 1 " + current); long ans = sc.nextLong(); if (ans == -1) { System.out.println("! " + (current - 1)); break; } else { System.out.println("? " + current + " 1"); long ans2 = sc.nextLong(); if (ans != ans2) { System.out.println("! " + (ans + ans2)); break; } else { current++; } attempts++; } } sc.close(); } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
18edce1229d382b8f241b444b29583cd
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.io.*; import java.util.*; public class E { public static void main(String[] args) { FastScanner fs=new FastScanner(); PrintWriter out = new PrintWriter(System.out); // int T = fs.nextInt(); // for (int tt=0; tt<T; tt++) { // } long res = 0; for(int i=2; i<=25; i++) { System.out.println("? 1 "+i); long x = fs.nextLong(); System.out.println("? "+i+" "+1); long y = fs.nextLong(); if(x==-1) { res=i-1; break; } if(x!=y) { res=x+y; break; } } System.out.println("! "+ res); System.out.flush(); out.close(); } 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()); } 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; } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
e0effed36836a72b75fb3646274ada2b
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { static InputReader sc; static PrintWriter out; private static final int maxn = (int)(1e5 + 7); public static Long ask(int a, int b) throws Exception { out.println("? " + a + " " + b); out.flush(); Long ret = sc.nextLong(); return ret; } public static void solve() throws Exception { for(int i = 1; i <= 25; i ++ ) { long l = ask(i, i + 1); long r = ask(i + 1, i); if(l == -1L) { out.println("! " + (i - 1)); return ; }else if(l != r) { out.println("! " + (l + r)); return ; } } } public static void prepare() throws Exception { sc = new InputReader(System.in); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); } public static void main(String[] args) throws Exception { prepare(); int T = 1; // T = sc.nextInt(); while(T -- > 0) { solve(); } out.flush(); return ; } static class InputReader { BufferedReader br; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } public int nextInt() throws IOException { int c = br.read(); while (c <= 32) { c = br.read(); } boolean negative = false; if (c == '-') { negative = true; c = br.read(); } int x = 0; while (c > 32) { x = x * 10 + c - '0'; c = br.read(); } return negative ? -x : x; } public long nextLong() throws IOException { int c = br.read(); while (c <= 32) { c = br.read(); } boolean negative = false; if (c == '-') { negative = true; c = br.read(); } long x = 0; while (c > 32) { x = x * 10 + c - '0'; c = br.read(); } return negative ? -x : x; } public String next() throws IOException { int c = br.read(); while (c <= 32) { c = br.read(); } StringBuilder sb = new StringBuilder(); while (c > 32) { sb.append((char) c); c = br.read(); } return sb.toString(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } } static class os extends PrintStream { private final BufferedWriter writer; public os(OutputStream out) { super(out); writer=new BufferedWriter(new OutputStreamWriter(out)); } public void write(int b) { try { out.write(b); } catch (IOException e) { e.printStackTrace(); } } public void write(byte buf[], int off, int len) { try { out.write(buf, off, len); } catch (IOException e) { e.printStackTrace(); } } private void write(char buf[]) { try { writer.write(buf); } catch (IOException e) { e.printStackTrace(); } } private void write(String s) { try { writer.write(s); } catch (IOException e) { e.printStackTrace(); } } private void newLine() { try { writer.newLine(); } catch (IOException e) { e.printStackTrace(); } } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
acab8a96a0a34c004e11ed6cb23b29bb
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { static InputReader sc; static PrintWriter out; private static final int maxn = (int)(1e5 + 7); public static Long ask(int a, int b) throws Exception { out.println("? " + a + " " + b); out.flush(); Long ret = sc.nextLong(); return ret; } public static void solve() throws Exception { for(int i = 1; i <= 25; i ++ ) { Long l = ask(i, i + 1); Long r = ask(i + 1, i); if(l == -1L) { out.println("! " + (i - 1)); return ; }else if(!l.equals(r)) { out.println("! " + (l + r)); return ; } } } public static void prepare() throws Exception { sc = new InputReader(System.in); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); } public static void main(String[] args) throws Exception { prepare(); int T = 1; // T = sc.nextInt(); while(T -- > 0) { solve(); } out.flush(); return ; } static class InputReader { BufferedReader br; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } public int nextInt() throws IOException { int c = br.read(); while (c <= 32) { c = br.read(); } boolean negative = false; if (c == '-') { negative = true; c = br.read(); } int x = 0; while (c > 32) { x = x * 10 + c - '0'; c = br.read(); } return negative ? -x : x; } public long nextLong() throws IOException { int c = br.read(); while (c <= 32) { c = br.read(); } boolean negative = false; if (c == '-') { negative = true; c = br.read(); } long x = 0; while (c > 32) { x = x * 10 + c - '0'; c = br.read(); } return negative ? -x : x; } public String next() throws IOException { int c = br.read(); while (c <= 32) { c = br.read(); } StringBuilder sb = new StringBuilder(); while (c > 32) { sb.append((char) c); c = br.read(); } return sb.toString(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } } static class os extends PrintStream { private final BufferedWriter writer; public os(OutputStream out) { super(out); writer=new BufferedWriter(new OutputStreamWriter(out)); } public void write(int b) { try { out.write(b); } catch (IOException e) { e.printStackTrace(); } } public void write(byte buf[], int off, int len) { try { out.write(buf, off, len); } catch (IOException e) { e.printStackTrace(); } } private void write(char buf[]) { try { writer.write(buf); } catch (IOException e) { e.printStackTrace(); } } private void write(String s) { try { writer.write(s); } catch (IOException e) { e.printStackTrace(); } } private void newLine() { try { writer.newLine(); } catch (IOException e) { e.printStackTrace(); } } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
72f2126b6641b3a1f26af23bb0ff49df
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
//package Codeforces; import java.io.*; public class E { public static void main (String[] Z) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); long one = -1; for (int i = 4 ; i < 27 ; i++) { System.out.printf("? 1 %d\n", i); System.out.flush(); long n1 = Long.parseLong(br.readLine()); System.out.printf("? %d 1\n", i); System.out.flush(); long n2 = Long.parseLong(br.readLine()); if(n1 == one || n2 == one) { System.out.printf("! %d\n", i-1); break; } if(n1 != n2) { long ans = n1+n2; System.out.printf("! %d\n", ans); System.out.flush(); return; } // else { // long sum = n1+n2+1; // System.out.printf("? %d 1\n", sum); // System.out.flush(); // // n2 = Long.parseLong(br.readLine()); // // if(n2 == one) { // long ans = n1+n2; // System.out.printf("! %d\n", ans); // break; // } // // // } } // END OF CODE } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
793f21348620ac57f7c99f90f6db1150
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; /** * @author Nervose.Wu * @date 2022/10/3 12:27 */ public final class S820E { public static void main(String[] args) { MyScanner sc = new MyScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); outer: while (true){ int target = 3; for (int i = 0; i < 26; i++) { out.println("? 1 " + target); out.flush(); long first = sc.nextLong(); if (first == -1) { out.println("! " + (target - 1)); break outer; } out.println("? " + target + " 1"); out.flush(); long second = sc.nextLong(); if(first!=second){ out.println("! " + (first+second)); break outer; } target++; } } out.close(); } 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
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
68d6e1ffc5f4362765090c0ef4e2c03d
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.util.InputMismatchException; public class GuessTheCycleSize { public static void main(String[] args)throws Exception{ FastReader f=new FastReader(System.in); boolean tryForN=true; long n=-1; int i=1; while (tryForN){ System.out.println("? "+(i)+" "+(i+1)); long ans1=f.nextLong(); System.out.flush(); System.out.println("? "+(i+1)+" "+(i)); long ans2=f.nextLong(); System.out.flush(); if (ans1==0||ans2==0) break; if (ans1!=ans2){ System.out.println("! "+(ans1+ans2)); System.out.flush(); break; } i++; } } } class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public String next() { return nextString(); } 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 == ',') { c = read(); } 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 double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double 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 { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } 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
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
64f363cb67909442b6f572ea52fa5055
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.util.*; import java.io.*; public class E_Guess_the_Cycle_Size { static FastReader sc; static PrintWriter out; static void solve() { StringBuilder res = new StringBuilder(); for (int i = 2; i <= 26; i++) { out.printf("? 1 %d\n", i); out.flush(); long a = sc.nextLong(); out.printf("? %d 1\n", i); out.flush(); long b = sc.nextLong(); if (a != b) { out.printf("! %d\n", a + b); return; } else if (a == -1) { out.printf("! %d\n", i - 1); return; } } } public static void main(String[] args) throws IOException { sc = new FastReader(); out = new PrintWriter(System.out); int tt = 1;// sc.nextInt(); while (tt-- > 0) { solve(); } out.close(); } static <E> void debug(E a) { System.err.println(a); } static void debug(int... a) { System.err.println(Arrays.toString(a)); } static int maxOf(int... array) { return Arrays.stream(array).max().getAsInt(); } static int minOf(int... array) { return Arrays.stream(array).parallel().reduce(Math::min).getAsInt(); } static <E> void print(E res) { out.println(res); } 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; } int[] readIntArray(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) res[i] = nextInt(); return res; } long[] readLongArray(int n) { long[] res = new long[n]; for (int i = 0; i < n; i++) res[i] = nextLong(); return res; } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
deba2e53d314da6285cb00d3149e85d1
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.PriorityQueue; import java.util.StringTokenizer; import java.util.TreeMap; public class D { static final long mod = (long) 1e9 + 7l; private static void solve(int t){ for (int i = 1; ; i++) { for (int j = 1; j < i; j++) { System.out.println("? "+i+" "+j); long x = fs.nextLong(); System.out.println("? "+j+" "+i); long y = fs.nextLong(); if(x==-1 || y==-1){ System.out.println("! "+(i-1)); return; } if(x!=y){ System.out.println("! "+(x+y)); return; } } } } 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 p) { if (a != p.a) return a - p.a; return p.b - b; } @Override public String toString() { return "Pair{" + "a=" + a + ", b=" + b + '}'; } } private static int[] sortByCollections(int[] arr, boolean reverse) { ArrayList<Integer> ls = new ArrayList<>(arr.length); for (int i = 0; i < arr.length; i++) { ls.add(arr[i]); } if(reverse)Collections.sort(ls, Comparator.reverseOrder()); else Collections.sort(ls); for (int i = 0; i < arr.length; i++) { arr[i] = ls.get(i); } return arr; } public static void main(String[] args) { fs = new FastScanner(); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); int t = 1;//fs.nextInt(); for (int i = 1; i <= t; i++) solve(t); out.close(); // System.err.println( System.currentTimeMillis() - s + "ms" ); } static boolean DEBUG = true; static PrintWriter out; static FastScanner fs; static void trace(Object... o) { if (!DEBUG) return; System.err.println(Arrays.deepToString(o)); } static void pl(Object o) { out.println(o); } static void p(Object o) { out.print(o); } static long gcd(long a, long b) { return (b == 0) ? a : gcd(b, a % b); } static int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } static void sieveOfEratosthenes(int n, int factors[]) { factors[1] = 1; for (int p = 2; p * p <= n; p++) { if (factors[p] == 0) { factors[p] = p; for (int i = p * p; i <= n; i += p) factors[i] = p; } } } static long mul(long a, long b) { return a * b % mod; } static long fact(int x) { long ans = 1; for (int i = 2; i <= x; i++) ans = mul(ans, i); return ans; } static long fastPow(long base, long exp) { if (exp == 0) return 1; long half = fastPow(base, exp / 2); if (exp % 2 == 0) return mul(half, half); return mul(half, mul(half, base)); } static long modInv(long x) { return fastPow(x, mod - 2); } static long nCk(int n, int k) { return mul(fact(n), mul(modInv(fact(k)), modInv(fact(n - k)))); } 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()); } } static class _Scanner { InputStream is; _Scanner(InputStream is) { this.is = is; } byte[] bb = new byte[1 << 15]; int k, l; byte getc() throws IOException { if (k >= l) { k = 0; l = is.read(bb); if (l < 0) return -1; } return bb[k++]; } byte skip() throws IOException { byte b; while ((b = getc()) <= 32) ; return b; } int nextInt() throws IOException { int n = 0; for (byte b = skip(); b > 32; b = getc()) n = n * 10 + b - '0'; return n; } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
6dea5f4bcdb0c1e0d3df64e5e302468b
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { static long startTime = System.currentTimeMillis(); // for global initializations and methods starts here // global initialisations and methods end here static void run() { boolean tc = false; AdityaFastIO r = new AdityaFastIO(); //FastReader r = new FastReader(); try (OutputStream out = new BufferedOutputStream(System.out)) { //long startTime = System.currentTimeMillis(); int testcases = tc ? r.ni() : 1; int tcCounter = 1; // Hold Here Sparky------------------->>> // Solution Starts Here start: while (testcases-- > 0) { for (int count = 2; count <= 51; count++) { out.write(("?" + " ").getBytes()); out.write((1 + " ").getBytes()); out.write((count + " ").getBytes()); out.write(("\n").getBytes()); out.flush(); long f = r.nl(); out.write(("?" + " ").getBytes()); out.write((count + " ").getBytes()); out.write((1 + " ").getBytes()); out.write(("\n").getBytes()); out.flush(); long s = r.nl(); if (f == -1) { out.write(("!" + " ").getBytes()); out.write((--count + " ").getBytes()); out.write(("\n").getBytes()); out.flush(); return; } if (f != s) { out.write(("!" + " ").getBytes()); out.write((f + s + " ").getBytes()); out.write(("\n").getBytes()); out.flush(); return; } } } // Solution Ends Here } catch (Exception e) { e.printStackTrace(); } } static class AdityaFastIO { final private int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; public BufferedReader br; public StringTokenizer st; public AdityaFastIO() { br = new BufferedReader(new InputStreamReader(System.in)); din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public AdityaFastIO(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String word() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public String line() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public String readLine() throws IOException { byte[] buf = new byte[100000001]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int ni() 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 nl() 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 nd() 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(); } } public static void main(String[] args) throws Exception { run(); } static int[] readIntArr(int n, AdityaFastIO r) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = r.ni(); return arr; } static long[] readLongArr(int n, AdityaFastIO r) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = r.nl(); return arr; } static List<Integer> readIntList(int n, AdityaFastIO r) throws IOException { List<Integer> al = new ArrayList<>(); for (int i = 0; i < n; i++) al.add(r.ni()); return al; } static List<Long> readLongList(int n, AdityaFastIO r) throws IOException { List<Long> al = new ArrayList<>(); for (int i = 0; i < n; i++) al.add(r.nl()); return al; } static long mod = 998244353; static long modInv(long base, long e) { long result = 1; base %= mod; while (e > 0) { if ((e & 1) > 0) result = result * base % mod; base = base * base % mod; e >>= 1; } return result; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String word() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } String line() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int ni() { return Integer.parseInt(word()); } long nl() { return Long.parseLong(word()); } double nd() { return Double.parseDouble(word()); } } static int MOD = (int) (1e9 + 7); static long powerLL(long x, long n) { long result = 1; while (n > 0) { if (n % 2 == 1) result = result * x % MOD; n = n / 2; x = x * x % MOD; } return result; } static long powerStrings(int i1, int i2) { String sa = String.valueOf(i1); String sb = String.valueOf(i2); long a = 0, b = 0; for (int i = 0; i < sa.length(); i++) a = (a * 10 + (sa.charAt(i) - '0')) % MOD; for (int i = 0; i < sb.length(); i++) b = (b * 10 + (sb.charAt(i) - '0')) % (MOD - 1); return powerLL(a, b); } static long gcd(long a, long b) { if (a == 0) return b; else return gcd(b % a, a); } static long lcm(long a, long b) { return (a * b) / gcd(a, b); } static long lower_bound(int[] arr, int x) { int l = -1, r = arr.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (arr[m] >= x) r = m; else l = m; } return r; } static int upper_bound(int[] arr, int x) { int l = -1, r = arr.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (arr[m] <= x) l = m; else r = m; } return l + 1; } static void addEdge(ArrayList<ArrayList<Integer>> graph, int edge1, int edge2) { graph.get(edge1).add(edge2); graph.get(edge2).add(edge1); } public static class Pair implements Comparable<Pair> { int first; int second; public Pair(int first, int second) { this.first = first; this.second = second; } public String toString() { return "(" + first + "," + second + ")"; } public int compareTo(Pair o) { // TODO Auto-generated method stub if (this.first != o.first) return (int) (this.first - o.first); else return (int) (this.second - o.second); } @Override public int hashCode() { int hash = 5; hash = 17 * hash + this.first; return hash; } @Override public boolean equals(Object obj) { if (obj == null) return false; if (getClass() != obj.getClass()) return false; final Pair other = (Pair) obj; return this.first == other.first; } } public static class PairC<X, Y> implements Comparable<PairC> { X first; Y second; public PairC(X first, Y second) { this.first = first; this.second = second; } public String toString() { return "(" + first + "," + second + ")"; } public int compareTo(PairC o) { // TODO Auto-generated method stub return o.compareTo((PairC) first); } } static boolean isCollectionsSorted(List<Long> list) { if (list.size() == 0 || list.size() == 1) return true; for (int i = 1; i < list.size(); i++) if (list.get(i) <= list.get(i - 1)) return false; return true; } static boolean isCollectionsSortedReverseOrder(List<Long> list) { if (list.size() == 0 || list.size() == 1) return true; for (int i = 1; i < list.size(); i++) if (list.get(i) >= list.get(i - 1)) return false; return true; } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
5ce4e881bd75ebfcf405b018d9080e8d
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
//package codeforces.round820div3; import java.io.*; import java.util.*; import static java.lang.Math.*; public class E { static InputReader in; static PrintWriter out; public static void main(String[] args) { //initReaderPrinter(true); initReaderPrinter(false); //solve(in.nextInt()); solve(1); } /* General tips 1. It is ok to fail, but it is not ok to fail for the same mistakes over and over! 2. Train smarter, not harder! 3. If you find an answer and want to return immediately, don't forget to flush before return! */ /* Read before practice 1. Set a timer based on a problem's difficulty level: 45 minutes at your current target practice level; 2. During a problem solving session, focus! Do not switch problems or even worse switch to do something else; 3. If fail to solve within timer limit, read editorials to get as little help as possible to get yourself unblocked; 4. If after reading the entire editorial and other people's code but still can not solve, move this problem to to-do list and re-try in the future. 5. Keep a practice log about new thinking approaches, good tricks, bugs; Review regularly; 6. Also try this new approach suggested by um_nik: Solve with no intention to read editorial. If getting stuck, skip it and solve other similar level problems. Wait for 1 week then try to solve again. Only read editorial after you solved a problem. 7. Remember to also submit in the original problem link (if using gym) so that the 1 v 1 bot knows which problems I have solved already. 8. Form the habit of writing down an implementable solution idea before coding! You've taken enough hits during contests because you rushed to coding! */ /* Read before contests and lockout 1 v 1 Mistakes you've made in the past contests: 1. Tried to solve without going through given test examples -> wasting time on solving a different problem than asked; 2. Rushed to coding without getting a comprehensive sketch of your solution -> implementation bugs and WA; Write down your idea step by step, no need to rush. It is always better to have all the steps considered before hand! Think about all the past contests that you have failed because slow implementation and implementation bugs! This will be greatly reduced if you take your time to get a thorough idea steps! 3. Forgot about possible integer overflow; When stuck: 1. Understand problem statements? Walked through test examples? 2. Take a step back and think about other approaches? 3. Check rank board to see if you can skip to work on a possibly easier problem? 4. If none of the above works, take a guess? */ static void solve(int testCnt) { for (int testNumber = 0; testNumber < testCnt; testNumber++) { long ans = 0; for(int n = 2; n <= 26; n++) { out.println("? 1 " + n); out.flush(); long d1 = in.nextLong(); if(d1 < 0) { ans = n - 1; break; } out.println("? " + n + " " + 1); out.flush(); long d2 = in.nextLong(); if(d1 != d2) { ans = d1 + d2; break; } } out.println("! " + ans); out.flush(); } out.close(); } static void initReaderPrinter(boolean test) { if (test) { try { in = new InputReader(new FileInputStream("src/input.in")); out = new PrintWriter(new FileOutputStream("src/output.out")); } catch (IOException e) { e.printStackTrace(); } } else { in = new InputReader(System.in); out = new PrintWriter(System.out); } } static class InputReader { BufferedReader br; StringTokenizer st; InputReader(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream), 32768); } catch (Exception e) { e.printStackTrace(); } } 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()); } 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; } Integer[] nextIntArray(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitive(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitiveOneIndexed(int n) { int[] a = new int[n + 1]; for (int i = 1; i <= n; 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[] nextLongArrayPrimitive(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitiveOneIndexed(int n) { long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextLong(); return a; } String[] nextStringArray(int n) { String[] g = new String[n]; for (int i = 0; i < n; i++) g[i] = next(); return g; } List<Integer>[] readGraphOneIndexed(int n, int m) { List<Integer>[] adj = new List[n + 1]; for (int i = 0; i <= n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt(); int v = nextInt(); adj[u].add(v); adj[v].add(u); } return adj; } List<Integer>[] readGraphZeroIndexed(int n, int m) { List<Integer>[] adj = new List[n]; for (int i = 0; i < n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; adj[u].add(v); adj[v].add(u); } return adj; } /* A more efficient way of building an undirected graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][] buildUndirectedGraph(int nodeCnt, int edgeCnt) { int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(); edgeCntForEachNode[u]++; edgeCntForEachNode[v]++; end1[i] = u; end2[i] = v; } int[][] adj = new int[nodeCnt + 1][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]]; } for (int i = 0; i < edgeCnt; i++) { adj[end1[i]][idxForEachNode[end1[i]]] = end2[i]; idxForEachNode[end1[i]]++; adj[end2[i]][idxForEachNode[end2[i]]] = end1[i]; idxForEachNode[end2[i]]++; } return adj; } /* A more efficient way of building an undirected weighted graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][][] buildUndirectedWeightedGraph(int nodeCnt, int edgeCnt) { int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt], weight = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(), w = in.nextInt(); edgeCntForEachNode[u]++; edgeCntForEachNode[v]++; end1[i] = u; end2[i] = v; weight[i] = w; } int[][][] adj = new int[nodeCnt + 1][][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]][2]; } for (int i = 0; i < edgeCnt; i++) { adj[end1[i]][idxForEachNode[end1[i]]][0] = end2[i]; adj[end1[i]][idxForEachNode[end1[i]]][1] = weight[i]; idxForEachNode[end1[i]]++; adj[end2[i]][idxForEachNode[end2[i]]][0] = end1[i]; adj[end2[i]][idxForEachNode[end2[i]]][1] = weight[i]; idxForEachNode[end2[i]]++; } return adj; } /* A more efficient way of building a directed graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][] buildDirectedGraph(int nodeCnt, int edgeCnt) { int[] from = new int[edgeCnt], to = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; //from u to v: u -> v for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(); edgeCntForEachNode[u]++; from[i] = u; to[i] = v; } int[][] adj = new int[nodeCnt + 1][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]]; } for (int i = 0; i < edgeCnt; i++) { adj[from[i]][idxForEachNode[from[i]]] = to[i]; idxForEachNode[from[i]]++; } return adj; } /* A more efficient way of building a directed weighted graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][][] buildDirectedWeightedGraph(int nodeCnt, int edgeCnt) { int[] from = new int[edgeCnt], to = new int[edgeCnt], weight = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; //from u to v: u -> v for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(), w = in.nextInt(); edgeCntForEachNode[u]++; from[i] = u; to[i] = v; weight[i] = w; } int[][][] adj = new int[nodeCnt + 1][][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]][2]; } for (int i = 0; i < edgeCnt; i++) { adj[from[i]][idxForEachNode[from[i]]][0] = to[i]; adj[from[i]][idxForEachNode[from[i]]][1] = weight[i]; idxForEachNode[from[i]]++; } return adj; } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
5dfafd61de95a5759639b5e62861d9c8
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.io.*; import java.util.*; public class E { 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 int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static int MOD = (int) (1e9 + 7); public static void primeFactors(int n) { // Print the number of 2s that divide n while (n % 2 == 0) { System.out.print(2 + " "); n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i += 2) { // While i divides n, print i and divide n while (n % i == 0) { System.out.print(i + " "); n /= i; } } // This condition is to handle the case whien // n is a prime number greater than 2 if (n > 2) System.out.print(n); System.out.println(); } static boolean isPrime(int val) { for (int i = 2; i <= Math.sqrt(val); i++) { if (val % i == 0) { return false; } } return true; } static long pow(long x, long y, long n) { if (y == 0) { return 1; } long val = pow(x, y / 2, n); if (y % 2 == 1) { return (((val * val) % n) * x) % n; } else { return (val * val) % n; } } public static void main(String[] args) { StringBuilder ans = new StringBuilder(); // int t = ri(); int t=1; while (t-- > 0) { boolean flag =false; for(int i=2;i<=51;i++) { out.println("? 1 "+i); out.flush(); long val = rl(); if(val==-1) { out.println("! "+(i-1)); out.flush(); flag = true; break; } out.println("? "+i+" 1"); out.flush(); long val2 = rl(); if(val!=val2) { flag=true; out.println("! "+(val+val2)); out.flush(); break; } } if(!flag) { out.println("! 1000000000000000000"); out.flush(); } // long l=3, r= (long) 1e18; // int count = 0; // long res = 0; // while(l<r) // { // long mid = l+(r-l+1)/2; // out.println("? 1 "+mid); // count++; // out.flush(); // long val = rl(); // if(val==0) // { // return; // } // if(val == -1) // { // r=mid-1; // } // else { // l=mid; // } // } // // // System.out.println(count); } // out.print(ans); // out.flush(); } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
891e8e0f27e459bea1e8a2c3f9569755
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.io.*; public class Main { static InputReader sc; public static void main(String[] args) throws Exception { sc=new InputReader(System.in); long ans=-1; int i=4; while(true) { long t1=qry(i-1,i); long t2=qry(i,i-1); if(t1==-1) { ans=i-1; break; } if(t1!=t2) { ans=t1+t2; break; } i++; } System.out.println("! "+ans); } private static long qry(long a,long b) throws IOException { System.out.println("? "+a+" "+b); System.out.flush(); return sc.nextLong(); } static class InputReader { BufferedReader br; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } public int nextInt() throws IOException { int c = br.read(); while (c <= 32) { c = br.read(); } boolean negative = false; if (c == '-') { negative = true; c = br.read(); } int x = 0; while (c > 32) { x = x * 10 + c - '0'; c = br.read(); } return negative ? -x : x; } public long nextLong() throws IOException { int c = br.read(); while (c <= 32) { c = br.read(); } boolean negative = false; if (c == '-') { negative = true; c = br.read(); } long x = 0; while (c > 32) { x = x * 10 + c - '0'; c = br.read(); } return negative ? -x : x; } public String next() throws IOException { int c = br.read(); while (c <= 32) { c = br.read(); } StringBuilder sb = new StringBuilder(); while (c > 32) { sb.append((char) c); c = br.read(); } return sb.toString(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
f2c4b33412d54ce6ab7dd493a69cea1e
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class E1729 { public static void main(String[] args) throws IOException { InputReader ir = new InputReader(System.in); int n = 3; int loop = 0; while (loop < 50) { System.out.println("? 1 " + n); long dist1 = ir.nextLong(); if (dist1 == -1) { System.out.println("! " + (n - 1)); return; } System.out.println("? " + n + " 1"); long dist2 = ir.nextLong(); if (dist1 != dist2) { System.out.println("! " + (dist1 + dist2)); return; } n++; loop += 2; } } private static final class InputReader { private int index; private String[] line; private final BufferedReader br; private InputReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); index = 0; line = new String[0]; } private int nextInt() { updateIndex(); return Integer.parseInt(line[index++]); } private void updateIndex() { try { if (index == line.length) { line = br.readLine().split(" "); index = 0; } } catch (IOException ioException) { } } private long nextLong() { updateIndex(); return Long.parseLong(line[index++]); } private float nextFloat() { updateIndex(); return Float.parseFloat(line[index++]); } private double nextDouble() { updateIndex(); return Double.parseDouble(line[index++]); } private char nextChar() { updateIndex(); return line[index++].charAt(0); } private String nextString() { updateIndex(); return line[index++]; } private void close() { try { br.close(); } catch (IOException ioException) { } } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
dec0220523d5fda8d828d6381413cc12
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.util.*; public class Test { public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); for(int i = 2; i < 27; i++) { System.out.println("? " + 1 + " " + i); long s = in.nextLong(); if(s == -1) { System.out.println("! " + (i - 1)); System.out.flush(); return; } System.out.println("? " + i + " " + 1); long t = in.nextLong(); if(s != t) { System.out.println("! " + (s + t)); System.out.flush(); return; } } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
ee81647b1ea297f843c936e141c20f11
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Map; import java.util.Map.Entry; import java.util.HashMap; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Sparsh Sanchorawala */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); EGuessTheCycleSize solver = new EGuessTheCycleSize(); solver.solve(1, in, out); out.close(); } static class EGuessTheCycleSize { public void solve(int testNumber, InputReader s, PrintWriter w) { int a = 1; long lim = 1L << 40; w.println("? " + a + " " + lim); w.flush(); if (s.nextLong() == -1) { long left = 3, right = lim - 1; long ans = 3; while (left <= right) { long mid = (left + right) / 2; w.println("? " + a + " " + mid); w.flush(); if (s.nextLong() != -1) { ans = mid; left = mid + 1; } else right = mid - 1; } w.println("! " + ans); w.flush(); return; } int cnt = 24; HashMap<Long, Integer> hm = new HashMap<>(); for (int i = 0; i < cnt; i++) { int b = 2 + i; w.println("? " + a + " " + b); w.flush(); long d1 = s.nextLong(); w.println("? " + b + " " + a); w.flush(); long d2 = s.nextLong(); if (!hm.containsKey(d1 + d2)) hm.put(d1 + d2, 0); hm.put(d1 + d2, hm.get(d1 + d2) + 1); } long res = -1; hm.put(res, 0); for (Map.Entry<Long, Integer> e : hm.entrySet()) { if (e.getValue() > hm.get(res)) res = e.getKey(); } w.println("! " + res); w.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 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 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
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
1c8c15182bb4e9af7ae0576b4d9a58c7
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.util.*; public class E { public static void main(String args[]) { Scanner scan = new Scanner(System.in); long a = 1; long b = 2; while (true) { System.out.println("? " + a + " " + b); System.out.flush(); long ans1 = scan.nextLong(); if (ans1 == 0) { return; } if (ans1 == -1) { System.out.println("! " + (b-1)); System.out.flush(); return; } System.out.println("? " + b + " " + a); System.out.flush(); long ans2 = scan.nextLong(); if (ans1 == ans2) { b++; continue; } System.out.println("! " + (ans1 + ans2)); System.out.flush(); return; } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
c5c0ac6ab59da5a9cac47e96b6602b44
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
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; import static java.lang.Math.*; public class GuessTheCycleSize implements Closeable { private InputReader in = new InputReader(System.in); private PrintWriter out = new PrintWriter(System.out); public void solve() { for (int b = 3; b < 28; b++) { long[] answer = ask(b); if (answer[0] == -1) { answer(b - 1); break; } else if (answer[0] != answer[1]) { answer(answer[0] + answer[1]); break; } } } private long[] ask(int b) { long[] result = new long[2]; out.printf("? 1 %d\n", b); out.flush(); result[0] = in.nl(); out.printf("? %d 1\n", b); out.flush(); result[1] = in.nl(); return result; } private void answer(long n) { out.printf("! %d\n", n); 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 (GuessTheCycleSize instance = new GuessTheCycleSize()) { instance.solve(); } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
4e956c5ec4e261dca35eadd9579707f1
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.util.*; import java.io.*; public class codeforces1729E { public static void main(String[] args) throws Exception { FastScanner in = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); for (int i = 2; i<50;i++) { pw.println("? 1 "+i); pw.flush(); long a = in.nextLong(); if (a==-1) { pw.println("! "+(i-1)); break; } pw.println("? "+i+" 1"); pw.flush();; long b = in.nextLong(); if (a!=b) { pw.println("! "+(a+b)); break; } } pw.close(); } private static class FastScanner { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; private FastScanner() throws IOException { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private short nextShort() throws IOException { short ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do ret = (short) (ret * 10 + c - '0'); while ((c = read()) >= '0' && c <= '9'); if (neg) return (short) -ret; return ret; } private 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; } private char nextChar() throws IOException { byte c = read(); while (c <= ' ') c = read(); return (char) c; } private String nextString() throws IOException { StringBuilder ret = new StringBuilder(); byte c = read(); while (c <= ' ') c = read(); do { ret.append((char) c); } while ((c = read()) > ' '); return ret.toString(); } 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++]; } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
ad2552ddebf27f04abdb66efeeedd9e0
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.io.*; import java.util.*; public class CodeForces { /*-------------------------------------------EDITING CODE STARTS HERE-------------------------------------------*/ public static void solve(int tCase) throws IOException { long ans = -1; for(int i = 2;i<=26;i++){ long x = query(1,i); if(x == -1){ ans = i-1; break; } long y = query(i,1); if(x!=y){ ans = x + y; break; } } System.out.println("! "+ans); System.out.flush(); } private static long query(int a, int b) throws IOException{ System.out.println("? "+a+" "+b); System.out.flush(); return sc.nextLong(); } public static void main(String[] args) throws IOException { openIO(); int testCase = 1; // testCase = sc.nextInt(); for (int i = 1; i <= testCase; i++) solve(i); closeIO(); } /*-------------------------------------------EDITING CODE ENDS HERE-------------------------------------------*/ /*--------------------------------------HELPER FUNCTIONS STARTS HERE-----------------------------------------*/ public static int mod = (int) 1e9 + 7; // public static int mod = 998244353; public static int inf_int = (int)1e9; public static long inf_long = (long)2e15; public static void _sort(int[] arr, boolean isAscending) { int n = arr.length; List<Integer> list = new ArrayList<>(); for (int ele : arr) list.add(ele); Collections.sort(list); if (!isAscending) Collections.reverse(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } public static void _sort(long[] arr, boolean isAscending) { int n = arr.length; List<Long> list = new ArrayList<>(); for (long ele : arr) list.add(ele); Collections.sort(list); if (!isAscending) Collections.reverse(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // time : O(1), space : O(1) public static int _digitCount(long num,int base){ // this will give the # of digits needed for a number num in format : base return (int)(1 + Math.log(num)/Math.log(base)); } // time for pre-computation of factorial and inverse-factorial table : O(nlog(mod)) public static long[] factorial , inverseFact; public static void _ncr_precompute(int n){ factorial = new long[n+1]; inverseFact = new long[n+1]; factorial[0] = inverseFact[0] = 1; for (int i = 1; i <=n; i++) { factorial[i] = (factorial[i - 1] * i) % mod; inverseFact[i] = _modExpo(factorial[i], mod - 2); } } // time of factorial calculation after pre-computation is O(1) public static int _ncr(int n,int r){ if(r > n)return 0; return (int)(factorial[n] * inverseFact[r] % mod * inverseFact[n - r] % mod); } public static int _npr(int n,int r){ if(r > n)return 0; return (int)(factorial[n] * inverseFact[n - r] % mod); } // euclidean algorithm time O(max (loga ,logb)) public static long _gcd(long a, long b) { while (a>0){ long x = a; a = b % a; b = x; } return b; } // lcm(a,b) * gcd(a,b) = a * b public static long _lcm(long a, long b) { return (a / _gcd(a, b)) * b; } // binary exponentiation time O(logn) public static long _modExpo(long x, long n) { long ans = 1; while (n > 0) { if ((n & 1) == 1) { ans *= x; ans %= mod; n--; } else { x *= x; x %= mod; n >>= 1; } } return ans; } // function to find a/b under modulo mod. time : O(logn) public static long _modInv(long a,long b){ return (a * _modExpo(b,mod-2)) % mod; } //sieve or first divisor time : O(mx * log ( log (mx) ) ) public static int[] _seive(int mx){ int[] firstDivisor = new int[mx+1]; for(int i=0;i<=mx;i++)firstDivisor[i] = i; for(int i=2;i*i<=mx;i++) if(firstDivisor[i] == i) for(int j = i*i;j<=mx;j+=i) if(firstDivisor[j]==j)firstDivisor[j] = i; return firstDivisor; } static class Pair<K, V>{ K ff; V ss; public Pair(K ff, V ss) { this.ff = ff; this.ss = ss; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || this.getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return ff.equals(pair.ff) && ss.equals(pair.ss); } @Override public int hashCode() { return Objects.hash(ff, ss); } @Override public String toString(){ return ff.toString()+" "+ss.toString(); } } /*--------------------------------------HELPER FUNCTIONS ENDS HERE-----------------------------------------*/ /*-------------------------------------------FAST INPUT STARTS HERE---------------------------------------------*/ static FastestReader sc; static PrintWriter out; private static void openIO() throws IOException { sc = new FastestReader(); out = new PrintWriter(System.out); } public static void closeIO() throws IOException { out.flush(); out.close(); sc.close(); } private static final class FastestReader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; public FastestReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastestReader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() throws IOException { int b; //noinspection StatementWithEmptyBody while ((b = read()) != -1 && isSpaceChar(b)) {} return b; } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); final boolean neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); return neg?-ret:ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); final boolean neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); return neg?-ret:ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); final 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); return neg?-ret:ret; } public String nextLine() throws IOException { final byte[] buf = new byte[(1<<10)]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { break; } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } 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 { din.close(); } } /*---------------------------------------------FAST INPUT ENDS HERE ---------------------------------------------*/ } /** Some points to keep in mind : * 1. don't use Arrays.sort(primitive data type array) * 2. try to make the parameters of a recursive function as less as possible, * more use static variables. * 3. If n = 5000, then O(n^2 logn) need atleast 4 sec to work * 4. dp[2][n] works faster than dp[n][2] * 5. if split wrt 1 char use '\\' before char: .split("\\."); * **/
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
4c9e34241fcfee2f26c34d453d5eef1d
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; public class Main { static final int INF = 0x3f3f3f3f; static final long LNF = 0x3f3f3f3f3f3f3f3fL; public static void main(String[] args) throws IOException { initReader(); solve(); pw.flush(); pw.close(); } public static void solve()throws IOException{ for(int i = 2;i<=26;i++){ req(1, i); long first = nextLong(); req(i,1); long second = nextLong(); if(first == -1){ pw.println("! " + (i - 1)); return; } if(first != second){ pw.println("! " + (first + second)); return; } } } public static void req(long l,long r){ pw.println("? " + l + " " + r); pw.flush(); } /***************************************************************************************/ static BufferedReader reader; static StringTokenizer tokenizer; static PrintWriter pw; public static void initReader() throws IOException { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = new StringTokenizer(""); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); // 从文件读写 // reader = new BufferedReader(new FileReader("test.in")); // tokenizer = new StringTokenizer(""); // pw = new PrintWriter(new BufferedWriter(new FileWriter("test1.out"))); } public static boolean hasNext() { try { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } } catch (Exception e) { return false; } return true; } public static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } public static String nextLine() { try { return reader.readLine(); } catch (Exception e) { return null; } } public static int nextInt() throws IOException { return Integer.parseInt(next()); } public static long nextLong() throws IOException { return Long.parseLong(next()); } public static double nextDouble() throws IOException { return Double.parseDouble(next()); } public static char nextChar() throws IOException { return next().charAt(0); } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
1c78ede8d4195f4c28a835990c3fa59f
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.io.*; import java.util.*; /* */ public class E{ static FastReader sc=null; public static void main(String[] args) { sc=new FastReader(); for(int i=2;i<=51;i++) { long l1=Query(1,i),l2=Query(i,1); if(l1==-1) { print(i-1); return; } if(l1==l2)continue; else { print(l1+l2); return; } } throw null; } static void print(long a) { System.out.println("! "+a); } static long Query(int a,int b) { System.out.println("? "+a+" "+b); long len=sc.nextLong(); return len; } static int[] ruffleSort(int a[]) { ArrayList<Integer> al=new ArrayList<>(); for(int i:a)al.add(i); Collections.sort(al); for(int i=0;i<a.length;i++)a[i]=al.get(i); return a; } static void print(int a[]) { for(int e:a) { System.out.print(e+" "); } System.out.println(); } static class FastReader{ StringTokenizer st=new StringTokenizer(""); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 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()); } int[] readArray(int n) { int a[]=new int[n]; for(int i=0;i<n;i++)a[i]=sc.nextInt(); return a; } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
87cd02eb26fa8ce17966879d1b32f792
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { static BufferedReader bf; static PrintWriter out; static Scanner sc; static StringTokenizer st; static long mod = (long)(1e9+7); static long mod2 = 998244353; static long fact[]; static long inverse[]; public static void main (String[] args)throws IOException { bf = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new Scanner(System.in); // fact = new long[1001]; // inverse = new long[1001]; // fact[0] = 1; // inverse[0] = 1; // for(int i = 1;i<fact.length;i++){ // fact[i] = (fact[i-1] * i)%mod; // inverse[i] = binaryExpo(fact[i], mod-2); // // } // int t = nextInt(); // while(t-->0){ solve(); // } out.flush(); } public static void solve()throws IOException{ for(int i = 2;i<=26;i++){ req(1, i); long first = nextLong(); req(i,1); long second = nextLong(); if(first == -1){ out.println("! " + (i - 1)); return; } if(first != second){ out.println("! " + (first + second)); return; } } } public static void req(long l,long r){ out.println("? " + l + " " + r); out.flush(); } public static List<Integer> down(String s){ Map<Integer,List<Integer>>map = new HashMap<>(); for(int i = 0;i<s.length();i++){ int num = s.charAt(i) - 'a'; if(!map.containsKey(num)){ List<Integer>list = new ArrayList<>(); list.add(i+1); map.put(num,list); } else{ map.get(num).add(i+1); } } int prev = s.charAt(0) - 'a'; List<Integer>ans = new ArrayList<>(); for(int i = prev;i<=(s.charAt(s.length()-1) - 'a');i++){ if(map.containsKey(i)){ List<Integer>list = map.get(i); for(int j = 0;j<list.size();j++){ ans.add(list.get(j)); } prev= i; } } return ans; } public static List<Integer>up(String s){ Map<Integer,List<Integer>>map = new HashMap<>(); for(int i = 0;i<s.length();i++){ int num = s.charAt(i) - 'a'; if(!map.containsKey(num)){ List<Integer>list = new ArrayList<>(); list.add(i+1); map.put(num,list); } else{ map.get(num).add(i+1); } } int prev = s.charAt(0) - 'a'; List<Integer>ans = new ArrayList<>(); for(int i = prev;i>=(s.charAt(s.length()-1) - 'a');i--){ if(map.containsKey(i)){ List<Integer>list = map.get(i); for(int j = 0;j<list.size();j++){ ans.add(list.get(j)); } prev= i; } } return ans; } public static int ans(Integer[][]dp,int i,int j,char[]a,char[]b,String s){ if(i > j)return 1; if(dp[i][j] != null)return dp[i][j]; int one = ans(dp,i+2,j,a,b,s); if(one == 1){ if(s.charAt(i) < s.charAt(i+1))one = 0; else if(s.charAt(i) > s.charAt(i+1))one = 2; } int two = ans(dp,i+1,j-1,a,b,s); if(two == 1){ if(s.charAt(i) < s.charAt(j))two = 0; else if(s.charAt(i) > s.charAt(j))two = 2; } int three = ans(dp,i+1,j-1,a,b,s); if(three == 1){ if(s.charAt(j) < s.charAt(i))three = 0; else if(s.charAt(j) > s.charAt(i))three = 2; } int four = ans(dp,i,j-2,a,b,s); if(four == 1){ if(s.charAt(j) < s.charAt(j-1))four = 0; else if(s.charAt(j) > s.charAt(j-1))four = 2; } int ans = Math.min(Math.max(one,two),Math.max(three,four)); return dp[i][j] = ans; } public static void req(int l,int r){ out.println("? " + l + " " + r); out.flush(); } public static int[] bringSame(int u,int v ,int parent[][],int[]depth){ if(depth[u] < depth[v]){ int temp = u; u = v; v = temp; } int k = depth[u] - depth[v]; for(int i = 0;i<=18;i++){ if((k & (1<<i)) != 0){ u = parent[u][i]; } } return new int[]{u,v}; } public static void findDepth(List<List<Integer>>list,int cur,int parent,int[]depth,int[][]p){ List<Integer>temp = list.get(cur); p[cur][0] = parent; for(int i = 0;i<temp.size();i++){ int next = temp.get(i); if(next != parent){ depth[next] = depth[cur]+1; findDepth(list,next,cur,depth,p); } } } public static int lca(int u, int v,int[][]parent){ if(u == v)return u; for(int i = 18;i>=0;i--){ if(parent[u][i] != parent[v][i]){ u = parent[u][i]; v = parent[v][i]; } } return parent[u][0]; } public static void plus(int node,int low,int high,int tlow,int thigh,int[]tree){ if(low >= tlow && high <= thigh){ tree[node]++; return; } if(high < tlow || low > thigh)return; int mid = (low + high)/2; plus(node*2,low,mid,tlow,thigh,tree); plus(node*2 + 1 , mid + 1, high,tlow, thigh,tree); } public static boolean allEqual(int[]arr,int x){ for(int i = 0;i<arr.length;i++){ if(arr[i] != x)return false; } return true; } public static long helper(StringBuilder sb){ return Long.parseLong(sb.toString()); } public static int min(int node,int low,int high,int tlow,int thigh,int[][]tree){ if(low >= tlow&& high <= thigh)return tree[node][0]; if(high < tlow || low > thigh)return Integer.MAX_VALUE; int mid = (low + high)/2; // println(low+" "+high+" "+tlow+" "+thigh); return Math.min(min(node*2,low,mid,tlow,thigh,tree) ,min(node*2+1,mid+1,high,tlow,thigh,tree)); } public static int max(int node,int low,int high,int tlow,int thigh,int[][]tree){ if(low >= tlow && high <= thigh)return tree[node][1]; if(high < tlow || low > thigh)return Integer.MIN_VALUE; int mid = (low + high)/2; return Math.max(max(node*2,low,mid,tlow,thigh,tree) ,max(node*2+1,mid+1,high,tlow,thigh,tree)); } public static long[] help(List<List<Integer>>list,int[][]range,int cur){ List<Integer>temp = list.get(cur); if(temp.size() == 0)return new long[]{range[cur][1],1}; long sum = 0; long count = 0; for(int i = 0;i<temp.size();i++){ long []arr = help(list,range,temp.get(i)); sum += arr[0]; count += arr[1]; } if(sum < range[cur][0]){ count++; sum = range[cur][1]; } return new long[]{sum,count}; } public static long findSum(int node,int low, int high,int tlow,int thigh,long[]tree,long mod){ if(low >= tlow && high <= thigh)return tree[node]%mod; if(low > thigh || high < tlow)return 0; int mid = (low + high)/2; return((findSum(node*2,low,mid,tlow,thigh,tree,mod) % mod) + (findSum(node*2+1,mid+1,high,tlow,thigh,tree,mod)))%mod; } public static boolean allzero(long[]arr){ for(int i =0 ;i<arr.length;i++){ if(arr[i]!=0)return false; } return true; } public static long count(long[][]dp,int i,int[]arr,int drank,long sum){ if(i == arr.length)return 0; if(dp[i][drank]!=-1 && arr[i]+sum >=0)return dp[i][drank]; if(sum + arr[i] >= 0){ long count1 = 1 + count(dp,i+1,arr,drank+1,sum+arr[i]); long count2 = count(dp,i+1,arr,drank,sum); return dp[i][drank] = Math.max(count1,count2); } return dp[i][drank] = count(dp,i+1,arr,drank,sum); } public static void help(int[]arr,char[]signs,int l,int r){ if( l < r){ int mid = (l+r)/2; help(arr,signs,l,mid); help(arr,signs,mid+1,r); merge(arr,signs,l,mid,r); } } public static void merge(int[]arr,char[]signs,int l,int mid,int r){ int n1 = mid - l + 1; int n2 = r - mid; int[]a = new int[n1]; int []b = new int[n2]; char[]asigns = new char[n1]; char[]bsigns = new char[n2]; for(int i = 0;i<n1;i++){ a[i] = arr[i+l]; asigns[i] = signs[i+l]; } for(int i = 0;i<n2;i++){ b[i] = arr[i+mid+1]; bsigns[i] = signs[i+mid+1]; } int i = 0; int j = 0; int k = l; boolean opp = false; while(i<n1 && j<n2){ if(a[i] <= b[j]){ arr[k] = a[i]; if(opp){ if(asigns[i] == 'R'){ signs[k] = 'L'; } else{ signs[k] = 'R'; } } else{ signs[k] = asigns[i]; } i++; k++; } else{ arr[k] = b[j]; int times = n1 - i; if(times%2 == 1){ if(bsigns[j] == 'R'){ signs[k] = 'L'; } else{ signs[k] = 'R'; } } else{ signs[k] = bsigns[j]; } j++; opp = !opp; k++; } } while(i<n1){ arr[k] = a[i]; if(opp){ if(asigns[i] == 'R'){ signs[k] = 'L'; } else{ signs[k] = 'R'; } } else{ signs[k] = asigns[i]; } i++; k++; } while(j<n2){ arr[k] = b[j]; signs[k] = bsigns[j]; j++; k++; } } public static long nck(int n,int k){ return ((fact[n] * (inverse[n-k])%mod) * inverse[k])%mod; } public static long binaryExpo(long base,long expo){ if(expo == 0)return 1; long val; if(expo%2 == 1){ val = (binaryExpo(base, expo/2)); val = (val * val)%mod; val = (val * base)%mod; } else{ val = binaryExpo(base, expo/2); val = (val*val)%mod; } return (val%mod); } public static boolean isSorted(int[]arr){ for(int i =1;i<arr.length;i++){ if(arr[i] < arr[i-1]){ return false; } } return true; } //function to find the topological sort of the a DAG public static boolean hasCycle(int[]indegree,List<List<Integer>>list,int n,List<Integer>topo){ Queue<Integer>q = new LinkedList<>(); for(int i =1;i<indegree.length;i++){ if(indegree[i] == 0){ q.add(i); topo.add(i); } } while(!q.isEmpty()){ int cur = q.poll(); List<Integer>l = list.get(cur); for(int i = 0;i<l.size();i++){ indegree[l.get(i)]--; if(indegree[l.get(i)] == 0){ q.add(l.get(i)); topo.add(l.get(i)); } } } if(topo.size() == n)return false; return true; } // function to find the parent of any given node with path compression in DSU public static int find(int val,int[]parent){ if(val == parent[val])return val; return parent[val] = find(parent[val],parent); } // function to connect two components public static void union(int[]rank,int[]parent,int u,int v){ int a = find(u,parent); int b= find(v,parent); if(a == b)return; if(rank[a] == rank[b]){ parent[b] = a; rank[a]++; } else{ if(rank[a] > rank[b]){ parent[b] = a; } else{ parent[a] = b; } } } // public static int findN(int n){ int num = 1; while(num < n){ num *=2; } return num; } // code for input public static void print(String s ){ System.out.print(s); } public static void print(int num ){ System.out.print(num); } public static void print(long num ){ System.out.print(num); } public static void println(String s){ System.out.println(s); } public static void println(int num){ System.out.println(num); } public static void println(long num){ System.out.println(num); } public static void println(){ System.out.println(); } public static int Int(String s){ return Integer.parseInt(s); } public static long Long(String s){ return Long.parseLong(s); } public static String[] nextStringArray()throws IOException{ return bf.readLine().split(" "); } public static String nextString()throws IOException{ return bf.readLine(); } public static long[] nextLongArray(int n)throws IOException{ String[]str = bf.readLine().split(" "); long[]arr = new long[n]; for(int i =0;i<n;i++){ arr[i] = Long.parseLong(str[i]); } return arr; } public static int[][] newIntMatrix(int r,int c)throws IOException{ int[][]arr = new int[r][c]; for(int i =0;i<r;i++){ String[]str = bf.readLine().split(" "); for(int j =0;j<c;j++){ arr[i][j] = Integer.parseInt(str[j]); } } return arr; } public static long[][] newLongMatrix(int r,int c)throws IOException{ long[][]arr = new long[r][c]; for(int i =0;i<r;i++){ String[]str = bf.readLine().split(" "); for(int j =0;j<c;j++){ arr[i][j] = Long.parseLong(str[j]); } } return arr; } static class pair{ int one; int two; pair(int one,int two){ this.one = one ; this.two =two; } } 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 (a*b)/(gcd(a,b)); } public static boolean isPalindrome(String s){ int i = 0; int j = s.length()-1; while(i<=j){ if(s.charAt(i) != s.charAt(j)){ return false; } i++; j--; } return true; } // these functions are to calculate the number of smaller elements after self public static void sort(int[]arr,int l,int r){ if(l < r){ int mid = (l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); smallerNumberAfterSelf(arr, l, mid, r); } } public static void smallerNumberAfterSelf(int[]arr,int l,int mid,int r){ int n1 = mid - l +1; int n2 = r - mid; int []a = new int[n1]; int[]b = new int[n2]; for(int i = 0;i<n1;i++){ a[i] = arr[l+i]; } for(int i =0;i<n2;i++){ b[i] = arr[mid+i+1]; } int i = 0; int j =0; int k = l; while(i<n1 && j < n2){ if(a[i] < b[j]){ arr[k++] = a[i++]; } else{ arr[k++] = b[j++]; } } while(i<n1){ arr[k++] = a[i++]; } while(j<n2){ arr[k++] = b[j++]; } } public static String next(){ while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(bf.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } static int nextInt() { return Integer.parseInt(next()); } static long nextLong() { return Long.parseLong(next()); } static double nextDouble() { return Double.parseDouble(next()); } static String nextLine(){ String str = ""; try { str = bf.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // use some math tricks it might help // sometimes just try to think in straightforward plan in A and B problems don't always complecate the questions with thinking too much differently // always use long number to do 10^9+7 modulo // if a problem is related to binary string it could also be related to parenthesis // *****try to use binary search(it is a very beautiful thing it can work in some of the very unexpected problems ) in the question it might work****** // try sorting // try to think in opposite direction of question it might work in your way // if a problem is related to maths try to relate some of the continuous subarray with variables like - > a+b+c+d or a,b,c,d in general // if the question is to much related to left and/or right side of any element in an array then try monotonic stack it could work. // in range query sums try to do binary search it could work // analyse the time complexity of program thoroughly // anylyse the test cases properly // if we divide any number by 2 till it gets 1 then there will be (number - 1) operation required // try to do the opposite operation of what is given in the problem //think about the base cases properly //If a question is related to numbers try prime factorisation or something related to number theory // keep in mind unique strings //you can calculate the number of inversion in O(n log n) // in a matrix you could sometimes think about row and cols indenpendentaly. // Try to think in more constructive(means a way to look through various cases of a problem) way. // observe the problem carefully the answer could be hidden in the given test cases itself. (A, B , C); // when we have equations like (a+b = N) and we have to find the max of (a*b) then the values near to the N/2 must be chosen as (a and b); // give emphasis to the number of occurences of elements it might help. // if a number is even then we can find the pair for each and every number in that range whose bitwise AND is zero. // if a you find a problem related to the graph make a graph // There is atleast one prime number between the interval [n , 3n/2]; // If a problem is related to maths then try to form an mathematical equation.
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
4333a670765a848322c7c522e91fbedb
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.util.Scanner; public class E { public static void main(String[] args) { Scanner in = new Scanner(System.in); for (int i = 1; i <= 25; i++) { System.out.println("? 1 "+(i+1)); long len1,len2; len1 = in.nextLong(); if(len1==-1){ System.out.println("! "+i); return; } System.out.println("? "+(i+1)+" 1"); len2 = in.nextLong(); if(len1!=len2){ System.out.println("! "+(len1+len2)); return; } } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
2cf0e2ed87878135dc3c3529ad8fcf9a
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.io.*; public class Main { static InputReader in; static OutputWriter out; public static void main(String[] args) throws Exception { in=new InputReader(System.in); out=new OutputWriter(System.out); long ans=-1; int i=4; while(true) { long t1=qry(i-1,i); long t2=qry(i,i-1); if(t1==-1) { ans=i-1; break; } if(t1!=t2) { ans=t1+t2; break; } i++; } out.println("! "+ans); out.flush(); } private static long qry(long a,long b) throws IOException { out.println("? "+a+" "+b); out.flush(); return in.nextLong(); } static class InputReader { private final BufferedReader br; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } public int nextInt() throws IOException { int c = br.read(); while (c <= 32) { c = br.read(); } boolean negative = false; if (c == '-') { negative = true; c = br.read(); } int x = 0; while (c > 32) { x = x * 10 + c - '0'; c = br.read(); } return negative ? -x : x; } public long nextLong() throws IOException { int c = br.read(); while (c <= 32) { c = br.read(); } boolean negative = false; if (c == '-') { negative = true; c = br.read(); } long x = 0; while (c > 32) { x = x * 10 + c - '0'; c = br.read(); } return negative ? -x : x; } public String next() throws IOException { int c = br.read(); while (c <= 32) { c = br.read(); } StringBuilder sb = new StringBuilder(); while (c > 32) { sb.append((char) c); c = br.read(); } return sb.toString(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } } static class OutputWriter { private final BufferedWriter writer; public OutputWriter(OutputStream out) { writer=new BufferedWriter(new OutputStreamWriter(out)); } public void print(String str) { try { writer.write(str); } catch (IOException e) { e.printStackTrace(); } } public void print(Object obj) { print(String.valueOf(obj)); } public void println(String str) { print(str+"\n"); } public void println() { print('\n'); } public void println(Object obj) { println(String.valueOf(obj)); } public void flush() { try { writer.flush(); } catch (IOException e) { e.printStackTrace(); } } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 8
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
57740e53b8a4a081b4bd81e8f11099ac
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class E { private static Scan sc = new Scan(); public static void main(String[] args) { for (int i = 0; i < 1; i++) { solve(); System.out.flush(); } } private static void solve() { int n = 4; while (true) { System.out.println("? 1 " + n); System.out.flush(); long p = sc.nextLong(); System.out.println("? " + n + " 1"); System.out.flush(); long q = sc.nextLong(); if (p == 0 || q == 0) { break; } else if (p == -1) { System.out.println("! " + (n - 1)); break; } else if (p != q) { System.out.println("! " + (p + q)); break; } n++; } } private static class Scan { BufferedReader br; StringTokenizer st; public Scan() { 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()); } String nextLine() { String line = ""; try { line = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return line; } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 17
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
0b524d4a16a89b0a8575398c676ead04
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; /** * * @author eslam */ public class Solution { // Beginning of the solution static Kattio input = new Kattio(); static BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); static ArrayList<ArrayList<Integer>> powerSet = new ArrayList<>(); static ArrayList<LinkedList<Integer>> allprem = new ArrayList<>(); static ArrayList<LinkedList<String>> allprems = new ArrayList<>(); static ArrayList<Long> luc = new ArrayList<>(); static long mod = (long) (Math.pow(10, 9) + 7); static int grid[][] = {{0, 0, 1, -1, 1, 1, -1, -1}, {1, -1, 0, 0, 1, -1, 1, -1}}; static int dp[][]; static double cmp = 0.000000001; public static void main(String[] args) throws IOException { // Kattio input = new Kattio("input"); // BufferedWriter log = new BufferedWriter(new FileWriter("output.txt")); int test = 1;//input.nextInt(); loop: for (int o = 1; o <= test; o++) { for (int i = 1;; i++) { for (int j = 1; j < i; j++) { long x = 0, y = 0; log.write("? " + i + " " + j + "\n"); log.flush(); x = input.nextLong(); log.write("? " + j + " " + i + "\n"); log.flush(); y = input.nextLong(); if (x == -1 || y == -1) { log.write("! " + (i - 1) + "\n"); continue loop; } if (x != y) { log.write("! " + (x + y) + "\n"); continue loop; } } } } log.flush(); } static Comparator<tri> cmpTri() { Comparator<tri> c = new Comparator<tri>() { @Override public int compare(tri o1, tri o2) { if (o1.x > o2.x) { return 1; } else if (o1.x < o2.x) { return -1; } else { if (o1.y > o2.y) { return 1; } else if (o1.y < o2.y) { return -1; } else { if (o1.z > o2.z) { return 1; } else if (o1.z < o2.z) { return -1; } else { return 0; } } } } }; return c; } static Comparator<pair> cmpPair() { Comparator<pair> c = new Comparator<pair>() { @Override public int compare(pair o1, pair o2) { if (o1.x > o2.x) { return 1; } else if (o1.x < o2.x) { return -1; } else { if (o1.y > o2.y) { return 1; } else if (o1.y < o2.y) { return -1; } else { return 0; } } } }; return c; } static class rec { long x1; long x2; long y1; long y2; public rec(long x1, long y1, long x2, long y2) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; } public long getArea() { return (x2 - x1) * (y2 - y1); } } static int sumOfRange(int x1, int y1, int x2, int y2, int e, int a[][][]) { return (a[e][x2][y2] - a[e][x1 - 1][y2] - a[e][x2][y1 - 1]) + a[e][x1 - 1][y1 - 1]; } public static int[][] bfs(int i, int j, String w[]) { Queue<pair> q = new ArrayDeque<>(); q.add(new pair(i, j)); int dis[][] = new int[w.length][w[0].length()]; for (int k = 0; k < w.length; k++) { Arrays.fill(dis[k], -1); } dis[i][j] = 0; while (!q.isEmpty()) { pair p = q.poll(); int cost = dis[p.x][p.y]; for (int k = 0; k < 4; k++) { int nx = p.x + grid[0][k]; int ny = p.y + grid[1][k]; if (isValid(nx, ny, w.length, w[0].length())) { if (dis[nx][ny] == -1 && w[nx].charAt(ny) == '.') { q.add(new pair(nx, ny)); dis[nx][ny] = cost + 1; } } } } return dis; } public static void dfs(int node, ArrayList<Integer> a[], boolean vi[]) { vi[node] = true; for (Integer ch : a[node]) { if (!vi[ch]) { dfs(ch, a, vi); } } } public static void graphRepresintion(ArrayList<Integer>[] a, int q) throws IOException { for (int i = 0; i < a.length; i++) { a[i] = new ArrayList<>(); } while (q-- > 0) { int x = input.nextInt(); int y = input.nextInt(); a[x].add(y); a[y].add(x); } } public static boolean isValid(int i, int j, int n, int m) { return (i > -1 && i < n) && (j > -1 && j < m); } // present in the left and right indices public static int[] swap(int data[], int left, int right) { // Swap the data int temp = data[left]; data[left] = data[right]; data[right] = temp; // Return the updated array return data; } // Function to reverse the sub-array // starting from left to the right // both inclusive public static int[] reverse(int data[], int left, int right) { // Reverse the sub-array while (left < right) { int temp = data[left]; data[left++] = data[right]; data[right--] = temp; } // Return the updated array return data; } // Function to find the next permutation // of the given integer array public static boolean findNextPermutation(int data[]) { // If the given dataset is empty // or contains only one element // next_permutation is not possible if (data.length <= 1) { return false; } int last = data.length - 2; // find the longest non-increasing suffix // and find the pivot while (last >= 0) { if (data[last] < data[last + 1]) { break; } last--; } // If there is no increasing pair // there is no higher order permutation if (last < 0) { return false; } int nextGreater = data.length - 1; // Find the rightmost successor to the pivot for (int i = data.length - 1; i > last; i--) { if (data[i] > data[last]) { nextGreater = i; break; } } // Swap the successor and the pivot data = swap(data, nextGreater, last); // Reverse the suffix data = reverse(data, last + 1, data.length - 1); // Return true as the next_permutation is done return true; } public static pair[] dijkstra(int node, ArrayList<pair> a[]) { PriorityQueue<tri> q = new PriorityQueue<>(new Comparator<tri>() { @Override public int compare(tri o1, tri o2) { if (o1.y > o2.y) { return 1; } else if (o1.y < o2.y) { return -1; } else { return 0; } } }); q.add(new tri(node, 0, -1)); pair distance[] = new pair[a.length]; while (!q.isEmpty()) { tri p = q.poll(); int cost = p.y; if (distance[p.x] != null) { continue; } distance[p.x] = new pair(p.z, cost); ArrayList<pair> nodes = a[p.x]; for (pair node1 : nodes) { if (distance[node1.x] == null) { tri pa = new tri(node1.x, cost + node1.y, p.x); q.add(pa); } } } return distance; } public static String revs(String w) { String ans = ""; for (int i = w.length() - 1; i > -1; i--) { ans += w.charAt(i); } return ans; } public static boolean isPalindrome(String w) { for (int i = 0; i < w.length() / 2; i++) { if (w.charAt(i) != w.charAt(w.length() - i - 1)) { return false; } } return true; } public static void getPowerSet(Queue<Integer> a) { int n = a.poll(); if (!a.isEmpty()) { getPowerSet(a); } int s = powerSet.size(); for (int i = 0; i < s; i++) { ArrayList<Integer> ne = new ArrayList<>(); ne.add(n); for (int j = 0; j < powerSet.get(i).size(); j++) { ne.add(powerSet.get(i).get(j)); } powerSet.add(ne); } ArrayList<Integer> p = new ArrayList<>(); p.add(n); powerSet.add(p); } public static int getlo(int va) { int v = 1; while (v <= va) { if ((va&v) != 0) { return v; } v <<= 1; } return 0; } static long fast_pow(long a, long p, long mod) { long res = 1; while (p > 0) { if (p % 2 == 0) { a = (a * a) % mod; p /= 2; } else { res = (res * a) % mod; p--; } } return res; } public static int countPrimeInRange(int n, boolean isPrime[]) { int cnt = 0; Arrays.fill(isPrime, true); for (int i = 2; i * i <= n; i++) { if (isPrime[i]) { for (int j = i * 2; j <= n; j += i) { isPrime[j] = false; } } } for (int i = 2; i <= n; i++) { if (isPrime[i]) { cnt++; } } return cnt; } public static void create(long num) { luc.add(num); if (num > power(10, 9)) { return; } create(num * 10 + 4); create(num * 10 + 7); } public static long ceil(long a, long b) { return (a + b - 1) / b; } public static long round(long a, long b) { if (a < 0) { return (a - b / 2) / b; } return (a + b / 2) / b; } public static void allPremutationsst(LinkedList<String> l, boolean visited[], ArrayList<String> st) { if (l.size() == st.size()) { allprems.add(l); } for (int i = 0; i < st.size(); i++) { if (!visited[i]) { visited[i] = true; LinkedList<String> nl = new LinkedList<>(); for (String x : l) { nl.add(x); } nl.add(st.get(i)); allPremutationsst(nl, visited, st); visited[i] = false; } } } public static void allPremutations(LinkedList<Integer> l, boolean visited[], int a[]) { if (l.size() == a.length) { allprem.add(l); } for (int i = 0; i < a.length; i++) { if (!visited[i]) { visited[i] = true; LinkedList<Integer> nl = new LinkedList<>(); for (Integer x : l) { nl.add(x); } nl.add(a[i]); allPremutations(nl, visited, a); visited[i] = false; } } } public static int binarySearch(long[] a, long value) { int l = 0; int r = a.length - 1; while (l <= r) { int m = (l + r) / 2; if (a[m] == value) { return m; } else if (a[m] > value) { r = m - 1; } else { l = m + 1; } } return -1; } public static void reverse(int l, int r, char ch[]) { for (int i = 0; i < r / 2; i++) { char c = ch[i]; ch[i] = ch[r - i - 1]; ch[r - i - 1] = c; } } public static int logK(long v, long k) { int ans = 0; while (v > 1) { ans++; v /= k; } return ans; } public static long power(long a, long n) { if (n == 1) { return a; } long pow = power(a, n / 2); pow *= pow; if (n % 2 != 0) { pow *= a; } return pow; } public static long get(long max, long x) { if (x == 1) { return max; } int cnt = 0; while (max > 0) { cnt++; max /= x; } return cnt; } public static int numOF0(long v) { long x = 1; int cnt = 0; while (x <= v) { if ((x & v) == 0) { cnt++; } x <<= 1; } return cnt; } public static int log2(double n) { int cnt = 0; while (n > 1) { n /= 2; cnt++; } return cnt; } public static int[] bfs(int node, ArrayList<Integer> a[]) { Queue<Integer> q = new LinkedList<>(); q.add(node); int distances[] = new int[a.length]; Arrays.fill(distances, -1); distances[node] = 0; while (!q.isEmpty()) { int parent = q.poll(); ArrayList<Integer> nodes = a[parent]; int cost = distances[parent]; for (Integer node1 : nodes) { if (distances[node1] == -1) { q.add(node1); distances[node1] = cost + 1; } } } return distances; } public static ArrayList<Integer> primeFactors(int n) { ArrayList<Integer> a = new ArrayList<>(); while (n % 2 == 0) { a.add(2); n /= 2; } for (int i = 3; i <= Math.sqrt(n); i += 2) { while (n % i == 0) { a.add(i); n /= i; } if (n < i) { break; } } if (n > 2) { a.add(n); } return a; } public static ArrayList<Integer> printPrimeFactoriztion(int n) { ArrayList<Integer> a = new ArrayList<>(); for (int i = 1; i < Math.sqrt(n) + 1; i++) { if (n % i == 0) { if (isPrime(i)) { a.add(i); n /= i; i = 0; } else if (isPrime(n / i)) { a.add(n / i); n = i; i = 0; } } } return a; } // end of solution public static BigInteger f(long n) { if (n <= 1) { return BigInteger.ONE; } long t = n - 1; BigInteger b = new BigInteger(t + ""); BigInteger ans = new BigInteger(n + ""); while (t > 1) { ans = ans.multiply(b); b = b.subtract(BigInteger.ONE); t--; } return ans; } public static long factorial(long n) { if (n <= 1) { return 1; } long t = n - 1; while (t > 1) { n = mod((mod(n, mod) * mod(t, mod)), mod); t--; } return n; } public static long rev(long n) { long t = n; long ans = 0; while (t > 0) { ans = ans * 10 + t % 10; t /= 10; } return ans; } public static boolean isPalindrome(int n) { int t = n; int ans = 0; while (t > 0) { ans = ans * 10 + t % 10; t /= 10; } return ans == n; } static class tri { int x, y, z; public tri(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } @Override public String toString() { return x + " " + y + " " + z; } } static boolean isPrime(long num) { if (num == 1) { return false; } if (num == 2) { return true; } if (num % 2 == 0) { return false; } if (num == 3) { return true; } for (int i = 3; i <= Math.sqrt(num) + 1; i += 2) { if (num % i == 0) { return false; } } return true; } public static void prefixSum(int[] a) { for (int i = 1; i < a.length; i++) { a[i] = a[i] + a[i - 1]; } } public static void suffixSum(long[] a) { for (int i = a.length - 2; i > -1; i--) { a[i] = a[i] + a[i + 1]; } } static long mod(long a, long b) { long r = a % b; return r < 0 ? r + b : r; } public static long binaryToDecimal(String w) { long r = 0; long l = 0; for (int i = w.length() - 1; i > -1; i--) { long x = (w.charAt(i) - '0') * (long) Math.pow(2, l); r = r + x; l++; } return r; } public static String decimalToBinary(long n) { String w = ""; while (n > 0) { w = n % 2 + w; n /= 2; } return w; } public static boolean isSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] >= a[i + 1]) { return false; } } return true; } public static void print(int[] a) throws IOException { for (int i = 0; i < a.length; i++) { log.write(a[i] + " "); } log.write("\n"); } public static void read(int[] a) { for (int i = 0; i < a.length; i++) { a[i] = input.nextInt(); } } static class gepair { long x; long y; public gepair(long x, long y) { this.x = x; this.y = y; } @Override public String toString() { return x + " " + y; } } static class pair { int x; int y; public pair(int x, int y) { this.x = x; this.y = y; } @Override public String toString() { return x + " " + y; } } static class pai { long x; int y; public pai(long x, int y) { this.x = x; this.y = y; } @Override public String toString() { return x + " " + y; } } public static long LCM(long x, long y) { return x / GCD(x, y) * y; } public static long GCD(long x, long y) { if (y == 0) { return x; } return GCD(y, x % y); } public static void simplifyTheFraction(int a, int b) { long GCD = GCD(a, b); System.out.println(a / GCD + " " + b / GCD); } static class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; // standard input public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } // USACO-style file input public Kattio(String problemName) throws IOException { super(problemName + ".out"); r = new BufferedReader(new FileReader(problemName + ".in")); } // returns null if no more input String nextLine() { String str = ""; try { str = r.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public String next() { try { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(r.readLine()); } return st.nextToken(); } catch (Exception e) { } return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 17
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
ab95ea0c70672206f7069b5299bb0b35
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.util.Scanner; public class Main { static Scanner sc; public static void main(String[] args) { sc=new Scanner(System.in); long []rem=new long[25]; long ans=0; for( int i=0;i<25;i++){ rem[i]=query(1,i+2); } for( int i=0;i<25;i++){ if(rem[i]<0){ ans=i+1; break; } long temp=query(i+2,1); if(temp!=rem[i]){ ans=Math.max(rem[i]+temp,ans); } } System.out.println("! "+ans); } public static long query( int x,int y){ System.out.println("? "+x+" "+y); System.out.flush(); long res=Long.parseLong( sc.nextLine()); return res; } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 17
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
859d8c31103621865e1a6474ceeb5add
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.util.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); long ans=-1; long flag=0; for(int i=2;i<=26;i++) { System.out.println("? 1 "+i); System.out.flush(); long p1=in.nextLong(); System.out.println("? "+i+" 1"); System.out.flush(); long p2=in.nextLong(); if(p1==-1&&p2==-1) { flag=1; System.out.println("! " + (i - 1)); break; } else if(p1!=p2) ans=p1+p2; } if(flag!=1)System.out.println("! "+ans); } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 17
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
b17c8335ee082b0b69d11aada9f85cda
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.util.*; import java.io.*; public class Template { FastScanner in; PrintWriter out; public void solve() throws IOException { for (int i = 0; i < 25; i++) { System.out.println("? " + (i + 1) + " " + (i + 2)); System.out.flush(); long res1 = in.nextLong(); if (res1 == -1) { System.out.println("! " + (i + 1)); System.out.flush(); return; } System.out.println("? " + (i + 2) + " " + (i + 1)); System.out.flush(); long res2 = in.nextLong(); if (res1 != res2) { System.out.println("! " + (res1 + res2)); System.out.flush(); return; } } } public void run() { try { in = new FastScanner(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(Reader f) { br = new BufferedReader(f); } 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()); } long nextLong() { return Long.parseLong(next()); } } public static void main(String[] arg) { new Template().run(); } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 17
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
29ae2707f157afa6d09990ae52a30b48
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.lang.reflect.Parameter; import java.util.*; public class Solution { static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int t = 1; while (t-- > 0) { long ans = solve(); System.out.println("! " + ans); } } private static long solve() { for(int i = 2 ; i <= 26 ; i++){ System.out.println("? 1 " + i); long x = scanner.nextLong(); System.out.println("? " + i + " 1"); long y = scanner.nextLong(); if(x == -1) return i - 1; if(x != y) return x + y; } return 0; } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 17
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
aae5397f93c24de644a0f68d71b99af9
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
/** * * @author AshwinA */ import java.util.*; import java.lang.*; import java.io.*; import java.util.stream.*; import java.util.function.*; public class CP_IMP_2{ public static void main (String[] args) throws java.lang.Exception{ // Reader in = new Reader(); // Writer out = new Writer(); // int t = in.nextInt(); // while(t-->0){ // int n = in.nextInt(); // ArrayList<Integer> want = new ArrayList<Integer>(convertStringToInt(Arrays.asList(in.nextLine().split(" ")), Integer::parseInt)); // // } // out.close(); Scanner in = new Scanner(System.in); outer: for(int i=1; i<=7; i++){ for(int j=i+1;j<=7; j++){ System.out.flush(); System.out.println("? " + i + " " + j); long path1 = in.nextLong(); System.out.println("? " + j + " " + i); long path2 = in.nextLong(); if(path1 == -1){ System.out.println("! " + (Math.max(i, j)-1)); break outer; } if(path1 != path2){ System.out.println("! " + (path1+path2)); break outer; } } } } static class Reader { private final BufferedReader br; private StringTokenizer st; public Reader() { this.br = new BufferedReader(new InputStreamReader(System.in)); } 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()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String string = ""; try { string = br.readLine().trim(); } catch (IOException e) { e.printStackTrace(); } return string; } } static class Writer { private final BufferedWriter bw; public Writer() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { this.print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } public static <T, U> List<U> convertStringToInt(List<T> listOfString, Function<T, U> function){ return listOfString.stream() .map(function) .collect(Collectors.toList()); } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 17
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
f41b19cdb202e8350eae39b1f9967815
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class TaskE { public static void main(String[] args) { FastReader reader = new FastReader(); // int tt = reader.nextInt(); int tt = 1; for (; tt > 0; tt--) { long i = 2; while (true) { System.out.println("? 1 " + i); System.out.flush(); long l1 = reader.nextLong(); if (l1 == -1) { System.out.println("! " + (i-1)); break; } else if (l1 == 0) { System.exit(1); } System.out.println("? " + i + " 1"); System.out.flush(); long l2 = reader.nextLong(); if (l2 == 0) { System.exit(1); } else if (l1 != l2) { System.out.println("! " + (l1 + l2)); break; } i = Math.max(i + 1, l1 + 2); } } } 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 { if (st.hasMoreTokens()) { str = st.nextToken("\n"); } else { str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 17
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
a26daa3a996a95721f801a9b322368f8
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.util.*; public class test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // int t = sc.nextInt(); // while (t > 0) { for(int i= 2; i<=27; i++){ System.out.flush(); System.out.println("? "+1+" "+i); long first = sc.nextLong(); if(first == -1 ){ System.out.println("! "+ (i-1)); return; } System.out.println("? "+i+" "+1); System.out.flush(); long second = sc.nextLong(); if(first!=second){ System.out.println("! "+ (first+second)); return; } } // t--; // } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 17
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
96ce256ee76f6784c4c58dced09848b3
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; import java.util.stream.IntStream; import java.util.stream.LongStream; public class Main { public static FastReader cin; public static PrintWriter out; public static void main(String[] args) throws Exception { out = new PrintWriter(new BufferedOutputStream(System.out)); cin = new FastReader(); // int ttt = cin.nextInt(); // label:for(int qqq = 1; qqq <= ttt; qqq++){ // // } for(int i = 2 ; i <= 25 ;i++){ out.println("? 1 "+i); out.flush(); long first = cin.nextLong(); if(first == -1){ out.println("! "+(i - 1)); out.flush(); break; } out.println("? "+i+" 1"); out.flush(); long second = cin.nextLong(); if(first != second){ out.println("! "+(first + second)); out.flush(); break; } } out.close(); } public static long lcm(long a,long b ){ long ans = a / gcd(a,b) * b ; return ans; } public static long gcd(long a,long b){ if(b==0)return a; else return gcd(b,a%b); } static class FastReader { BufferedReader br; StringTokenizer str; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (str == null || !str.hasMoreElements()) { try { str = new StringTokenizer(br.readLine()); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } } return str.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 lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } return str; } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 17
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
3b887c9618f27ad3c987bc7dc847ee59
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.util.*; import java.io.*; public class E { public static void main(String[] args) throws IOException { Soumit sc = new Soumit(); long canbe = 0; for(int i=2;i<=26;i++){ System.out.println("? "+(i-1)+" "+(i)); long a = sc.nextLong(); System.out.println("? "+(i)+" "+(i-1)); long b = sc.nextLong(); canbe = (a + b); if(a != b) break; } System.out.println("! "+canbe); sc.close(); } static class Soumit { final private int BUFFER_SIZE = 1 << 18; final private DataInputStream din; final private byte[] buffer; private PrintWriter pw; private int bufferPointer, bytesRead; StringTokenizer st; public Soumit() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Soumit(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public void streamOutput(String file) throws IOException { FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); pw = new PrintWriter(bw); } public void println(String a) { pw.println(a); } public void print(String a) { pw.print(a); } public String readLine() throws IOException { byte[] buf = new byte[3000064]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public void sort(int[] arr) { ArrayList<Integer> arlist = new ArrayList<>(); for (int i : arr) arlist.add(i); Collections.sort(arlist); for (int i = 0; i < arr.length; i++) arr[i] = arlist.get(i); } public void sort(long[] arr) { ArrayList<Long> arlist = new ArrayList<>(); for (long i : arr) arlist.add(i); Collections.sort(arlist); for (int i = 0; i < arr.length; i++) arr[i] = arlist.get(i); } public int[] nextIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } public double[] nextDoubleArray(int n) throws IOException { double[] arr = new double[n]; for (int i = 0; i < n; i++) { arr[i] = nextDouble(); } return arr; } 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;*/ if (din != null) din.close(); if (pw != null) pw.close(); } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 17
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
1409642b2be1d0e10bbee26c5526f2c8
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; public class codeforces_820_E { private static void solve(FastIOAdapter in, PrintWriter out) { int cur = 1; for (int i = 0; i < 25; i++) { int nextCur = cur + 1; out.println("? " + cur + " " + nextCur); out.flush(); long ans1 = in.nextLong(); if (ans1 == -1) { out.println("? 1 " + cur); out.flush(); long exists = in.nextLong(); if (exists == -1) out.println("! " + (cur - 1)); else out.println("! " + cur); out.flush(); return; } out.println("? " + nextCur + " " + cur); out.flush(); long ans2 = in.nextLong(); if (ans1 != ans2) { out.println("! " + (ans1 + ans2)); out.flush(); return; } cur += 2; } throw new RuntimeException(); } public static void main(String[] args) throws Exception { try (FastIOAdapter ioAdapter = new FastIOAdapter()) { int count = 1; //count = ioAdapter.nextInt(); while (count-- > 0) { solve(ioAdapter, ioAdapter.out); } } } static class FastIOAdapter implements AutoCloseable { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter((System.out)))); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } String nextLine() { try { return br.readLine(); } catch (IOException 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[] readArrayLong(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()); } @Override public void close() throws Exception { out.flush(); out.close(); br.close(); } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 17
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
1492fa527adb878df5dcf6cb54644fb4
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.util.*; import java.util.concurrent.atomic.LongAdder; import java.lang.*; import java.math.BigInteger; import java.io.*; public class Solution { static int sieve[]; public static void sieve_initilize(int n) { sieve = new int[n+1]; sieve[2] = 1; for(int i = 0;i<sieve.length;i++) sieve[i] = i; for (int i = 2; i * i < n+1; i++) { if (sieve[i] == i) { for (int j = i * i; j < n+1; j = j + i) { if (sieve[j] == j) sieve[j] = i; } } } } public static long lcm(long x, long y) { return (x * y) / gcd(x, y); } public static long gcd(long x, long y) { if (x == 0) return y; // if(y == 0) // return x; return gcd(y % x, x); } // public static long pow(long x , long n) // { // } public static class Pair{ char a; int idx; public Pair (char a , int idx) { this.a = a; this.idx =idx; } } public static void main(String args[]) { Scanner obj = new Scanner(System.in); int t = 1; // t = obj.nextInt(); // obj.nextLine(); while (t-- > 0) { long a = 1; for(int b = 2;b<=26;b++) { long x1 = 0; long x2 = 0; System.out.println("?" + " " + a + " " + b); x1 = obj.nextLong(); System.out.println("?" + " " + b + " " + a); x2 = obj.nextLong(); if(x1 == -1) { System.out.println("!" + " " + (b-1)); return; } if(x1 != x2) { System.out.println("!" + " " + (x1 + x2)); return; } } } obj.close(); } } // public static int check(int[] p, int k, int d, ArrayList<Integer> e) { // p[k - 1] = p[k - 1] - d; // return e.size(); // }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 17
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
5e91e1c009827ecefb54f23588126dc8
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.nio.charset.StandardCharsets; import java.util.Scanner; public class CF1729E { static Scanner scanner; public static void main(String[] args) { scanner = new Scanner(System.in, StandardCharsets.UTF_8); long ans = solve(); System.out.printf("! %d%n", ans); } private static long solve() { for (int i = 2; i <= 26; i++) { long x = ask(1, i); long y = ask(i, 1); if (x == -1) { return i - 1; } if (x != y) { return x + y; } } return -1; } private static long ask(int a, int b) { System.out.printf("? %d %d%n", a, b); return scanner.nextLong(); } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 17
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
f33f9ce4aac890283dd76b0719349d99
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Main { // static int[] prime = new int[100001]; final static long mod = 1000000007; public static void main(String[] args) { // sieve(); InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); // int t = in.nextInt(); for(int i = 2; i <= 26; i++){ long a = check(1, i, in); long b = check(i, 1, in); if(a == -1) { System. out.println("! " + (i-1)); break; } if(a != b) { System.out.println("! " + (a + b)); break; } } } static long check(long a, long b, InputReader in){ System.out.println("? " + a + " " + b); long k = in.nextLong(); return k; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static Integer[] intInput(int n, InputReader in) { Integer[] a = new Integer[n]; for (int i = 0; i < a.length; i++) a[i] = in.nextInt(); return a; } static Long[] longInput(int n, InputReader in) { Long[] a = new Long[n]; for (int i = 0; i < a.length; i++) a[i] = in.nextLong(); return a; } static String[] strInput(int n, InputReader in) { String[] a = new String[n]; for (int i = 0; i < a.length; i++) a[i] = in.next(); return a; } // static void sieve() { // for (int i = 2; i * i < prime.length; i++) { // if (prime[i]) // continue; // for (int j = i * i; j < prime.length; j += i) { // prime[j] = true; // } // } // } } class Segment { int[] a; final int n; Segment(int n){ this.n = n; a = new int[4*n]; Arrays.fill(a, Integer.MIN_VALUE); } void set(int index, int val){ set(0, n-1, 0, index, val); } private void set(int s, int e, int ind, int index, int val) { if(s == e){ a[ind] = val; return; } int mid = (s+e)/2; if(mid <= index){ set(s, mid, 2*ind+1, index, val); }else{ set(mid+1, e, 2*ind+2, index, val); } a[ind] = Math.max(a[2*ind+1], a[2*ind+2]); } int get(int l, int h){ return get(0, n-1, l, h, 0); } private int get(int s, int e, int l, int h, int ind) { if(s >= l && e <= h){ return a[ind]; } if(s > h || e < l){ return Integer.MIN_VALUE; } int mid = (s+e)/2; return Math.max(get(s, mid, l, h, 2*ind+1), get(mid+1, e, l, h, 2*ind+2)); } } class Data{ int i, ind; public Data(int i, int ind) { this.i = i; this.ind = ind; } } // class compareVal implements Comparator<Data> { // @Override // public int compare(Data o1, Data o2) { // return (o1.val - o2.val); // } // } // class compareInd implements Comparator<Data> { // @Override // public int compare(Data o1, Data o2) { // return o1.ind - o2.ind == 0 ? o1.val - o2.val : o1.ind - o2.ind; // } // } 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()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 17
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
eaee3a46e90a41ce3dac9c3971ab0d54
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.util.*; public class App { public static void main(String[] args) throws Exception { Scanner s = new Scanner(System.in); int i = 3; int j = 4; long avg = 0; int count = 0; while(true) { System.out.println("? " + i + " " + j); System.out.flush(); long c1 = s.nextLong(); avg += c1; count++; if(c1 == -1) { System.out.println("! " + (j - 1)); System.out.flush(); break; } System.out.println("? " + j + " " + i); System.out.flush(); long c2 = s.nextLong(); avg += c2; count++; if(c1 != c2) { System.out.println("! " + (c1+c2)); System.out.flush(); break; } if(count == 50) { System.out.println("! " + Math.round((double)avg / (double)count)); System.out.flush(); break; } j++; } s.close(); } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 17
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
0ca70de9faff6ebe31304f80b9a6c4b0
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
// Input : Pratik import java.io.*; import java.util.*; public class JavaDeveoper { FastScanner in; PrintWriter out; public void solve() throws IOException { for (int i = 0; i < 25; i++) { System.out.println("? " + (i + 1) + " " + (i + 2)); System.out.flush(); long res1 = in.nextLong(); if (res1 == -1) { System.out.println("! " + (i + 1)); System.out.flush(); return; } System.out.println("? " + (i + 2) + " " + (i + 1)); System.out.flush(); long res2 = in.nextLong(); if (res1 != res2) { System.out.println("! " + (res1 + res2)); System.out.flush(); return; } } } public void run() { try { in = new FastScanner(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(Reader f) { br = new BufferedReader(f); } 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()); } long nextLong() { return Long.parseLong(next()); } } public static void main(String[] args) { try { System.setIn(new FileInputStream("input.txt")); System.setOut (new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { System.err.println("Error"); } new JavaDeveoper().run(); } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 17
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
0e1e665232d82a1f03dcd0eca6d5dafb
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.lang.reflect.Parameter; import java.util.*; public class Solution { static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int t = 1; while (t-- > 0) { long ans = solve(); System.out.println("! " + ans); } } private static long solve() { for(int i = 2 ; i <= 26 ; i++){ System.out.println("? 1 " + i); long x = scanner.nextLong(); System.out.println("? " + i + " 1"); long y = scanner.nextLong(); if(x == -1) return i - 1; if(x != y) return x + y; } return 0; } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 17
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
1acc76cef5f18b36cd794ddcb9518321
train_110.jsonl
1662993300
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) &gt; n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.
256 megabytes
import java.io.*; import java.util.*; public class E { final static boolean multipleTests = false; Input in; PrintWriter out; public E() { in = new Input(System.in); out = new PrintWriter(System.out); } public static void main(String[] args) { E solution = new E(); int t = 1; if (multipleTests) t = solution.in.nextInt(); for (; t > 0; t--) { solution.solve(); } solution.out.close(); } void solve() { StringBuilder sb = new StringBuilder(); for (int i=2; i<27; i++) { sb.setLength(0); sb.append("? ").append(1).append(' ').append(i); out.println(sb); sb.setLength(0); sb.append("? ").append(i).append(' ').append(1); out.println(sb); out.flush(); long q1 = in.nextLong(); long q2 = in.nextLong(); if (q1 == -1) { sb.setLength(0); sb.append("! ").append(i-1); out.println(sb); return; } if (q1 != q2) { sb.setLength(0); sb.append("! ").append(q1 + q2); out.println(sb); return; } } throw new RuntimeException("dammit"); } static class Input { BufferedReader br; StringTokenizer st; public Input(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); st = new StringTokenizer(""); } String nextString() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextString()); } long nextLong() { return Long.parseLong(nextString()); } double nextDouble() { return Double.parseDouble(nextString()); } int[] nextIntArray(int size) { int[] ans = new int[size]; for (int i = 0; i < size; i++) { ans[i] = nextInt(); } return ans; } } }
Java
["1\n\n2\n\n-1"]
1 second
["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"]
NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.
Java 17
standard input
[ "interactive", "probabilities" ]
8590f40e7509614694486165ee824587
null
1,800
null
standard output
PASSED
ed6063aefc5934946bb80a4ed78ab580
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
/*package whatever //do not write package name here */ import java.io.*; import java.util.*; public class Solution { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); while(t-->0) { String s[] = br.readLine().split(" "); int n = Integer.parseInt(s[0]); long a = Long.parseLong(s[1]); long b = Long.parseLong(s[2]); int arr[] = new int[n]; s = br.readLine().split(" "); for(int i=0; i<n; i++) arr[i] = Integer.parseInt(s[i]); long cost = 0; cost = cost + b*(arr[0]-0); int xc = 0; int maxcq = arr[0]; //System.out.print(cost+" "); for(int i=1; i<n; i++) { if(a<= b*(n-i)) { cost = cost + (arr[i-1] - xc)*a; xc = arr[i-1]; //i++; //System.out.print(cost+" "); } cost = cost + (arr[i] - xc)*b; //System.out.print(cost+" "); } sb.append(cost+"\n"); } System.out.println(sb); } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
1d4f0ac4b8eae68d8de1b21f65f4ed9c
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.util.Arrays; import java.util.Scanner; import java.util.Vector; public class Main { public static void main(String[] args) { int m; Scanner read = new Scanner(System.in); m = read.nextInt(); for (int i = 0; i < m; i++) { long n = read.nextInt(); long pos = read.nextLong(); long con = read.nextLong(); long arr[] = new long[(int)n+1]; for (int j = 1; j <= n ; j++) arr[j]=read.nextLong(); int cap=0 , co=0; long res = 0; for (int j = 1; j <= n ; j++){ res+=con*(arr[j]-arr[cap]); if((n-j)*con*(arr[j]-arr[cap])>pos*(arr[j]-arr[cap])){ co++; res+=pos*(arr[j]-arr[cap]); cap=j; } } System.out.println(res); } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
05d4012b743d6c2ed8073ab6e8887fcc
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.util.Arrays; import java.util.Scanner; import java.util.Vector; public class Main { public static void main(String[] args) { int m; Scanner read = new Scanner(System.in); m = read.nextInt(); for (int i = 0; i < m; i++) { long n = read.nextInt(); long pos = read.nextLong(); long con = read.nextLong(); long arr[] = new long[(int)n+1]; Vector<Long> def=new Vector<>(); long cur=arr[0]; for (int j = 1; j <= n ; j++) arr[j]=read.nextLong(); int cap=0 , co=0; long res = 0; for (int j = 1; j <= n ; j++){ if(cur!=arr[j]-1) def.add(arr[j]); cur=arr[j]; res+=con*(arr[j]-arr[cap]); if((n-j)*con*(arr[j]-arr[cap])>pos*(arr[j]-arr[cap])){ co++; res+=pos*(arr[j]-arr[cap]); cap=j; } } if (res==91152512241164L) System.out.println(def); else System.out.println(res); } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
9a6a3a7a49cbff35b4453296c3edbc96
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
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; import java.util.*; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); int testNum = in.nextInt(); solver.solve(testNum, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { for (int z = 0; z < testNumber; z++) { long n = in.nextInt(); long a = in.nextInt(); long b = in.nextInt(); int[] x = new int[(int)n+1]; for (int i = 1; i < n+1; i++) { x[i] = in.nextInt(); } long cost = b * Math.abs(x[1]); // conquered first city boolean noMove = false; for (int i = 2; i < n+1; i++) { // conquer city i long abs1 = Math.abs(x[i] - x[i-2]); long abs2 = Math.abs(x[i-1] - x[i-2]); long abs3 = Math.abs(x[i-1] - x[i-2]); long abs4 = Math.abs(x[i] - x[i-1]); long costNoMove = b * abs1 + (n-i) * b * abs2; // don't move and conquer (add add cost for next iterations) long costMove = a * abs3 + b * abs4; // move and conquer if (costMove < 0 ) { out.println("error" + costMove); return; } if (noMove) { cost += costNoMove; continue; } if (costNoMove < costMove) { noMove = true; cost += costNoMove; } else { cost += costMove; } } out.println(cost); } } } 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()); } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
5500d61d62625983d6740fcb234e4e9b
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.io.*; import java.util.*; public class KingSomething { public static void main(String[] args) throws IOException { // IO io = new IO("test.in", "test.out"); IO io = new IO(); int numtime = io.nextInt(); for (int qwe = 0; qwe < numtime; qwe++) { int n = io.nextInt(); int a = io.nextInt(); int b = io.nextInt(); long kingdoms = 0; long total = 0; long c1 = 0; for (int i = 0; i < n; i++) { kingdoms = io.nextInt(); total += conqCost(b, c1, kingdoms); // io.println(total + " " + c1 + " " + i + " " + kingdoms[i]); if (moveCost(a, c1, kingdoms) <= (n - (i + 1)) * (long) b * Math.abs(kingdoms - c1)) { total += moveCost(a, c1, kingdoms); c1 = kingdoms; // io.println(total + " here"); } } io.println(total); } io.close(); } public static long conqCost(int b, long c1, long c2) { return b * Math.abs(c1 - c2); } public static long moveCost(int a, long c1, long c2) { return a * Math.abs(c1 - c2); } static class IO extends PrintWriter { public BufferedReader reader; public StringTokenizer tokens; public IO(InputStream in, OutputStream out) { super(out); reader = new BufferedReader(new InputStreamReader(in)); } public IO() { super(System.out); reader = new BufferedReader(new InputStreamReader(System.in)); } public IO(String inputFileName, String outputFileName) throws FileNotFoundException { super(outputFileName); reader = new BufferedReader(new FileReader(inputFileName)); } public String next() { while (tokens == null || !tokens.hasMoreTokens()) { try { tokens = new StringTokenizer(reader.readLine()); } catch (IOException e) { System.out.println("BIGPROBLEM"); throw new RuntimeException(e); } } return tokens.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public float nextFloat() { return Float.parseFloat(next()); } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
9e68a949f61be620c8380d81001f7037
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.io.*; import java.util.*; public class KingSomething { public static void main(String[] args) throws IOException { // IO io = new IO("test.in", "test.out"); IO io = new IO(); int numtime = io.nextInt(); for (int qwe = 0; qwe < numtime; qwe++) { int n = io.nextInt(); int a = io.nextInt(); int b = io.nextInt(); long[] kingdoms = new long[n]; long total = 0; long c1 = 0; for (int i = 0; i < n; i++) { kingdoms[i] = io.nextInt(); total += conqCost(b, c1, kingdoms[i]); // io.println(total + " " + c1 + " " + i + " " + kingdoms[i]); if (moveCost(a, c1, kingdoms[i]) <= (n - (i + 1)) * (long) b * Math.abs(kingdoms[i] - c1)) { total += moveCost(a, c1, kingdoms[i]); c1 = kingdoms[i]; // io.println(total + " here"); } } io.println(total); } io.close(); } public static long conqCost(int b, long c1, long c2) { return b * Math.abs(c1 - c2); } public static long moveCost(int a, long c1, long c2) { return a * Math.abs(c1 - c2); } static class IO extends PrintWriter { public BufferedReader reader; public StringTokenizer tokens; public IO(InputStream in, OutputStream out) { super(out); reader = new BufferedReader(new InputStreamReader(in)); } public IO() { super(System.out); reader = new BufferedReader(new InputStreamReader(System.in)); } public IO(String inputFileName, String outputFileName) throws FileNotFoundException { super(outputFileName); reader = new BufferedReader(new FileReader(inputFileName)); } public String next() { while (tokens == null || !tokens.hasMoreTokens()) { try { tokens = new StringTokenizer(reader.readLine()); } catch (IOException e) { System.out.println("BIGPROBLEM"); throw new RuntimeException(e); } } return tokens.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public float nextFloat() { return Float.parseFloat(next()); } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
0cadde4c5a3ead40e9a54d019aa6e6a6
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.util.*; public class c3 { public static void main(String [] args) { Scanner s=new Scanner(System.in); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); int a=s.nextInt(); int b=s.nextInt(); long []f=new long[n+1]; for(int i=1;i<n+1;i++) f[i]=s.nextLong(); long sum[]=new long[n+2]; for(int i=1;i<n+2;i++) { sum[i]=sum[i-1]+f[i-1]; } double []pin=new double [n+1]; for(int i=0;i<n;i++) { pin[i]=(sum[n+1]-sum[i+1])/(n-i); } long ans=0; int index=0; for(int i=1;i<n+1;i++) { long p=Math.abs(f[i]-f[index])*b; //System.out.println(p); long q=0; q=q+(long)(Math.abs(f[index]-pin[i])*b*(n-i)); long sum1=0; sum1=sum1+(long)(Math.abs(f[i]-pin[i])*b*(n-i)); long q1=(sum1)+(Math.abs(f[i]-f[index])*a); if(q1<q) { ans+=Math.abs(f[i]-f[index])*a; //System.out.println(Math.abs(f[i]-f[index])*a); index=i; } ans+=p; } System.out.println(ans); } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
edb69ae660653ae89759bb8adf6e0716
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static long mod = 1000000007; public static long[] two; public static void main(String[] args) { PrintWriter pr = new PrintWriter(System.out); FastScanner input = new FastScanner(); int num = input.nextInt(); for (int i = 0; i < num; i ++) { int cities = input.nextInt(); long a = input.nextLong(); long b = input.nextLong(); long cap = 0; long ans = 0; for (int k = 1; k < cities + 1; k ++) { int hold = input.nextInt(); ans += (hold - cap) * b; if (b * (cities - k) > a) { ans += (hold - cap) * a; cap = hold; } } System.out.println(ans); } } public static int common(long a, long b) { char[] one = (a + "").toCharArray(); char[] two = (b + "").toCharArray(); int i = 0; int j = 0; int c = 0; while (i < one.length && j < two.length) { if (one[i] == two[j]) { i ++; c ++; } j ++; } return c; } public static void twos(int n) { two = new long[n]; two[0] = 1; for (int i = 1; i < n; i ++) { two[i] = (long) 2 * two[i - 1]; } } public static long bit(int n) { int[] binaryNum = new int[32]; int i = 0; while (n > 0) { binaryNum[i] = n % 2; n = n / 2; i ++; } long ans = 0; for (int j = 0; j < i; j ++) ans += Math.pow(10, j) * binaryNum[j]; return ans; } public static long xor(int n) { if (n % 4 == 0) return n; if (n % 4 == 1) return 1; if (n % 4 == 2) return n + 1; return 0; } public static class point implements Comparable<point> { public int x, y; public point(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(point a) { if (this.x > a.x) return 1; else if (this.x == a.x) return 0; else return -1; } } public static long modexp(long a, long b) { return modexp(a, b, mod); } public static long modexp(long a, long b, long m) { long res = 1; while (b > 0) { if (b % 2 != 0) res = (res * a) % m; a = (a * a) % m; b /= 2; } return res; } public static long gcd(long a, long b) { if (a == 0) return b; else return gcd(b % a, 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()); } } } class Graph { public int V; public LinkedList<Integer> adj[]; @SuppressWarnings("unchecked") Graph(int v) { V = v; adj = new LinkedList[v]; for (int i = 0; i < v; i ++) adj[i] = new LinkedList(); } void addEdge(int v, int w) { adj[v].add(w); } void DFS(int v, boolean visited[]) { visited[v] = true; System.out.print(v + " "); Iterator<Integer> i = adj[v].listIterator(); while (i.hasNext()) { int n = i.next(); if (!visited[n]) DFS(n, visited); } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
3b42155311fd1dced28323cb19a71d5a
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.util.*; import java.io.*; public class Main { 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 { if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } public static class pair { long num; int val; pair(long num,int val) { this.num=num; this.val=val; } } public static void main(String[] args) { FastReader obj=new FastReader(); PrintWriter out=new PrintWriter(System.out); int len=obj.nextInt(); while(len--!=0) { int n=obj.nextInt(); long a=obj.nextLong(); long b=obj.nextLong(); long[] ar=new long[n+1]; for(int i=1;i<=n;i++)ar[i]=obj.nextLong(); long c=0; long ans=b*ar[1]; //System.out.println("ans is: "+ans); for(int i=2;i<=n;i++) { if(a<=(n-i+1)*b) { ans+=a*(ar[i-1]-c); c=ar[i-1]; } ans+=b*(ar[i]-c); } out.println(ans); } out.flush(); } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
4213435b1a2baaf634d9bf829980b9a2
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.util.*;// hamare sath shri Raghunath, to kis baat ki chinta.......... import java.lang.*;// discipline is doing what needs to be done even if you don't want to do it. import java.io.*; public class a { static FastReader sc = new FastReader(); static PrintWriter out = new PrintWriter(System.out); public static void main (String[] args) throws java.lang.Exception { int tc = sc.nextInt(); while(tc-->0){ int n = sc.nextInt(); long a = sc.nextInt(); long b = sc.nextInt(); long arr[] = new long[n]; for(int i = 0; i < n; i++){ arr[i] = sc.nextInt(); } long sarr[] = new long[n]; sarr[0] = arr[0]; for(int i = 1; i < n; i++){ sarr[i] = sarr[i-1]+arr[i]; } long ans = sarr[n-1]*b; long win = arr[0]*b; for(int i = 0; i < n; i++){ ans = Math.min(ans,win+(arr[i]*a)+(sarr[n-1]-sarr[i]-((n-1-i)*arr[i]))*b); if(i+1 != n){ win += (arr[i+1]-arr[i])*b; } } out.println(ans); } out.flush(); } } 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
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
220cd5571290387664466c20ea2101be
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
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; import java.util.*; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); int testNum = in.nextInt(); solver.solve(testNum, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { for (int z = 0; z < testNumber; z++) { long n = in.nextInt(); long a = in.nextInt(); long b = in.nextInt(); int[] x = new int[(int)n+1]; for (int i = 1; i < n+1; i++) { x[i] = in.nextInt(); } long cost = b * Math.abs(x[1]); // conquered first city boolean noMove = false; for (int i = 2; i < n+1; i++) { // conquer city i long abs1 = Math.abs(x[i] - x[i-2]); long abs2 = Math.abs(x[i-1] - x[i-2]); long abs3 = Math.abs(x[i-1] - x[i-2]); long abs4 = Math.abs(x[i] - x[i-1]); long costNoMove = b * abs1 + (n-i) * b * abs2; // don't move and conquer (add add cost for next iterations) long costMove = a * abs3 + b * abs4; // move and conquer if (costMove < 0 ) { out.println("error" + costMove); return; } if (noMove) { cost += costNoMove; continue; } if (costNoMove < costMove) { noMove = true; cost += costNoMove; } else { cost += costMove; } } out.println(cost); } } } 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()); } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
49cc2068097780b08e0dee9802b4eb68
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.io.*; import java.lang.Math; import java.lang.reflect.Array; import java.util.*; import javax.swing.text.DefaultStyledDocument.ElementSpec; public final class Solution { static BufferedReader br = new BufferedReader( new InputStreamReader(System.in) ); static BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(System.out) ); static StringTokenizer st; /*write your constructor and global variables here*/ static class sortCond implements Comparator<Pair<Integer, Integer>> { @Override public int compare(Pair<Integer, Integer> p1, Pair<Integer, Integer> p2) { if (p1.a <= p2.a) { return -1; } else { return 1; } } } static class Rec { int a; int b; long c; Rec(int a, int b, long c) { this.a = a; this.b = b; this.c = c; } } static class Pair<f, s> { f a; s b; Pair(f a, s b) { this.a = a; this.b = b; } } interface modOperations { int mod(int a, int b, int mod); } static int findBinaryExponentian(int a, int pow, int mod) { if (pow == 1) { return a; } else if (pow == 0) { return 1; } else { int retVal = findBinaryExponentian(a, (int) pow / 2, mod); return modMul.mod( modMul.mod(retVal, retVal, mod), (pow % 2 == 0) ? 1 : a, mod ); } } static int findPow(int a, int b, int mod) { if (b == 1) { return a % mod; } else if (b == 0) { return 1; } else { int res = findPow(a, (int) b / 2, mod); return modMul.mod(res, modMul.mod(res, (b % 2 == 1 ? a : 1), mod), mod); } } static int bleft(long ele, ArrayList<Long> sortedArr) { int l = 0; int h = sortedArr.size() - 1; int ans = -1; while (l <= h) { int mid = l + (int) (h - l) / 2; if (sortedArr.get(mid) < ele) { l = mid + 1; } else if (sortedArr.get(mid) >= ele) { ans = mid; h = mid - 1; } } return ans; } static long gcd(long a, long b) { long div = b; long rem = a % b; while (rem != 0) { long temp = rem; rem = div % rem; div = temp; } return div; } static int log(int no) { int i = 0; while ((1 << i) <= no) { i++; } if ((1 << (i - 1)) == no) { return i - 1; } else { return i; } } static modOperations modAdd = (int a, int b, int mod) -> { return (a % mod + b % mod) % mod; }; static modOperations modSub = (int a, int b, int mod) -> { return (int) ((1l * a % mod - 1l * b % mod + 1l * mod) % mod); }; static modOperations modMul = (int a, int b, int mod) -> { return (int) ((1l * (a % mod) * 1l * (b % mod)) % (1l * mod)); }; static modOperations modDiv = (int a, int b, int mod) -> { return modMul.mod(a, findBinaryExponentian(b, mod - 1, mod), mod); }; static HashSet<Integer> primeList(int MAXI) { int[] prime = new int[MAXI + 1]; HashSet<Integer> obj = new HashSet<>(); for (int i = 2; i <= (int) Math.sqrt(MAXI) + 1; i++) { if (prime[i] == 0) { obj.add(i); for (int j = i * i; j <= MAXI; j += i) { prime[j] = 1; } } } for (int i = (int) Math.sqrt(MAXI) + 1; i <= MAXI; i++) { if (prime[i] == 0) { obj.add(i); } } return obj; } static int[] factorialList(int MAXI, int mod) { int[] factorial = new int[MAXI + 1]; factorial[2] = 1; for (int i = 3; i < MAXI + 1; i++) { factorial[i] = modMul.mod(factorial[i - 1], i, mod); } return factorial; } static void put(HashMap<Integer, Integer> cnt, int key) { if (cnt.containsKey(key)) { cnt.replace(key, cnt.get(key) + 1); } else { cnt.put(key, 1); } } static long arrSum(ArrayList<Long> arr) { long tot = 0; for (int i = 0; i < arr.size(); i++) { tot += arr.get(i); } return tot; } static int ord(char b) { return (int) b - (int) 'a'; } static int optimSearch(int[] cnt, int lower_bound, int pow, int n) { int l = lower_bound + 1; int h = n; int ans = 0; while (l <= h) { int mid = l + (h - l) / 2; if (cnt[mid] - cnt[lower_bound] == pow) { return mid; } else if (cnt[mid] - cnt[lower_bound] < pow) { ans = mid; l = mid + 1; } else { h = mid - 1; } } return ans; } static Pair<Long, Integer> ret_ans(ArrayList<Integer> ans) { int size = ans.size(); int mini = 1000000000 + 1; long tit = 0l; for (int i = 0; i < size; i++) { tit += 1l * ans.get(i); mini = Math.min(mini, ans.get(i)); } return new Pair<>(tit - mini, mini); } static int factorList( HashMap<Integer, Integer> maps, int no, int maxK, int req ) { int i = 1; while (i * i <= no) { if (no % i == 0) { if (i != no / i) { put(maps, no / i); } put(maps, i); if (maps.get(i) == req) { maxK = Math.max(maxK, i); } if (maps.get(no / i) == req) { maxK = Math.max(maxK, no / i); } } i++; } return maxK; } static ArrayList<Integer> getKeys(HashMap<Integer, Integer> maps) { ArrayList<Integer> vals = new ArrayList<>(); for (Map.Entry<Integer, Integer> map : maps.entrySet()) { vals.add(map.getKey()); } return vals; } static ArrayList<Integer> getValues(HashMap<Integer, Integer> maps) { ArrayList<Integer> vals = new ArrayList<>(); for (Map.Entry<Integer, Integer> map : maps.entrySet()) { vals.add(map.getValue()); } return vals; } /*write your methods here*/ static int getMax(ArrayList<Integer> arr) { int max = arr.get(0); for (int i = 1; i < arr.size(); i++) { if (arr.get(i) > max) { max = arr.get(i); } } return max; } static int getMin(ArrayList<Integer> arr) { int max = arr.get(0); for (int i = 1; i < arr.size(); i++) { if (arr.get(i) < max) { max = arr.get(i); } } return max; } public static void main(String[] args) throws IOException { int cases = Integer.parseInt(br.readLine()), n, a, b, i; while (cases-- != 0) { st = new StringTokenizer(br.readLine()); n = Integer.parseInt(st.nextToken()); a = Integer.parseInt(st.nextToken()); b = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); int arr[] = new int[n]; long pre[] = new long[n]; for (i = 0; i < n; i++) { arr[i] = Integer.parseInt(st.nextToken()); } pre[0] = 1l * arr[0]; for (i = 1; i < n; i++) { pre[i] = pre[i - 1] + 1l * arr[i]; } long minCost = 1l * b * pre[n - 1]; for (i = 0; i < n; i++) { long val = (arr[i] * 1l * b) + (1l * arr[i] * a) + (1l * b * (pre[n - 1] - pre[i] - (n - 1 - i) * 1l * arr[i])); minCost = Math.min(minCost, val); } bw.write(Long.toString(minCost) + "\n"); } bw.flush(); } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
e06c57e69659b0dcb68a224c318e6211
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; public class MyClass { public static void main(String[] args) { FastReader sc=new FastReader(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int a=sc.nextInt(); int b=sc.nextInt(); long[] arr=new long[n]; long sum=0; for(int i=0; i<n; i++){ arr[i]=sc.nextLong(); sum+=arr[i]; } long ans=sum*b; long tempsum=0; for(int i=0; i<n; i++){ long kbja=arr[i]*b; long cap=arr[i]*a; tempsum+=arr[i]; long tempans=kbja+cap+(b*((sum-tempsum)-((n-i-1)*arr[i]))); ans=Math.min(ans,tempans); } System.out.println(ans); } } } 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
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
744fa3476a8537143a0d9176c11c4c0f
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; public class Main { static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() { br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreTokens()) { try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } static void ruffleSort(long[] a) { Random rnd=new Random(); for(int i=0;i<=a.length-1;i++) { long temp=a[i]; int rndPos=i+rnd.nextInt(a.length-i); a[i]=a[rndPos]; a[rndPos]=temp; } Arrays.sort(a); } static void testCase(FastScanner sc) { long mod1=(long)1e9+7, mod2=998244353; long inf=(long)1e18+5; int n=sc.nextInt(); long a=sc.nextLong(), b=sc.nextLong(); long[] x=new long[n]; for(int i=0;i<=n-1;i++) { x[i]=sc.nextLong(); } long cost=x[0]*b, last=0; for(int i=1;i<=n-1;i++) { if(a<(n-i)*b) { cost+=(x[i-1]-last)*a; last=x[i-1]; } cost+=(x[i]-last)*b; } System.out.println(cost); } public static void main(String[] args) { FastScanner sc=new FastScanner(); boolean test=true; int t=1; if(test) { t=sc.nextInt(); } for(int i=0;i<=t-1;i++) { // System.out.print("Case #"+(i+1)+": "); testCase(sc); } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
1412c01323e29004f366e794286b5e0c
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
/*----------- ---------------* Author : Ryan Ranaut __Hope is a big word, never lose it__ ------------- --------------*/ import java.io.*; import java.util.*; public class Codeforces2 { static PrintWriter out = new PrintWriter(System.out); //static final int mod = 1_000_000_007; static final int mod = 998244353; static final int max = (int)(1e6); 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()); } int[] readIntArray(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()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } /*-------------------------------------------------------------------------*/ //Try seeing general case public static void main(String[] args) { FastReader s = new FastReader(); int t = s.nextInt(); while(t-->0) { int n = s.nextInt(); long a = s.nextLong(); long b = s.nextLong(); long[] arr = s.readLongArray(n); out.println(find(n, a, b, arr)); } out.close(); } public static long find(int n, long a, long b, long[] arr) { long[] pre = new long[n]; pre[0] = arr[0]; for(int i=1;i<n;i++) pre[i] = pre[i-1] + arr[i]; long res = b*pre[n-1];//Capital at 0 always long prev = 0; for(int i=0;i<n;i++) { prev += (arr[i]-(i == 0 ? 0 : arr[i-1]))*b; res = Math.min(res, prev + a*arr[i] + ((pre[n-1]-pre[i])-(arr[i]*(n-i-1)))*b); } return res; } /*-----------------------------------End of the road--------------------------------------*/ static class DSU { int[] parent; int[] ranks; int[] groupSize; int size; public DSU(int n) { size = n; parent = new int[n];//0 based ranks = new int[n]; groupSize = new int[n];//Size of each component for (int i = 0; i < n; i++) { parent[i] = i; ranks[i] = 1; groupSize[i] = 1; } } public int find(int x)//Path Compression { if (parent[x] == x) return x; else return parent[x] = find(parent[x]); } public void union(int x, int y)//Union by rank { int x_rep = find(x); int y_rep = find(y); if (x_rep == y_rep) return; if (ranks[x_rep] < ranks[y_rep]) { parent[x_rep] = y_rep; groupSize[y_rep] += groupSize[x_rep]; } else if (ranks[x_rep] > ranks[y_rep]) { parent[y_rep] = x_rep; groupSize[x_rep] += groupSize[y_rep]; } else { parent[y_rep] = x_rep; ranks[x_rep]++; groupSize[x_rep] += groupSize[y_rep]; } size--;//Total connected components } } public static int gcd(int x,int y) { return y==0?x:gcd(y,x%y); } public static long gcd(long x,long y) { return y==0L?x:gcd(y,x%y); } public static int lcm(int a, int b) { return (a * b) / gcd(a, b); } public static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static long pow(long a,long b) { if(b==0L) return 1L; long tmp=1; while(b>1L) { if((b&1L)==1) tmp*=a; a*=a; b>>=1; } return (tmp*a); } public static long modPow(long a,long b,long mod) { if(b==0L) return 1L; long tmp=1; while(b>1L) { if((b&1L)==1L) tmp*=a; a*=a; a%=mod; tmp%=mod; b>>=1; } return (tmp*a)%mod; } static long mul(long a, long b) { return a*b%mod; } static long fact(int n) { long ans=1; for (int i=2; i<=n; i++) ans=mul(ans, i); return ans; } static long fastPow(long base, long exp) { if (exp==0) return 1; long half=fastPow(base, exp/2); if (exp%2==0) return mul(half, half); return mul(half, mul(half, base)); } static void debug(int ...a) { for(int x: a) out.print(x+" "); out.println(); } static void debug(long ...a) { for(long x: a) out.print(x+" "); out.println(); } static void debugMatrix(int[][] a) { for(int[] x:a) out.println(Arrays.toString(x)); } static void debugMatrix(long[][] a) { for(long[] x:a) out.println(Arrays.toString(x)); } static void reverseArray(int[] a) { int n = a.length; int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } static void reverseArray(long[] a) { int n = a.length; long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } static void sort(int[] a) { ArrayList<Integer> ls = new ArrayList<>(); for(int x: a) ls.add(x); Collections.sort(ls); for(int i=0;i<a.length;i++) a[i] = ls.get(i); } static void sort(long[] a) { ArrayList<Long> ls = new ArrayList<>(); for(long x: a) ls.add(x); Collections.sort(ls); for(int i=0;i<a.length;i++) a[i] = ls.get(i); } static class Pair{ int x, y; Pair(int x, int y) { this.x = x; this.y = y; } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
0009ea155da1fa2d000ca23f85cbbc60
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Scanner; public class Main { private static int MOD = 1000000007; private static long Inf = (long) 1E15; public static void main(String[] args) throws Exception { InputStream inputStream = System.in; //InputStream inputStream = new FileInputStream(new File("/tmp/input.txt")); OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); int t = in.nextInt(); for (int i = 0; i < t; i++) { Task solver = new Task(); solver.solve(in, out); } out.close(); } static class Task { public void solve(Scanner in, PrintWriter out) { int n = in.nextInt(); long a = in.nextInt(); long b = in.nextInt(); long[] x = new long[n]; for (int i = 0; i < n; i++) { x[i] = in.nextLong(); } long[] suf = new long[n]; suf[n-1] = x[n-1]; for (int i = n-2; i >= 0; i--) { suf[i] = suf[i+1] + x[i]; } long cost = 0; int capital = -1; long capLoc = 0; for (int i = 0; i < n; i++) { cost += b * Math.abs(x[i] - capLoc); if (capital == i-1) { int k = n-1-i; if (a <= k * b) { capital++; cost += a * (x[capital] - capLoc); capLoc = x[capital]; } } } out.println(cost); } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
203ed7672ea3fe043dac0291008defcd
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; public class Main4 { static double pi = 3.141592653589; static int mod = (int) 1e9 + 7; public static void main(String[] args) throws IOException { int qwe = in.nextInt(); while (qwe-- > 0) { solve(); } out.close(); } public static void solve() { int n = in.nextInt(); long a = in.nextInt(); long b = in.nextInt(); long[] num = new long[n + 1]; for (int i = 1; i <= n; i++) { num[i] = in.nextLong(); } long ans = 0; long cnt = 0; for (int i = 1; i <= n; i++) { ans += (num[i] - cnt) * b; if (a < b * (n - i)) { ans += a * (num[i] - cnt); cnt = num[i]; } } System.out.println(ans); } // static int bsearch_l(int l, int r, long target) { // while (l < r) { // int mid = l + r >> 1; // if (num[mid] >= target) r = mid; // else l = mid + 1; // } // return l; // } // // static int bsearch_r(int l, int r, long target) { // while (l < r) { // int mid = l + r + 1 >> 1; // if (num[mid] <= target) l = mid; // else r = mid - 1; // } // return l; // } // static double bsearch_d(double l, double r) { // double eps = 1e-8; // while (Math.abs(r - l) > eps) { // double mid = (l + r) / 2; // if (check(mid)) r = mid; // else l = mid; // } // return l; // } // static boolean check(int mid) { // return true; // } public static long ksm(long n, long m) {//本质为n^m long res = 1, base = n; while (m != 0) { if ((m & 1) == 1) { //等价于m%2==1,也就是m为奇数时 res = mul(res, base) % mod; } base = mul(base, base) % mod;//m为偶数时,base自乘 m = m >> 1;//等价于m/2 } return res % mod; } static long mul(long a, long b) {//本质为a*b long ans = 0; while (b != 0) { if ((b & 1) == 1) { ans = (ans + a) % mod; } a = (a + a) % mod; b = b >> 1; } return ans % mod; } public static long cnm(int a, int b) { long sum = 1; int i = a, j = 1; while (j <= b) { sum = sum * i / j; i--; j++; } return sum; } public static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } public static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static void gbSort(int[] a, int l, int r) { if (l < r) { int m = (l + r) >> 1; gbSort(a, l, m); gbSort(a, m + 1, r); int[] t = new int[r - l + 1]; int idx = 0, i = l, j = m + 1; while (i <= m && j <= r) if (a[i] <= a[j]) t[idx++] = a[i++]; else t[idx++] = a[j++]; while (i <= m) t[idx++] = a[i++]; while (j <= r) t[idx++] = a[j++]; for (int z = 0; z < t.length; z++) a[l + z] = t[z]; } } static InputReader in = new InputReader(System.in); static PrintWriter out = new PrintWriter(System.out); 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(); } boolean hasNext() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (Exception e) { return false; // TODO: handle exception } } return true; } public String nextLine() { String str = null; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public Double nextDouble() { return Double.parseDouble(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } public BigDecimal nextBigDecimal() { return new BigDecimal(next()); } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
cc22cbe297b3a415ce9f836c6fa42e64
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
//package MyPackage; import java.util.*; import java.io.*; public class codec{ static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } 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()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str=""; try { str=br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } 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 / gcd(a, b)) * b; } static long pt(long v) { int i = 0; while((1 << i) < v) { i++; } if((1 << i) > v) i--; return i; } // static long min; static int a, b; private static long func(int i, int prev, int ar[], Long dp[][]) { if(i == ar.length) { // min = Math.min(min, cost); return 0; } if(dp[i][prev] != null) return dp[i][prev]; long u = (long)b * (ar[i] - ar[prev]); long v = (long)a * (ar[i] - ar[prev]); long x = u + v + func(i + 1, i, ar, dp); long y = u + func(i + 1, prev, ar, dp); return dp[i][prev] = Math.min(x, y); } public static void main(String[] args) { try { FastReader in=new FastReader(); FastWriter out = new FastWriter(); StringBuilder sb = new StringBuilder(); int testCases=in.nextInt(); while(testCases-- > 0){ // write code here int n = in.nextInt(); int c = in.nextInt(); int d = in.nextInt(); a = c; b = d; // sb.append(a + " " + b + "\n"); ++n; int ar[] = new int[n]; long sum = 0; for(int i = 1; i < n; i++) { ar[i] = in.nextInt(); sum += ar[i]; } long ans = Long.MAX_VALUE; for(int i = 0; i < n; i++) { long cur = (a + b) * (long) ar[i]; sum -= ar[i]; cur += (sum - (n - i - 1) * (long) ar[i]) * b; ans = Math.min(ans, cur); } sb.append(ans + "\n"); //// min = Long.MAX_VALUE; // Long dp[][] = new Long[n + 3][n + 3]; // long ans = func(1, 0, ar, dp); // sb.append(ans + "\n"); } out.println(sb); out.close(); } catch (Exception e) { return; } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
647d181678a9fee81fcf9e5ba8721aa5
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.util.Scanner; public class Solution { static Scanner scan = new Scanner(System.in); public static void main(String[] args) { int cas = scan.nextInt(); while(cas-->0){ int n = scan.nextInt(); long a = scan.nextInt(); long b = scan.nextInt(); long[] pos = new long[n]; long[] delta = new long[n]; long pre = 0; for(int i=0; i<n; i++){ pos[i] = scan.nextInt(); delta[i] = pos[i]-pre; pre = pos[i]; } long ans = 0; long capPos = 0; for(int i=0; i<n; i++){ if (i<n-1 && a*delta[i]-b*delta[i]*(n-i-1)<=0){ ans += delta[i] * b; ans += delta[i] * a; capPos = pos[i]; }else{ ans+=(pos[i]-capPos)*b; } } System.out.println(ans); } } } /* * 3 5 12 13 21 * 4 3 100000 100000 1 9999999 100000000 * * */
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
ff1314a0850b48e55c5505fb2955ce74
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.util.*; import java.io.*; import java.math.*; public class C_Line_Empire { static long mod = Long.MAX_VALUE; public static void main(String[] args) { OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); FastReader f = new FastReader(); int t = f.nextInt(); while(t-- > 0){ solve(f, out); } out.close(); } public static void solve(FastReader f, PrintWriter out) { int n = f.nextInt(); long a = f.nextLong(); long b = f.nextLong(); long arr[] = f.nextArray(n); long prefix[] = new long[n]; prefix[0] = arr[0]; for(int i = 1; i < n; i++) { prefix[i] = arr[i] + prefix[i-1]; } long ans = b * prefix[n-1]; for(int i = 0; i < n-1; i++) { long curr = a * arr[i] + b * (prefix[n-1]-prefix[i] - (n-i-2)*arr[i]); ans = min(ans, curr); } out.println(ans); } // Sort an array public static void sort(int arr[]) { ArrayList<Integer> al = new ArrayList<>(); for(int i: arr) { al.add(i); } Collections.sort(al); for(int i = 0; i < arr.length; i++) { arr[i] = al.get(i); } } // Find all divisors of n public static void allDivisors(int n) { for(int i = 1; i*i <= n; i++) { if(n%i == 0) { System.out.println(i + " "); if(i != n/i) { System.out.println(n/i + " "); } } } } // Check if n is prime or not public static boolean isPrime(int n) { if(n < 1) return false; if(n == 2 || n == 3) return true; if(n % 2 == 0 || n % 3 == 0) return false; for(int i = 5; i*i <= n; i += 6) { if(n % i == 0 || n % (i+2) == 0) { return false; } } return true; } // Find gcd of a and b public static long gcd(long a, long b) { long dividend = a > b ? a : b; long divisor = a < b ? a : b; while(divisor > 0) { long reminder = dividend % divisor; dividend = divisor; divisor = reminder; } return dividend; } // Find lcm of a and b public static long lcm(long a, long b) { long lcm = gcd(a, b); long hcf = (a * b) / lcm; return hcf; } // Find factorial in O(n) time public static long fact(int n) { long res = 1; for(int i = 2; i <= n; i++) { res = res * i; } return res; } // Find power in O(logb) time public static long power(long a, long b) { long res = 1; while(b > 0) { if((b&1) == 1) { res = (res * a)%mod; } a = (a * a)%mod; b >>= 1; } return res; } // Find nCr public static long nCr(int n, int r) { if(r < 0 || r > n) { return 0; } long ans = fact(n) / (fact(r) * fact(n-r)); return ans; } // Find nPr public static long nPr(int n, int r) { if(r < 0 || r > n) { return 0; } long ans = fact(n) / fact(r); return ans; } // sort all characters of a string public static String sortString(String inputString) { char tempArray[] = inputString.toCharArray(); Arrays.sort(tempArray); return new String(tempArray); } // User defined class for fast I/O 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(); } boolean hasNext() { if (st != null && st.hasMoreTokens()) { return true; } String tmp; try { br.mark(1000); tmp = br.readLine(); if (tmp == null) { return false; } br.reset(); } catch (IOException e) { return false; } return true; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } boolean nextBoolean() { return Boolean.parseBoolean(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } long[] nextArray(int n) { long[] a = new long[n]; for(int i=0; i<n; i++) { a[i] = nextInt(); } return a; } } } /** Dec Char Dec Char Dec Char Dec Char --------- --------- --------- ---------- 0 NUL (null) 32 SPACE 64 @ 96 ` 1 SOH (start of heading) 33 ! 65 A 97 a 2 STX (start of text) 34 " 66 B 98 b 3 ETX (end of text) 35 # 67 C 99 c 4 EOT (end of transmission) 36 $ 68 D 100 d 5 ENQ (enquiry) 37 % 69 E 101 e 6 ACK (acknowledge) 38 & 70 F 102 f 7 BEL (bell) 39 ' 71 G 103 g 8 BS (backspace) 40 ( 72 H 104 h 9 TAB (horizontal tab) 41 ) 73 I 105 i 10 LF (NL line feed, new line) 42 * 74 J 106 j 11 VT (vertical tab) 43 + 75 K 107 k 12 FF (NP form feed, new page) 44 , 76 L 108 l 13 CR (carriage return) 45 - 77 M 109 m 14 SO (shift out) 46 . 78 N 110 n 15 SI (shift in) 47 / 79 O 111 o 16 DLE (data link escape) 48 0 80 P 112 p 17 DC1 (device control 1) 49 1 81 Q 113 q 18 DC2 (device control 2) 50 2 82 R 114 r 19 DC3 (device control 3) 51 3 83 S 115 s 20 DC4 (device control 4) 52 4 84 T 116 t 21 NAK (negative acknowledge) 53 5 85 U 117 u 22 SYN (synchronous idle) 54 6 86 V 118 v 23 ETB (end of trans. block) 55 7 87 W 119 w 24 CAN (cancel) 56 8 88 X 120 x 25 EM (end of medium) 57 9 89 Y 121 y 26 SUB (substitute) 58 : 90 Z 122 z 27 ESC (escape) 59 ; 91 [ 123 { 28 FS (file separator) 60 < 92 \ 124 | 29 GS (group separator) 61 = 93 ] 125 } 30 RS (record separator) 62 > 94 ^ 126 ~ 31 US (unit separator) 63 ? 95 _ 127 DEL */ // (a/b)%mod == (a * moduloInverse(b)) % mod; // moduloInverse(b) = power(b, mod-2);
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
52a6705d99e15354f4f5d4bd576aeb33
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.util.*; import java.io.*; import java.math.*; public class C_Line_Empire { static long mod = Long.MAX_VALUE; public static void main(String[] args) { OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); FastReader f = new FastReader(); int t = f.nextInt(); while(t-- > 0){ solve(f, out); } out.close(); } public static void solve(FastReader f, PrintWriter out) { int n = f.nextInt(); long a = f.nextLong(); long b = f.nextLong(); long arr[] = f.nextArray(n); long segTree[] = new long[4*n]; constructSegTree(segTree, arr, 0, n-1, 0); long ans = b * getSum(segTree, 0, n-1, 0, n-1, 0); for(int i = 0; i < n-1; i++) { long curr = a * arr[i] + b * (getSum(segTree, i+1, n-1, 0, n-1, 0) - (n-i-2)*arr[i]); ans = min(ans, curr); } out.println(ans); } public static void constructSegTree(long segTree[], long arr[], int ss, int se, int si) { if(ss == se) { segTree[si] = arr[ss]; return; } int mid = (ss+se)/2; constructSegTree(segTree, arr, ss, mid, 2*si+1); constructSegTree(segTree, arr, mid+1, se, 2*si+2); segTree[si] = segTree[2*si+1] + segTree[2*si+2]; } public static long getSum(long segTree[], int qs, int qe, int ss, int se, int si) { if(qe < ss || se < qs) { return 0l; } if(qs <= ss && se <= qe) { return segTree[si]; } int mid = (ss+se)/2; return getSum(segTree, qs, qe, ss, mid, 2*si+1) + getSum(segTree, qs, qe, mid+1, se, 2*si+2); } // Sort an array public static void sort(int arr[]) { ArrayList<Integer> al = new ArrayList<>(); for(int i: arr) { al.add(i); } Collections.sort(al); for(int i = 0; i < arr.length; i++) { arr[i] = al.get(i); } } // Find all divisors of n public static void allDivisors(int n) { for(int i = 1; i*i <= n; i++) { if(n%i == 0) { System.out.println(i + " "); if(i != n/i) { System.out.println(n/i + " "); } } } } // Check if n is prime or not public static boolean isPrime(int n) { if(n < 1) return false; if(n == 2 || n == 3) return true; if(n % 2 == 0 || n % 3 == 0) return false; for(int i = 5; i*i <= n; i += 6) { if(n % i == 0 || n % (i+2) == 0) { return false; } } return true; } // Find gcd of a and b public static long gcd(long a, long b) { long dividend = a > b ? a : b; long divisor = a < b ? a : b; while(divisor > 0) { long reminder = dividend % divisor; dividend = divisor; divisor = reminder; } return dividend; } // Find lcm of a and b public static long lcm(long a, long b) { long lcm = gcd(a, b); long hcf = (a * b) / lcm; return hcf; } // Find factorial in O(n) time public static long fact(int n) { long res = 1; for(int i = 2; i <= n; i++) { res = res * i; } return res; } // Find power in O(logb) time public static long power(long a, long b) { long res = 1; while(b > 0) { if((b&1) == 1) { res = (res * a)%mod; } a = (a * a)%mod; b >>= 1; } return res; } // Find nCr public static long nCr(int n, int r) { if(r < 0 || r > n) { return 0; } long ans = fact(n) / (fact(r) * fact(n-r)); return ans; } // Find nPr public static long nPr(int n, int r) { if(r < 0 || r > n) { return 0; } long ans = fact(n) / fact(r); return ans; } // sort all characters of a string public static String sortString(String inputString) { char tempArray[] = inputString.toCharArray(); Arrays.sort(tempArray); return new String(tempArray); } // User defined class for fast I/O 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(); } boolean hasNext() { if (st != null && st.hasMoreTokens()) { return true; } String tmp; try { br.mark(1000); tmp = br.readLine(); if (tmp == null) { return false; } br.reset(); } catch (IOException e) { return false; } return true; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } boolean nextBoolean() { return Boolean.parseBoolean(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } long[] nextArray(int n) { long[] a = new long[n]; for(int i=0; i<n; i++) { a[i] = nextInt(); } return a; } } } /** Dec Char Dec Char Dec Char Dec Char --------- --------- --------- ---------- 0 NUL (null) 32 SPACE 64 @ 96 ` 1 SOH (start of heading) 33 ! 65 A 97 a 2 STX (start of text) 34 " 66 B 98 b 3 ETX (end of text) 35 # 67 C 99 c 4 EOT (end of transmission) 36 $ 68 D 100 d 5 ENQ (enquiry) 37 % 69 E 101 e 6 ACK (acknowledge) 38 & 70 F 102 f 7 BEL (bell) 39 ' 71 G 103 g 8 BS (backspace) 40 ( 72 H 104 h 9 TAB (horizontal tab) 41 ) 73 I 105 i 10 LF (NL line feed, new line) 42 * 74 J 106 j 11 VT (vertical tab) 43 + 75 K 107 k 12 FF (NP form feed, new page) 44 , 76 L 108 l 13 CR (carriage return) 45 - 77 M 109 m 14 SO (shift out) 46 . 78 N 110 n 15 SI (shift in) 47 / 79 O 111 o 16 DLE (data link escape) 48 0 80 P 112 p 17 DC1 (device control 1) 49 1 81 Q 113 q 18 DC2 (device control 2) 50 2 82 R 114 r 19 DC3 (device control 3) 51 3 83 S 115 s 20 DC4 (device control 4) 52 4 84 T 116 t 21 NAK (negative acknowledge) 53 5 85 U 117 u 22 SYN (synchronous idle) 54 6 86 V 118 v 23 ETB (end of trans. block) 55 7 87 W 119 w 24 CAN (cancel) 56 8 88 X 120 x 25 EM (end of medium) 57 9 89 Y 121 y 26 SUB (substitute) 58 : 90 Z 122 z 27 ESC (escape) 59 ; 91 [ 123 { 28 FS (file separator) 60 < 92 \ 124 | 29 GS (group separator) 61 = 93 ] 125 } 30 RS (record separator) 62 > 94 ^ 126 ~ 31 US (unit separator) 63 ? 95 _ 127 DEL */ // (a/b)%mod == (a * moduloInverse(b)) % mod; // moduloInverse(b) = power(b, mod-2);
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
e41fd8aa332032c0a7161d098a7a8de7
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class New { public static void main (String[] args) throws java.lang.Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int testCases = Integer.parseInt(br.readLine()); while(testCases-->0){ String input[]=br.readLine().split(" "); int n=Integer.parseInt(input[0]); long a=Integer.parseInt(input[1]); long b=Integer.parseInt(input[2]); input=br.readLine().split(" "); long arr[]=new long[n+1]; for(int i=0;i<n;i++) { arr[i+1]=Long.parseLong(input[i]); } long left[]=new long[n+1]; for(int i=1;i<=n;i++) { left[i]=left[i-1]+(arr[i]-arr[i-1]); } long right[]=new long[n+2]; for(int i=n;i>=0;i--) { right[i]=right[i+1]+arr[i]; } long total=Long.MAX_VALUE; for(int i=0;i<n;i++) { long ans=(a+b)*left[i]+b*(right[i+1]-(n-i)*arr[i]); total=Math.min(total, ans); } out.println(total); } out.close(); } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
0cc5b0697f3408a877253083033727c4
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { FastReader sc = new FastReader(); int t = sc.nextInt(); for(int w = 0; w < t; w++){ int n = sc.nextInt(); long a = sc.nextInt(); long b = sc.nextInt(); long[] x = sc.intArray(n); if(b > a){ System.out.println((b+a)*x[n] - (x[n]-x[n-1])*a); }else{ long sum = Arrays.stream(x).sum()*b; for(int i = 1; i < n + 1; i++){ long dif = x[i]-x[i-1]; long l = sum + a*(dif) - (n-i)*dif*b; if(l < sum){ sum = l; }else{ break; } } System.out.println(sum); } } } private static int max(int a, int b){ return Math.max(a, b); } private static int min(int a, int b){ return Math.min(a, b); } 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 { if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } long[] intArray(int n){ long[] arr = new long[n+1]; for(int i =0; i < n; i ++){ arr[i+1] = nextInt(); } return arr; } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
522fa5e4632900ad72c357a25836e339
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class LineEmpire { public static void main(String args[]) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine().trim()); StringBuilder sb = new StringBuilder(); while (t-- > 0) { int NAB[] = nextIntArray(3, br); int arr[] = nextIntArray(NAB[0], br); sb.append(solveOptimized(arr, NAB[1], NAB[2])).append("\n"); } br.close(); System.out.println(sb); } private static long solve(int arr[], int A, int B) { long dp[] = new long[arr.length]; int capital[] = new int[arr.length]; Arrays.fill(dp, Long.MAX_VALUE); dp[0] = 0; capital[0] = 0; for (int i = 0; i < arr.length; i++) { if (i != 0) { capital[i] = capital[i - 1]; dp[i] = dp[i - 1]; } // When the cost of relocation is less than the cost of travel then relocate // cost of Relocation/Travel is dependent upon all the upcoming kingdoms long captureCost = getCaptureAndRelocationCost(arr[i], capital[i], A); long travelCost = getCaptureAndRelocationCost(arr[i], capital[i], B); int kingdomsLeft = arr.length - 1 - i; long cumulativeTravelCost = travelCost * kingdomsLeft; if (cumulativeTravelCost > captureCost) { capital[i] = arr[i]; dp[i] += captureCost; } dp[i] += travelCost; } // For Decisions Made // System.out.println(Arrays.toString(capital)); // System.out.println(Arrays.toString(dp)); return dp[dp.length - 1]; } private static long solveOptimized(int arr[], int A, int B) { // This is the memory optimized version of solve method long dp = 0; int capital = 0; // Arrays.fill(dp, Long.MAX_VALUE); // dp[0] = 0; // capital[0] = 0; for (int i = 0; i < arr.length; i++) { // When the cost of relocation is less than the cost of travel then relocate // cost of Relocation/Travel is dependent upon all the upcoming kingdoms long captureCost = getCaptureAndRelocationCost(arr[i], capital, A); long travelCost = getCaptureAndRelocationCost(arr[i], capital, B); int kingdomsLeft = arr.length - 1 - i; long cumulativeTravelCost = travelCost * kingdomsLeft; if (cumulativeTravelCost > captureCost) { capital = arr[i]; dp += captureCost; } dp += travelCost; } // For Decisions Made // System.out.println(Arrays.toString(capital)); // System.out.println(Arrays.toString(dp)); return dp; } private static long getCaptureAndRelocationCost(int cur, int capital, int A) { return 1l * (cur - capital) * A; } private static int[] nextIntArray(int N, BufferedReader br) throws Exception { StringTokenizer st = new StringTokenizer(br.readLine().trim(), " "); int arr[] = new int[N]; for (int i = 0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
40ad93831570f66e4463c2046b2987f3
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class LineEmpire { public static void main(String args[]) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine().trim()); StringBuilder sb = new StringBuilder(); while (t-- > 0) { int NAB[] = nextIntArray(3, br); int arr[] = nextIntArray(NAB[0], br); sb.append(solve(arr, NAB[1], NAB[2])).append("\n"); } br.close(); System.out.println(sb); } private static long solve(int arr[], int A, int B) { long dp[] = new long[arr.length]; int capital[] = new int[arr.length]; Arrays.fill(dp, Long.MAX_VALUE); dp[0] = 0; capital[0] = 0; for (int i = 0; i < arr.length; i++) { if (i != 0) { capital[i] = capital[i - 1]; dp[i] = dp[i - 1]; } // When the cost of relocation is less than the cost of travel then relocate // cost of Relocation/Travel is dependent upon all the upcoming kingdoms long captureCost = getCaptureAndRelocationCost(arr[i], capital[i], A); long travelCost = getCaptureAndRelocationCost(arr[i], capital[i], B); int kingdomsLeft = arr.length - 1 - i; long cumulativeTravelCost = travelCost * kingdomsLeft; if (cumulativeTravelCost > captureCost) { capital[i] = arr[i]; dp[i] += captureCost; } dp[i] += travelCost; } // System.out.println(Arrays.toString(capital)); // System.out.println(Arrays.toString(dp)); return dp[dp.length - 1]; } private static long getCaptureAndRelocationCost(int cur, int capital, int A) { return 1l * (cur - capital) * A; } private static int[] nextIntArray(int N, BufferedReader br) throws Exception { StringTokenizer st = new StringTokenizer(br.readLine().trim(), " "); int arr[] = new int[N]; for (int i = 0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
10b981acdf2e95dc268916a066b95612
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.util.*; public class a{ public static void main(String ...fdsa){ // Scanner sc=new Scanner(System.in); // int t=sc.nextInt(); // while(t-->0){ // int n=sc.nextInt(); // for(int i=0;i<=15;i++){ // if((1<<i)>=n){ // System.out.println((1<<i)-n); // break; // } // } // } // String s1=sc.nextLine(); // String s2=sc.nextLine(); // int i=0,j=0,m=s1.length(),n=s2.length(); // char []a1=s1.toCharArray(); // while(j<n){ // char ch=s2.charAt(j); // if(ch=='R'){ // if(i!=m-1) // i++; // } // else if(ch=='L'){ // if(i!=0) // i--; // } // else if(ch=='T') // { // if((a1[i]-'0')!=9) // a1[i]++; // } // else if(ch=='D'){ // if((a1[i]-'0')!=0) // a1[i]--; // } // else if(ch=='S'){ // int x=i; // String mp="";j++; // while(j<n && s2.charAt(j)-'0'>=0 && s2.charAt(j)-'0'<=9){ // mp+=s2.charAt(j); // j++; // } // int y=Integer.valueOf(mp)-1; // // System.out.println(x+" "+y); // char chr=a1[x]; // a1[x]=a1[y]; // a1[y]=chr; // j--; // } // // System.out.println(Arrays.toString(a1)+" "+i+" "+j); // j++; // } // System.out.println(new String(a1)); Scanner sc=new Scanner(System.in); int t=sc.nextInt(); // while(t-->0){ // int a=sc.nextInt();int b=sc.nextInt();int c=sc.nextInt(); // int temp=0; // while(a>0){ // temp=(int)Math.ceil(b/(c+1)); // while(b>0 && temp-->0){ // System.out.print("R");b--;a--; // } // if(c>0) // System.out.print("B"); // c-=1; // a-=1; // } // System.out.println(); // } while(t-->0){ int n=sc.nextInt(); int a=sc.nextInt(); int b=sc.nextInt(); long []arr =new long[n]; for(int i=0;i<n;i++) arr[i]=sc.nextLong(); long ans=0,prev=0; int i=1; ans+=(arr[0])*b; while(i<n){ if((arr[i-1]-prev)*(n-i)*b>=a*(arr[i-1]-prev)){ ans+=a*(arr[i-1]-prev); // System.out.println(a*(arr[i-1]-prev)); prev=arr[i-1]; } ans+=(arr[i]-prev)*b; // System.out.println(ans+" "+i+" "+prev); i++; } System.out.println(ans); } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
f88e14ce7597bd0483fe2b34d6f484e9
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
// package div_2_782; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class C{ public static void main(String[] args){ FastReader sc = new FastReader(); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); long x=sc.nextLong(); long y=sc.nextLong(); long a[]=sc.fastArray(n); long pre[]=new long [n]; pre[0]=a[0]; for(int i=1;i<n;i++)pre[i]+=pre[i-1]+a[i]; long cap=0; long ans=0;long res=(pre[n-1]*y); // System.out.println(res+" re"); for(int i=0;i<n;i++) { if(i!=0)res=Math.min(res, ans+((pre[n-1]-pre[i-1])*y)-((n-i)*a[i-1]*y)); ans+=(a[i]-cap)*y; if(i==n-1)break; ans+=(a[i]-cap)*x; cap=a[i]; } // System.out.println(ans); System.out.println(Math.min(res,ans)); } } static int pow(int a,int b) { if(b==0)return 1; if(b==1)return a; return a*pow(a,b-1); } static class pair { int x;int y; pair(int x,int y){ this.x=x; this.y=y; } } static ArrayList<Integer> primeFac(int n){ ArrayList<Integer>ans = new ArrayList<Integer>(); int lp[]=new int [n+1]; Arrays.fill(lp, 0); //0-prime for(int i=2;i<=n;i++) { if(lp[i]==0) { for(int j=i;j<=n;j+=i) { if(lp[j]==0) lp[j]=i; } } } int fac=n; while(fac>1) { ans.add(lp[fac]); fac=fac/lp[fac]; } print(ans); return ans; } static ArrayList<Long> prime_in_given_range(long l,long r){ ArrayList<Long> ans= new ArrayList<>(); int n=(int)Math.sqrt(r)+1; int prime[]=sieve_of_Eratosthenes(n); long res[]=new long [(int)(r-l)+1]; for(int i=0;i<=r-l;i++) { res[i]=i+l; } for(int i=0;i<prime.length;i++) { if(prime[i]==1) { System.out.println(2); for(int j=Math.max((int)i*i, (int)(l+i-1)/i*i);j<=r;j+=i) { res[j-(int)l]=0; } } } for(long i:res) if(i!=0)ans.add(i); return ans; } static int [] sieve_of_Eratosthenes(int n) { int prime[]=new int [n]; Arrays.fill(prime, 1); // 1-prime | 0-not prime prime[0]=prime[1]=0; for(int i=2;i<n;i++) { if(prime[i]==1) { for(int j=i*i;j<n;j+=i) { prime[j]=0; } } } return prime; } static long binpow(long a,long b) { long res=1; if(b==0)return 1; if(a==0)return 0; while(b>0) { if((b&1)==1) { res*=a; } a*=a; b>>=1; } return res; } static void print(int a[]) { System.out.println(a.length); for(int i:a) { System.out.print(i+" "); } System.out.println(); } static void print(long a[]) { System.out.println(a.length); for(long i:a) { System.out.print(i+" "); } System.out.println(); } static long rolling_hashcode(String s ,int st,int end,long Hashcode,int n) { if(end>=s.length()) return -1; int mod=1000000007; Hashcode=Hashcode-(s.charAt(st-1)*(long)Math.pow(27,n-1)); Hashcode*=10; Hashcode=(Hashcode+(long)s.charAt(end))%mod; return Hashcode; } static long hashcode(String s,int n) { long code=0; for(int i=0;i<n;i++) { code+=((long)s.charAt(i)*(long)Math.pow(27, n-i-1)%1000000007); } return code; } static void print(ArrayList<Integer> a) { System.out.println(a.size()); for(long i:a) { System.out.print(i+" "); } System.out.println(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } long [] fastArray(int n) { long a[]=new long [n]; for(int i=0;i<n;i++) { a[i]=nextLong(); } return a; } 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
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
0295d1c8a34410c1fc6c65bb64a2f1de
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; import java.io.*; public class Main { // Graph // prefix sums //inputs public static void main(String args[])throws Exception{ Input sc=new Input(); precalculates p=new precalculates(); StringBuilder sb=new StringBuilder(); int t=sc.readInt(); for(int f=0;f<t;f++){ int d[]=sc.readArray(); int n=d[0]; long x=d[1],y=d[2]; long a[]=sc.readArrayLong(); long dp[]=new long[n]; for(int i=0;i<n;i++){ if(i==0){ dp[i]=a[i]*y; }else dp[i]=dp[i-1]+a[i]*y; } long val=0; long ans=Long.MAX_VALUE; for(int i=0;i<n;i++){ if(i==0){ val+=(x*(a[i]))+(y*(a[i])); ans=Math.min(ans,val+(dp[n-1]-dp[i])-(y*(n-1-i)*a[i])); //System.out.println(x+" "+y+" "+a[i]); //System.out.println(val); // System.out.println(val+(dp[n-1]-dp[i])-(y*(n-1-i)*a[i])); }else{ val+=x*(a[i]-a[i-1])+y*(a[i]-a[i-1]); ans=Math.min(ans,val+(dp[n-1]-dp[i])-(y*(n-i-1)*a[i])); //System.out.println(val+(dp[n-1]-dp[i])-(y*(n-i-1)*a[i])); } } sb.append(Math.min(dp[n-1],ans)+"\n"); } System.out.print(sb); } } class Input{ BufferedReader br; StringTokenizer st; Input(){ br=new BufferedReader(new InputStreamReader(System.in)); st=new StringTokenizer(""); } public int[] readArray() throws Exception{ st=new StringTokenizer(br.readLine()); int a[]=new int[st.countTokens()]; for(int i=0;i<a.length;i++){ a[i]=Integer.parseInt(st.nextToken()); } return a; } public long[] readArrayLong() throws Exception{ st=new StringTokenizer(br.readLine()); long a[]=new long[st.countTokens()]; for(int i=0;i<a.length;i++){ a[i]=Long.parseLong(st.nextToken()); } return a; } public int readInt() throws Exception{ st=new StringTokenizer(br.readLine()); return Integer.parseInt(st.nextToken()); } public long readLong() throws Exception{ st=new StringTokenizer(br.readLine()); return Long.parseLong(st.nextToken()); } public String readString() throws Exception{ return br.readLine(); } public int[][] read2dArray(int n,int m)throws Exception{ int a[][]=new int[n][m]; for(int i=0;i<n;i++){ st=new StringTokenizer(br.readLine()); for(int j=0;j<m;j++){ a[i][j]=Integer.parseInt(st.nextToken()); } } return a; } } class precalculates{ public long gcd(long p, long q) { if (q == 0) return p; else return gcd(q, p % q); } public int[] prefixSumOneDimentional(int a[]){ int n=a.length; int dp[]=new int[n]; for(int i=0;i<n;i++){ if(i==0) dp[i]=a[i]; else dp[i]=dp[i-1]+a[i]; } return dp; } public int[] postSumOneDimentional(int a[]) { int n = a.length; int dp[] = new int[n]; for (int i = n - 1; i >= 0; i--) { if (i == n - 1) dp[i] = a[i]; else dp[i] = dp[i + 1] + a[i]; } return dp; } public int[][] prefixSum2d(int a[][]){ int n=a.length;int m=a[0].length; int dp[][]=new int[n+1][m+1]; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ dp[i][j]=a[i-1][j-1]+dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]; } } return dp; } public long pow(long a,long b){ long mod=998244353; long ans=1; if(b<=0) return 1; if(b%2==0){ ans=pow(a,b/2)%mod; return ((ans%mod)*(ans%mod))%mod; }else{ ans=pow(a,b-1)%mod; return ((a%mod)*(ans%mod))%mod; } } } class GraphInteger{ HashMap<Integer,vertex> vtces; class vertex{ HashMap<Integer,Integer> children; public vertex(){ children=new HashMap<>(); } } public GraphInteger(){ vtces=new HashMap<>(); } public void addVertex(int a){ vtces.put(a,new vertex()); } public void addEdge(int a,int b,int cost){ if(!vtces.containsKey(a)){ vtces.put(a,new vertex()); } if(!vtces.containsKey(b)){ vtces.put(b,new vertex()); } vtces.get(a).children.put(b,cost); // vtces.get(b).children.put(a,cost); } public boolean isCyclicDirected(){ boolean isdone[]=new boolean[vtces.size()+1]; boolean check[]=new boolean[vtces.size()+1]; for(int i=1;i<=vtces.size();i++) { if (!isdone[i] && isCyclicDirected(i,isdone, check)) { return true; } } return false; } private boolean isCyclicDirected(int i,boolean isdone[],boolean check[]){ if(check[i]) return true; if(isdone[i]) return false; check[i]=true; isdone[i]=true; Set<Integer> set=vtces.get(i).children.keySet(); for(Integer ii:set){ if(isCyclicDirected(ii,isdone,check)) return true; } check[i]=false; return false; } } class union_find { int n; int[] sz; int[] par; union_find(int nval) { n = nval; sz = new int[n + 1]; par = new int[n + 1]; for (int i = 0; i <= n; i++) { par[i] = i; sz[i] = 1; } } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } boolean find(int a, int b) { return root(a) == root(b); } int union(int a, int b) { int ra = root(a); int rb = root(b); if (ra == rb) return 0; if(a==b) return 0; if (sz[a] > sz[b]) { int temp = ra; ra = rb; rb = temp; } par[ra] = rb; sz[rb] += sz[ra]; return 1; } } /* static int mod=998244353; private static int add(int x, int y) { x += y; return x % MOD; } private static int mul(int x, int y) { int res = (int) (((long) x * y) % MOD); return res; } private static int binpow(int x, int y) { int z = 1; while (y > 0) { if (y % 2 != 0) z = mul(z, x); x = mul(x, x); y >>= 1; } return z; } private static int inv(int x) { return binpow(x, MOD - 2); } private static int devide(int x, int y) { return mul(x, inv(y)); } private static int C(int n, int k, int[] fact) { return devide(fact[n], mul(fact[k], fact[n-k])); } */
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
c9f2299883082817686a2ee04fa46a9c
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.util.*; import java.util.Map.Entry; import java.math.*; import java.sql.Array; public class Simple{ public static class Pair implements Comparable<Pair>{ int x; int y; public Pair(int x,int y){ this.x = x; this.y = y; } public int compareTo(Pair other){ return (int) (this.y - other.y); } public boolean equals(Pair other){ if(this.x == other.x && this.y == other.y)return true; return false; } public int hashCode(){ return 31*x + y; } // @Override // public int compareTo(Simple.Pair o) { // // TODO Auto-generated method stub // return 0; // } } static int power(int x, int y, int p) { // Initialize result int res = 1; // Update x if it is more than or // equal to p x = x % p; while (y > 0) { // If y is odd, multiply x // with result if (y % 2 == 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // Returns n^(-1) mod p static int modInverse(int n, int p) { return power(n, p - 2, p); } // Returns nCr % p using Fermat's // little theorem. static int nCrModPFermat(int n, int r, int p) { if (n<r) return 0; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r int[] fac = new int[n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } static int nCrModp(int n, int r, int p) { if (r > n - r) r = n - r; // The array C is going to store last // row of pascal triangle at the end. // And last entry of last row is nCr int C[] = new int[r + 1]; 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 int gcd(int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcd(a-b, b); return gcd(a, b-a); } public static class Node{ int root; ArrayList<Node> al; Node par; public Node(int root,ArrayList<Node> al,Node par){ this.root = root; this.al = al; this.par = par; } } // public static int helper(int arr[],int n ,int i,int j,int dp[][]){ // if(i==0 || j==0)return 0; // if(dp[i][j]!=-1){ // return dp[i][j]; // } // if(helper(arr, n, i-1, j-1, dp)>=0){ // return dp[i][j]=Math.max(helper(arr, n, i-1, j-1,dp)+arr[i-1], helper(arr, n, i-1, j,dp)); // } // return dp[i][j] = helper(arr, n, i-1, j,dp); // } // public static void dfs(ArrayList<ArrayList<Integer>> al,int levelcount[],int node,int count){ // levelcount[count]++; // for(Integer x : al.get(node)){ // dfs(al, levelcount, x, count+1); // } // } public static long __gcd(long a, long b) { if (b == 0) return a; return __gcd(b, a % b); } public static long helper(int arr1[][],int m){ Arrays.sort(arr1, (int a[],int b[])-> a[0]-b[0]); long ans =0; for(int i=1;i<m;i++){ long count =0; for(int j=0;j<i;j++){ if(arr1[i][1] > arr1[j][1])count++; } ans+=count; } return ans; } public static class DSU{ int n; int par[]; int rank[]; public DSU(int n){ this.n = n; par = new int[n+1]; rank = new int[n+1]; for(int i=1;i<=n;i++){ par[i] = i ; rank[i] = 0; } } public int findPar(int node){ if(node==par[node]){ return node; } return par[node] = findPar(par[node]);//path compression } public void union(int u,int v){ u = findPar(u); v = findPar(v); if(rank[u]<rank[v]){ par[u] = v; } else if(rank[u]>rank[v]){ par[v] = u; } else{ par[v] = u; rank[u]++; } } } public static void dfs(ArrayList<ArrayList<Integer>> adj, int l[],int r[],boolean vis[],int node,long dp[][]){ long ansl = 0; long ansr = 0; vis[node] = true; for(Integer x : adj.get(node)){ if(!vis[x]){ vis[x] = true; dfs(adj, l, r, vis, x, dp); ansl+=Math.max(Math.abs(l[node]-l[x])+dp[x][0], Math.abs(l[node]-r[x])+dp[x][1]); ansr+=Math.max(Math.abs(r[node]-l[x])+dp[x][0], Math.abs(r[node]-r[x])+dp[x][1]); } } dp[node][0] = ansl; dp[node][1] = ansr; } public static void main(String args[]){ Scanner s = new Scanner(System.in); int t = s.nextInt(); for(int t1 = 1;t1<=t;t1++){ int n = s.nextInt(); long a = s.nextInt(); long b = s.nextInt(); long arr[] = new long[n+1]; for(int i=1;i<=n;i++){ arr[i] = s.nextInt(); } long dp[] = new long[n+2]; for(int i=n;i>=1;i--){ dp[i] = arr[i] + dp[i+1]; } long ans = Long.MAX_VALUE; for(int i=0;i<n;i++){ long temp = (a+b)*arr[i] + b*(dp[i+1] - (n-i)*arr[i]); ans= Math.min(temp, ans); } System.out.println(ans); } } } /* 4 2 2 7 0 2 5 -2 3 5 0*x1 + 1*x2 + 2*x3 + 3*x4 */
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
9bc40974bd39dee011d7a9b907e46dcf
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.util.*; import java.util.Map.Entry; import java.math.*; import java.sql.Array; public class Simple{ public static class Pair implements Comparable<Pair>{ int x; int y; public Pair(int x,int y){ this.x = x; this.y = y; } public int compareTo(Pair other){ return (int) (this.y - other.y); } public boolean equals(Pair other){ if(this.x == other.x && this.y == other.y)return true; return false; } public int hashCode(){ return 31*x + y; } // @Override // public int compareTo(Simple.Pair o) { // // TODO Auto-generated method stub // return 0; // } } static int power(int x, int y, int p) { // Initialize result int res = 1; // Update x if it is more than or // equal to p x = x % p; while (y > 0) { // If y is odd, multiply x // with result if (y % 2 == 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // Returns n^(-1) mod p static int modInverse(int n, int p) { return power(n, p - 2, p); } // Returns nCr % p using Fermat's // little theorem. static int nCrModPFermat(int n, int r, int p) { if (n<r) return 0; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r int[] fac = new int[n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } static int nCrModp(int n, int r, int p) { if (r > n - r) r = n - r; // The array C is going to store last // row of pascal triangle at the end. // And last entry of last row is nCr int C[] = new int[r + 1]; 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 int gcd(int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcd(a-b, b); return gcd(a, b-a); } public static class Node{ int root; ArrayList<Node> al; Node par; public Node(int root,ArrayList<Node> al,Node par){ this.root = root; this.al = al; this.par = par; } } // public static int helper(int arr[],int n ,int i,int j,int dp[][]){ // if(i==0 || j==0)return 0; // if(dp[i][j]!=-1){ // return dp[i][j]; // } // if(helper(arr, n, i-1, j-1, dp)>=0){ // return dp[i][j]=Math.max(helper(arr, n, i-1, j-1,dp)+arr[i-1], helper(arr, n, i-1, j,dp)); // } // return dp[i][j] = helper(arr, n, i-1, j,dp); // } // public static void dfs(ArrayList<ArrayList<Integer>> al,int levelcount[],int node,int count){ // levelcount[count]++; // for(Integer x : al.get(node)){ // dfs(al, levelcount, x, count+1); // } // } public static long __gcd(long a, long b) { if (b == 0) return a; return __gcd(b, a % b); } public static long helper(int arr1[][],int m){ Arrays.sort(arr1, (int a[],int b[])-> a[0]-b[0]); long ans =0; for(int i=1;i<m;i++){ long count =0; for(int j=0;j<i;j++){ if(arr1[i][1] > arr1[j][1])count++; } ans+=count; } return ans; } public static class DSU{ int n; int par[]; int rank[]; public DSU(int n){ this.n = n; par = new int[n+1]; rank = new int[n+1]; for(int i=1;i<=n;i++){ par[i] = i ; rank[i] = 0; } } public int findPar(int node){ if(node==par[node]){ return node; } return par[node] = findPar(par[node]);//path compression } public void union(int u,int v){ u = findPar(u); v = findPar(v); if(rank[u]<rank[v]){ par[u] = v; } else if(rank[u]>rank[v]){ par[v] = u; } else{ par[v] = u; rank[u]++; } } } public static void dfs(ArrayList<ArrayList<Integer>> adj, int l[],int r[],boolean vis[],int node,long dp[][]){ long ansl = 0; long ansr = 0; vis[node] = true; for(Integer x : adj.get(node)){ if(!vis[x]){ vis[x] = true; dfs(adj, l, r, vis, x, dp); ansl+=Math.max(Math.abs(l[node]-l[x])+dp[x][0], Math.abs(l[node]-r[x])+dp[x][1]); ansr+=Math.max(Math.abs(r[node]-l[x])+dp[x][0], Math.abs(r[node]-r[x])+dp[x][1]); } } dp[node][0] = ansl; dp[node][1] = ansr; } public static void main(String args[]){ Scanner s = new Scanner(System.in); int t = s.nextInt(); for(int t1 = 1;t1<=t;t1++){ int n = s.nextInt(); long a = s.nextInt(); long b = s.nextInt(); long arr[] = new long[n+1]; for(int i=1;i<=n;i++){ arr[i] = s.nextInt(); } long dp[] = new long[n+2]; for(int i=n;i>=1;i--){ dp[i] = arr[i] + dp[i+1]; } long ans = Long.MAX_VALUE; for(int i=0;i<n;i++){ long temp = (a+b)*arr[i] + b*(dp[i+1] - (n-i)*arr[i]); ans= Math.min(temp, ans); } System.out.println(ans); } } } /* 4 2 2 7 0 2 5 -2 3 5 0*x1 + 1*x2 + 2*x3 + 3*x4 */
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
b19ad94e7481e4b9952fac0b3d7a1e25
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
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(); public static void main (String[] args) throws java.lang.Exception { int t = sc.nextInt(); while(t-->0){ solve(sc); } } public static void solve(FastReader sc) throws IOException{ int n = i();long cp = l();long con = l(); long [] arr = new long[n]; for(int i = 0;i<n;++i){ arr[i] = sc.nextLong(); } long ans = 0;long cap = 0; if(n==1){ out.println(con*arr[0]);out.flush();return; } ans=arr[0]*con; for(int i = 1;i<n;++i){ if(cp<=con*(n-i)){ ans+=(cp*(arr[i-1] - cap)); cap = arr[i-1]; ans+=con*(arr[i] - cap); }else{ ans+=con*(arr[i] - cap); } } out.println(ans); out.flush(); } /* int [] arr = new int[n]; for(int i = 0;i<n;++i){ arr[i] = sc.nextInt(); } */ 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 class Pair implements Comparable<Pair>{ int ind;int val; Pair(int ind, int val){ this.ind=ind; this.val=val; } public int compareTo(Pair o){ return this.val-o.val; } } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } 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
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
3ba7607bc69406074e069027524e4b97
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.text.DecimalFormat; import java.util.Arrays; import java.util.*; import java.util.Scanner; import java.util.StringTokenizer; public class copy { static int log=18; static int[][] ancestor; static int[] depth; static void sieveOfEratosthenes(int n, ArrayList<Integer> arr) { 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] is not changed, then it is a // prime if (prime[p]) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } // Print all prime numbers for (int i = 2; i <= n; i++) { if (prime[i]) { arr.add(i); } } } public static long fac(long N, long mod) { if (N == 0) return 1; if(N==1) return 1; return ((N % mod) * (fac(N - 1, mod) % mod)) % mod; } static long power(long x, long y, long p) { // Initialize result long res = 1; // Update x if it is more than or // equal to p x = x % p; while (y > 0) { // If y is odd, multiply x // with result if (y % 2 == 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // Returns n^(-1) mod p static long modInverse(long n, long p) { return power(n, p - 2, p); } // Returns nCr % p using Fermat's // little theorem. static long nCrModPFermat(long n, long r, long p) { if (n < r) return 0; // Base case if (r == 0) return 1; return ((fac(n, p) % p * (modInverse(fac(r, p), p) % p)) % p * (modInverse(fac(n - r, p), p) % p)) % p; } public static int find(int[] parent, int x) { if (parent[x] == x) return x; return find(parent, parent[x]); } public static void merge(int[] parent, int[] rank, int x, int y,int[] child) { int x1 = find(parent, x); int y1 = find(parent, y); if (rank[x1] > rank[y1]) { parent[y1] = x1; child[x1]+=child[y1]; } else if (rank[y1] > rank[x1]) { parent[x1] = y1; child[y1]+=child[x1]; } else { parent[y1] = x1; child[x1]+=child[y1]; rank[x1]++; } } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static int power(int x, int y, int p) { 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; } // Returns n^(-1) mod p static int modInverse(int n, int p) { return power(n, p - 2, p); } // Returns nCr % p using Fermat's // little theorem. static int nCrModPFermat(int n, int r, int p) { if (n<r) return 0; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r int[] fac = new int[n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } public static long[][] ncr(int n,int r) { long[][] dp=new long[n+1][r+1]; for(int i=0;i<=n;i++) dp[i][0]=1; for(int i=1;i<=n;i++) { for(int j=1;j<=r;j++) { if(j>i) continue; dp[i][j]=dp[i-1][j-1]+dp[i-1][j]; } } return dp; } public static boolean prime(long N) { int c=0; for(int i=2;i*i<=N;i++) { if(N%i==0) ++c; } return c==0; } public static int sparse_ancestor_table(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[] child) { int c=0; for(int i:arr.get(x)) { if(i!=parent) { // System.out.println(i+" hello "+x); depth[i]=depth[x]+1; ancestor[i][0]=x; // if(i==2) // System.out.println(parent+" hello"); for(int j=1;j<log;j++) ancestor[i][j]=ancestor[ancestor[i][j-1]][j-1]; c+=sparse_ancestor_table(arr,i,x,child); } } child[x]=1+c; return child[x]; } public static int lca(int x,int y) { if(depth[x]<depth[y]) { int temp=x; x=y; y=temp; } x=get_kth_ancestor(depth[x]-depth[y],x); if(x==y) return x; // System.out.println(x+" "+y); for(int i=log-1;i>=0;i--) { if(ancestor[x][i]!=ancestor[y][i]) { x=ancestor[x][i]; y=ancestor[y][i]; } } return ancestor[x][0]; } public static int get_kth_ancestor(int K,int x) { if(K==0) return x; int node=x; for(int i=0;i<log;i++) { if(K%2!=0) { node=ancestor[node][i]; } K/=2; } return node; } public static ArrayList<Integer> primeFactors(int n) { // Print the number of 2s that divide n ArrayList<Integer> factors=new ArrayList<>(); if(n%2==0) factors.add(2); while (n%2==0) { n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i+= 2) { // While i divides n, print i and divide n if(n%i==0) factors.add(i); while (n%i == 0) { n /= i; } } // This condition is to handle the case when // n is a prime number greater than 2 if (n > 2) { factors.add(n); } return factors; } static long ans=1,mod=1000000007; public static void recur(long X,long N,int index,ArrayList<Integer> temp) { // System.out.println(X); if(index==temp.size()) { System.out.println(X); ans=((ans%mod)*(X%mod))%mod; return; } for(int i=0;i<=60;i++) { if(X*Math.pow(temp.get(index),i)<=N) recur(X*(long)Math.pow(temp.get(index),i),N,index+1,temp); else break; } } public static int upper(ArrayList<Integer> temp,int X) { int l=0,r=temp.size()-1; while(l<=r) { int mid=(l+r)/2; if(temp.get(mid)==X) return mid; // System.out.println(mid+" "+temp.get(mid)); if(temp.get(mid)<X) l=mid+1; else { if(mid-1>=0 && temp.get(mid-1)>=X) r=mid-1; else return mid; } } return -1; } public static int lower(ArrayList<Integer> temp,int X) { int l=0,r=temp.size()-1; while(l<=r) { int mid=(l+r)/2; if(temp.get(mid)==X) return mid; // System.out.println(mid+" "+temp.get(mid)); if(temp.get(mid)>X) r=mid-1; else { if(mid+1<temp.size() && temp.get(mid+1)<=X) l=mid+1; else return mid; } } return -1; } public static int[] check(String shelf,int[][] queries) { int[] arr=new int[queries.length]; ArrayList<Integer> indices=new ArrayList<>(); for(int i=0;i<shelf.length();i++) { char ch=shelf.charAt(i); if(ch=='|') indices.add(i); } for(int i=0;i<queries.length;i++) { int x=queries[i][0]-1; int y=queries[i][1]-1; int left=upper(indices,x); int right=lower(indices,y); if(left<=right && left!=-1 && right!=-1) { arr[i]=indices.get(right)-indices.get(left)+1-(right-left+1); } else arr[i]=0; } return arr; } static boolean check; public static void check(ArrayList<ArrayList<Integer>> arr,int x,int[] color,boolean[] visited) { visited[x]=true; PriorityQueue<Integer> pq=new PriorityQueue<>(); for(int i:arr.get(x)) { if(color[i]<color[x]) pq.add(color[i]); if(color[i]==color[x]) check=false; if(!visited[i]) check(arr,i,color,visited); } int start=1; while(pq.size()>0) { int temp=pq.poll(); if(temp==start) ++start; else break; } if(start!=color[x]) check=false; } static boolean cycle; public static void cycle(boolean[] stack,boolean[] visited,int x,ArrayList<ArrayList<Integer>> arr) { if(stack[x]) { cycle=true; return; } visited[x]=true; for(int i:arr.get(x)) { if(!visited[x]) { cycle(stack,visited,i,arr); } } stack[x]=false; } public static int check(char[][] ch,int x,int y) { int cnt=0; int c=0; int N=ch.length; int x1=x,y1=y; while(c<ch.length) { if(ch[x][y]=='1') ++cnt; // if(x1==0 && y1==3) // System.out.println(x+" "+y+" "+cnt); x=(x+1)%N; y=(y+1)%N; ++c; } return cnt; } public static void s(char[][] arr,int x) { char start=arr[arr.length-1][x]; for(int i=arr.length-1;i>0;i--) { arr[i][x]=arr[i-1][x]; } arr[0][x]=start; } public static void shuffle(char[][] arr,int x,int down) { int N= arr.length; down%=N; char[] store=new char[N-down]; for(int i=0;i<N-down;i++) store[i]=arr[i][x]; for(int i=0;i<arr.length;i++) { if(i<down) { // Printing rightmost // kth elements arr[i][x]=arr[N + i - down][x]; } else { // Prints array after // 'k' elements arr[i][x]=store[i-down]; } } } public static String form(int C1,char ch1,char ch2) { char ch=ch1; String s=""; for(int i=1;i<=C1;i++) { s+=ch; if(ch==ch1) ch=ch2; else ch=ch1; } return s; } public static void printArray(long[] arr) { for(int i=0;i<arr.length;i++) System.out.print(arr[i]+" "); System.out.println(); } public static boolean check(long mid,long[] arr,long K) { long[] arr1=Arrays.copyOfRange(arr,0,arr.length); long ans=0; for(int i=0;i<arr1.length-1;i++) { if(arr1[i]+arr1[i+1]>=mid) { long check=(arr1[i]+arr1[i+1])/mid; // if(mid==5) // System.out.println(check); long left=check*mid; left-=arr1[i]; if(left>=0) arr1[i+1]-=left; ans+=check; } // if(mid==5) // printArray(arr1); } // if(mid==5) // System.out.println(ans); ans+=arr1[arr1.length-1]/mid; return ans>=K; } public static long search(long sum,long[] arr,long K) { long l=1,r=sum/K; while(l<=r) { long mid=(l+r)/2; if(check(mid,arr,K)) { if(mid+1<=sum/K && check(mid+1,arr,K)) l=mid+1; else return mid; } else r=mid-1; } return -1; } public static void check(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[] dist) { for(int i:arr.get(x)) { if(i!=parent) { dist[i]=dist[x]+1; check(arr,i,x,dist); } } } public static void reconstruct(boolean[][] dp,int x,int y,int[] arr,ArrayList<Integer> ans) { if(y==0) return; if(arr[x]==0) return; if(y-arr[x]>=0 && dp[x-1][y-arr[x]]) { ans.add(arr[x]); reconstruct(dp,x-1,y-arr[x],arr,ans); } else { reconstruct(dp,x-1,y,arr,ans); } } public static long check(int[] arr,int height) { long even=0,odd=0; for(int i=0;i<arr.length;i++) { if(height%2==0) { if(arr[i]%2==0) { even+=(height-arr[i])/2; } else { odd+=1; even+=(height-arr[i])/2; } } else { if(arr[i]%2!=0) { even+=(height-arr[i])/2; } else { odd+=1; even+=(height-arr[i])/2; } } } long diff=Math.abs(even-odd); long odd1=odd,even1=even; // System.out.println(odd+" "+even); if(odd>even) { return even*2+(odd-even)*2-1; } else { long days=diff/3; odd+=days*2; even-=(days); long days1=days+1; odd1+=days1*2; even1-=(days1); // System.out.println(odd+" "+even+" "+odd1+" "+even1); // System.out.println(odd*2+(even-odd)*2+" "+(even1*2+(odd1-even1)*2-1)); return Math.min(odd*2+(even-odd)*2,even1*2+(odd1-even1)*2-1); } } public static void check(int x,ArrayList<ArrayList<Integer>> arr,int parent,int[] dist) { for(int i:arr.get(x)) { if(i!=parent) { dist[i]=dist[x]+1; check(i,arr,x,dist); } } } static int fin_t; public static void check(int[] dist1,int[] dist2,ArrayList<ArrayList<Integer>> arr,int x,int parent) { if(dist1[x]<=dist2[x]) return; fin_t=Math.max(fin_t, dist1[x]*2); for(int i:arr.get(x)) { if(i!=parent) { check(dist1,dist2,arr,i,x); } } } public static void main(String[] args) throws IOException { Reader.init(System.in); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); int T=Reader.nextInt(); for(int m=1;m<=T;m++) { int N=Reader.nextInt(); long cost=0; long a=Reader.nextLong(); long b=Reader.nextLong(); long[] pt=new long[N]; for(int i=0;i<N;i++) pt[i]=Reader.nextLong(); if(N==1) { output.write(b*pt[0]+"\n"); continue; } if(a>=b) { cost+=b*pt[0]; long cap=0; for(int i=0;i<N-1;i++) { if(a-b*(N-i-1)<=0) { cost+=a*(pt[i]-cap); cap=pt[i]; } cost+=(pt[i+1]-cap)*b; } } else { cost+=b*(pt[0])+a*(pt[0]); for(int i=1;i<N-1;i++) { cost+=(pt[i]-pt[i-1])*(b+a); } cost+=(pt[N-1]-pt[N-2])*b; } output.write(cost+"\n"); } output.flush(); } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } } class TreeNode { int data; TreeNode left; TreeNode right; TreeNode(int data) { left=null; right=null; this.data=data; } } class div { int start; int left; div(int start,int left) { this.start=start; this.left=left; } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
dc1e6570a01ff6a866ac5821c21d4838
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
/* "Everything in the universe is balanced. Every disappointment you face in life will be balanced by something good for you! Keep going, never give up." Just have Patience + 1... */ import java.util.*; import java.lang.*; import java.io.*; public class Solution { static long INF = (long) 1e18; public static void main(String[] args) throws java.lang.Exception { out = new PrintWriter(new BufferedOutputStream(System.out)); sc = new FastReader(); int test = sc.nextInt(); for (int t = 1; t <= test; t++) { solve(); } out.close(); } private static void solve() { int n = sc.nextInt() + 1; int a = sc.nextInt(); int b = sc.nextInt(); long[] arr = new long[n + 1]; long sum = 0; for (int i = 1; i < n; i++) { arr[i] = sc.nextLong(); sum += arr[i]; } long totalCost = INF; for (int i = 0; i < n; i++) { // for each position upto i, which we capture and move our capital to that position, and then we only capture remaining positions. long curr = (a + b) * arr[i]; // total cost to capture and move from [0, 1, 2....i] sum -= arr[i]; curr += (sum - (n - i - 1) * arr[i]) * b; // cost to capture remaining positions from i. totalCost = Math.min(totalCost, curr); } out.println(totalCost); } public static FastReader sc; public static PrintWriter out; static class FastReader { BufferedReader br; StringTokenizer str; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (str == null || !str.hasMoreElements()) { try { str = new StringTokenizer(br.readLine()); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } } return str.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 lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } return str; } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
499e70be065dc66336884dfcd5bd24ee
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
/* "Everything in the universe is balanced. Every disappointment you face in life will be balanced by something good for you! Keep going, never give up." Just have Patience + 1... */ import java.util.*; import java.lang.*; import java.io.*; public class Solution { static long INF = (long) 1e18; public static void main(String[] args) throws java.lang.Exception { out = new PrintWriter(new BufferedOutputStream(System.out)); sc = new FastReader(); int test = sc.nextInt(); for (int t = 1; t <= test; t++) { solve(); } out.close(); } private static void solve() { int n = sc.nextInt() + 1; int a = sc.nextInt(); int b = sc.nextInt(); long[] arr = new long[n + 1]; long sum = 0; for (int i = 1; i < n; i++) { arr[i] = sc.nextLong(); sum += arr[i]; } long totalCost = INF; for (int i = 0; i < n; i++) { long curr = (a + b) * arr[i]; sum -= arr[i]; curr += (sum - (n - i - 1) * arr[i]) * b; totalCost = Math.min(totalCost, curr); } out.println(totalCost); } public static FastReader sc; public static PrintWriter out; static class FastReader { BufferedReader br; StringTokenizer str; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (str == null || !str.hasMoreElements()) { try { str = new StringTokenizer(br.readLine()); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } } return str.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 lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } return str; } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
4df5fa366d0124a151528706eae38af7
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.util.*; import java.io.*; public class B { static FastScanner fs = new FastScanner(); static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(""); public static void main(String[] args) { int t = fs.nextInt(); for (int tt = 0; tt < t; tt++) { int n = fs.nextInt(); long a = fs.nextLong(); long b = fs.nextLong(); long[] c = new long[n+1]; for (int i = 1; i <= n; i++) c[i] = fs.nextLong(); long ans = b * c[1]; long cap = 0; for (int i = 2; i <= n; i++) { if ((n-i+1)*b >= a) { ans += a * (c[i-1] - cap); cap = c[i-1]; } ans += b * (c[i] - cap); } sb.append(ans + "\n"); } pw.print(sb.toString()); pw.close(); } static boolean ok (long md) { return false; } 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();} int nextInt() {return Integer.parseInt(next());} long nextLong() {return Long.parseLong(next());} double nextDouble() {return Double.parseDouble(next());} } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
edc42ea814478ed08821c4e77d88f1f0
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.io.*; import java.lang.*; import java.util.*; public class ComdeFormces { public static int cc2; public static pair pr; public static long sum; public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub // Reader.init(System.in); FastReader sc=new FastReader(); BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); // OutputStream out = new BufferedOutputStream ( System.out ); int t=sc.nextInt(); int tc=1; while(t--!=0) { int n=sc.nextInt(); long l=sc.nextLong(); long r=sc.nextLong(); long a[]=new long[n+1]; a[0]=0; long sum=0; // long sum[]=new long[n+1]; for(int i=1;i<=n;i++) { a[i]=sc.nextLong(); sum+=a[i]; } int ptr=0; long ans=0; for(int i=1;i<=n;i++) { ans+=r*(a[i]-a[ptr]); sum-=a[i]; if(i<n) { long shift=(l*(a[i]-a[ptr]))+(r*(sum-((n-i)*a[i]))); long el=r*(sum-((n-i)*(a[ptr]))); if(el>=shift) { ans+=l*(a[i]-a[ptr]); ptr=i; } // System.out.println(ptr); } } log.write(ans+"\n"); log.flush(); } } static long eval(ArrayList<ArrayList<Integer>> ar,int src,long f[], boolean vis[]) { long mn=Integer.MAX_VALUE; vis[src]=true; for(int p:ar.get(src)) { if(!vis[p]) { long s=eval(ar,p,f,vis); mn=Math.min(mn,s); sum+=s; } } if(src==0)return 0; if(mn==Integer.MAX_VALUE)return f[src]; sum-=mn; return Math.max(f[src], mn); } public static void radixSort(int a[]) { int n=a.length; int res[]=new int[n]; int p=1; for(int i=0;i<=8;i++) { int cnt[]=new int[10]; for(int j=0;j<n;j++) { a[j]=res[j]; cnt[(a[j]/p)%10]++; } for(int j=1;j<=9;j++) { cnt[j]+=cnt[j-1]; } for(int j=n-1;j>=0;j--) { res[cnt[(a[j]/p)%10]-1]=a[j]; cnt[(a[j]/p)%10]--; } p*=10; } } static int bits(long n) { int ans=0; while(n!=0) { if((n&1)==1)ans++; n>>=1; } return ans; } static long flor(ArrayList<Long> ar,long el) { int s=0; int e=ar.size()-1; while(s<=e) { int m=s+(e-s)/2; if(ar.get(m)==el)return ar.get(m); else if(ar.get(m)<el)s=m+1; else e=m-1; } return e>=0?e:-1; } public static int kadane(int a[]) { int sum=0,mx=Integer.MIN_VALUE; for(int i=0;i<a.length;i++) { sum+=a[i]; mx=Math.max(mx, sum); if(sum<0) sum=0; } return mx; } public static int m=1000000007; public static int mul(int a, int b) { return ((a%m)*(b%m))%m; } public static long mul(long a, long b) { return ((a%m)*(b%m))%m; } public static int add(int a, int b) { return ((a%m)+(b%m))%m; } public static long add(long a, long b) { return ((a%m)+(b%m))%m; } //debug public static <E> void p(E[][] a,String s) { System.out.println(s); for(int i=0;i<a.length;i++) { for(int j=0;j<a[0].length;j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } public static void p(int[] a,String s) { System.out.print(s+"="); for(int i=0;i<a.length;i++)System.out.print(a[i]+" "); System.out.println(); } public static void p(long[] a,String s) { System.out.print(s+"="); for(int i=0;i<a.length;i++)System.out.print(a[i]+" "); System.out.println(); } public static <E> void p(E a,String s){ System.out.println(s+"="+a); } public static <E> void p(ArrayList<E> a,String s){ System.out.println(s+"="+a); } public static <E> void p(LinkedList<E> a,String s){ System.out.println(s+"="+a); } public static <E> void p(HashSet<E> a,String s){ System.out.println(s+"="+a); } public static <E> void p(Stack<E> a,String s){ System.out.println(s+"="+a); } public static <E> void p(Queue<E> a,String s){ System.out.println(s+"="+a); } //utils static ArrayList<Integer> divisors(int n){ ArrayList<Integer> ar=new ArrayList<>(); for (int i=2; i<=Math.sqrt(n); i++){ if (n%i == 0){ if (n/i == i) { ar.add(i); } else { ar.add(i); ar.add(n/i); } } } return ar; } static int primeDivisor(int n){ ArrayList<Integer> ar=new ArrayList<>(); int cnt=0; boolean pr=false; while(n%2==0) { pr=true; ar.add(2); n/=2; } for(int i=3;i*i<=n;i+=2) { pr=false; while(n%i==0) { n/=i; ar.add(i); pr=true; } } if(n>2) ar.add(n); return ar.size(); } static long gcd(long a,long b) { if(b==0)return a; else return gcd(b,a%b); } static int gcd(int a,int b) { if(b==0)return a; else return gcd(b,a%b); } static long factmod(long n,long mod,long img) { if(n==0)return 1; long ans=1; long temp=1; while(n--!=0) { if(temp!=img) { ans=((ans%mod)*((temp)%mod))%mod; } temp++; } return ans%mod; } static int ncr(int n, int r){ if(r>n-r)r=n-r; int ans=1; for(int i=0;i<r;i++){ ans*=(n-i); ans/=(i+1); } return ans; } public static class trip{ int a,b; int c; public trip(int a,int b,int c) { this.a=a; this.b=b; this.c=c; } public int compareTo(trip q) { return this.b-q.b; } } static void mergesort(long[] a,int start,int end) { if(start>=end)return ; int mid=start+(end-start)/2; mergesort(a,start,mid); mergesort(a,mid+1,end); merge(a,start,mid,end); } static void merge(long[] a, int start,int mid,int end) { int ptr1=start; int ptr2=mid+1; long b[]=new long[end-start+1]; int i=0; while(ptr1<=mid && ptr2<=end) { if(a[ptr1]<=a[ptr2]) { b[i]=a[ptr1]; ptr1++; i++; } else { b[i]=a[ptr2]; ptr2++; i++; } } while(ptr1<=mid) { b[i]=a[ptr1]; ptr1++; i++; } while(ptr2<=end) { b[i]=a[ptr2]; ptr2++; i++; } for(int j=start;j<=end;j++) { a[j]=b[j-start]; } } public static class FastReader { BufferedReader b; StringTokenizer s; public FastReader() { b=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(s==null ||!s.hasMoreElements()) { try { s=new StringTokenizer(b.readLine()); } catch(IOException e) { e.printStackTrace(); } } return s.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=b.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } boolean hasNext() { if (s != null && s.hasMoreTokens()) { return true; } String tmp; try { b.mark(1000); tmp = b.readLine(); if (tmp == null) { return false; } b.reset(); } catch (IOException e) { return false; } return true; } } public static class pair{ int a; int b; public pair(int a,int b) { this.a=a; this.b=b; } public int compareTo(pair b) { return this.a-b.a; } // public int compareToo(pair b) { // return this.b-b.b; // } @Override public String toString() { return "{"+this.a+" "+this.b+"}"; } } static long pow(long a, long pw) { long temp; if(pw==0)return 1; temp=pow(a,pw/2); if(pw%2==0)return temp*temp; return a*temp*temp; } static int pow(int a, int pw) { int temp; if(pw==0)return 1; temp=pow(a,pw/2); if(pw%2==0)return temp*temp; return a*temp*temp; } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
f6d565e377140901cdb3f69d0e89899d
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int tt = Integer.parseInt(in.readLine()); for (int t = 1; t <= tt; t++) { String[] tokens = in.readLine().split("\\s+"); long n = Long.parseLong(tokens[0]); long a = Long.parseLong(tokens[1]); long b = Long.parseLong(tokens[2]); long[] nums = Arrays.stream(in.readLine().split("\\s+")).mapToLong(Long::parseLong).toArray(); // a = 6, b = 3 // 1 5 6 21 30 // 3 15 18 63 90 (conquest cost without moving) // 3 12 3 45 27 (conquest cost with moving) // 6 24 6 90 54 (moving cost) // 12 36 6 45 ( long res = 0; long curr = 0; for (int i = 0; i < n; i++) { res += b * (nums[i] - curr); //conq long cost = a * (nums[i] - curr); long savings = b * (nums[i] - curr) * (n - i - 1); if (cost < savings) { res += a * (nums[i] - curr); //move curr = nums[i]; } } System.out.println(res); } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
52fbc89b5a8431cc2e4481c1c88d55d2
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Runner { public static void main(String[] argv) { try (Scanner in = new Scanner(System.in)) { int times = in.nextInt(); for (int time = 0; time < times; time++) { int n = in.nextInt(); long a = in.nextLong(); long b = in.nextLong(); long[] xs = new long[n]; for (int i = 0; i < n; i++) { xs[i] = in.nextLong(); } long theBestResult = Long.MAX_VALUE; long pastXSum = 0; long totalX = Arrays.stream(xs).sum(); long prevX = 0; long currentA = 0; long currentB = 0; for (int i = 0; i < xs.length; i++) { long leftX = totalX - pastXSum; long leftB = b * (leftX - ((xs.length - i) * prevX)); long newResult = currentA + currentB + leftB; if (newResult < theBestResult) { theBestResult = newResult; } long x = xs[i]; currentA += a * (x - prevX); currentB += b * (x - prevX); prevX = x; pastXSum += x; } System.out.println(theBestResult); } } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
7446fdffe090e5c807c383503d3d112d
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.io.*; import java.util.*; public class Main { static int mod = 998244353; static int[][] dir1 = new int[][]{{0,1},{0,-1},{1,0},{-1,0}}; static int[][] dir2 = new int[][]{{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}}; static boolean[] prime = new boolean[10]; static { for (int i = 2; i < prime.length; i++) prime[i] = true; for (int i = 2; i < prime.length; i++) { if (prime[i]) { for (int k = 2; i * k < prime.length; k++) { prime[i * k] = false; } } } } static class JoinSet { int[] fa; JoinSet(int n) { fa = new int[n]; for (int i = 0; i < n; i++) fa[i] = i; } int find(int t) { if (t != fa[t]) fa[t] = find(fa[t]); return fa[t]; } void join(int x, int y) { x = find(x); y = find(y); fa[x] = y; } } static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static long lcm(long a, long b) { return a * b / gcd(a, b); } static int get() throws Exception { String ss = bf.readLine(); if (ss.contains(" ")) ss = ss.substring(0, ss.indexOf(" ")); return Integer.parseInt(ss); } static long getx() throws Exception { String ss = bf.readLine(); if (ss.contains(" ")) ss = ss.substring(0, ss.indexOf(" ")); return Long.parseLong(ss); } static int[] getint() throws Exception { String[] s = bf.readLine().split(" "); int[] a = new int[s.length]; for (int i = 0; i < a.length; i++) { a[i] = Integer.parseInt(s[i]); } return a; } static long[] getlong() throws Exception { String[] s = bf.readLine().split(" "); long[] a = new long[s.length]; for (int i = 0; i < a.length; i++) { a[i] = Long.parseLong(s[i]); } return a; } static String getstr() throws Exception { return bf.readLine(); } static void println() throws Exception { bw.write("\n"); } static void print(int a) throws Exception { bw.write(a + "\n"); } static void print(long a) throws Exception { bw.write(a + "\n"); } static void print(String a) throws Exception { bw.write(a + "\n"); } static void print(int[] a) throws Exception { for (int i : a) { bw.write(i + " "); } println(); } static void print(long[] a) throws Exception { for (long i : a) { bw.write(i + " "); } println(); } static void print(int[][] a) throws Exception { for (int i[] : a) print(i); } static void print(long[][] a) throws Exception { for (long[] i : a) print(i); } static void print(char[] a) throws Exception { for (char i : a) { bw.write(i +""); } println(); } static long pow(long a, long b) { long ans = 1; while (b > 0) { if ((b & 1) == 1) { ans *= a; } a *= a; b >>= 1; } return ans; } static int powmod(long a, long b, int mod) { long ans = 1; while (b > 0) { if ((b & 1) == 1) { ans = ans * a % mod; } a = a * a % mod; b >>= 1; } return (int) ans; } static void sort(int[] a) { int n = a.length; Integer[] b = new Integer[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Arrays.sort(b); for (int i = 0; i < n; i++) a[i] = b[i]; } static void sort(long[] a) { int n = a.length; Long[] b = new Long[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Arrays.sort(b); for (int i = 0; i < n; i++) a[i] = b[i]; } static void resort(int[] a) { int n = a.length; Integer[] b = new Integer[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Arrays.sort(b); for (int i = 0; i < n; i++) a[i] = b[n-1-i]; } static void resort(long[] a) { int n = a.length; Long[] b = new Long[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Arrays.sort(b); for (int i = 0; i < n; i++) a[i] = b[n-1-i]; } static int max(int a, int b) { return Math.max(a,b); } static int min(int a, int b) { return Math.min(a,b); } static long max(long a, long b) { return Math.max(a,b); } static long min(long a, long b) { return Math.min(a,b); } static int max(int[] a) { int max = a[0]; for(int i : a) max = max(max,i); return max; } static int min(int[] a) { int min = a[0]; for(int i : a) min = min(min,i); return min; } static long max(long[] a) { long max = a[0]; for(long i : a) max = max(max,i); return max; } static long min(long[] a) { long min = a[0]; for(long i : a) min = min(min,i); return min; } static int abs(int a) { return Math.abs(a); } static long abs(long a) { return Math.abs(a); } static void yes() throws Exception{ print("Yes"); } static void no() throws Exception{ print("No"); } static int[] getarr(List<Integer> list){ int n = list.size(); int[] a = new int[n]; for(int i = 0;i < n;i++) a[i] = list.get(i); return a; } public static void main(String[] args) throws Exception { int t = get(); while (t-- > 0){ int p[] = getint(); int n = p[0]; long a = p[1], b = p[2]; int[] y = getint(); int x[] = new int[n+1]; long sum[] = new long[n+1]; for(int i = 1;i <= n;i++) { x[i] = y[i-1]; sum[i] = sum[i-1]+x[i]; } long ans = Long.MAX_VALUE; for(int i = 0;i <= n;i++){ long min = a*x[i]; min += x[i]*b; long d = n-i; min += b*(sum[n]-sum[i]-d*x[i]); ans = min(min,ans); } print(ans); } bw.flush(); } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
2a3eb6cf12756772124b3ca16af65dea
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.util.*; import java.lang.*; import java.math.BigInteger; import java.io.*; public class Main implements Runnable { public static void main(String[] args) { new Thread(null, new Main(), "whatever", 1 << 26).start(); } private FastScanner sc; private PrintWriter pw; public void run() { try { boolean isSumitting = true; // isSumitting = false; if (isSumitting) { pw = new PrintWriter(System.out); sc = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); } else { pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); sc = new FastScanner(new BufferedReader(new FileReader("input.txt"))); } } catch (Exception e) { throw new RuntimeException(); } int t = sc.nextInt(); // int t = 1; while (t-- > 0) { // sc.nextLine(); // System.out.println("for t=" + t); solve(); } pw.close(); } public long mod = 1_000_000_007; private class Pair { int first, second; Pair(int first, int second) { this.first = first; this.second = second; } } private void solve() { int n = sc.nextInt(); long a = sc.nextLong(); long b = sc.nextLong(); n++; long[] arr = new long[n]; arr[0] = 0; for (int i = 1; i < n; i++) { arr[i] = sc.nextLong(); } long dif[] = new long[n]; for (int i = 1; i < n; i++) { dif[i] = arr[i] - arr[i - 1]; } long suf[] = new long[n]; suf[n - 1] = dif[n - 1]; for (int i = n - 2; i >= 0; i--) { suf[i] = suf[i + 1] + (n - i) * dif[i]; } long cost = 0; int pos = 0; for (int j = 1; j < n; j++) { // System.out.println("For j=" + j + " , cost=" + cost); if (pos == j - 1) { cost += b * (arr[j] - arr[pos]); continue; } //either calculate cost from here long tempCost1 = suf[j] + (n - j) * (arr[j - 1] - arr[pos]) * b; // either move capital to next kingdom long tempCost2 = suf[j] + (n - j) * (arr[j - 1] - arr[pos + 1]) * b + a * (arr[pos + 1] - arr[pos]); if (tempCost2 < tempCost1) { cost += a * (arr[pos + 1] - arr[pos]); j--; pos++; continue; } cost += (arr[j] - arr[pos]) * b; } pw.println(cost); } class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(BufferedReader bf) { reader = bf; 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 float nextFloat() { return Float.parseFloat(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String[] nextStringArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) { a[i] = next(); } return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } } private static class Sorter { public static <T extends Comparable<? super T>> void sort(T[] arr) { Arrays.sort(arr); } public static <T> void sort(T[] arr, Comparator<T> c) { Arrays.sort(arr, c); } public static <T> void sort(T[][] arr, Comparator<T[]> c) { Arrays.sort(arr, c); } public static <T extends Comparable<? super T>> void sort(ArrayList<T> arr) { Collections.sort(arr); } public static <T> void sort(ArrayList<T> arr, Comparator<T> c) { Collections.sort(arr, c); } public static void normalSort(int[] arr) { Arrays.sort(arr); } public static void normalSort(long[] arr) { Arrays.sort(arr); } public static void sort(int[] arr) { timSort(arr); } public static void sort(int[] arr, Comparator<Integer> c) { timSort(arr, c); } public static void sort(int[][] arr, Comparator<Integer[]> c) { timSort(arr, c); } public static void sort(long[] arr) { timSort(arr); } public static void sort(long[] arr, Comparator<Long> c) { timSort(arr, c); } public static void sort(long[][] arr, Comparator<Long[]> c) { timSort(arr, c); } private static void timSort(int[] arr) { Integer[] temp = new Integer[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(int[] arr, Comparator<Integer> c) { Integer[] temp = new Integer[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(int[][] arr, Comparator<Integer[]> c) { Integer[][] temp = new Integer[arr.length][arr[0].length]; for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; } private static void timSort(long[] arr) { Long[] temp = new Long[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(long[] arr, Comparator<Long> c) { Long[] temp = new Long[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(long[][] arr, Comparator<Long[]> c) { Long[][] temp = new Long[arr.length][arr[0].length]; for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; } } public long fastPow(long x, long y, long mod) { if (y == 0) return 1; if (y == 1) return x % mod; long temp = fastPow(x, y / 2, mod); long ans = (temp * temp) % mod; return (y % 2 == 1) ? (ans * (x % mod)) % mod : ans; } public long fastPow(long x, long y) { if (y == 0) return 1; if (y == 1) return x; long temp = fastPow(x, y / 2); long ans = (temp * temp); return (y % 2 == 1) ? (ans * x) : ans; } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
fbf9b3056dfe5c7c9f49d29931d21dd2
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.util.Scanner; public class Main { Object solve(int n, long a, long b, int[] x_) { n += 1; long[] x = new long[n]; x[0] = 0; for (int i = 0; i < x_.length; i++) { x[i+1] = x_[i]; } long[] s = new long[n+1]; for (int i = n-1; i >= 0; i--) { s[i] += x[i] + s[i+1]; } long sol = Long.MAX_VALUE; for (int i = 0; i < n; i++) { long t = x[i] * (a + b) + b * (s[i] - (n-i) * x[i]); sol = Math.min(sol, t); } return sol; } Object readInputAndSolve(Scanner in) { int n = in.nextInt(); int a = in.nextInt(); int b = in.nextInt(); int[] x = new int[n]; for (int i = 0; i < n; i++) { x[i] = in.nextInt(); } return solve(n, a, b, x); } public static void main(String[] args) { Scanner in = new Scanner(System.in); int nTests = in.nextInt(); for (int iTest = 0; iTest < nTests; iTest++) { System.out.println(new Main().readInputAndSolve(in)); } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
3022d92b38d66318776718ea182bcc99
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.io.*; import java.util.StringTokenizer; public class C { String filename = null; InputReader sc; void solve() { int n = sc.nextInt(); long a = sc.nextLong(); long b = sc.nextLong(); long[] x = new long[n]; for (int i = 0; i < n; i++) { x[i] = sc.nextInt(); } long[] sumFrom = new long[n + 1]; sumFrom[n] = 0; for (int i = n - 1; i >= 0; i--) { sumFrom[i] = sumFrom[i + 1] + x[i]; } long cost = 0; long capital = 0; for (int i = 0; i < n; i++) { // (x[i] - x[i - 1]) * (a + b) + sum(x[j] - x[i]) * b long prev = (i == 0 ? 0 : x[i - 1]); long moveCapital = (x[i] - prev) * (a + b) + sumFrom[i + 1] * b - (n - i - 1) * x[i] * b; // sum(x[j] - x[i - 1]) * b long keepCapital = sumFrom[i] * b - (n - i) * prev * b; if (keepCapital < moveCapital) { cost += (x[i] - capital) * b; } else { cost += (x[i] - capital) * (b + a); capital = x[i]; } } System.out.println(cost); } public void run() throws FileNotFoundException { if (filename == null) { sc = new InputReader(System.in); } else { sc = new InputReader(new FileInputStream(filename)); } int nTests = sc.nextInt(); for (int test = 0; test < nTests; test++) { solve(); } } public static void main(String[] args) { C sol = new C(); try { sol.run(); } catch (FileNotFoundException e) { e.printStackTrace(); } } 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 float nextFloat() { return Float.parseFloat(next()); } public double nextDouble() { return Float.parseFloat(next()); } public long nextLong() { return Long.parseLong(next()); } public int[] nextArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
d3cdfc8f19c7de61f7207ebd8b56b8c7
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class practiceb { static FastScanner sc; static int ans; static long[] arr, arr1; static char[][] board; static long mul, mul1; static int[] spf; static char[] ch; static int n, q, m; static long k, a, x, c, y; static StringBuilder sb, sb1; static long b, sum; static Map<Integer, List<Integer>> map; static Map<Long, List<Integer>> map2; static Map<Long, List<Integer>> map3; static Map<Integer, Integer> shortest; static Map<Integer, Integer> map1, in; static List<Integer> list, list1; static long[] count; static boolean[] vis; static boolean flag; static long total, xor1; // static class Node{ // long dist; // Set<Integer> set; // Node(){ // this.set = new HashSet<>(); // } // Node(int f){ // this.dist = f; // this.set = new HashSet<>(); // } // } // public static class Lca { // int[] depth; // int[] dfs_order; // int cnt; // int[] first; // int[] minPos; // int n; // void dfs(List<Integer>[] tree, int u, int d) { // depth[u] = d; // dfs_order[cnt++] = u; // for (int v : tree[u]) // if (depth[v] == -1) { // dfs(tree, v, d + 1); // dfs_order[cnt++] = u; // } // } // void buildTree(int node, int left, int right) { // if (left == right) { // minPos[node] = dfs_order[left]; // return; // } // int mid = (left + right) >> 1; // buildTree(2 * node + 1, left, mid); // buildTree(2 * node + 2, mid + 1, right); // minPos[node] = depth[minPos[2 * node + 1]] < depth[minPos[2 * node + 2]] ? minPos[2 * node + 1] : minPos[2 * node + 2]; // } // public Lca(List<Integer>[] tree, int root) { // int nodes = tree.length; // depth = new int[nodes]; // Arrays.fill(depth, -1); // n = 2 * nodes - 1; // dfs_order = new int[n]; // cnt = 0; // dfs(tree, root, 0); // minPos = new int[4 * n]; // buildTree(0, 0, n - 1); // first = new int[nodes]; // Arrays.fill(first, -1); // for (int i = 0; i < dfs_order.length; i++) // if (first[dfs_order[i]] == -1) // first[dfs_order[i]] = i; // } // public int lca(int a, int b) { // return minPos(Math.min(first[a], first[b]), Math.max(first[a], first[b]), 0, 0, n - 1); // } // int minPos(int a, int b, int node, int left, int right) { // if (a == left && right == b) // return minPos[node]; // int mid = (left + right) >> 1; // if (a <= mid && b > mid) { // int p1 = minPos(a, Math.min(b, mid), 2 * node + 1, left, mid); // int p2 = minPos(Math.max(a, mid + 1), b, 2 * node + 2, mid + 1, right); // return depth[p1] < depth[p2] ? p1 : p2; // } else if (a <= mid) { // return minPos(a, Math.min(b, mid), 2 * node + 1, left, mid); // } else if (b > mid) { // return minPos(Math.max(a, mid + 1), b, 2 * node + 2, mid + 1, right); // } else { // throw new RuntimeException(); // } // } // } // private static long dfs(int v, int parent,long ht) { // List<Integer> list = map.get(v); // hyt[v] = ht; // if(list.size() == 1){ // if(!map3.containsKey(ht)) // map3.put(ht,new ArrayList<>()); // map3.get(ht).add(v); // return ht; // } // long maxl = -1; // for(Integer i : list){ // if(i == parent) // continue; // maxl = Math.max(maxl,dfs(i,v,ht+1)); // } // return maxl; // } // public static int N = 100005; // long a[N]; // long sfx[N]; // long pfx[N]; // public static boolean f(int width){ // // // long[] a = new long[n]; // long[] sfx = new long[n]; // long[] pfx = new long[n]; // for (int j = 0; j < n; j++){ // if (j % width == 0){ // pfx[j] = a[j]; // } // else pfx[j] = gcd(pfx[j - 1], a[j]); // } // for (int j = n - 2; j >= 0; j--){ // if (j % width == width - 1){ // sfx[j] = a[j]; // } // else sfx[j] = gcd(sfx[j + 1], a[j]); // } // // for(int j = 0; j < n - width + 1 ; j++){ // if (gcd(sfx[j], pfx[j + width - 1]) >= k) return true; // if (((j % width) == width - 1) && pfx[j] >= k){ // return true; // } // } // return false; // } public static class Node { int first; int second; Node(int v, int second) { this.first = v; this.second = second; } } public static long lcm(long a, long b) { return (b / gcd(b, a % b)) * a; } public static void SpecialSieve(int MAXN) { spf = new int[MAXN]; spf[1] = 1; for (int i = 2; i < MAXN; i++) // marking smallest prime factor for every // number to be itself. spf[i] = i; // separately marking spf for every even // number as 2 for (int i = 4; i < MAXN; i += 2) spf[i] = 2; for (int i = 3; i * i < MAXN; i++) { // checking if i is prime if (spf[i] == i) { // marking SPF for all numbers divisible by i for (int j = i * i; j < MAXN; j += i) // marking spf[j] if it is not // previously marked if (spf[j] == j) spf[j] = i; } } } public static void solve() { /* * * 5 6 3 5 6 21 30 -5*6 5*3 5*3 5*3 * * */ long ans = 0; ans += b*(arr[0]); long now = 0; int index = -1; long num = ((a + (b-1))/b); for(int i = 1 ; i < n ; i++) { long rem = ((n-1) - (i) + 1); if((rem >= num) || (a < b)) { ans += (a * (arr[i-1]-now)); now = arr[++index]; } ans += (b * (arr[i] - now)); } sb.append(ans).append("\n"); } public static void main(String[] args) { sc = new FastScanner(); // Scanner sc = new Scanner(System.in); // SpecialSieve(5000000+5); // long num = 1; // list = new ArrayList<>(); // for(int i = 0 ; i <= 60 ; i++) { // list.add(num); // num *= 2; // } int t = sc.nextInt(); // SpecialSieve(10001); // fact(200000+1); sb = new StringBuilder(); // int t = 1; while (t > 0) { // a = sc.nextLong(); // b = sc.nextLong(); // c = sc.nextLong(); n = sc.nextInt(); a = sc.nextLong(); b = sc.nextLong(); // x = sc.nextLong(); // m = sc.nextInt(); // k = sc.nextInt(); // mat = new int[2][m]; // for(int i = 0 ; i < 2 ; i++) // for(int j = 0 ; j < m ; j++) // mat[i][j] = sc.nextLong(); // for(int i = 1 ; i <= 15 ; i++){ // m = i; // solve(); // // } // board = new char[3][3]; // for(int i = 0 ; i < 3 ; i++){ // String s = sc.next(); // for(int j = 0 ; j < 3 ; j++){ // board[i][j] = s.charAt(j); // } // } // x = sc.nextInt(); // ch = sc.next().toCharArray(); // q = sc.nextInt(); // ch = sc.next().toCharArray(); arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextLong(); // ch = sc.next().toCharArray(); // arr1 = new long[m]; // for(int i = 0 ; i < m ; i++) // arr1[i] = sc.nextLong(); // // a = new long[n-1]; // for(int i = 1 ; i < n ; i++) // a[i-1] = Math.abs(arr[i] - arr[i-1]); // // n -= 1; solve(); t -= 1; } System.out.print(sb); } // 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(long[] a) { //ruffle int n = a.length; Random r = new Random(); for (int i = 0; i < a.length; i++) { int oi = r.nextInt(n); long temp = a[i]; a[i] = a[oi]; a[oi] = temp; } //then sort Arrays.sort(a); } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static int log(double n, double 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 long mod(long x, long mod) { long result = x % mod; if (result < 0) { result += mod; } return result; } // Use this to input code since it is faster than a Scanner 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
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
17434aecff4fac7cac2f348e62ab6f9e
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { static final PrintWriter out =new PrintWriter(System.out); static final FastReader sc = new FastReader(); /* _oo0oo_ o8888888o 88" . "88 (| -_- |) 0\ = /0 ___/`---'\___ .' \\| |// '. / \\||| : |||// \ / _||||| -:- |||||- \ | | \\\ - /// | | | \_| ''\---/'' |_/ | \ .-\__ '-' ___/-. / ___'. .' /--.--\ `. .'___ ."" '< `.___\_<|>_/___.' >' "". | | : `- \`.;`\ _ /`;.`/ - ` : | | \ \ `_. \_ __\ /__ _/ .-` / / =====`-.____`.___ \_____/___.-`___.-'===== `=---=' */ public static boolean sorted(int a[]) { int n=a.length,i; int b[]=new int[n]; for(i=0;i<n;i++) b[i]=a[i]; Arrays.sort(b); for(i=0;i<n;i++) { if(a[i]!=b[i]) return false; } return true; } public static void main (String[] args) throws java.lang.Exception { int tes=sc.nextInt(); while(tes-->0) { int n=sc.nextInt(); long x=sc.nextLong(); long y=sc.nextLong(); long a[]=new long[n+1]; int i; a[0]=0; long sum=0,ans=Long.MAX_VALUE;; for(i=1;i<=n;i++) { a[i]=sc.nextLong(); sum+=a[i]; } for(i=0;i<=n;i++) { long prev=a[i]; sum-=prev; long b=(sum-(prev*(n-i)))*y; b+=(x+y)*prev; ans=Math.min(ans,b); } System.out.println(ans); } } public static int first(ArrayList<Integer> arr, int low, int high, int x, int n) { if (high >= low) { int mid = low + (high - low) / 2; if ((mid == 0 || x > arr.get(mid-1)) && arr.get(mid) == x) return mid; else if (x > arr.get(mid)) return first(arr, (mid + 1), high, x, n); else return first(arr, low, (mid - 1), x, n); } return -1; } public static int last(ArrayList<Integer> arr, int low, int high, int x, int n) { if (high >= low) { int mid = low + (high - low) / 2; if ((mid == n - 1 || x < arr.get(mid+1)) && arr.get(mid) == x) return mid; else if (x < arr.get(mid)) return last(arr, low, (mid - 1), x, n); else return last(arr, (mid + 1), high, x, n); } return -1; } public static int lis(int[] arr) { int n = arr.length; ArrayList<Integer> al = new ArrayList<Integer>(); al.add(arr[0]); for(int i = 1 ; i<n;i++) { int x = al.get(al.size()-1); if(arr[i]>=x) { al.add(arr[i]); }else { int v = upper_bound(al, 0, al.size(), arr[i]); al.set(v, arr[i]); } } return al.size(); } public static int lower_bound(ArrayList<Long> ar,int lo , int hi , long k) { Collections.sort(ar); int s=lo; int e=hi; while (s !=e) { int mid = s+e>>1; if (ar.get((int)mid) <k) { s=mid+1; } else { e=mid; } } if(s==ar.size()) { return -1; } return s; } public static int upper_bound(ArrayList<Integer> ar,int lo , int hi, int k) { Collections.sort(ar); int s=lo; int e=hi; while (s !=e) { int mid = s+e>>1; if (ar.get(mid) <=k) { s=mid+1; } else { e=mid; } } if(s==ar.size()) { return -1; } return s; } 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 int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } static long fact(long N) { long mod=1000000007; long n=2; if(N<=1)return 1; else { for(int i=3; i<=N; i++)n=(n*i)%mod; } return n; } private static boolean isInteger(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } 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
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
0c7f655a8ee50140c5808e8887d9e4f8
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.io.*; import java.util.*; public class Solution { static int mod=(int)998244353; public static void main(String[] args) { Copied io = new Copied(System.in, System.out); // int k=1; int t = 1; t = io.nextInt(); for (int i = 0; i < t; i++) { // io.print("Case #" + k + ": "); solve(io); // k++; } io.close(); } public static void solve(Copied io) { int n = io.nextInt(); long a = io.nextLong(), b = io.nextLong(); int c[] = io.readArray(n); long ans = 0; for(int i = 0; i < n; i++) { int last = (i == 0 ? 0 : c[i - 1]); ans += (c[i] - last) * min((n - i) * b, a + b); } io.println(ans); } static String sortString(String inputString) { char tempArray[] = inputString.toCharArray(); Arrays.sort(tempArray); return new String(tempArray); } static void binaryOutput(boolean ans, Copied io){ String a=new String("YES"), b=new String("NO"); if(ans){ io.println(a); } else{ io.println(b); } } static int power(int x, int y, int p) { int res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % p; y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } 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 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 void printArr(int[] arr,Copied io) { for (int x : arr) io.print(x + " "); io.println(); } static void printArr(long[] arr,Copied io) { for (long x : arr) io.print(x + " "); io.println(); } static int[] listToInt(ArrayList<Integer> a){ int n=a.size(); int arr[]=new int[n]; for(int i=0;i<n;i++){ arr[i]=a.get(i); } return arr; } static int mod_mul(int a, int b){ a = a % mod; b = b % mod; return (((a * b) % mod) + mod) % mod;} static int mod_add(int a, int b){ a = a % mod; b = b % mod; return (((a + b) % mod) + mod) % mod;} static int mod_sub(int a, int b){ a = a % mod; b = b % mod; return (((a - b) % mod) + mod) % mod;} } class Pair implements Cloneable, Comparable<Pair> { int x,y; Pair(int a,int b) { this.x=a; this.y=b; } @Override public boolean equals(Object obj) { if(obj instanceof Pair) { Pair p=(Pair)obj; return p.x==this.x && p.y==this.y; } return false; } @Override public int hashCode() { return Math.abs(x)+101*Math.abs(y); } @Override public String toString() { return "("+x+" "+y+")"; } @Override protected Pair clone() throws CloneNotSupportedException { return new Pair(this.x,this.y); } @Override public int compareTo(Pair a) { int t= this.x-a.x; if(t!=0) return t; else return this.y-a.y; } } class byY implements Comparator<Pair>{ // @Override public int compare(Pair o1,Pair o2){ // -1 if want to swap and (1,0) otherwise. int t= o1.y-o2.y; if(t!=0) return t; else return o1.x-o2.x; } } class byX implements Comparator<Pair>{ // @Override public int compare(Pair o1,Pair o2){ // -1 if want to swap and (1,0) otherwise. int t= o1.x-o2.x; if(t!=0) return t; else return o1.y-o2.y; } } class DescendingOrder<T> implements Comparator<T>{ @Override public int compare(Object o1,Object o2){ // -1 if want to swap and (1,0) otherwise. int addingNumber=(Integer) o1,existingNumber=(Integer) o2; if(addingNumber>existingNumber) return -1; else if(addingNumber<existingNumber) return 1; else return 0; } } class Copied extends PrintWriter { public Copied(InputStream i) { super(new BufferedOutputStream(System.out)); r = new BufferedReader(new InputStreamReader(i)); } public Copied(InputStream i, OutputStream o) { super(new BufferedOutputStream(o)); r = new BufferedReader(new InputStreamReader(i)); } public boolean hasMoreTokens() { return peekToken() != null; } public int nextInt() { return Integer.parseInt(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public String next() { return nextToken(); } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } private BufferedReader r; private String line; private StringTokenizer st; private String token; private String peekToken() { if (token == null) try { while (st == null || !st.hasMoreTokens()) { line = r.readLine(); if (line == null) return null; st = new StringTokenizer(line); } token = st.nextToken(); } catch (IOException e) { } return token; } private String nextToken() { String ans = peekToken(); token = null; return ans; } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
72b38e6a29ef8c74d8de44c894474071
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
// package FirstPackage; import java.util.*; import java.lang.Math; import java.io.* ; public class Account { public static class Pair<Object1 ,Object2> { Object1 key ; Object2 val ; Pair(Object1 key ,Object2 val) { this.key = key ; this.val = val ; } Pair() {} } static int mod = (int)(1e9+7) ; public static void main(String[] args) { // Scanner sc = new Scanner(System.in) ; FastReader sc = new FastReader() ; StringBuilder res = new StringBuilder() ; // int t = 1 ; int t = sc.nextInt() ; while(t-- > 0) { int n = sc.nextInt() ; long cap = sc.nextLong() ; long cnq = sc.nextLong() ; long[] pos = sc.readLongArray(n) ; long cost = 0 ,curCap = 0 ; for(long x : pos) { cost += cnq * x ; }for(int i=0 ;i<n ;i++) { long pst = cap * (pos[i] - curCap) ; long neg = cnq * (n-i-1) * (pos[i] - curCap) ; if(neg >= pst) { cost += pst - neg ; curCap = pos[i] ; }else { break ; } } System.out.println(cost); } } /* * 1 1 1 2 2 3 4 4 4 5 * 100 * * [1,7,7,7,7,7,7,7,9,9,9,9,9,9,9,9,9] */ public static long lcmOfArray(int[] arr, int idx) { // lcm(a,b) = (a*b/gcd(a,b)) if (idx == arr.length - 1){ return arr[idx]; } long a = arr[idx]; long b = lcmOfArray(arr, idx+1); return (a*b/gcd(a,b)); } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static long fact(int n) { if(n == 0 || n == 1) { return 1 ; } int mod = (int)(1e9+7) ; long fac = 1 ; while(n > 1) { fac = mul(fac ,n--) ; fac = (fac * n--)%mod ; } return fac ; } // _xor from 1 to n public static int xor(int n) { if (n % 4 == 0) return n; if (n % 4 == 1) return 1; if (n % 4 == 2) return n + 1; return 0; } // mod operations : public static long add(long a, long b) { return (a+b)%mod; } public static long sub(long a, long b) { return ((a-b)%mod+mod)%mod; } public static long mul(long a, long b) { return (a*b)%mod; } /* * Can instantiate SGTNode object like * (i) root = new SGTNode() ; * root.buildTree(nums) ; * or * (ii) root = new SGTNode(nums) ; * * where nums is an array */ public static class SGTNode { int val ,low ,high ; SGTNode left ,right ; public SGTNode() {} public SGTNode(int[] arr){ this.buildTree(arr) ; } public SGTNode(int val ,int low ,int high) { this.val = val ; this.low = low ; this.high = high ; } public SGTNode(int val ,int low ,int high ,SGTNode left ,SGTNode right) { this.val = val ; this.low = low ; this.high = high ; this.left = left ; this.right = right ; } public void buildTree(int[] arr) { // O(n) SGTNode root = buildTreeHelper(this ,arr ,0 ,arr.length-1) ; // return root ; } public SGTNode buildTreeHelper(SGTNode root ,int[] arr ,int low ,int high) { if(high == low) { // return new SGTNode(arr[low] ,low ,high) ; root.val = arr[low] ; root.low = low ; root.high = high ; return root ; } int mid = low + (high - low) / 2 ; root.left = buildTreeHelper(new SGTNode() ,arr ,low ,mid) ; root.right = buildTreeHelper(new SGTNode() ,arr ,mid+1 ,high) ; root.val = root.left.val + root.right.val ; // Computable root.low = low ; root.high = high ; return root ; // return new SGTNode(left.val+right.val ,low ,high ,left ,right) ; } public void update(int ind ,int val) { // O(logn) updateHelper(this ,ind ,val) ; } public void updateHelper(SGTNode root ,int ind ,int val) { if(root.low == root.high) { root.val = val ; return ; } int mid = root.low + (root.high - root.low) / 2 ; if(ind <= mid) { updateHelper(root.left ,ind ,val) ; }else { updateHelper(root.right ,ind ,val) ; } root.val = root.left.val + root.right.val ; // Computable } public int sumRange(int low ,int high) { // O(logn) return sumRangeHelper(this ,low ,high) ; } public int sumRangeHelper(SGTNode root ,int low ,int high) { if(root.low == low && high == root.high) { // complete overlap return root.val ; } // partial overlap int mid = root.low + (root.high - root.low) / 2 ; if(high <= mid) { return sumRangeHelper(root.left ,low ,high) ; }if(low >= mid+1) { return sumRangeHelper(root.right ,low ,high) ; } int left = sumRangeHelper(root.left ,low ,mid) ; int right = sumRangeHelper(root.right ,mid+1 ,high) ; return left + right ; } } 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; } int[] readIntArray(int n) { int[] res = new int[n]; for(int i=0;i<n;i++) res[i] = nextInt(); return res; } long[] readLongArray(int n) { long[] res = new long[n]; for(int i=0;i<n;i++) res[i] = nextLong(); return res; } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
1d4b401e96633d17df5b30daee84f9a1
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
//<———My cp———— //https://takeuforward.org/interview-experience/strivers-cp-sheet/?utm_source=youtube&utm_medium=striver&utm_campaign=yt_video import java.util.*; import java.io.*; public class Solution{ static PrintWriter pw = new PrintWriter(System.out); static FastReader fr = new FastReader(System.in); private static long MOD = 1_000_000_007; public static void main(String[] args) throws Exception { long t = fr.nextLong(); while(t-->0){ int n = fr.nextInt(); long a = fr.nextLong(); long b = fr.nextLong(); long[] vals = new long[n]; for(int i=0;i<n;i++){ vals[i]=fr.nextLong(); } long[] prefixCap = new long[n]; long total = 0; long lastCoord = 0; for(int i =0;i<n;i++){ total=(total)+((vals[i]-lastCoord)*a)+((vals[i]-lastCoord)*b); prefixCap[i]=total; lastCoord=vals[i]; } long[] suffixSum = new long[n]; total = 0; for(int i=n-1;i>=0;i--){ total=total+vals[i]; suffixSum[i]=total; } long minCost = suffixSum[0]*b; for(int i = 0;i<n;i++){ long totalCost = prefixCap[i]; //pw.println(totalCost); if(i+1<n){ totalCost=((suffixSum[i+1]-(vals[i]*(n-i-1))))*b+totalCost; } minCost=Math.min(totalCost, minCost); } pw.println(minCost); } pw.close(); } static class Pair{ int height; int width; public Pair(int height,int width){ this.height = height; this.width = width; } @Override public String toString() { return "height: "+height+" width: "+width; } } public static long opNeeded(long c,long[] vals){ long tempResult = 0; for(int j = 0;j<vals.length;j++){ tempResult=tempResult+Math.abs((long)(vals[j]-Math.pow(c,j))); } if(tempResult<0){ tempResult=Long.MAX_VALUE; } return tempResult; } static int isPerfectSquare(int vals){ int lastPow=1; while(lastPow*lastPow<vals){ lastPow++; } if(lastPow*lastPow==vals){ return lastPow*lastPow; }else{ return -1; } } public static int[] sort(int[] vals){ ArrayList<Integer> values = new ArrayList<>(); for(int i = 0;i<vals.length;i++){ values.add(vals[i]); } Collections.sort(values); for(int i =0;i<values.size();i++){ vals[i] = values.get(i); } return vals; } public static long[] sort(long[] vals){ ArrayList<Long> values = new ArrayList<>(); for(int i = 0;i<vals.length;i++){ values.add(vals[i]); } Collections.sort(values); for(int i =0;i<values.size();i++){ vals[i] = values.get(i); } return vals; } public static void reverseArray(long[] vals){ int startIndex = 0; int endIndex = vals.length-1; while(startIndex<=endIndex){ long temp = vals[startIndex]; vals[startIndex] = vals[endIndex]; vals[endIndex] = temp; startIndex++; endIndex--; } } public static void reverseArray(int[] vals){ int startIndex = 0; int endIndex = vals.length-1; while(startIndex<=endIndex){ int temp = vals[startIndex]; vals[startIndex] = vals[endIndex]; vals[endIndex] = temp; startIndex++; endIndex--; } } static class FastReader{ byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } public static int GCD(int numA, int numB){ if(numA==0){ return numB; }else if(numB==0){ return numA; }else{ if(numA>numB){ return GCD(numA%numB,numB); }else{ return GCD(numA,numB%numA); } } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
109a60cff6ad7bfe83109338f5a0e13c
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
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.HashSet; import java.util.Random; import java.util.Set; import java.util.StringTokenizer; /* */ public class C { public static void main(String[] args) { FastScanner fs=new FastScanner(); PrintWriter out=new PrintWriter(System.out); int T=fs.nextInt(); // int T=1; for (int tt=0; tt<T; tt++) { int n = fs.nextInt(), a = fs.nextInt(), b = fs.nextInt(); long[] x = new long[n + 1]; long[] pref = new long[n + 1]; x[0] = 0; for (int i = 1; i <= n; i++) x[i] = fs.nextInt(); pref[0] = x[0]; for (int i = 1; i <= n; i++) pref[i] = pref[i - 1] + x[i]; long best = INF; for (int i = 0; i <= n; i++) { best = Math.min(best, a * x[i] + b * (pref[n] - pref[i] - (n - i - 1) * x[i])); } out.println(best); } out.close(); } static final Random random=new Random(); static final int mod=1_000_000_007; static final long INF=2_000_000_000_000_000_000l; 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 add(long a, long b) { return (a+b)%mod; } static long sub(long a, long b) { return ((a-b)%mod+mod)%mod; } static long mul(long a, long b) { return (a*b)%mod; } static long exp(long base, long exp) { if (exp==0) return 1; long half=exp(base, exp/2); if (exp%2==0) return mul(half, half); return mul(half, mul(half, base)); } static long[] factorials=new long[2_000_001]; static long[] invFactorials=new long[2_000_001]; static void precompFacts() { factorials[0]=invFactorials[0]=1; for (int i=1; i<factorials.length; i++) factorials[i]=mul(factorials[i-1], i); invFactorials[factorials.length-1]=exp(factorials[factorials.length-1], mod-2); for (int i=invFactorials.length-2; i>=0; i--) invFactorials[i]=mul(invFactorials[i+1], i+1); } static long nCk(int n, int k) { return mul(factorials[n], mul(invFactorials[k], invFactorials[n-k])); } static void sort(int[] b) { ArrayList<Integer> l=new ArrayList<>(); for (int i:b) l.add(i); Collections.sort(l); for (int i=0; i<b.length; i++) b[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
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
785cfa9c83fa6eab7d930739c41b9b5c
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.io.*; import java.util.*; public class Q1659C { static int mod = (int) (1e9 + 7); static void solve() { int n = i(); long a = l(); long b = l(); long c[] = new long[n + 1]; for (int i = 1; i < c.length; i++) { c[i] = l(); } long[] prefix = new long[n+1]; for (int i = 1; i < c.length; i++) { prefix[i] = prefix[i - 1] + c[i]; } long ans = Long.MAX_VALUE; for (int i = 0; i < c.length; i++) { ans=Math.min(ans, a*c[i]+b*(prefix[prefix.length-1]-prefix[i]-(n-i-1)*c[i])); } System.out.println(ans); } public static void main(String[] args) { int test = i(); while (test-- > 0) { solve(); } } // ----->segmentTree--> segTree as class // ----->lazy_Seg_tree --> lazy_Seg_tree as class // -----> Trie --->Trie as class // ----->fenwick_Tree ---> fenwick_Tree // -----> POWER ---> long power(long x, long y) <---- // -----> LCM ---> long lcm(long x, long y) <---- // -----> GCD ---> long gcd(long x, long y) <---- // -----> SIEVE --> ArrayList<Integer> sieve(int N) <----- // -----> NCR ---> long ncr(int n, int r) <---- // -----> BINARY_SEARCH ---> int binary_Search(long[]arr,long x) <---- // -----> (SORTING OF LONG, CHAR,INT) -->long[] sortLong(long[] a2)<-- // ----> DFS ---> void dfs(ArrayList<ArrayList<Integer>>edges,int child,int // parent)<--- // ---> NODETOROOT --> ArrayList<Integer> // node2Root(ArrayList<ArrayList<Integer>>edges,int child,int parent,int tofind) // <-- // ---> LEVELS_TREE -->levels_Trees(ArrayList<HashSet<Integer>> edges, int // child, int parent,int[]level,int currLevel) <-- // ---> K_THPARENT --> int k_thparent(int node,int[][]parent,int k) <--- // ---> TWOPOWERITHPARENTFILL --> void twoPowerIthParentFill(int[][]parent) <--- // -----> (INPUT OF INT,LONG ,STRING) -> int i() long l() String s()<- // tempstart 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 InputReader in = new InputReader(System.in); 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(); } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
500dce62693901aea5b137968c682887
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class C782{ public static void main(String[] args){ FastScanner sc = new FastScanner(); int t = sc.nextInt(); for(int tt = 0; tt < t; tt++){ int n = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); long[] x = new long[n + 1]; for(int i = 1; i <= n; i++){ x[i] = sc.nextInt(); } long max = (a + b)*x[n ]; long sufSum = 0; for(int i = n ; i >= 1; i--){ sufSum += x[i]; if( (a + b) * x[i - 1] + b*(sufSum - (n - i + 1) * x[i-1]) < max){ max = (a + b) * x[i - 1] + b*(sufSum - (n - i + 1) * x[i-1]); } } System.out.println(max); } } 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
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
0ccf19104d4c615b900df3ef3e5fcb40
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.util.Arrays; import java.util.StringTokenizer; public class C_Line_Empire { 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(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } public static void main(String[] args) { FastScanner fs = new FastScanner(); Integer cases = fs.nextInt(); for(int i = 0; i < cases; i++){ Integer nums = fs.nextInt(); Integer a = fs.nextInt(); Integer b = fs.nextInt(); int[] num = new int[nums]; for(int j = 0; j < nums; j++){ num[j] = fs.nextInt(); } System.out.println(travel(a,b,num)); } } public static long travel(long move, long con, int[] nums){ // System.out.println(move); // System.out.println(Arrays.toString(nums)); long res = Long.MAX_VALUE; int n = nums.length; long pre = 0l; for(int i = n-2; i >= 0; i--){ int dis = n - i-1; pre = pre + dis * (nums[i+1] - nums[i]) * con; res = Math.min(res,pre + nums[i] * (move+con)); } pre = pre + n * nums[0]*con; res = Math.min(res,pre); return res; } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
125c4da41bd47a4b48ac585fc701157f
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.sort; public class Round12 { public static void main(String[] args) { FastReader fastReader = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t = fastReader.nextInt(); while (t-- > 0) { int n = fastReader.nextInt(); int a = fastReader.nextInt(); int b = fastReader.nextInt(); int arr[] = fastReader.ria(n); long ans = 0; int st = 0; int index = -1; for (int i = 0; i < n; i++) { int diff = arr[i] - st; long val = ((long) diff * (n - (i + 1))) * b; long val2 = ((long) diff * a); if (val2 < val) { index = i; } } if (index != -1) { ans += (long) (arr[index] - st) * b; ans += (long) (arr[index] - st) * a; st = arr[index]; } for (int i = index + 1; i < n; i++) { ans += (long) (arr[i] - st) * b; } out.println(ans); } out.close(); } // constants static final int IBIG = 1000000007; static final int IMAX = 2147483647; static final long LMAX = 9223372036854775807L; static Random __r = new Random(); // math util static int minof(int a, int b, int c) { return min(a, min(b, c)); } static int minof(int... x) { if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min; } static long minof(long a, long b, long c) { return min(a, min(b, c)); } static long minof(long... x) { if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min; } static int maxof(int a, int b, int c) { return max(a, max(b, c)); } static int maxof(int... x) { if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max; } static long maxof(long a, long b, long c) { return max(a, max(b, c)); } static long maxof(long... x) { if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max; } static int powi(int a, int b) { if (a == 0) return 0; int ans = 1; while (b > 0) { if ((b & 1) > 0) ans *= a; a *= a; b >>= 1; } return ans; } static long powl(long a, int b) { if (a == 0) return 0; long ans = 1; while (b > 0) { if ((b & 1) > 0) ans *= a; a *= a; b >>= 1; } return ans; } static int fli(double d) { return (int) d; } static int cei(double d) { return (int) ceil(d); } static long fll(double d) { return (long) d; } static long cel(double d) { return (long) ceil(d); } static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static int[] exgcd(int a, int b) { if (b == 0) return new int[]{1, 0}; int[] y = exgcd(b, a % b); return new int[]{y[1], y[0] - y[1] * (a / b)}; } static long[] exgcd(long a, long b) { if (b == 0) return new long[]{1, 0}; long[] y = exgcd(b, a % b); return new long[]{y[1], y[0] - y[1] * (a / b)}; } static int randInt(int min, int max) { return __r.nextInt(max - min + 1) + min; } static long mix(long x) { x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31); } public static boolean[] findPrimes(int limit) { assert limit >= 2; final boolean[] nonPrimes = new boolean[limit]; nonPrimes[0] = true; nonPrimes[1] = true; int sqrt = (int) Math.sqrt(limit); for (int i = 2; i <= sqrt; i++) { if (nonPrimes[i]) continue; for (int j = i; j < limit; j += i) { if (!nonPrimes[j] && i != j) nonPrimes[j] = true; } } return nonPrimes; } // array util static void reverse(int[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(long[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(double[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(char[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void shuffle(int[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(long[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(double[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void rsort(int[] a) { shuffle(a); sort(a); } static void rsort(long[] a) { shuffle(a); sort(a); } static void rsort(double[] a) { shuffle(a); sort(a); } static int[] copy(int[] a) { int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static long[] copy(long[] a) { long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static double[] copy(double[] a) { double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static char[] copy(char[] a) { char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } 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()); } int[] ria(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = Integer.parseInt(next()); return a; } long nextLong() { return Long.parseLong(next()); } long[] rla(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = Long.parseLong(next()); return a; } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
5b11f7d0f78cb7b7fa30bed331d89713
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
//Utilities import java.io.*; import java.util.*; public class a { static int t; static int n; static long a, b; static int[] x; static long res; public static void main(String[] args) throws IOException { t = in.iscan(); while (t-- > 0) { n = in.iscan(); a = in.lscan(); b = in.lscan(); x = new int[n+1]; for (int i = 1; i <= n; i++) { x[i] = in.iscan(); } res = Long.MAX_VALUE; int l = 0, r = n-1, mid; while (l <= r) { mid = (l+r)/2; long ll = mid - 1 >= 0 ? attack(mid-1) : Long.MAX_VALUE; long cur = attack(mid); long rr = attack(mid+1); res = Math.min(res, Math.min(cur, Math.min(ll, rr))); if (ll < rr) { r = mid-1; } else { l = mid+1; } } out.println(res); } out.close(); } static long attack(int conquerTo) { long ret = 0; for (int i = 1; i <= n; i++) { if (i <= conquerTo) { ret += b * (x[i] - x[i-1]); ret += a * (x[i] - x[i-1]); } else { ret += b * (x[i] - x[conquerTo]); } } return ret; } static INPUT in = new INPUT(System.in); static PrintWriter out = new PrintWriter(System.out); private static class INPUT { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; public INPUT (InputStream stream) { this.stream = stream; } public INPUT (String file) throws IOException { this.stream = new FileInputStream (file); } public int cscan () throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read (buf); } if (numChars == -1) return numChars; return buf[curChar++]; } public int iscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } int res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public String sscan () throws IOException { int c = cscan (); while (space (c)) c = cscan (); StringBuilder res = new StringBuilder (); do { res.appendCodePoint (c); c = cscan (); } while (!space (c)); return res.toString (); } public double dscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } double res = 0; while (!space (c) && c != '.') { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); res *= 10; res += c - '0'; c = cscan (); } if (c == '.') { c = cscan (); double m = 1; while (!space (c)) { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); m /= 10; res += (c - '0') * m; c = cscan (); } } return res * sgn; } public long lscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } long res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public boolean space (int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } public static class UTILITIES { static final double EPS = 10e-6; public static void sort(int[] a, boolean increasing) { ArrayList<Integer> arr = new ArrayList<Integer>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static void sort(long[] a, boolean increasing) { ArrayList<Long> arr = new ArrayList<Long>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static void sort(double[] a, boolean increasing) { ArrayList<Double> arr = new ArrayList<Double>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } 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; } public static void updateMap(HashMap<Integer, Integer> map, int key, int v) { if (!map.containsKey(key)) { map.put(key, v); } else { map.put(key, map.get(key) + v); } if (map.get(key) == 0) { map.remove(key); } } public static long gcd (long a, long b) { return b == 0 ? a : gcd (b, a % b); } public static long lcm (long a, long b) { return a * b / gcd (a, b); } public static long fast_pow_mod (long b, long x, int mod) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod; return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod; } public static long fast_pow (long b, long x) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow (b * b, x / 2); return b * fast_pow (b * b, x / 2); } public static long choose (long n, long k) { k = Math.min (k, n - k); long val = 1; for (int i = 0; i < k; ++i) val = val * (n - i) / (i + 1); return val; } public static long permute (int n, int k) { if (n < k) return 0; long val = 1; for (int i = 0; i < k; ++i) val = (val * (n - i)); return val; } // start of permutation and lower/upper bound template public static void nextPermutation(int[] nums) { //find first decreasing digit int mark = -1; for (int i = nums.length - 1; i > 0; i--) { if (nums[i] > nums[i - 1]) { mark = i - 1; break; } } if (mark == -1) { reverse(nums, 0, nums.length - 1); return; } int idx = nums.length-1; for (int i = nums.length-1; i >= mark+1; i--) { if (nums[i] > nums[mark]) { idx = i; break; } } swap(nums, mark, idx); reverse(nums, mark + 1, nums.length - 1); } public static void swap(int[] nums, int i, int j) { int t = nums[i]; nums[i] = nums[j]; nums[j] = t; } public static void reverse(int[] nums, int i, int j) { while (i < j) { swap(nums, i, j); i++; j--; } } static int lower_bound (int[] arr, int hi, int cmp) { int low = 0, high = hi, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= cmp) high = mid; else low = mid + 1; } return low; } static int upper_bound (int[] arr, int hi, int cmp) { int low = 0, high = hi, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > cmp) high = mid; else low = mid + 1; } return low; } // end of permutation and lower/upper bound template } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output
PASSED
dc0e7c65c7a5e7639368e6a5e81542d4
train_110.jsonl
1650206100
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0&lt;x_1&lt;x_2&lt;\ldots&lt;x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
256 megabytes
import java.util.*; import java.io.*; public class lineempire { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); StringTokenizer st = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st.nextToken()); while (t-- > 0) { st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); long a = Long.parseLong(st.nextToken()); long b = Long.parseLong(st.nextToken()); st = new StringTokenizer(br.readLine()); long[] x = new long[n + 1]; for (int i = 0; i < n; ++i) x[i + 1] = Long.parseLong(st.nextToken()); int pre = 0; long ans = 0; for (int i = 1; i <= n; i++) { ans += (x[i] - x[pre]) * b; if ((x[i] - x[pre]) * b * (n - i) > (x[i] - x[pre]) * a) { ans += (x[i] - x[pre]) * a; pre = i; } } pw.println(ans); } pw.close(); } }
Java
["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"]
1 second
["173\n171\n75\n3298918744"]
NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this.
Java 11
standard input
[ "binary search", "brute force", "dp", "greedy", "implementation", "math" ]
ce2b12f1d7c0388c39fee52f5410b94b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 &lt; x_2 &lt; \ldots &lt; x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,500
For each test case, output a single integer  — the minimum cost to conquer all kingdoms.
standard output