exec_outcome
stringclasses
1 value
code_uid
stringlengths
32
32
file_name
stringclasses
111 values
prob_desc_created_at
stringlengths
10
10
prob_desc_description
stringlengths
63
3.8k
prob_desc_memory_limit
stringclasses
18 values
source_code
stringlengths
117
65.5k
lang_cluster
stringclasses
1 value
prob_desc_sample_inputs
stringlengths
2
802
prob_desc_time_limit
stringclasses
27 values
prob_desc_sample_outputs
stringlengths
2
796
prob_desc_notes
stringlengths
4
3k
βŒ€
lang
stringclasses
5 values
prob_desc_input_from
stringclasses
3 values
tags
listlengths
0
11
src_uid
stringlengths
32
32
prob_desc_input_spec
stringlengths
28
2.37k
βŒ€
difficulty
int64
-1
3.5k
βŒ€
prob_desc_output_spec
stringlengths
17
1.47k
βŒ€
prob_desc_output_to
stringclasses
3 values
hidden_unit_tests
stringclasses
1 value
PASSED
5b0286a7c1c0a6e28e197c117e08c053
train_002.jsonl
1302879600
In one very large and very respectable company there is a cloakroom with a coat hanger. It is represented by n hooks, positioned in a row. The hooks are numbered with positive integers from 1 to n from the left to the right.The company workers have a very complicated work schedule. At the beginning of a work day all the employees are not there and the coat hanger in the cloakroom is empty. At some moments of time the employees arrive and some of them leave.When some employee arrives, he hangs his cloak on one of the available hooks. To be of as little discomfort to his colleagues as possible, the hook where the coat will hang, is chosen like this. First the employee chooses the longest segment among available hooks following in a row. If there are several of such segments, then he chooses the one closest to the right. After that the coat is hung on the hook located in the middle of this segment. If the segment has an even number of hooks, then among two central hooks we choose the one closest to the right.When an employee leaves, he takes his coat. As all the company workers deeply respect each other, no one takes somebody else's coat.From time to time the director of this respectable company gets bored and he sends his secretary to see how many coats hang on the coat hanger from the i-th to the j-th hook inclusive. And this whim is always to be fulfilled, otherwise the director gets angry and has a mental breakdown.Not to spend too much time traversing from the director's office to the cloakroom and back again, the secretary asked you to write a program, emulating the company cloakroom's work.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.fill; import static java.util.Arrays.binarySearch; import static java.util.Arrays.sort; public class Main { public static void main(String[] args) throws IOException { new Thread(null, new Runnable() { public void run() { try { try { if (new File("input.txt").exists()) System.setIn(new FileInputStream("input.txt")); } catch (SecurityException e) {} new Main().run(); } catch (IOException e) { e.printStackTrace(); } } }, "1", 1L << 24).start(); } BufferedReader in; PrintWriter out; StringTokenizer st = new StringTokenizer(""); int MAXQ = 100000; Map<Integer, Person> map = new HashMap<Integer, Main.Person>(MAXQ); TreeSet<Range> set = new TreeSet<Main.Range>(); TreeSet<Integer> sorter = new TreeSet<Integer>(); Map<Integer, Integer> num; void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int n = nextInt(); set.add(new Range(1, n, null, null)); int qNum = nextInt(); Query[] querys = new Query [qNum]; for (int q = 0; q < qNum; q++) { int p = nextInt(); if (p == 0) { querys[q] = new Query(p, nextInt(), nextInt()); } else { Person pers = null; if (map.containsKey(p)) { pers = map.get(p); removePerson(pers); map.remove(p); } else { pers = addPerson(); map.put(p, pers); } sorter.add(pers.pos); querys[q] = new Query(p, pers.pos, -1); } } sorter.add(0); sorter.add(n + 1); num = new HashMap<Integer, Integer>(sorter.size()); int number = 0; for (Integer x : sorter) num.put(x, number++); RSQ rsq = new RSQ(sorter.size()); for (Query q : querys) { if (q.person == 0) { // System.out.println("get " + left(q.left) + "(" + q.left + ") " + right(q.right) + "(" + q.right + ")"); out.println(rsq.get(left(q.left), right(q.right))); } else { // System.out.println("set " + num.get(q.left) + "(" + q.left + ")"); rsq.set(num.get(q.left)); } } out.close(); } int left(int x) { if (num.containsKey(x)) return num.get(x); return num.get(sorter.higher(x)); } int right(int x) { if (num.containsKey(x)) return num.get(x); return num.get(sorter.lower(x)); } Person addPerson() { Range r = set.pollFirst(); int pos = r.getPos(); Person ret = new Person(pos); set.add(ret.leftRange = new Range(r.left, pos - 1, r.leftPerson, ret)); set.add(ret.rightRange = new Range(pos + 1, r.left + r.size - 1, ret, r.rightPerson)); if (r.leftPerson != null) r.leftPerson.rightRange = ret.leftRange; if (r.rightPerson != null) r.rightPerson.leftRange = ret.rightRange; return ret; } void removePerson(Person p) { Range lr = p.leftRange; Range rr = p.rightRange; Range mr = new Range(lr.left, lr.left + lr.size + rr.size, lr.leftPerson, rr.rightPerson); if (lr.leftPerson != null) lr.leftPerson.rightRange = mr; if (rr.rightPerson != null) rr.rightPerson.leftRange = mr; set.remove(lr); set.remove(rr); set.add(mr); } class Query { int person; int left; int right; Query(int person, int left, int right) { this.person = person; this.left = left; this.right = right; } } class Range implements Comparable<Range> { int left; int size; Person leftPerson; Person rightPerson; Range(int left, int right, Person leftPerson, Person rightPerson) { this.left = left; this.size = right - left + 1; this.leftPerson = leftPerson; this.rightPerson = rightPerson; } int getPos() { return left + size / 2; } @Override public int compareTo(Range r) { if (size != r.size) return size > r.size ? -1 : 1; if (left != r.left) return left > r.left ? -1 : 1; return 0; } @Override public String toString() { return "Range [left=" + left + ", size=" + size + ", leftPerson=" + leftPerson + ", rightPerson=" + rightPerson + "]"; } } class Person { int pos; Range leftRange = null; Range rightRange = null; Person(int pos) { this.pos = pos; } } class RSQ { int n; int[] sum; RSQ(int n) { this.n = n; sum = new int [2 * n]; } void set(int i) { sum[n + i] = 1 - sum[n + i]; for (int v = (n + i) >> 1; v > 0; v >>= 1) sum[v] = sum[v << 1] + sum[(v << 1) + 1]; } int get(int l, int r) { l += n; r += n; int ret = 0; while (l <= r) { if ((l & 1) == 1) ret += sum[l]; if ((r & 1) == 0) ret += sum[r]; l = (l + 1) >> 1; r = (r - 1) >> 1; } return ret; } } /*************************************************************** * Input **************************************************************/ String nextToken() throws IOException { while (!st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String nextLine() throws IOException { st = new StringTokenizer(""); return in.readLine(); } boolean EOF() throws IOException { while (!st.hasMoreTokens()) { String s = in.readLine(); if (s == null) return true; st = new StringTokenizer(s); } return false; } }
Java
["9 11\n1\n2\n0 5 8\n1\n1\n3\n0 3 8\n9\n0 6 9\n6\n0 1 9"]
4 seconds
["2\n3\n2\n5"]
null
Java 6
standard input
[ "data structures" ]
1250f103aa5fd1ac300a8a71b816b3e4
The first line contains two integers n, q (1 ≀ n ≀ 109, 1 ≀ q ≀ 105), which are the number of hooks on the hanger and the number of requests correspondingly. Then follow q lines with requests, sorted according to time. The request of the type "0 i j" (1 ≀ i ≀ j ≀ n) β€” is the director's request. The input data has at least one director's request. In all other cases the request contains a positive integer not exceeding 109 β€” an employee identificator. Each odd appearance of the identificator of an employee in the request list is his arrival. Each even one is his leaving. All employees have distinct identificators. When any employee arrives, there is always at least one free hook.
2,400
For each director's request in the input data print a single number on a single line β€” the number of coats hanging on the hooks from the i-th one to the j-th one inclusive.
standard output
PASSED
472991fa99ca54f2883562ae54399d32
train_002.jsonl
1585924500
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { // File file = new File("input.txt"); // Scanner in = new Scanner(file); // PrintWriter out = new PrintWriter(new FileWriter("output.txt")); public static void main(String[] args) { // Scanner in = new Scanner(System.in); FastReader in = new FastReader(); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(), x = in.nextInt(); int[] arr = new int[10000]; for (int i = 0; i < n; i++) { arr[in.nextInt()]++; } int ans = 0, max = Integer.MIN_VALUE; for(int i = 1; i<arr.length; i++){ if(arr[i]==0){ if(x==0){ ans = 0; continue; } x--; ans++; }else ans++; max = Math.max(ans, max); } System.out.println(max); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20"]
1 second
["5\n101\n2\n2\n60"]
NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.
Java 11
standard input
[ "implementation" ]
e5fac4a1f3a724234990fe758debc33f
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).
900
For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.
standard output
PASSED
1d9eb96c2512acf0c6d03142fc7f070d
train_002.jsonl
1585924500
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.
256 megabytes
import java.util.Scanner; import java.util.ArrayList; import java.util.Arrays; import java.util.Map; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.lang.Math; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i = 0; i<t; i++) { int n = sc.nextInt(), x = sc.nextInt(), count = 1, index = 1; boolean places[] = new boolean[251]; for(int j = 0; j< n; j++) places[sc.nextInt()] = true; while(count <= x && index < 251) { if(places[index] == false) count++; index++; } while(index <251) { if(places[index] == false) { System.out.println(index-1); break; } index++; } } sc.close(); } }
Java
["5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20"]
1 second
["5\n101\n2\n2\n60"]
NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.
Java 11
standard input
[ "implementation" ]
e5fac4a1f3a724234990fe758debc33f
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).
900
For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.
standard output
PASSED
e91d586dddcdab4dbf7a5218f85ace65
train_002.jsonl
1585924500
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; public class A { static class Task { public void solve(int testNumber, InputReader in, PrintWriter out) { int t = in.nextInt(); while ((t--) > 0) { int n = in.nextInt(); int x = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } sort(arr); int pre = 0; for (int i = 0; i < n; i++) { int diff = arr[i] - pre; if (diff <= 1) { pre = arr[i]; continue; } if (diff == x + 1) { pre = arr[i]; x = 0; for (int j = i + 1; j < n; j++) { if (arr[j] == arr[j - 1] + 1) { pre = arr[j]; } else if (arr[j] > arr[j - 1] + 1) { break; } } break; } else if (diff > x + 1) { pre = pre + x; x = 0; break; } else { x -= diff - 1; pre = arr[i]; } } if (x > 0) { pre += x; } out.println(pre); } } } private static void sort(double[] arr) { Double[] objArr = Arrays.stream(arr).boxed().toArray(Double[]::new); Arrays.sort(objArr); for (int i = 0; i < arr.length; i++) { arr[i] = objArr[i]; } } private static void sort(int[] arr) { Integer[] objArr = Arrays.stream(arr).boxed().toArray(Integer[]::new); Arrays.sort(objArr); for (int i = 0; i < arr.length; i++) { arr[i] = objArr[i]; } } private static void sort(long[] arr) { Long[] objArr = Arrays.stream(arr).boxed().toArray(Long[]::new); Arrays.sort(objArr); for (int i = 0; i < arr.length; i++) { arr[i] = objArr[i]; } } private static void solve() { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task task = new Task(); task.solve(1, in, out); out.close(); } public static void main(String[] args) { new Thread(null, () -> solve(), "1", 1 << 26).start(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
Java
["5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20"]
1 second
["5\n101\n2\n2\n60"]
NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.
Java 11
standard input
[ "implementation" ]
e5fac4a1f3a724234990fe758debc33f
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).
900
For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.
standard output
PASSED
207fdc48b870bb74141ba40015ab030f
train_002.jsonl
1585924500
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.
256 megabytes
import javax.annotation.processing.Filer; import java.util.*; import java.io.*; public class dreamooon { public static void main(String[]args)throws Exception{ Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); for(int i = 0; i < t ; i++){ int n = sc.nextInt(); int a = sc.nextInt(); ArrayList<Integer>usedPlaces = new ArrayList<>(); for(int j = 0; j < n; j++){ int nj = sc.nextInt(); usedPlaces.add(nj); } int count = 0; int maxInt = 1; while(count<=a){ if(usedPlaces.contains(maxInt)){ maxInt++; } else if(count == a){ if(usedPlaces.contains(maxInt)){ maxInt++; } else{ break; } } else{ count++; maxInt++; } } out.println(maxInt-1); } out.close(); } }
Java
["5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20"]
1 second
["5\n101\n2\n2\n60"]
NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.
Java 11
standard input
[ "implementation" ]
e5fac4a1f3a724234990fe758debc33f
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).
900
For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.
standard output
PASSED
ad96d295a151acdbd3e619422fd14a1d
train_002.jsonl
1585924500
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for(int t = 0; t<T; t++){ int len = sc.nextInt(); int x = sc.nextInt(); int arr[] = new int[101]; for(int i=0; i<len; i++){ arr[sc.nextInt()] = 1; } int i = 1; for(i=1; i<=100; ++i){ //System.out.println(i + " " + x); if(x == 0){ while(i < 101){ if(arr[i] == 0){ //i--; break; } i++; } break; } if(arr[i] == 0)x--; } i--; System.out.println(x + i); } } }
Java
["5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20"]
1 second
["5\n101\n2\n2\n60"]
NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.
Java 11
standard input
[ "implementation" ]
e5fac4a1f3a724234990fe758debc33f
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).
900
For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.
standard output
PASSED
a513bc33239952e020b8f7b737e177e2
train_002.jsonl
1585924500
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.
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 { public static void main (String[]args) throws IOException { Scanner in = new Scanner(System.in); try (PrintWriter or = new PrintWriter(System.out)) { int t=in.nextInt(); outer: while (t-->0){ int n=in.nextInt(),k=in.nextInt(); int []a=new int[1000]; for (int i = 0; i < n; i++) { a[in.nextInt()]++; } for (int i = 1; i <a.length; i++) { if (a[i]==0){ --k; if (k<0){ or.println(i-1); continue outer; } } } } } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static int getlen(int r,int l,int a){ return (r-l+1+1)/(a+1); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) { if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) { f *= 10; } } } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } } class Pair implements Comparable<Pair> { int first, second; public Pair(int first, int second) { this.first = first; this.second = second; } @Override public int compareTo(Pair o) { if (first!=o.first)return first-o.first; return second-o.second; } } class Tempo { int first,second,third; public Tempo(int first, int second, int third) { this.first = first; this.second = second; this.third = third; } }
Java
["5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20"]
1 second
["5\n101\n2\n2\n60"]
NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.
Java 11
standard input
[ "implementation" ]
e5fac4a1f3a724234990fe758debc33f
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).
900
For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.
standard output
PASSED
415290d7a17114291b404a73f49e3532
train_002.jsonl
1585924500
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.
256 megabytes
import java.util.*; public class Main{ public static void main( String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while(t-- > 0) { int n = scan.nextInt(); int x = scan.nextInt(); int a[] = new int[n+1]; int f[] = new int[252]; for(int i=0;i<200;i++) f[i] = 0; for(int i=0;i<n;i++) { a[i] = scan.nextInt(); f[a[i]]++; } int ans = 0; for(int i=1;i<252;i++) { if(f[i] == 0){ if(x>0) { x--; f[i]++; } else break; } ans++; } System.out.println(ans); } } }
Java
["5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20"]
1 second
["5\n101\n2\n2\n60"]
NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.
Java 11
standard input
[ "implementation" ]
e5fac4a1f3a724234990fe758debc33f
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).
900
For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.
standard output
PASSED
4b2b273ee419e4ef9ec606600fb24495
train_002.jsonl
1585924500
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.
256 megabytes
import java.util.*; public class ranking { public static void main(String[] args){ Scanner myScanner = new Scanner(System.in); int t = myScanner.nextInt(); for (int i = 0; i < t; i++) { int n = myScanner.nextInt(); int x = myScanner.nextInt(); boolean[] arr = new boolean[n+x]; for (int j = 0; j < n; j++) { int a = myScanner.nextInt(); if (a > n+x) { continue; } arr[a-1] = true; } int j = 0; for (; j < arr.length; j++) { if (!arr[j]) { if (x < 1) { break; } x--; } } System.out.println(j); } } }
Java
["5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20"]
1 second
["5\n101\n2\n2\n60"]
NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.
Java 11
standard input
[ "implementation" ]
e5fac4a1f3a724234990fe758debc33f
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).
900
For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.
standard output
PASSED
ccd3b511930d3bab321e514ad07d5d10
train_002.jsonl
1585924500
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.
256 megabytes
import java.util.HashSet; import java.util.Scanner; public class codeforces { public static void main(String[] args) { Scanner scn=new Scanner(System.in); int t=scn.nextInt(); StringBuilder sb=new StringBuilder(); while(t-->0){ int n=scn.nextInt(); int x=scn.nextInt(); HashSet<Integer>hm=new HashSet<>(); int a[]=new int[n]; for(int i=0;i<n;i++){ a[i]=scn.nextInt(); hm.add(a[i]); } int ct=0; while(x>0){ ct++; if(!hm.contains(ct)){ x--; } } ct++; while(hm.contains(ct)){ ct++; } sb.append(ct-1+"\n"); } System.out.println(sb); } }
Java
["5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20"]
1 second
["5\n101\n2\n2\n60"]
NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.
Java 11
standard input
[ "implementation" ]
e5fac4a1f3a724234990fe758debc33f
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).
900
For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.
standard output
PASSED
9d83e1db0dfcef37906384105ab7412d
train_002.jsonl
1585924500
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while (t-- > 0) { int n = scan.nextInt(); int x = scan.nextInt(); boolean[] arr = new boolean[206]; for(int i=0;i<n;i++) { arr[scan.nextInt()] = true; } int i=1; for(;i<=205 && x > 0;i++) { if(!arr[i]) { x--; if(x == 0) { if(i<= 205 && arr[i+1]) { i++; while (i <= 205 && arr[i]) { i++; } if(i<=205) { i--; } } break; } } } System.out.println(i); } } }
Java
["5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20"]
1 second
["5\n101\n2\n2\n60"]
NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.
Java 11
standard input
[ "implementation" ]
e5fac4a1f3a724234990fe758debc33f
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).
900
For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.
standard output
PASSED
815efd957aba8cadd9d82740bd6aed5d
train_002.jsonl
1585924500
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.
256 megabytes
import java.util.*;import java.io.*;import java.math.*; public class RankingCollection { public static void process()throws IOException { int n = ni(), x = ni(); int[] a = nai(n); TreeSet<Integer> st = new TreeSet<Integer>(); for(int i : a) st.add(i); //pn(st); int ans = 0; int c = 1; while(x > 0){ if(st.contains(c)) ans++; else { x--; ans++; } c++; } for(int i = ans+1; ; i++){ if(st.contains(i)) ans++; else break; } pn(ans); } static AnotherReader sc; static PrintWriter out; public static void main(String[]args)throws IOException { boolean oj = System.getProperty("ONLINE_JUDGE") != null; if(oj){sc=new AnotherReader();out=new PrintWriter(System.out);} else{sc=new AnotherReader(100);out=new PrintWriter("output.txt");} long s = System.currentTimeMillis(); int t=1; t=ni(); while(t-->0) {process();} out.flush(); System.err.println(System.currentTimeMillis()-s+"ms"); out.close(); } static void pn(Object o){out.println(o);} static void p(Object o){out.print(o);} static void pni(Object o){out.println(o);out.flush();} static int ni()throws IOException{return sc.nextInt();} static long nl()throws IOException{return sc.nextLong();} static double nd()throws IOException{return sc.nextDouble();} static String nln()throws IOException{return sc.nextLine();} static int[] nai(int N)throws IOException{int[]A=new int[N];for(int i=0;i!=N;i++){A[i]=ni();}return A;} static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;} static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} static long power(long k, long c, long mod){ long y = 1; while(c > 0){ if(c%2 == 1) y = y * k % mod; c = c/2; k = k * k % mod; } return y; } static final Random random=new Random(); static void ruffleSort(int[] a) { int n = a.length; 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 AnotherReader{BufferedReader br; StringTokenizer st; AnotherReader()throws FileNotFoundException{ br=new BufferedReader(new InputStreamReader(System.in));} AnotherReader(int a)throws FileNotFoundException{ br = new BufferedReader(new FileReader("input.txt"));} String next()throws IOException{ while (st == null || !st.hasMoreElements()) {try{ st = new StringTokenizer(br.readLine());} catch (IOException e){ e.printStackTrace(); }} return st.nextToken(); } int nextInt() throws IOException{ return Integer.parseInt(next());} long nextLong() throws IOException {return Long.parseLong(next());} double nextDouble()throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException{ String str = ""; try{ str = br.readLine();} catch (IOException e){ e.printStackTrace();} return str;}} ///////////////////////////////////////////////////////////////////////////////////////////////////////////// }
Java
["5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20"]
1 second
["5\n101\n2\n2\n60"]
NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.
Java 11
standard input
[ "implementation" ]
e5fac4a1f3a724234990fe758debc33f
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).
900
For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.
standard output
PASSED
cae767b057639e6e5db8c7cddb93bda0
train_002.jsonl
1585924500
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.
256 megabytes
import java.io.IOException; import java.io.InputStreamReader; import java.io.BufferedReader; import java.util.*; import java.math.*; import java.io.*; import java.text.*; import java.math.BigInteger; public class Main { //Life's a bitch public static boolean[] sieve(long n) { boolean[] prime = new boolean[(int)n+1]; Arrays.fill(prime,true); prime[0] = false; prime[1] = false; long m = (long)Math.sqrt(n); for(int i=2;i<=m;i++) { if(prime[i]) { for(int k=i*i;k<=n;k+=i) { prime[k] = false; } } } return prime; } static long GCD(long a,long b) { if(b==0) { return a; } return GCD(b,a%b); } static long CountCoPrimes(long n) { long res = n; for(int i=2;i*i<=n;i++) { if(n%i==0) { while(n%i==0) { n/=i; } res-=res/i; } } if(n>1) { res-=res/n; } return res; } static boolean prime(int n) { for(int i=2;i*i<=n;i++) { if(n%i==0) { return false; } } return true; } public static void main(String[] args) throws IOException { new Main().run(); } static void reverse(long[] a,int start,int end) { while(start<end) { long temp = a[start]; a[start] = a[end]; a[end] = temp; start++; end--; } } static long LCM(long a,long b) { return (a*b)/GCD(a,b); } Scanner in = new Scanner(System.in); // static int[] arr,tree[],lev; void run() throws IOException { int t = ni(); while(t-->0) { int n = ni(); int x = ni(); ArrayList<Integer> arr = new ArrayList<Integer>(); for(int i=0;i<n;i++) { int p = ni(); if(!arr.contains(p)) { arr.add(p); } } int i=1; while(x>0){ if(!arr.contains(i)) { arr.add(i); x--; } i++; } Collections.sort(arr); int j=0; for(;j<arr.size()-1;j++){ if(arr.get(j+1)-arr.get(j)!=1) { break; } } printL(j+1); } } //xor range query static long xor(long n) { if(n%4==0) { return n; } if(n%4==1) { return 1; } if(n%4==2) { return n+1; } return 0; } static long xor(long a,long b) { return xor(b)^xor(a-1); } void printL(long a) { System.out.println(a); } void printS(String s) { System.out.println(s); } void printD(Double d) { System.out.println(d); } static void swap(char[] c,int a,int b) { char t = c[a]; c[a] = c[b]; c[b] = t; } static long max(long n,long m) { return Math.max(n,m); } static long min(long n,long m) { return Math.min(n,m); } double nd() throws IOException { return Double.parseDouble(in.next()); } int ni() throws IOException { return Integer.parseInt(in.next()); } long nl() throws IOException { return Long.parseLong(in.next()); } String si() throws IOException { return in.next(); } static long abs(long n) { return Math.abs(n); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} 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 boolean ready() throws IOException {return br.ready();} } } class Pair implements Comparable<Pair> { int x,y; public Pair(int x,int y) { this.x = x; this.y = y; } public int compareTo(Pair o) { return this.y-o.y; } }
Java
["5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20"]
1 second
["5\n101\n2\n2\n60"]
NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.
Java 11
standard input
[ "implementation" ]
e5fac4a1f3a724234990fe758debc33f
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).
900
For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.
standard output
PASSED
8da20ad03a63c1695785a53470c56d3f
train_002.jsonl
1585924500
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { private static final boolean N_CASE = true; private void solve() { int n = sc.nextInt(); int x = sc.nextInt(); List<Integer> a = sc.nextList(n); Collections.sort(a); int ans = 0; for (int i = 0; i < n; i++) { int add = a.get(i) - ans - 1; if (add >= 0 && x >= add) { x -= add; ans = a.get(i); } } ans += x; out.println(ans); } private void run() { int T = N_CASE ? sc.nextInt() : 1; for (int t = 0; t < T; ++t) { solve(); } } private static MyWriter out; private static MyScanner sc; private static class MyScanner { BufferedReader br; StringTokenizer st; private 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()); } int[] nextArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } List<Integer> nextList(int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(nextInt()); } return list; } } private static class MyWriter extends PrintWriter { private MyWriter(OutputStream outputStream) { super(outputStream); } void printArray(int[] a) { for (int i = 0; i < a.length; ++i) { print(a[i]); print(i == a.length - 1 ? '\n' : ' '); } } void printlnArray(int[] a) { for (int v : a) { println(v); } } void printList(List<Integer> list) { for (int i = 0; i < list.size(); ++i) { print(list.get(i)); print(i == list.size() - 1 ? '\n' : ' '); } } void printlnList(List<Integer> list) { list.forEach(this::println); } } public static void main(String[] args) { out = new MyWriter(new BufferedOutputStream(System.out)); sc = new MyScanner(); new Main().run(); out.close(); } }
Java
["5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20"]
1 second
["5\n101\n2\n2\n60"]
NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.
Java 11
standard input
[ "implementation" ]
e5fac4a1f3a724234990fe758debc33f
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).
900
For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.
standard output
PASSED
e6a8d7e4a696f8dcd35b150f78232e48
train_002.jsonl
1585924500
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Q1 { public static long solver(Scanner sc) { return 0; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); //long val = solver(sc); int t = sc.nextInt(); for(int tt=0;tt<t;tt++){ int n =sc.nextInt(); int x =sc.nextInt(); int[] a = new int[n]; for(int i=0;i<n;i++){ a[i] = sc.nextInt(); } Arrays.sort(a); int count =0; int currentval = 0; int lasti = n; for(int i=0;i<n;i++){ int val=a[i]; int sub=0; if(val==currentval) continue; sub=val-currentval-1; if(x>sub){ count = count + sub+1; x=x-sub; } else{ if(currentval+x+1==val) count+=1; count = count+x; x=0; lasti =i; break; } currentval = val; } for(int i=lasti;i<n;i++){ if(a[i]-count==1) count+=1; else if(count!=a[i]) break; } System.out.println(count+x); } } }
Java
["5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20"]
1 second
["5\n101\n2\n2\n60"]
NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.
Java 11
standard input
[ "implementation" ]
e5fac4a1f3a724234990fe758debc33f
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).
900
For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.
standard output
PASSED
0f3d49528ba3cab87873cd3a8da7e0a6
train_002.jsonl
1585924500
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Class1 { public static int resolver(int[]dizi,int contest_sayisi) { boolean flag = false; int sonuc = 0; while(contest_sayisi>0) { for(int i=1;;i++) { flag =false; for(int j=0;j<dizi.length;j++) { if(dizi[j]==i) { flag = true; break; } } if(!flag) { contest_sayisi--; } if(contest_sayisi==0) { sonuc = i; for(int n=0;n<dizi.length;n++) { if(dizi[n]-sonuc == 1) { sonuc = dizi[n]; n = -1 ; } } // sonuc = i; return sonuc; } } } return sonuc; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int test_case = sc.nextInt(); int dizi_boyutu ; int kalan_contest ; int []dizi; int sonuclar[] = new int[test_case]; for(int i=0;i<test_case;i++) { dizi_boyutu = sc.nextInt(); kalan_contest = sc.nextInt(); dizi = new int[dizi_boyutu]; for(int j=0;j<dizi_boyutu;j++) { dizi[j]= sc.nextInt(); } sonuclar[i] = resolver(dizi,kalan_contest); } for(int i=0;i<sonuclar.length;i++) { System.out.println(sonuclar[i]); } /* int[] dizi = {43, 92, 90, 52 ,17 ,33 ,53, 16 ,33 ,66 ,75 ,79 ,79, 86 ,88 ,10 ,81 ,24 ,32 ,89 ,32 ,49 ,89 ,73 ,67 ,38 ,79 ,38 ,55 ,64 ,7 ,86 ,77 ,33 ,36 , 36 ,83, 94, 17, 46, 58, 75, 94, 76, 93, 65, 2, 87 ,93, 9 ,18, 11, 8 ,58 ,9 ,45 ,35 ,62 ,22 ,97 ,24 ,42, 50 ,13 ,80 , 78, 75, 92, 5 ,82 ,17 ,40 ,77 ,4 ,41, 57, 65, 74, 85, 95, 71 ,57 ,26 ,44 ,1, 5, 38, 24 ,100 ,58 ,14 ,3 ,94, 3 ,49, 51, 12 ,70, 71}; System.out.println(resolver(dizi,1)); Arrays.sort(dizi); System.out.println(Arrays.toString(dizi)); */ } } //43 92 90 52 17 33 53 16 33 66 75 79 79 86 88 10 81 24 32 89 32 49 89 73 67 38 79 38 55 64 7 86 77 33 36 36 83 94 17 46 58 75 94 76 93 65 2 87 93 9 18 11 8 58 9 45 35 62 22 97 24 42 50 13 80 78 75 92 5 82 17 40 77 4 41 57 65 74 85 95 71 57 26 44 1 5 38 24 100 58 14 3 94 3 49 51 12 70 71
Java
["5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20"]
1 second
["5\n101\n2\n2\n60"]
NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.
Java 11
standard input
[ "implementation" ]
e5fac4a1f3a724234990fe758debc33f
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).
900
For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.
standard output
PASSED
07eb4ab47a21aed0bd2efec7cd909cbb
train_002.jsonl
1585924500
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.
256 megabytes
import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class a { public static void main(String[] args) { Scanner s=new Scanner(System.in); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); int x=s.nextInt(); int arr[]=new int[n]; //System.out.println(); HashMap<Integer,Integer>hm=new HashMap<>(); for(int i=0;i<n;i++) { arr[i]=s.nextInt(); } for(int i=0;i<n;i++) { hm.put(arr[i],1); } Arrays.sort(arr); int ans=0,i=1; int c=x; for( i=1;i<=arr[n-1]+c;i++) { if(!hm.containsKey(i)) { if(x==0) { //ans=i; break; } x--; } ans=i; } System.out.println(ans); } } }
Java
["5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20"]
1 second
["5\n101\n2\n2\n60"]
NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.
Java 11
standard input
[ "implementation" ]
e5fac4a1f3a724234990fe758debc33f
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).
900
For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.
standard output
PASSED
63893ba5f36308962105dffbee214e90
train_002.jsonl
1585924500
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.util.StringTokenizer; import java.util.Scanner; import java.lang.*; import java.io.*; import java.util.*; import java.lang.Integer; import java.util.HashMap; public class Main { // driver function to test the above functions public static void main(String args[]) throws IOException { Reader.init(System.in); int t = Reader.nextInt(); for (int i=0; i<t; i++) { int n = Reader.nextInt(); int x= Reader.nextInt(); boolean[] arr = new boolean[202]; for (int j=0; j<n; j++){ arr[Reader.nextInt()-1] = true; } int ans=0; for (int j=0; j<202; j++){ if (arr[j]==true){ ans++; } else { if (x>0){ x--; ans++; } else { System.out.println(ans); break; } } } } } } class Node{ char val; Node nxt = null; Node prev=null; } class solu{ } /** Class for buffered reading int and double values */ class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary 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() ); } static String line() throws IOException { return reader.readLine(); } }
Java
["5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20"]
1 second
["5\n101\n2\n2\n60"]
NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.
Java 11
standard input
[ "implementation" ]
e5fac4a1f3a724234990fe758debc33f
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).
900
For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.
standard output
PASSED
d3110325f168ae12fdfb38b528af2ba0
train_002.jsonl
1585924500
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.
256 megabytes
import javax.management.MBeanRegistration; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.util.StringTokenizer; import java.util.Scanner; import java.lang.*; import java.io.*; import java.util.*; import java.lang.Integer; import java.util.HashMap; public class Main { // driver function to test the above functions public static void main(String args[]) throws IOException { Reader.init(System.in); int t = Reader.nextInt(); for (int i=0; i<t; i++){ int n = Reader.nextInt(); int x = Reader.nextInt(); boolean[] arr = new boolean[202]; for (int j=0; j<n; j++){ int index = Reader.nextInt(); arr[index-1] = true; } int ans = 0; for (int j=0; j<202; j++){ boolean val = arr[j]; if (val==false&&x>0){ arr[j] = true; x--; } else if (val==false&&x<=0) { break; } ans = j; } System.out.println(ans+1); } } } class Node{ char val; Node nxt = null; Node prev=null; } class solu{ int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } } /** Class for buffered reading int and double values */ class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary 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() ); } static String line() throws IOException { return reader.readLine(); } }
Java
["5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20"]
1 second
["5\n101\n2\n2\n60"]
NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.
Java 11
standard input
[ "implementation" ]
e5fac4a1f3a724234990fe758debc33f
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).
900
For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.
standard output
PASSED
1415fc647161dfd7ef7921396ccd2718
train_002.jsonl
1585924500
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class cp { static String removedup(char str[], int n) { int index = 0; for (int i = 0; i < n; i++) { int j; for (j = 0; j < i; j++) { if (str[i] == str[j]) { break; } } if (j == i) { str[index++] = str[i]; } } return String.valueOf(Arrays.copyOf(str, index)); } public static void main( String[] args) { // // Scanner in = new Scanner(System.in); int s = in.nextInt(); for(int i = 0;i<s;i++) { int n = in.nextInt(); int x = in.nextInt(); int arr[] = new int[n]; for(int j = 0;j<n;j++) { arr[j]= in.nextInt(); } Arrays.sort(arr); int diff []= new int[n]; diff[0] = arr[0]-1; for(int k=0;k<n-1;k++) { if(arr[k+1]==arr[k]) { diff[k+1] = 0; } else { diff[k+1] = arr[k+1] - arr[k] -1; } } int c = 0; for(int y = 0;y<n;y++) { if(x>=diff[y]) { x = x- diff[y]; c = c+1; } else if(x<diff[y]) { break; } } if(c>0) { System.out.println(arr[c-1]+x); } else if(c==0) { System.out.println(x); } } }}
Java
["5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20"]
1 second
["5\n101\n2\n2\n60"]
NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.
Java 11
standard input
[ "implementation" ]
e5fac4a1f3a724234990fe758debc33f
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).
900
For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.
standard output
PASSED
cd1d249bba522fd4839a2ac089d4d3cc
train_002.jsonl
1585924500
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.
256 megabytes
import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { //solution start :-) Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int x = sc.nextInt(); int a[] = new int[n]; int fre[] = new int[220]; for(int i=0;i<n;i++) { a[i] = sc.nextInt(); fre[a[i]] = 1; } for(int i=1;i<220;i++) { if(x>0){ if(fre[i]==0) { fre[i] = 1; x--; } } else { break; } } int count = 0; for(int i=1;i<220;i++) { if(fre[i]==1) { count++; } else break; } System.out.println(count); } // solution end \(^-^)/ // | // / \ }}
Java
["5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20"]
1 second
["5\n101\n2\n2\n60"]
NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.
Java 11
standard input
[ "implementation" ]
e5fac4a1f3a724234990fe758debc33f
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).
900
For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.
standard output
PASSED
a865ea1482356dd6379de403b73c9e0f
train_002.jsonl
1585924500
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.
256 megabytes
import java.util.*; public class ranking{ public static void main (String args []){ Scanner sc = new Scanner(System.in); int numCases = sc.nextInt(); for(int i = 0; i < numCases; i++){ int contestDone= sc.nextInt(); int contestLeft = sc.nextInt(); boolean arr [] = new boolean[202]; for(int j = 0; j < contestDone; j++){ int num = sc.nextInt(); arr[num] = true; } int counter = 0; for(int x = 1;; x++){ if(arr[x] == false){ counter++; } if(counter > contestLeft){ System.out.println(x-1); break; } } } } }
Java
["5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20"]
1 second
["5\n101\n2\n2\n60"]
NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.
Java 11
standard input
[ "implementation" ]
e5fac4a1f3a724234990fe758debc33f
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).
900
For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.
standard output
PASSED
a321ecde9930e4ef0913b6eecf686d0c
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.util.Scanner; public class C { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int r = sc.nextInt(); int[] x = new int[n]; for(int i=0;i<n;i++) { x[i] = sc.nextInt(); } double[] pY = new double[n]; pY[0]=r; for(int i=1;i<n;i++) { pY[i] = ccc(x,pY,i,r); } for(int i=0;i<n;i++) { System.out.print(String.valueOf(pY[i])+" "); } } private static double ccc(int[] x,double[] pY,int c,int r) { double out=r; int rs = 4*r*r; for(int i=0;i<c;i++) { double ysq=rs - Math.pow(x[i]-x[c], 2); if(ysq>0) { out = Math.max(pY[i]+Math.sqrt(ysq),out); }else if(ysq ==0) { out = Math.max(pY[i], out); } } return out; } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
6dd83f8846dee2ad098dc84147ebc8bf
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
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; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Abhilash */ 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); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int r = in.nextInt(); int x[] = new int[n]; for (int i = 0; i < n; i++) x[i] = in.nextInt(); double y[] = new double[n]; y[0] = r; for (int i = 1; i < n; i++) { for (int j = i - 1; j >= 0; j--) { int delx = Math.abs(x[i] - x[j]); if (delx <= 2 * r) { y[i] = Math.max(y[i], y[j] + Math.sqrt(4 * r * r - delx * delx)); } } y[i] = Math.max(y[i], r); } for (int i = 0; i < n; i++) out.print(y[i] + " "); } } 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
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
cbec639d114b4b0a85151bce6d5a1c68
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.io.*; import java.util.*; public class C { String fileName = "C"; public double r; public double getY(double x2, double x1, double y1) { /*double y1 = y[i - 1]; double x1 = x[i - 1]; double x2 = x[i];*/ double C = y1 * y1 - 4D * r * r + (x2 - x1) * (x2 - x1); double D = 4D * y1 * y1 - 4D * C; double possibleY1 = (2D * y1 + Math.sqrt(D)) / 2D; double possibleY2 = (2D * y1 - Math.sqrt(D)) / 2D; if (possibleY1 > y1) return possibleY1; else return possibleY2; } public static class Pair implements Comparable<Pair> { public double y; public int ind; public Pair(double y, int ind) { this.y = y; this.ind = ind; } @Override public int compareTo(Pair o) { return Double.compare(o.y, y); } } public void solve() throws IOException { int n = nextInt(); r = nextDouble(); double[] x = new double[n]; double[] y = new double[n]; List<Pair> pairs = new ArrayList<>(n); for (int i = 0; i < n; i++) { x[i] = nextDouble(); } y[0] = r; pairs.add(new Pair(r, 0)); for (int i = 1; i < n; i++) { double ans = r; for (int j = 0; j < pairs.size(); j++) { Pair curPair = pairs.get(j); double curY = getY(x[i], x[curPair.ind], curPair.y); if (curY > ans) { ans = curY; } } pairs.add(new Pair(ans, i)); y[i] = ans; } for (int i = 0; i < n; i++) { out.printf("%.15f ", y[i]); } } public void run() { try { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } BufferedReader br; StringTokenizer in; PrintWriter out; public String nextToken() throws IOException { while (in == null || !in.hasMoreTokens()) { in = new StringTokenizer(br.readLine()); } return in.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public static void main(String[] args) throws IOException { Locale.setDefault(Locale.US); new C().run(); } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
77a30273d0a97635790733c5d519b62a
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.util.StringTokenizer; public class main { public static void main(String[] args) throws UnsupportedEncodingException, IOException { Reader.init(System.in); StringBuilder out = new StringBuilder(); int n = Reader.nextInt(), r = Reader.nextInt(), r2 = 2 * r, rs = r2 * r2, x[] = new int[n]; double[] y = new double[n]; for(int i = 0, j; i < n; i++) { x[i] = Reader.nextInt(); y[i] = r; for(j = 0; j < i; j++) if(Math.abs(x[i] - x[j]) <= r2) y[i] = Math.max(y[i], y[j] + Math.sqrt(rs - (x[i] - x[j]) * (x[i] - x[j]))); out.append(y[i]).append(' '); } out.setCharAt(out.length() - 1, '\n'); PrintWriter pw = new PrintWriter(System.out); pw.print(out); pw.close(); } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) throws UnsupportedEncodingException { reader = new BufferedReader( new InputStreamReader(input, "UTF-8")); tokenizer = new StringTokenizer(""); } static void init(String url) throws FileNotFoundException { reader = new BufferedReader(new FileReader(url)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static String nextLine() throws IOException { return reader.readLine(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
905ac5057c97f3d7b72a9a73c25a7c7b
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ //package javaapplication4; /** * * @author DINH */ 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; public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int r = in.nextInt(); int[] x = new int[n]; double[] y = new double[n]; for (int i = 0; i < n; ++i) { x[i] = in.nextInt(); double mx = r; for (int j = 0; j < i; ++j) { int d = Math.abs(x[j] - x[i]); if (d <= 2 * r) { mx = Math.max(mx, y[j] + Math.sqrt(4 * r * r - d * d)); } } if (i > 0) out.print(" "); out.print(String.format("%.10f", mx)); y[i] = mx; } out.println(); } } 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
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
dcaf07c57a1f3a254cf16d447fae8a43
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.awt.List; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Codeforces908C { public static ArrayList<Double> realHeights = new ArrayList<Double>(); public static ArrayList<Integer> heights = new ArrayList<>(); public static int n; public static int r; public static void calculateTheHeight(int newDisk, int update) { int distance = Math.abs(newDisk - update); // System.out.println(newDisk + " " + update + " " + distance); double newHeight = realHeights.get(newDisk) + Math.sqrt(2 * r * 2 * r - distance * distance); // System.out.println(update); if (realHeights.get(update) < newHeight) { realHeights.set(update, newHeight); } } public static void update(int i) { double height = realHeights.get(i); System.out.print(realHeights.get(i)+" "); for (int j = -2 * r; j <= 2 * r; j++) { if (i + j < 0) { } else if (j == 0) { } else { // System.out.println(i + j); calculateTheHeight(i, i + j); } // realHeights.set(i,realHeights.get(i)+2*r); // calculateTheHeight(i, i); } calculateTheHeight(i, i); // System.out.println(realHeights); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); n = scanner.nextInt(); r = scanner.nextInt(); scanner.nextLine(); for (int i = 0; i < n; i++) { heights.add(scanner.nextInt()); } int max = Collections.max(heights); for (int i = 0; i < max + 2 * r + 1; i++) { realHeights.add(new Double(r)); } for (int i = 0; i < n; i++) { // System.out.println("height" + "" + heights.get(i)); update(heights.get(i)); } } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
7b77c0b485bfadcab445000752e9acb6
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class C908{ void solve() { int n = ni(), r = ni(); int[] a = ia(n); double[] ys = new double[n]; for(int i=0;i<n;i++) { ys[i] = r; for(int j=0;j<i;j++) if(a[i] <= a[j] + 2 * r && a[i] >= a[j] - 2 * r) { double leg = Math.abs(a[j] - a[i]); double dy = Math.sqrt(4 * r * r - leg * leg) + ys[j]; ys[i] = Math.max(ys[i], dy); } } for(double d : ys) out.printf("%.14f ", d); } public static void main(String[] args){new C908().run();} private byte[] bufferArray = new byte[1024]; private int bufLength = 0; private int bufCurrent = 0; InputStream inputStream; PrintWriter out; public void run() { inputStream = System.in; out = new PrintWriter(System.out); solve(); out.flush(); } int nextByte() { if(bufLength == -1) throw new InputMismatchException(); if(bufCurrent >= bufLength) { bufCurrent = 0; try {bufLength = inputStream.read(bufferArray);} catch(IOException e) { throw new InputMismatchException();} if(bufLength <= 0) return -1; } return bufferArray[bufCurrent++]; } boolean isSpaceChar(int x) {return (x < 33 || x > 126);} boolean isDigit(int x) {return (x >= '0' && x <= '9');} int nextNonSpace() { int x; while((x=nextByte()) != -1 && isSpaceChar(x)); return x; } int ni() { long ans = nl(); if (ans >= Integer.MIN_VALUE && ans <= Integer.MAX_VALUE) return (int)ans; throw new InputMismatchException(); } long nl() { long ans = 0; boolean neg = false; int x = nextNonSpace(); if(x == '-') { neg = true; x = nextByte(); } while(!isSpaceChar(x)) { if(isDigit(x)) { ans = ans * 10 + x -'0'; x = nextByte(); } else throw new InputMismatchException(); } return neg ? -ans : ans; } String ns() { StringBuilder sb = new StringBuilder(); int x = nextNonSpace(); while(!isSpaceChar(x)) { sb.append((char)x); x = nextByte(); } return sb.toString(); } char nc() { return (char)nextNonSpace();} double nd() { return (double)Double.parseDouble(ns()); } char[] ca() { return ns().toCharArray();} char[][] ca(int n) { char[][] ans = new char[n][]; for(int i=0;i<n;i++) ans[i] = ca(); return ans; } int[] ia(int n) { int[] ans = new int[n]; for(int i=0;i<n;i++) ans[i] = ni(); return ans; } void db(Object... o) {System.out.println(Arrays.deepToString(o));} }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
49e38496406cf396455a2d58fbfa7cf7
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.io.*; import java.util.*; public class Main { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); int n = in.nextInt(); double r = in.nextInt(); double a[] = new double[n]; double b[] = new double[n]; for(int i=0; i<n; i++){ a[i] = in.nextInt(); b[i] = r; } b[0] = r; for(int i=1; i<n; i++){ for(int j=i-1; j>=0; j--){ if(4*r*r - (a[j]-a[i])*(a[j]-a[i])>=0){ b[i] = Math.max(b[i], Math.sqrt((4*r*r - (a[j]-a[i])*(a[j]-a[i]))) + b[j]); } } } for(int i=0; i<n; i++) System.out.print(b[i]+" "); } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
4a29520cd03e7aa5dc04c085e0af7574
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable; import jdk.management.cmm.SystemResourcePressureMXBean; import java.awt.*; import java.io.*; import java.lang.reflect.Array; import java.text.DecimalFormat; import java.util.*; import java.util.List; import java.math.*; public class Newbie { static InputReader sc = new InputReader(System.in); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { solver s = new solver(); int t = 1; while (t > 0) { s.solve(); t--; } out.close(); } /* static class descend implements Comparator<pair1> { public int compare(pair1 o1, pair1 o2) { if (o1.pop != o2.pop) return (int) (o1.pop - o2.pop); else return o1.in - o2.in; } }*/ static class InputReader { public BufferedReader br; public StringTokenizer token; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream), 32768); token = null; } public String next() { while (token == null || !token.hasMoreTokens()) { try { token = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return token.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } static class card { long a; int cnt; int i; public card(long a, int cnt, int i) { this.a = a; this.cnt = cnt; this.i = i; } } static class ascend implements Comparator<pair1> { public int compare(pair1 o1, pair1 o2) { return o1.in - o2.in; } } static class extra { static boolean v[] = new boolean[100001]; static List<Integer> l = new ArrayList<>(); static int t; static void shuffle(long a[]) { List<Long> l = new ArrayList<>(); for (int i = 0; i < a.length; i++) l.add(a[i]); Collections.shuffle(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static long gcd(long a, long b) { if (b == 0) return a; else return gcd(b, a % b); } static boolean valid(int i, int j, int r, int c) { if (i >= 0 && i < r && j >= 0 && j < c) return true; else return false; } static void seive() { for (int i = 2; i < 100001; i++) { if (!v[i]) { t++; l.add(i); for (int j = 2 * i; j < 100001; j += i) v[j] = true; } } } static int binary(long a[], long val, int n) { int mid = 0, l = 0, r = n - 1, ans = 0; while (l <= r) { mid = (l + r) >> 1; if (a[mid] == val) { r = mid - 1; ans = mid; } else if (a[mid] > val) r = mid - 1; else { l = mid + 1; ans = l; } } return (ans + 1); } static long fastexpo(int x, int y) { long res = 1; while (y > 0) { if ((y & 1) == 1) { res *= x; } y = y >> 1; x = x * x; } return res; } static long lfastexpo(int x, int y, int p) { long res = 1; x = x % p; while (y > 0) { if ((y & 1) == 1) { res = (res * x) % p; } y = y >> 1; x = (x * x) % p; } return res; } } static class pair { int a; int b; public pair(int a, int i) { this.a = a; this.b = i; } } static class pair1 { pair p; int in; public pair1(pair a, int n) { this.p = a; this.in = n; } } static long m = (long) 1e9 + 7; static class solver { void solve() { int n = sc.nextInt(); int r = sc.nextInt(); double ans[] = new double[n]; int x[] = new int[n]; for (int i = 0; i < n; i++) { x[i] = sc.nextInt(); if (i == 0) ans[0] = r * 1.0; else { if (x[i] == x[i - 1]) ans[i] = ans[i - 1] + 2 * r; else { int in = -1; double maxi = Long.MIN_VALUE; for (int j = i - 1; j >= 0; j--) { if (x[i] > x[j]) { if ((x[i] - r) <= (x[j] + r)) { in = j; } } else { if ((x[i] + r) >= (x[j] - r)) { in = j; } } if (in != -1) { maxi = Math.max(maxi, ans[in] + getval(Math.abs(x[in] - x[i]), r)); } } if (in == -1) ans[i] = r * 1.0; else { ans[i] = maxi; } } } } DecimalFormat df = new DecimalFormat("0.000000000000"); for (int i = 0; i < n; i++) { System.out.print(df.format(ans[i]) + " "); } System.out.println(); } double getval(int base, int r) { return Math.sqrt((4 * r * r) - (base * base)); } } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
5d7ad6f4842864ec303a3b3ca0b76c22
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class C_2017 { InputStream is; PrintWriter out; int n; long a[]; private boolean oj = System.getProperty("ONLINE_JUDGE") != null; void solve() { int n = ni(); int r = ni(); int arr[] = na(n); double center[] = new double[n]; // center[0]=2; for (int i = 0; i < n; i++) { center[i] = r; for (int j = 0; j < i; j++) { if ((4 * r * r - (arr[i] - arr[j]) * (arr[i] - arr[j])) >= 0) center[i] = Math.max(center[i], Math.sqrt(4 * r * r - (arr[i] - arr[j]) * (arr[i] - arr[j])) + center[j]); } out.print(center[i] + " "); } } void run() throws Exception { String INPUT = "C:\\Users\\Admin\\Desktop\\input.txt"; is = oj ? System.in : new FileInputStream(INPUT); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception { new Thread(null, new Runnable() { public void run() { try { new C_2017().run(); } catch (Exception e) { e.printStackTrace(); } } }, "1", 1 << 26).start(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private void tr(Object... o) { if (!oj) System.out.println(Arrays.deepToString(o)); } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
4671528235256f64662ef39f6d02a5a9
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
//package practice; import java.io.*; import java.math.*; import java.util.*; /** * * @author Tushar 29 Dec, 2017 */ public class C { public static void main(String args[]) { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int t = 1; Task solver = new Task(); solver.solve(t, in, out); out.flush(); out.close(); } static class Task { long mod = 1000000007; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int r = in.nextInt(); int[] dx = new int[n]; double[] dy = new double[n]; for (int i = 0; i < n; i++) { dx[i] = in.nextInt(); double max = Integer.MIN_VALUE; for (int j = i - 1; j >= 0; j--) { if (Math.abs(dx[i] - dx[j]) <= 2 * r) { double temp = Math.sqrt(4 * r * r - Math.pow(dx[i] - dx[j], 2)) + dy[j]; if (temp > max) { max = temp; } } } dy[i] = max < 0 ? r : max; } for (int i = 0; i < n; i++) { System.out.printf("%.12f ", dy[i]); } } int LOG2(long item) { int count = 0; while (item > 1) { item >>= 1; count++; } return count; } long power(long x, long n) { if (n <= 0) { return 1; } long y = power(x, n / 2); if ((n & 1) == 1) { return (((y * y) % mod) * x) % mod; } return (y * y) % mod; } long gcd(long a, long b) { a = Math.abs(a); b = Math.abs(b); return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).longValue(); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) { throw new InputMismatchException(); } if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String next() { return readString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
6e9c2f8d6820a4b0a156b7a1b1fe8c21
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.util.Scanner; public class C { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); int n = input.nextInt(); int r = input.nextInt(); int coordinatesX[] = new int[n]; for(int i = 0;i < n;i++) coordinatesX[i] = input.nextInt(); double coordinatesY[] = new double[n]; coordinatesY[0] = r; for(int i = 1;i < n;i++){ int sign = 0; for(int j = 0;j < i;j++) if((coordinatesX[i] + r >= coordinatesX[j] - r && coordinatesX[i] + r <= coordinatesX[j] + r) || (coordinatesX[i] - r >= coordinatesX[j] - r && coordinatesX[i] - r <= coordinatesX[j] + r)){ sign = 1; coordinatesY[i] = Math.max(coordinatesY[i], coordinatesY[j] + Math.sqrt(4 * Math.pow(r, 2) - Math.pow(coordinatesX[i] - coordinatesX[j],2))); } if(sign == 0) coordinatesY[i] = r; } for(int i = 0;i < n;i++) System.out.print(coordinatesY[i] + " "); } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
79c8426568b57a8b158ba12c740a86e3
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
//package codeforces.GoodBye17; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class C { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); int r = sc.nextInt(); int[] x = new int[n]; for (int i = 0; i < n; i++) x[i] = sc.nextInt(); double[] y = new double[n]; for (int i = 0; i < x.length; i++) { int ind = -1; double hi = -10; for (int j = 0; j < i; j++) { if(Math.abs(x[i]-x[j]) > 2*r) continue; int dx = Math.abs(x[i]-x[j]); hi = Math.max(y[j] + Math.sqrt(4*r*r - dx*dx), hi); ind = 1; } if(ind == -1) y[i] = r; else { y[i] = hi; } } for (int j = 0; j < y.length; j++) { pw.print(y[j] + " "); } pw.flush(); pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File((s)))); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
5891512d409e8bf0158cfec1b3e237da
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import javafx.scene.layout.Priority; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; public class C { int N,r; int[] x; double[] y; private void solve() { N = nextInt(); r = nextInt(); x = new int[N]; y = new double[N]; for(int i = 0;i < N;i++) { x[i] = nextInt(); y[i] = r; } for(int i = 0;i < N;i++) { double maxY = r; for(int j = 0;j < i;j++) { int distanceX = Math.abs(x[i] - x[j]); if (distanceX <= r * 2) { maxY = Math.max(maxY,y[j] + Math.sqrt((r * 2) * (r * 2) - distanceX * distanceX)); } } y[i] = maxY; } for(int i = 0;i < N;i++) { if (i != 0) { out.print(" "); } out.print(String.format("%.09f",y[i])); } out.println(); } public static void main(String[] args) { out.flush(); new C().solve(); out.close(); } /* Input */ private static final InputStream in = System.in; private static final PrintWriter out = new PrintWriter(System.out); private final byte[] buffer = new byte[2048]; private int p = 0; private int buflen = 0; private boolean hasNextByte() { if (p < buflen) return true; p = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) return false; return true; } public boolean hasNext() { while (hasNextByte() && !isPrint(buffer[p])) { p++; } return hasNextByte(); } private boolean isPrint(int ch) { if (ch >= '!' && ch <= '~') return true; return false; } private int nextByte() { if (!hasNextByte()) return -1; return buffer[p++]; } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = -1; while (isPrint((b = nextByte()))) { sb.appendCodePoint(b); } return sb.toString(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
68a4c0a455688e39921fe0e7e88813d5
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import javafx.scene.layout.Priority; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; public class C { int N,r; int[] x; double[] y; private void solve() { N = nextInt(); r = nextInt(); x = new int[N]; y = new double[N]; for(int i = 0;i < N;i++) { x[i] = nextInt(); } for(int i = 0;i < N;i++) { double maxY = r; for(int j = 0;j < i;j++) { int distanceX = Math.abs(x[i] - x[j]); if (distanceX <= r * 2) { maxY = Math.max(maxY,y[j] + Math.sqrt((r * 2) * (r * 2) - distanceX * distanceX)); } } y[i] = maxY; } for(int i = 0;i < N;i++) { if (i != 0) { out.print(" "); } out.print(String.format("%.09f",y[i])); } out.println(); } public static void main(String[] args) { out.flush(); new C().solve(); out.close(); } /* Input */ private static final InputStream in = System.in; private static final PrintWriter out = new PrintWriter(System.out); private final byte[] buffer = new byte[2048]; private int p = 0; private int buflen = 0; private boolean hasNextByte() { if (p < buflen) return true; p = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) return false; return true; } public boolean hasNext() { while (hasNextByte() && !isPrint(buffer[p])) { p++; } return hasNextByte(); } private boolean isPrint(int ch) { if (ch >= '!' && ch <= '~') return true; return false; } private int nextByte() { if (!hasNextByte()) return -1; return buffer[p++]; } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = -1; while (isPrint((b = nextByte()))) { sb.appendCodePoint(b); } return sb.toString(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
423de6f421fd8e3f12801faf4c706ede
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import javafx.scene.layout.Priority; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; public class C { int N,r; int[] x; ArrayList<Point> points; private class Point implements Comparable<Point>{ double y,x; public Point(double y,double x) { this.y = y; this.x = x; } public int compareTo(Point point) { return Double.compare(point.y,this.y); } } private double dis(Point p,double y,double x) { return (p.y - y) * (p.y - y) + (p.x - x) * (p.x - x); } private boolean isHit(double y,double x) { for(Point p : points) { if (dis(p,y,x) <= (r * 2) * (r * 2)) { return true; } } return false; } private double getY(PriorityQueue<Point> q,double x) { double lowY = r; while(q.size() > 0) { Point p = q.poll(); if (isHit(p.y,x)) { lowY = Math.max(lowY,p.y); } } double low = lowY; double high = 10000000; for(int j = 0;j < 100;j++) { double mid = (high + low) / 2; if (isHit(mid,x)) { low = mid; } else { high = mid; } } return high; } private void solve() { N = nextInt(); r = nextInt(); x = new int[N]; for(int i = 0;i < N;i++) { x[i] = nextInt(); } PriorityQueue<Point> pq = new PriorityQueue<>(); points = new ArrayList<>(); for(int i = 0;i < N;i++) { PriorityQueue<Point> tmp = new PriorityQueue<>(); tmp.addAll(pq); double minY = getY(tmp,x[i]); pq.add(new Point(minY,x[i])); points.add(new Point(minY,x[i])); } for(int i = 0;i < points.size();i++) { if (i != 0) { out.print(" "); } out.print(String.format("%.09f",points.get(i).y)); } out.println(); } public static void main(String[] args) { out.flush(); new C().solve(); out.close(); } /* Input */ private static final InputStream in = System.in; private static final PrintWriter out = new PrintWriter(System.out); private final byte[] buffer = new byte[2048]; private int p = 0; private int buflen = 0; private boolean hasNextByte() { if (p < buflen) return true; p = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) return false; return true; } public boolean hasNext() { while (hasNextByte() && !isPrint(buffer[p])) { p++; } return hasNextByte(); } private boolean isPrint(int ch) { if (ch >= '!' && ch <= '~') return true; return false; } private int nextByte() { if (!hasNextByte()) return -1; return buffer[p++]; } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = -1; while (isPrint((b = nextByte()))) { sb.appendCodePoint(b); } return sb.toString(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
a27050964084e063ee8f7c085ce81752
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.io.InputStream; /** * 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); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); long r = in.nextInt(); int x[] = in.parseInt1D(n); ArrayList<Circle> circles = new ArrayList<>(); circles.add(new Circle(x[0], r, 0)); for (int i = 1; i < x.length; i++) { double maxHeight = r; for (Circle circle : circles) { if (Math.abs(circle.x - x[i]) <= 2 * r) { long lenSide = Math.abs(circle.x - x[i]); long lenSq = (4 * r * r) - lenSide * lenSide; double height = circle.y + Math.sqrt(lenSq); maxHeight = Math.max(maxHeight, height); //break ; } } circles.add(new Circle(x[i], maxHeight, i)); } //Collections.sort(circles, Comparator.comparingInt(c -> c.index)); for (int i = 0; i < circles.size(); i++) { out.print(circles.get(i).y + " "); } } class Circle { int x; double y; int index; Circle(int x, double y, int index) { this.x = x; this.y = y; this.index = index; } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int nextInt() { return readInt(); } public int[] parseInt1D(int n) { int r[] = new int[n]; for (int i = 0; i < n; i++) { r[i] = nextInt(); } return r; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
56818e79b3ef709d925ad30d06fc0c4a
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
/*package whatever //do not write package name here */ import java.io.*; import java.util.* ; public class Main { public static int mod(int n) { if(n<0) { return -n ; } return n ; } public static void main (String[] args) { Scanner s=new Scanner(System.in) ; int n=s.nextInt() ; int r=s.nextInt() ; int arr[]=new int[n] ; for(int i=0;i<arr.length;i++) { arr[i]=s.nextInt() ; } double ans[]=new double[n] ; ans[0]=r*1.0 ; for(int i=1;i<n;i++) { for(int j=0;j<i;j++) { double y=4*r*r ; int diff=mod(arr[i]-arr[j]) ; double ans1=0.0 ; if(diff>2*r) { ans1=r*1.0 ; } else { double d=diff*diff ; y=y-d ; ans1= Math.pow(y,0.5)+ans[j] ; } if(ans1>ans[i]) { ans[i]=ans1 ; } } } for(int i=0;i<ans.length;i++) { System.out.printf("%.9f",ans[i]) ; System.out.print(" "); } } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
53db7192fa30fd40f741635fffa48af4
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author rishus23 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int r = in.nextInt(); int[] arr = in.nextIntArray(n); double[] h = new double[n]; Arrays.fill(h, -1); int[] mark = new int[5000]; Arrays.fill(mark, -1); for (int i = 0; i < n; i++) { double x = -1; //int id = -1; double len = 0; ArrayList<Integer> ar = new ArrayList<Integer>(); for (int j = arr[i] - r + 2000; j <= arr[i] + r + 2000; j++) { if (mark[j] != -1) { //x = Math.max(h[mark[j]], x); x = 0; ar.add(mark[j]); // if(h[mark[j]]>x) // { // x = h[mark[j]]; // id = mark[j]; // } } } if (x == -1) { h[i] = r; } else { for (int id : ar) { int d = Math.abs(arr[i] - arr[id]); len = h[id] + Math.sqrt((4 * (Math.pow(r, 2)) - (Math.pow(d, 2)))); h[i] = Math.max(h[i], len); } } for (int j = arr[i] - r + 2000; j <= arr[i] + r + 2000; j++) mark[j] = i; } for (double k : h) out.print(k + " "); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void close() { writer.close(); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public 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 int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
96f080bb19c7f1710c0bcd57a42e1ff3
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.io.*; import java.util.*; public class CODEFORCES { @SuppressWarnings("rawtypes") static InputReader in; static PrintWriter out; public static double distance(int a, int b, int x, int y) { return Math.sqrt(((long) (a - x) * (long) (a - x)) + ((long) (b - y) * (long) (b - y))); } static void solve() { int n = in.ni(), r = in.ni(); int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = in.ni(); double ans[] = new double[n]; Arrays.fill(ans, r); for (int i = 1; i < n; i++) { for (int j = i - 1; j >= 0; j--) { if (Math.abs(arr[i] - arr[j]) <= 2 * r) ans[i] = Math.max(ans[j] + Math.sqrt(4 * r * r - (arr[i] - arr[j]) * (arr[i] - arr[j])), ans[i]); } } for (int i = 0; i < n; i++) out.print(ans[i] + " "); } @SuppressWarnings("rawtypes") static void soln() { in = new InputReader(System.in); out = new PrintWriter(System.out); solve(); out.flush(); } static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } public static void main(String[] args) { new Thread(null, new Runnable() { public void run() { try { soln(); } catch (Exception e) { e.printStackTrace(); } } }, "1", 1 << 26).start(); } // To Get Input // Some Buffer Methods static class InputReader<SpaceCharFilter> { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int ni() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nl() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public long[] nextLongArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nl(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(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; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
49aa57597ee0d974a689df0656737b80
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; import java.math.*; public class Prac{ static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int ni() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nl() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nia(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public String rs() { 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 isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } public static class Key { private final int x; private final int y; public Key(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Key)) return false; Key key = (Key) o; return x == key.x && y == key.y; } @Override public int hashCode() { int result = x; result = 31 * result + y; return result; } } static class Pair{ int x,y; public Pair(int x,int y){ this.y=y; this.x=x; } } static PrintWriter w = new PrintWriter(System.out); static long mod=998244353L,mod1=1000000007; public static void main(String [] args){ InputReader sc=new InputReader(System.in); int n=sc.ni(); double r=sc.ni(); double arr[]=new double[n+1]; double h[]=new double[n+1]; Arrays.fill(h,r); for(int i=1;i<=n;i++){ arr[i]=sc.ni(); } for(int i=1;i<=n;i++){ for(int j=i-1;j>=1;j--){ if(Math.abs(arr[i]-arr[j])<=2*r){ double d=arr[i]-arr[j]; h[i]=Math.max(h[i],Math.sqrt(4*r*r-d*d)+h[j]); } } } for(int i=1;i<=n;i++)w.print(h[i]+" "); w.println(); w.close(); } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
2dfea077c8599574151a3ee1399db6e5
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.io.*; public class Solution { public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String temp1=br.readLine(); String[] temp2=temp1.split(" "); int n=Integer.parseInt(temp2[0]); int r=Integer.parseInt(temp2[1]); int[] x=new int[n]; double[] y=new double[n]; String temp3=br.readLine(); String[] temp4=temp3.split(" "); for(int i=0;i<n;i++) { x[i]=Integer.parseInt(temp4[i]); if(i==0) { y[0]=r; System.out.print(r+" "); } else { double height=r; double newHeight=0; for(int j=0;j<i;j++) { if(Math.abs(x[i]-x[j])<=2*r) { newHeight=Math.sqrt(4*r*r-Math.pow(x[i]-x[j],2))+y[j]; } if(newHeight>height) { height=newHeight; } } y[i]=height; System.out.print(height+" "); } } } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
8ee6a6ce0dc72e3c584f064f767ab3ee
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
//package goodbye2017; import java.util.*; public class problemC { public static void main(String args[]) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int r = in.nextInt(); int[] x = new int[n]; double[] result = new double[n]; StringBuilder sb = new StringBuilder(); for(int i = 0; i < n; i++) { x[i] = in.nextInt(); result[i] = r; for(int j = 0; j < i; j++) { if(Math.abs(x[i] - x[j]) <= 2*r) { double temp = Math.abs(x[i]-x[j])/2.0; double tempY = Math.sqrt(r*r - temp*temp)*2 + result[j]; result[i] = Math.max(result[i], tempY); } } sb.append(result[i] + " "); } System.out.print(sb); } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
958d4385c7cc1f5f43511dbb4fdca283
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; import java.math.*; public class Codeforces { static class FastReader { BufferedReader br; StringTokenizer st; FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } FastReader(final InputStream is) { if (is != null) br = new BufferedReader(new InputStreamReader(is)); else new FastReader(); } String next() throws IOException { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } } static PrintWriter writer; static int MOD = 1_000_000_007; public static void main(String args[]) throws Exception { final FastReader reader = new FastReader(); writer = new PrintWriter(new BufferedOutputStream(System.out)); int n = reader.nextInt(); int r = reader.nextInt(); int[] x = new int[n]; for (int i = 0; i < n; i++) { x[i] = reader.nextInt(); } double[] y = new double[n]; for (int i = 0; i < n; i++) { if (i == 0) { y[i] = r; } else { //find index of x which is before current i y[i]=r; for (int j = 0 ; j < i; j++) { if(Math.abs(x[i] - x[j]) > 2*r) continue; double yans = y[j]; double temp = 4 * r * r - (x[i] - x[j]) * (x[i] - x[j]); temp = Math.sqrt(temp); yans += temp; y[i] = Math.max(y[i], yans); } } } for (int i = 0; i < n; i++) { writer.printf("%.10f ", y[i]); } writer.close(); } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
bcdab5dc29004ca4bfc3e5c2e041f3eb
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { TaskC Solver = new TaskC(); Solver.Solve(); } private static class TaskC { private void Solve() { Scanner in = new Scanner(System.in); int n = in.nextInt(); int R = in.nextInt(); int x[] = new int[n]; double ansY[] = new double[n]; for (int i = 0; i < n; i++) x[i] = in.nextInt(); ansY[0] = R; for (int i = 1; i < n; i++) { int curX = x[i]; double maxH = 0; for (int j = 0; j < i; j++) { int dx = x[j] - curX; if (Math.abs(dx) <= 2 * R) { double h = ansY[j] + Math.sqrt(4 * R * R - dx * dx); maxH = Math.max(maxH, h); } } if (maxH == 0) ansY[i] = R; else ansY[i] = maxH; } for (int i = 0; i < n; i++) System.out.print(ansY[i] + " "); } } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
47d1210cda87c5e9a2ac63024b19f980
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.OutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.FileNotFoundException; import java.util.StringTokenizer; import java.io.Writer; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Asgar Javadov */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); FastWriter out = new FastWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, FastWriter out) { int n = in.nextInt(); int r = in.nextInt(); int[] x = in.readIntArray(n); double[] y = new double[n]; for (int i = 0; i < n; ++i) { y[i] = (double) r; for (int j = i - 1; j >= 0; --j) { if ((x[i] <= x[j] && (x[i] + r) >= x[j] - r) || (x[i] > x[j] && (x[i] - r) <= x[j] + r)) { double diff = x[j] - x[i]; y[i] = Math.max(y[i], y[j] + Math.sqrt(4 * r * r - diff * diff)); } } } for (int i = 0; i < n; ++i) { if (i > 0) out.print(' '); out.print(y[i]); } out.println(); } } static class InputReader extends BufferedReader { StringTokenizer tokenizer; public InputReader(InputStream inputStream) { super(new InputStreamReader(inputStream), 32768); } public InputReader(String filename) { super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename))); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(readLine()); } catch (IOException e) { throw new RuntimeException(); } } return tokenizer.nextToken(); } public Integer nextInt() { return Integer.valueOf(next()); } public int[] readIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = nextInt(); return array; } } static class FastWriter extends PrintWriter { public FastWriter(OutputStream outputStream) { super(outputStream); } public FastWriter(Writer writer) { super(writer); } public FastWriter(String filename) throws FileNotFoundException { super(filename); } } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
ebfd2f72661c5106256157f44d2f73bc
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc =new Scanner(System.in); int n=sc.nextInt(); double r=sc.nextDouble(); int xcoord[] =new int[n]; double ycoord[]= new double[n]; for(int i=0;i<n;i++) { xcoord[i]=sc.nextInt(); double y=r; for(int j=0;j<i;j++) { int x=Math.abs(xcoord[j]-xcoord[i]); if(x<=2*r) y=Math.max(y,ycoord[j]+Math.sqrt(4*r*r-x*x)); } ycoord[i]=y; if(i>0) System.out.print(" "); System.out.print(ycoord[i]); } } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
faf0f1f3f4b93fee7d28aa0d51cd76e5
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.util.HashMap; import java.util.Scanner; public class NEW_YEAR_AND_CURLING { public static void main(String[] args) { Scanner nik = new Scanner(System.in); int n = nik.nextInt(); int r = nik.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nik.nextInt(); } HashMap<Integer, Double> hm = new HashMap<>(); hm.put(0, r * 1.0); for (int i = 1; i < n; i++) { boolean b = true; Double max = Double.MIN_VALUE; for (int j = i - 1; j >= 0; j--) { int temp = Math.abs(a[i] - a[j]); if (temp <= 2 * r) { double temp2 = Math.sqrt(4 * r * r - temp * temp); double g = hm.get(j); double g1 = g + temp2; b = false; max = max > g1 ? max : g1; } } if (b == true) { hm.put(i, r * 1.0); } else { hm.put(i, max); } } for (int val : hm.keySet()) { System.out.print(hm.get(val) + " "); } } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
e085f4bebddce7cbd73d2d60d3433f60
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.io.*; import java.util.*; public class C { public static void main(String[] args) throws IOException { FastScanner in = new FastScanner(System.in); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int n = in.nextInt(); double r = in.nextInt(); double[] x = new double[n+1]; double[] y = new double[n+1]; for (int i = 1; i <= n; i++) { x[i] = in.nextInt(); } int[] lastx = new int[1001]; for (int i = 1; i <= n; i++) { double s = x[i] - r, e = x[i] + r; for (int j = (int)Math.max(0, s); j <= (int)Math.min(1000, e); j++) { if (lastx[j] == 0) { y[i] = Math.max(y[i], findY(x[i], x[i], 0 - r, 2 * r)); } else { y[i] = Math.max(y[i], findY(x[lastx[j]], x[i], y[lastx[j]], 2 * r)); } lastx[j] = i; } } for (int i = 1; i <= n; i++) { out.println(y[i]); } out.close(); } public static double findY(double x1, double x2, double y1, double d) { return Math.max(y1 + Math.sqrt(-1 * Math.pow(x1, 2) + 2 * x1 * x2 + Math.pow(d, 2) - Math.pow(x2, 2)), y1 - Math.sqrt(-1 * Math.pow(x1, 2) + 2 * x1 * x2 + Math.pow(d, 2) - Math.pow(x2, 2))); } static class FastScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastScanner(InputStream stream) { this.stream = stream; } 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++]; } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { 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 (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndline(c)); return res.toString(); } } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
2d9089894a7a211592e82b7d40b7e831
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Sanket Makani */ 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); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader sc, PrintWriter w) { int n = sc.nextInt(); int r = sc.nextInt(); int x[] = new int[n]; double cord[] = new double[n]; Arrays.fill(cord, Double.NEGATIVE_INFINITY); for (int i = 0; i < n; i++) x[i] = sc.nextInt(); for (int i = 0; i < n; i++) { double max = Double.NEGATIVE_INFINITY; int flag = 0; for (int j = 0; j < n; j++) { if (i == j) continue; if (Math.abs(x[i] - x[j]) <= 2 * r) { if (cord[j] == Double.NEGATIVE_INFINITY) continue; double r1 = 4 * r * r; double x1 = x[i] - x[j]; x1 *= x1; double y1 = Math.sqrt(r1 - x1); y1 += cord[j]; max = Math.max(max, y1); flag++; } } if (flag == 0) cord[i] = r; else cord[i] = max; } for (int i = 0; i < n; i++) w.print(cord[i] + " "); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
ae491da9af00ac6c15d09adae9bfdaab
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.File; import java.io.FileNotFoundException; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.FileReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author ShekharN */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, FastScanner in, PrintWriter out) { int n = in.nextInt(), r = in.nextInt(); int[] xcord = new int[n]; for (int i = 0; i < n; i++) { xcord[i] = in.nextInt(); } double[] result = new double[n]; result[0] = r; for (int i = 1; i < n; i++) { double y = r; for (int j = 0; j < i; j++) { if (Math.abs(xcord[i] - xcord[j]) <= 2 * r) { double tmp = 4 * r * r - (xcord[j] - xcord[i]) * (xcord[j] - xcord[i]); tmp = Math.sqrt(tmp); y = Math.max(y, result[j] + tmp); } } result[i] = y; } //DecimalFormat df = new DecimalFormat("#.#####"); for (int i = 0; i < n; i++) { out.print(result[i] + " "); } } } static class FastScanner { private BufferedReader br; private StringTokenizer st; public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } public String nextString() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextString()); } } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
ad6adb4397355ad8a8eebfc56a6774a3
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.util.Scanner; import java.util.Stack; public class soez { public static void main(String args[]) { Scanner scan=new Scanner(System.in); int n=scan.nextInt(); int r=scan.nextInt(); int ar[]=new int[n]; double ys[]=new double[100000]; for(int i=0;i<n;i++) { ar[i]=scan.nextInt(); } //will always collide with the nearrest x value for(int i=0;i<n;i++) { double mx=r; int x=ar[i]; for(int j=0;j<i;j++) { int d=Math.abs(ar[j]-ar[i]); if(d<=(2*r)) { mx=Math.max(mx,ys[j]+Math.sqrt(4*r *r-d*d)); } } if(i>0)System.out.print(" "); System.out.print(mx); ys[i]=mx; } } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
f8117aa6bf2757e54deaf0ff1dc17853
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.io.*; import java.util.*; public class C { static class Disk { int i; double yc; Disk(int i, double yc) { this.i = i; this.yc = yc;} @Override public String toString() { return "Disk " + i + " @ " + yc; } } public static void main(String av[]) { Scanner s = new Scanner(); int n = s.nextInt(); int r = s.nextInt(); int [] x = new int[n]; for (int i = 0; i < n; i++) { x[i] = s.nextInt(); } double [] y = new double[n]; List<Disk> placed = new ArrayList<>(); PrintWriter out = new PrintWriter(System.out, false); for (int i = 0; i < n; i++) { int li = x[i] - r; int ri = x[i] + r; y[i] = r; for (Disk d : placed) { int ld = x[d.i] - r; int rd = x[d.i] + r; if (ld <= ri && ri <= rd || li <= ld && ld <= ri || ld <= li && li <= rd || li <= rd && rd <= ri) { double dy = Math.sqrt(4*r*r - (x[d.i] - x[i]) * (x[d.i] - x[i])); y[i] = Math.max(y[i], d.yc + dy); } } placed.add(new Disk(i, y[i])); // System.err.println(placed); } for (int i = 0; i < n; i++) out.print(y[i] + " "); out.println(); out.flush(); out.close(); } //-----------Scanner class for faster input---------- /* Provides similar API as java.util.Scanner but does not * use regular expression engine. */ public static class Scanner { BufferedReader br; StringTokenizer st; public Scanner(Reader in) { br = new BufferedReader(in); } public Scanner() { this(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()); } // Slightly different from java.util.Scanner.nextLine(), // which returns any remaining characters in current line, // if any. String readNextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } //-------------------------------------------------------- }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
a87416799937b252fdadc31211fbc82c
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.awt.*; import java.io.*; import java.util.*; public class TaskC { public static void main(String[] args) { new TaskC(System.in, System.out); } static class Solver implements Runnable { int n; double r; double[] x, heights; InputReader in; PrintWriter out; void solve() throws IOException { n = in.nextInt(); r = in.nextInt(); x = new double[n]; heights = new double[n]; heights[0] = r; for (int i = 0; i < n; i++) x[i] = in.nextInt(); for (int i = 1; i < n; i++) { double max = r; for (int j = 0; j < i; j++) { // check for ith one falling. if (willTouch(j, i)) max = Math.max(max, getHeight(j, i)); } heights[i] = max; } for (int i = 0; i < n; i++) out.printf("%.10f ", heights[i]); } boolean willTouch(int a, int b) { return Point.distance(x[a], 0, x[b], 0) <= 2 * r; } double getHeight(int a, int b) { return heights[a] + Math.sqrt(-Math.pow(x[b] - x[a], 2) + Math.pow(r + r, 2)); } public Solver(InputReader in, PrintWriter out) { this.in = in; this.out = out; } @Override public void run() { try { solve(); } catch (IOException e) { e.printStackTrace(); } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; 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 nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c & 15; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public void close() { try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } public InputReader(InputStream stream) { this.stream = stream; } } public TaskC(InputStream inputStream, OutputStream outputStream) { InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Thread thread = new Thread(null, new Solver(in, out), "TaskC", 1 << 29); try { thread.start(); thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } finally { in.close(); out.flush(); out.close(); } } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
14290463177d93d54827c2310196cbde
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.io.*; import java.util.*; /* * CodeForces GoodBye2017 Curling * @derrick20 */ public class Curling { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); R = sc.nextDouble(); x = new double[N]; y = new double[N]; for (int i = 0; i < N; i++) { x[i] = sc.nextDouble(); // check all previous circles, see if any are close enough to constrain us // before we hit the ground y[i] = R; for (int j = 0; j < i; j++) { double deltaX = (x[i] - x[j]); // NEED equality case since tangency counts as sticking! if (Math.abs(deltaX) <= 2*R) { // We need to put our y such that it is ABOVE this one at the very least // That is, we may find something that constrains us to be even higher, so keep maxing our height // we solve for the delta Y double deltaY = Math.sqrt(4*R*R - deltaX*deltaX); // See if being ABOVE y[j] by that much is the most constraining yet // Notice the other solution would be subtracting deltaY y[i] = Math.max(y[i], y[j] + deltaY); } } } for (double yval : y) { System.out.print(yval + " "); } } static double[] x; static double[] y; static double R; /*// Using the index, we can get 3 parameters, the prev x and y, and this x // We are searching for this point's y such that distance is minimized, without circles intersecting static double binarySearch(int index, double ylo, double yhi) { double x1 = x[index - 1]; double y1 = y[index - 1]; double x2 = x[index]; double d = Math.abs(dist(x1, y1, x2, yhi) - 2 * R); // just upper bound it while (true) { double ypivot = ylo + (yhi - ylo) / 2; double newD = Math.abs(dist(x1, y1, x2, ypivot) - 2 * R); if (newD < d) { // this is a good direction, so shrink yhi yhi = ypivot; } else { ylo = ypivot; // we went too far, so move y up a bit } if (Math.abs(newD - d) <= 1e-6) // If we've stopped improving to some error, then done return ypivot; else { // else, keep trying d = newD; } } }*/ static double dist(double x1, double y1, double x2, double y2) { return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); } static class Scanner { BufferedReader br; StringTokenizer st; Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } Scanner(FileReader s) { br = new BufferedReader(s); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
2d65b54f108d1435f18256852562717a
train_002.jsonl
1514562000
Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.
256 megabytes
import java.awt.Point; import java.awt.geom.Point2D; import java.math.BigDecimal; import java.math.MathContext; import java.util.Scanner; import java.util.*; public class Ctask { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(), r = scan.nextInt(); int[] xs = new int[n]; double[] ys = new double[n]; xs[0] = scan.nextInt(); ys[0] = (double)r; System.out.print(ys[0]); for(int i = 1; i < n; i++) { int c = scan.nextInt(); xs[i] = c; ys[i] = (double)r; for (int j = i - 1; j >= 0; j--) { int a = xs[j]; int d = 4 * r * r - (c - a) * (c - a); if (d < 0) continue; double b = ys[j]; double sqrt = Math.sqrt(d); ys[i] = Math.max(ys[i], b + sqrt); } System.out.print(' '); System.out.print(ys[i]); } } }
Java
["6 2\n5 5 6 8 3 12"]
2 seconds
["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"]
NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk.
Java 8
standard input
[ "implementation", "geometry", "brute force", "math" ]
3cd019d1016cb3b872ea956c312889eb
The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≀ xi ≀ 1 000)Β β€” the x-coordinates of the disks.
1,500
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
standard output
PASSED
143e40cef7ce88483e848960c6d401ec
train_002.jsonl
1589628900
You have to restore the wall. The wall consists of $$$N$$$ pillars of bricks, the height of the $$$i$$$-th pillar is initially equal to $$$h_{i}$$$, the height is measured in number of bricks. After the restoration all the $$$N$$$ pillars should have equal heights.You are allowed the following operations: put a brick on top of one pillar, the cost of this operation is $$$A$$$; remove a brick from the top of one non-empty pillar, the cost of this operation is $$$R$$$; move a brick from the top of one non-empty pillar to the top of another pillar, the cost of this operation is $$$M$$$.You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes $$$0$$$.What is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height?
256 megabytes
import java.io.BufferedOutputStream; import java.io.Closeable; import java.io.DataInputStream; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.InputMismatchException; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { static class TaskAdapter implements Runnable { @Override public void run() { long startTime = System.currentTimeMillis(); InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); Output out = new Output(outputStream); ERestorerDistance solver = new ERestorerDistance(); solver.solve(1, in, out); out.close(); System.err.println(System.currentTimeMillis()-startTime+"ms"); } } public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "", 1<<28); thread.start(); thread.join(); } static class ERestorerDistance { private final int iinf = 1_000_000_000; private final long linf = 4_000_000_000_000_000_000L; int n; long a; long r; long m; int[] arr; public ERestorerDistance() { } public long f(int x) { long lo = 0, hi = 0; for(int i: arr) { if(i>=x) { hi += i-x; }else { lo += x-i; } } long sub = Math.min(lo, hi); lo -= sub; hi -= sub; return sub*m+lo*a+hi*r; } public void solve(int kase, InputReader in, Output pw) { n = in.nextInt(); a = in.nextInt(); r = in.nextInt(); m = Math.min(in.nextInt(), a+r); arr = in.nextInt(n); int l = 0, r = iinf; while((r-l)>=3) { int a = l+(r-l)/3, b = r-(r-l)/3; long x = f(a), y = f(b); if(x<y) { r = b; }else if(x>y) { l = a; }else { l = a; r = b; } } long ans = linf; for(int i = l; i<=r; i++) { ans = Math.min(ans, f(i)); } pw.println(ans); } } static class FastReader implements InputReader { final private int BUFFER_SIZE = 1<<16; private DataInputStream din; private byte[] buffer; private int bufferPointer; private int bytesRead; public FastReader(InputStream is) { din = new DataInputStream(is); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public int nextInt() { int ret = 0; byte c = skipToDigit(); 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 boolean isDigit(byte b) { return b>='0'&&b<='9'; } private byte skipToDigit() { byte ret; while(!isDigit(ret = read())&&ret!='-') ; return ret; } private void fillBuffer() { try { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); }catch(IOException e) { e.printStackTrace(); throw new InputMismatchException(); } if(bytesRead==-1) { buffer[0] = -1; } } private byte read() { if(bytesRead==-1) { throw new InputMismatchException(); }else if(bufferPointer==bytesRead) { fillBuffer(); } return buffer[bufferPointer++]; } } static interface InputReader { int nextInt(); default int[] nextInt(int n) { int[] ret = new int[n]; for(int i = 0; i<n; i++) { ret[i] = nextInt(); } return ret; } } static class Output implements Closeable, Flushable { public StringBuilder sb; public OutputStream os; public int BUFFER_SIZE; public String lineSeparator; public Output(OutputStream os) { this(os, 1<<16); } public Output(OutputStream os, int bs) { BUFFER_SIZE = bs; sb = new StringBuilder(BUFFER_SIZE); this.os = new BufferedOutputStream(os, 1<<17); lineSeparator = System.lineSeparator(); } public void println(long l) { println(String.valueOf(l)); } public void println(String s) { sb.append(s); println(); } public void println() { sb.append(lineSeparator); } private void flushToBuffer() { try { os.write(sb.toString().getBytes()); }catch(IOException e) { e.printStackTrace(); } sb = new StringBuilder(BUFFER_SIZE); } public void flush() { try { flushToBuffer(); os.flush(); }catch(IOException e) { e.printStackTrace(); } } public void close() { flush(); try { os.close(); }catch(IOException e) { e.printStackTrace(); } } } }
Java
["3 1 100 100\n1 3 8", "3 100 1 100\n1 3 8", "3 100 100 1\n1 3 8", "5 1 2 4\n5 5 3 6 5", "5 1 2 2\n5 5 3 6 5"]
1 second
["12", "9", "4", "4", "3"]
null
Java 11
standard input
[ "greedy", "math", "sortings", "binary search", "ternary search" ]
c9c65223b8575676647fe943f8dff25a
The first line of input contains four integers $$$N$$$, $$$A$$$, $$$R$$$, $$$M$$$ ($$$1 \le N \le 10^{5}$$$, $$$0 \le A, R, M \le 10^{4}$$$)Β β€” the number of pillars and the costs of operations. The second line contains $$$N$$$ integers $$$h_{i}$$$ ($$$0 \le h_{i} \le 10^{9}$$$)Β β€” initial heights of pillars.
2,100
Print one integerΒ β€” the minimal cost of restoration.
standard output
PASSED
97dc38a65a1d4d802b391a11bfa0751f
train_002.jsonl
1589628900
You have to restore the wall. The wall consists of $$$N$$$ pillars of bricks, the height of the $$$i$$$-th pillar is initially equal to $$$h_{i}$$$, the height is measured in number of bricks. After the restoration all the $$$N$$$ pillars should have equal heights.You are allowed the following operations: put a brick on top of one pillar, the cost of this operation is $$$A$$$; remove a brick from the top of one non-empty pillar, the cost of this operation is $$$R$$$; move a brick from the top of one non-empty pillar to the top of another pillar, the cost of this operation is $$$M$$$.You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes $$$0$$$.What is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height?
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; public class E { static class Task { private long getCost(int h, int[] hArr, int a, int r, int m) { long addCount = 0; long removeCount = 0; for (int num : hArr) { if (num < h) { addCount += h - num; } else if (num > h) { removeCount += num - h; } } int minMoveCost = Math.min(m, a + r); long minCount = Math.min(addCount, removeCount); long diffCount = Math.abs(addCount - removeCount); return minMoveCost * minCount + (addCount >= removeCount ? a * diffCount : r * diffCount); } public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int add = in.nextInt(); int remove = in.nextInt(); int move = in.nextInt(); int[] hArr = new int[n]; for (int i = 0; i < n; i++) { hArr[i] = in.nextInt(); } if (n == 1) { out.println(0); return; } sort(hArr); // δΈ‰εˆ†οΌŒηŒœζœ€η»ˆηš„ι«˜εΊ¦οΌŒ int low = hArr[0]; int high = hArr[n - 1]; while (high - low > 2) { int l = low + (high - low) / 3; int r = low + (high - low) * 2 / 3; long costL = getCost(l, hArr, add, remove, move); long costR = getCost(r, hArr, add, remove, move); if (costL < costR) { high = r; } else { low = l; } } long minCost = Long.MAX_VALUE; for (int i = low; i <= high; i++) { minCost = Math.min(minCost, getCost(i, hArr, add, remove, move)); } out.println(minCost); } } private static void sort(double[] arr) { Double[] objArr = Arrays.stream(arr).boxed().toArray(Double[]::new); Arrays.sort(objArr); for (int i = 0; i < arr.length; i++) { arr[i] = objArr[i]; } } private static void sort(int[] arr) { Integer[] objArr = Arrays.stream(arr).boxed().toArray(Integer[]::new); Arrays.sort(objArr); for (int i = 0; i < arr.length; i++) { arr[i] = objArr[i]; } } private static void sort(long[] arr) { Long[] objArr = Arrays.stream(arr).boxed().toArray(Long[]::new); Arrays.sort(objArr); for (int i = 0; i < arr.length; i++) { arr[i] = objArr[i]; } } private static void solve() { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task task = new Task(); task.solve(1, in, out); out.close(); } public static void main(String[] args) { new Thread(null, () -> solve(), "1", 1 << 26).start(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
Java
["3 1 100 100\n1 3 8", "3 100 1 100\n1 3 8", "3 100 100 1\n1 3 8", "5 1 2 4\n5 5 3 6 5", "5 1 2 2\n5 5 3 6 5"]
1 second
["12", "9", "4", "4", "3"]
null
Java 11
standard input
[ "greedy", "math", "sortings", "binary search", "ternary search" ]
c9c65223b8575676647fe943f8dff25a
The first line of input contains four integers $$$N$$$, $$$A$$$, $$$R$$$, $$$M$$$ ($$$1 \le N \le 10^{5}$$$, $$$0 \le A, R, M \le 10^{4}$$$)Β β€” the number of pillars and the costs of operations. The second line contains $$$N$$$ integers $$$h_{i}$$$ ($$$0 \le h_{i} \le 10^{9}$$$)Β β€” initial heights of pillars.
2,100
Print one integerΒ β€” the minimal cost of restoration.
standard output
PASSED
ab75828e81e37e1f8e9d45083895c13a
train_002.jsonl
1589628900
You have to restore the wall. The wall consists of $$$N$$$ pillars of bricks, the height of the $$$i$$$-th pillar is initially equal to $$$h_{i}$$$, the height is measured in number of bricks. After the restoration all the $$$N$$$ pillars should have equal heights.You are allowed the following operations: put a brick on top of one pillar, the cost of this operation is $$$A$$$; remove a brick from the top of one non-empty pillar, the cost of this operation is $$$R$$$; move a brick from the top of one non-empty pillar to the top of another pillar, the cost of this operation is $$$M$$$.You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes $$$0$$$.What is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height?
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; public class E { static class Task { private long getCost(int h, int[] hArr, int a, int r, int m) { long addCount = 0; long removeCount = 0; for (int num : hArr) { if (num < h) { addCount += h - num; } else if (num > h) { removeCount += num - h; } } int minMoveCost = Math.min(m, a + r); long minCount = Math.min(addCount, removeCount); long diffCount = Math.abs(addCount - removeCount); return minMoveCost * minCount + (addCount >= removeCount ? a * diffCount : r * diffCount); } public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int a = in.nextInt(); int r = in.nextInt(); int m = in.nextInt(); int[] hArr = new int[n]; for (int i = 0; i < n; i++) { hArr[i] = in.nextInt(); } if (n == 1) { out.println(0); return; } sort(hArr); // δΊŒεˆ†οΌŒηŒœζœ€η»ˆηš„ι«˜εΊ¦οΌŒ int low = hArr[0]; int high = hArr[n - 1]; while (low < high) { int mid = low + (high - low) / 2; long costL = getCost(mid, hArr, a, r, m); long costR = getCost(mid + 1, hArr, a, r, m); if (costL <= costR) { high = mid; } else { low = mid + 1; } } out.println(getCost(low, hArr, a, r, m)); } } private static void sort(double[] arr) { Double[] objArr = Arrays.stream(arr).boxed().toArray(Double[]::new); Arrays.sort(objArr); for (int i = 0; i < arr.length; i++) { arr[i] = objArr[i]; } } private static void sort(int[] arr) { Integer[] objArr = Arrays.stream(arr).boxed().toArray(Integer[]::new); Arrays.sort(objArr); for (int i = 0; i < arr.length; i++) { arr[i] = objArr[i]; } } private static void sort(long[] arr) { Long[] objArr = Arrays.stream(arr).boxed().toArray(Long[]::new); Arrays.sort(objArr); for (int i = 0; i < arr.length; i++) { arr[i] = objArr[i]; } } private static void solve() { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task task = new Task(); task.solve(1, in, out); out.close(); } public static void main(String[] args) { new Thread(null, () -> solve(), "1", 1 << 26).start(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
Java
["3 1 100 100\n1 3 8", "3 100 1 100\n1 3 8", "3 100 100 1\n1 3 8", "5 1 2 4\n5 5 3 6 5", "5 1 2 2\n5 5 3 6 5"]
1 second
["12", "9", "4", "4", "3"]
null
Java 11
standard input
[ "greedy", "math", "sortings", "binary search", "ternary search" ]
c9c65223b8575676647fe943f8dff25a
The first line of input contains four integers $$$N$$$, $$$A$$$, $$$R$$$, $$$M$$$ ($$$1 \le N \le 10^{5}$$$, $$$0 \le A, R, M \le 10^{4}$$$)Β β€” the number of pillars and the costs of operations. The second line contains $$$N$$$ integers $$$h_{i}$$$ ($$$0 \le h_{i} \le 10^{9}$$$)Β β€” initial heights of pillars.
2,100
Print one integerΒ β€” the minimal cost of restoration.
standard output
PASSED
7304578a18f5a0ecc1c470623a0260ba
train_002.jsonl
1589628900
You have to restore the wall. The wall consists of $$$N$$$ pillars of bricks, the height of the $$$i$$$-th pillar is initially equal to $$$h_{i}$$$, the height is measured in number of bricks. After the restoration all the $$$N$$$ pillars should have equal heights.You are allowed the following operations: put a brick on top of one pillar, the cost of this operation is $$$A$$$; remove a brick from the top of one non-empty pillar, the cost of this operation is $$$R$$$; move a brick from the top of one non-empty pillar to the top of another pillar, the cost of this operation is $$$M$$$.You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes $$$0$$$.What is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height?
256 megabytes
import java.io.*; import java.util.*; public class Abc { static long n,a,r,m; public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); n=sc.nextInt();a=sc.nextInt();r=sc.nextInt();m=Math.min(sc.nextInt(),a+r); long h[]=new long[(int) n]; for (int i=0;i<n;i++)h[i]=sc.nextInt(); long l=0,r=(long) 1e9+1; long ans=(long) 1e18; while (r-l>10){ long m1=l+(r-l)/3; long m2=r-(r-l)/3; long c1=cost(h,m1),c2=cost(h,m2); if (c1<ans)ans=c1; if (c2<ans)ans=c2; if (c1>c2){ l=m1; }else if (c2>c1){ r=m2; }else { l=m1; r=m2; } } for (;l<=r;l++){ ans=Math.min(ans,cost(h,l)); } System.out.println(ans); } static long cost(long h[],long x){ long miss=0,extra=0; for (int i=0;i<h.length;i++){ long c=h[i]-x; if (c>0){ extra+=c; }else { miss-=c; } } if (miss>=extra)return a*(miss-extra)+extra*m; return (extra-miss)*r+miss*m; } 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
["3 1 100 100\n1 3 8", "3 100 1 100\n1 3 8", "3 100 100 1\n1 3 8", "5 1 2 4\n5 5 3 6 5", "5 1 2 2\n5 5 3 6 5"]
1 second
["12", "9", "4", "4", "3"]
null
Java 11
standard input
[ "greedy", "math", "sortings", "binary search", "ternary search" ]
c9c65223b8575676647fe943f8dff25a
The first line of input contains four integers $$$N$$$, $$$A$$$, $$$R$$$, $$$M$$$ ($$$1 \le N \le 10^{5}$$$, $$$0 \le A, R, M \le 10^{4}$$$)Β β€” the number of pillars and the costs of operations. The second line contains $$$N$$$ integers $$$h_{i}$$$ ($$$0 \le h_{i} \le 10^{9}$$$)Β β€” initial heights of pillars.
2,100
Print one integerΒ β€” the minimal cost of restoration.
standard output
PASSED
81f82ce55833337e361b794afd41e19d
train_002.jsonl
1589628900
You have to restore the wall. The wall consists of $$$N$$$ pillars of bricks, the height of the $$$i$$$-th pillar is initially equal to $$$h_{i}$$$, the height is measured in number of bricks. After the restoration all the $$$N$$$ pillars should have equal heights.You are allowed the following operations: put a brick on top of one pillar, the cost of this operation is $$$A$$$; remove a brick from the top of one non-empty pillar, the cost of this operation is $$$R$$$; move a brick from the top of one non-empty pillar to the top of another pillar, the cost of this operation is $$$M$$$.You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes $$$0$$$.What is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height?
256 megabytes
// Working program with FastReader import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; import java.util.TreeMap; public class problem5 { 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; } } class Pair<U, V> { public U first; // first field of a Pair public V second; // second field of a Pair // Constructs a new Pair with specified values public Pair(U first, V second) { this.first = first; this.second = second; } public U getKey() { return first; } public V getValue() { return second; } @Override // Checks specified object is "equal to" current object or not public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; // call equals() method of the underlying objects if (!first.equals(pair.first)) return false; return second.equals(pair.second); } @Override // Computes hash code for an object to support hash tables public int hashCode() { // use hash codes of the underlying objects return 31 * first.hashCode() + second.hashCode(); } @Override public String toString() { return "(" + first + ", " + second + ")"; } } public static void main(String[] args) { FastReader s=new FastReader(); int n = s.nextInt(); int a = s.nextInt(); int r = s.nextInt(); int m = s.nextInt(); int[] heights = new int[n]; for(int j=0;j<n;j++){ heights[j] = s.nextInt(); } System.out.println(minimalheight(n,a,r,m,heights)); } public static long minimalheight(int n,int a,int r,int m,int[] heights){ m = Math.min(a+r,m); Arrays.sort(heights); int start =0; int end = heights[n-1]; while(start<end){ int mid = start+((end-start)/2); int mid1 = mid+1; long l = cost(mid,heights,n,m,a,r); long ri = cost(mid1,heights,n,m,a,r); if(ri<l){ start = mid1; } else { end = mid; } // System.out.println(start); // System.out.println(end); } return cost(start,heights,n,m,a,r); } public static long cost(int h,int[] heights,int n,int m,int a,int r){ long nbgh = 0; long total =0; for(int i=0;i<n;i++){ if(heights[i]>=h){ nbgh+=(long)(heights[i]-h); } total+=(long)heights[i]; } long req = (long)n*h; long total_cost =0; long k = Math.min((long)(req+nbgh-total),nbgh); if(total>=req){ total_cost =(long) m*k +(long) r*(nbgh-k); } else { total_cost = (long)m*k+(long)(a*(req-total)); } return total_cost; } }
Java
["3 1 100 100\n1 3 8", "3 100 1 100\n1 3 8", "3 100 100 1\n1 3 8", "5 1 2 4\n5 5 3 6 5", "5 1 2 2\n5 5 3 6 5"]
1 second
["12", "9", "4", "4", "3"]
null
Java 11
standard input
[ "greedy", "math", "sortings", "binary search", "ternary search" ]
c9c65223b8575676647fe943f8dff25a
The first line of input contains four integers $$$N$$$, $$$A$$$, $$$R$$$, $$$M$$$ ($$$1 \le N \le 10^{5}$$$, $$$0 \le A, R, M \le 10^{4}$$$)Β β€” the number of pillars and the costs of operations. The second line contains $$$N$$$ integers $$$h_{i}$$$ ($$$0 \le h_{i} \le 10^{9}$$$)Β β€” initial heights of pillars.
2,100
Print one integerΒ β€” the minimal cost of restoration.
standard output
PASSED
e0e56153e9f7c4cda0b16775b3b2840c
train_002.jsonl
1589628900
You have to restore the wall. The wall consists of $$$N$$$ pillars of bricks, the height of the $$$i$$$-th pillar is initially equal to $$$h_{i}$$$, the height is measured in number of bricks. After the restoration all the $$$N$$$ pillars should have equal heights.You are allowed the following operations: put a brick on top of one pillar, the cost of this operation is $$$A$$$; remove a brick from the top of one non-empty pillar, the cost of this operation is $$$R$$$; move a brick from the top of one non-empty pillar to the top of another pillar, the cost of this operation is $$$M$$$.You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes $$$0$$$.What is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.stream.IntStream; import java.util.Arrays; import java.util.InputMismatchException; import java.io.IOException; import java.util.Random; import java.util.OptionalInt; import java.io.InputStream; /** * Built using CHelper plug-in Actual solution is at the top * * @author dauom */ 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); ERestorerDistance solver = new ERestorerDistance(); solver.solve(1, in, out); out.close(); } static class ERestorerDistance { public final void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int a = in.nextInt(); int r = in.nextInt(); int m = in.nextInt(); int[] h = in.nextIntArray(n); ArrayShuffler.shuffle(h); Arrays.sort(h); int lo = 0, hi = IntStream.of(h).max().getAsInt(); long ans = Constants.INF64; while (hi - lo >= 6) { int m1 = lo + (hi - lo) / 3; int m2 = hi - (hi - lo) / 3; long c1 = cost(n, a, r, m, h, m1); long c2 = cost(n, a, r, m, h, m2); ans = Math.min(ans, Math.min(c1, c2)); if (c1 <= c2) { hi = m2 - 1; } if (c1 >= c2) { lo = m1 + 1; } } for (int i = lo; i <= hi; i++) { ans = Math.min(ans, cost(n, a, r, m, h, i)); } out.println(ans); } private final long cost(int n, int a, int r, int m, int[] h, int target) { long below = 0; long above = 0; for (int x : h) { if (x > target) { above += x - target; } else { below += target - x; } } if (m < a + r) { return above > below ? below * m + (above - below) * r : above * m + (below - above) * a; } return above * r + below * a; } } static class Constants { public static final long INF64 = 0x3f3f3f3f3f3f3f3fL; } static class ArrayShuffler { private static Random r = new Random(System.nanoTime() * 21); private ArrayShuffler() { throw new RuntimeException("DON'T"); } public static int[] shuffle(int[] arr, int from, int to) { int n = to - from + 1; for (int i = from; i <= to; i += 4) { int p = r.nextInt(n + 1) + from; if (p <= to) { int temp = arr[i]; arr[i] = arr[p]; arr[p] = temp; } } return arr; } public static int[] shuffle(int[] arr) { return shuffle(arr, 0, arr.length - 1); } } static final class InputReader { private final InputStream stream; private final byte[] buf = new byte[1 << 16]; private int curChar; private int numChars; public InputReader() { this.stream = System.in; } public InputReader(final InputStream stream) { this.stream = stream; } private final int read() { if (this.numChars == -1) { throw new UnknownError(); } else { if (this.curChar >= this.numChars) { this.curChar = 0; try { this.numChars = this.stream.read(this.buf); } catch (IOException ex) { throw new InputMismatchException(); } if (this.numChars <= 0) { return -1; } } return this.buf[this.curChar++]; } } public final int nextInt() { int c; for (c = this.read(); isSpaceChar(c); c = this.read()) {} byte sgn = 1; if (c == 45) { // 45 == '-' sgn = -1; c = this.read(); } int res = 0; while (c >= 48 && c <= 57) { // 48 == '0', 57 == '9' res *= 10; res += c - 48; // 48 == '0' c = this.read(); if (isSpaceChar(c)) { return res * sgn; } } throw new InputMismatchException(); } private static final boolean isSpaceChar(final int c) { return c == 32 || c == 10 || c == 13 || c == 9 || c == -1; // 32 == ' ', 10 == '\n', 13 == '\r', 9 == '\t' } public final int[] nextIntArray(final int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } } }
Java
["3 1 100 100\n1 3 8", "3 100 1 100\n1 3 8", "3 100 100 1\n1 3 8", "5 1 2 4\n5 5 3 6 5", "5 1 2 2\n5 5 3 6 5"]
1 second
["12", "9", "4", "4", "3"]
null
Java 11
standard input
[ "greedy", "math", "sortings", "binary search", "ternary search" ]
c9c65223b8575676647fe943f8dff25a
The first line of input contains four integers $$$N$$$, $$$A$$$, $$$R$$$, $$$M$$$ ($$$1 \le N \le 10^{5}$$$, $$$0 \le A, R, M \le 10^{4}$$$)Β β€” the number of pillars and the costs of operations. The second line contains $$$N$$$ integers $$$h_{i}$$$ ($$$0 \le h_{i} \le 10^{9}$$$)Β β€” initial heights of pillars.
2,100
Print one integerΒ β€” the minimal cost of restoration.
standard output
PASSED
b56bda84fd0595607dce3736c47b43f4
train_002.jsonl
1589628900
You have to restore the wall. The wall consists of $$$N$$$ pillars of bricks, the height of the $$$i$$$-th pillar is initially equal to $$$h_{i}$$$, the height is measured in number of bricks. After the restoration all the $$$N$$$ pillars should have equal heights.You are allowed the following operations: put a brick on top of one pillar, the cost of this operation is $$$A$$$; remove a brick from the top of one non-empty pillar, the cost of this operation is $$$R$$$; move a brick from the top of one non-empty pillar to the top of another pillar, the cost of this operation is $$$M$$$.You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes $$$0$$$.What is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height?
256 megabytes
//package round643; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class E { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(); long A = nl(), R = nl(), M = nl(); int[] a = na(n); int low = 0, high = 1000000001; while(high - low > 2){ int l = low + (high-low)/3; int r = low + (high-low)/3*2; long costl = f(l, a, A, R, M); long costr = f(r, a, A, R, M); if(costl < costr){ high = r; }else{ low = l; } } long ans = Long.MAX_VALUE; for(int i = low;i <= high;i++){ ans = Math.min(ans, f(i, a, A, R, M)); } out.println(ans); } long f(int h, int[] a, long A, long R, long M) { long na = 0, nr = 0; for(int v : a){ if(v < h){ na += h-v; }else{ nr += v-h; } } long cost = A*na + R*nr; if(A+R > M){ cost += Math.min(na, nr) * (M-(A+R)); } return cost; } public static int[] uniq(int[] a) { int n = a.length; int p = 0; for(int i = 0;i < n;i++) { if(i == 0 || a[i] != a[i-1])a[p++] = a[i]; } return Arrays.copyOf(a, p); } public static int[] radixSort(int[] f){ return radixSort(f, f.length); } public static int[] radixSort(int[] f, int n) { int[] to = new int[n]; { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(f[i]&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[f[i]&0xffff]++] = f[i]; int[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(f[i]>>>16)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[f[i]>>>16]++] = f[i]; int[] d = f; f = to;to = d; } return f; } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new E().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
Java
["3 1 100 100\n1 3 8", "3 100 1 100\n1 3 8", "3 100 100 1\n1 3 8", "5 1 2 4\n5 5 3 6 5", "5 1 2 2\n5 5 3 6 5"]
1 second
["12", "9", "4", "4", "3"]
null
Java 11
standard input
[ "greedy", "math", "sortings", "binary search", "ternary search" ]
c9c65223b8575676647fe943f8dff25a
The first line of input contains four integers $$$N$$$, $$$A$$$, $$$R$$$, $$$M$$$ ($$$1 \le N \le 10^{5}$$$, $$$0 \le A, R, M \le 10^{4}$$$)Β β€” the number of pillars and the costs of operations. The second line contains $$$N$$$ integers $$$h_{i}$$$ ($$$0 \le h_{i} \le 10^{9}$$$)Β β€” initial heights of pillars.
2,100
Print one integerΒ β€” the minimal cost of restoration.
standard output
PASSED
a3001f87b88dfe86c1e0eccca24d62c6
train_002.jsonl
1589628900
You have to restore the wall. The wall consists of $$$N$$$ pillars of bricks, the height of the $$$i$$$-th pillar is initially equal to $$$h_{i}$$$, the height is measured in number of bricks. After the restoration all the $$$N$$$ pillars should have equal heights.You are allowed the following operations: put a brick on top of one pillar, the cost of this operation is $$$A$$$; remove a brick from the top of one non-empty pillar, the cost of this operation is $$$R$$$; move a brick from the top of one non-empty pillar to the top of another pillar, the cost of this operation is $$$M$$$.You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes $$$0$$$.What is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height?
256 megabytes
import java.io.*; import java.util.*; public class E { public static void main (String[] args) { new E(); } int N; int ADD, REM, MOV; long TOTSUM; int[] a; public E() { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); System.err.println(""); N = fs.nextInt(); ADD = fs.nextInt(); REM = fs.nextInt(); MOV = fs.nextInt(); a = fs.nextIntArray(N); sort(a); for(int x : a) TOTSUM += x; TreeSet<Integer> critN = new TreeSet<>(); critN.add(0); for(int i = 0; i < N; i++) { int val = a[i]; critN.add(val); } ArrayList<Integer> Ns = new ArrayList<>(critN); int M = Ns.size(); long[] sumBelow = new long[M]; int[] cntBelow = new int[M]; int ptr = 0; long sum = 0; int cnt = 0; for(int i = 0; i < Ns.size(); i++) { int cur = Ns.get(i); while(ptr < N && a[ptr] <= cur) { sum += a[ptr]; cnt++; ptr++; } sumBelow[i] = sum; cntBelow[i] = cnt; } long res = ((long)N*Ns.get(M-1) - TOTSUM) * ADD; long curBelow = 0, curAbove = TOTSUM; for(int i = 0; i < N; i++) { curAbove -= a[i]; long curCntBelow = i; long curCntAbove = N-i-1; long ret = 0; long sumToBelow = (curCntBelow * a[i] - curBelow); long sumToAbove = (curAbove - a[i] * curCntAbove); if(ADD+REM <= MOV) { ret += sumToBelow * ADD; ret += sumToAbove * REM; } else { long X = Math.min(sumToAbove, sumToBelow); long min = X; ret += MOV * min; ret += ADD * (sumToBelow-X); ret += REM * (sumToAbove-X); } res = Math.min(res, ret); curBelow += a[i]; } // System.out.println("R " + res); // System.out.println(Ns); // System.out.println(Arrays.toString(sumBelow)); // System.out.println(Arrays.toString(cntBelow)); for(int i = 0; i < M - 1; i++) { int end1 = Ns.get(i); int end2 = Ns.get(i+1)-1; int cntLess = cntBelow[i]; long sumUp = (long)cntLess*end1 - sumBelow[i]; int cntMore = N-cntLess; long sumDown = (TOTSUM - sumBelow[i]) - (long)cntMore*end1; if(REM+ADD <= MOV) { //one of the end points long res1 = sumUp*ADD + sumDown*REM; res = Math.min(res, res1); long nSumUp = sumUp + (long)cntLess*(end2-end1); long nSumDown = sumDown - (long)cntMore*(end2-end1); res1 = nSumUp*ADD + nSumDown*REM; res = Math.min(res, res1); continue; } //find the critical passing point if it exists in the range.. boolean initRelation = (sumUp <= sumDown); //first time initRelation is not the same.. int lo = end1+1, hi = end2; int fnd = -1; while(lo <= hi) { int mid = (lo+hi)/2; long nSumUp = sumUp + (long)cntLess*(mid-end1); long nSumDown = sumDown - (long)cntMore*(mid-end1); boolean nowRelation = (nSumUp <= nSumDown); if(nowRelation != initRelation) { fnd = mid; hi = mid-1; } else { lo = mid+1; } } //4 values to test //end1, end2, fnd, fnd-1 long testEnd1 = testIt(sumUp, sumDown, cntLess, cntMore, end1, end1); long testEnd2 = testIt(sumUp, sumDown, cntLess, cntMore, end2, end1); long testFnd = Long.MAX_VALUE; long testFnd1 = Long.MAX_VALUE; if(fnd != -1) { testFnd = testIt(sumUp, sumDown, cntLess, cntMore, fnd, end1); if(fnd-1 >= end1) { testFnd1 = testIt(sumUp, sumDown, cntLess, cntMore, fnd-1, end1); } } long ret1 = Math.min(testEnd1, testEnd2); ret1 = Math.min(ret1, Math.min(testFnd, testFnd1)); // System.out.println(" kay " + sumUp + " " + sumDown); // System.out.println(" " + testEnd1); // System.out.println(" " + testEnd2); // System.out.println(" " + testFnd); // System.out.println(" " + testFnd1); // System.out.println("AT " + i + " " + ret1); res = Math.min(res, ret1); } out.println(res); out.close(); } long testIt(long sumUp, long sumDown, long cntLess, long cntMore, int nxtPoint, int end1) { sumUp += cntLess*(nxtPoint-end1); sumDown -= cntMore*(nxtPoint-end1); long X = Math.min(sumUp, sumDown); long res = MOV*X; res += ADD * (sumUp-X); res += REM * (sumDown-X); return res; } void sort(int[] a) { int n = a.length; Random rand = new Random(); for(int i = 0; i < n; i++) { int x = rand.nextInt(n); int y = rand.nextInt(n); int t = a[x]; a[x] = a[y]; a[y] = t; } Arrays.sort(a); } class FastScanner { public int BS = 1<<16; public char NC = (char)0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } public char nextChar(){ while(bId==size) { try { size = in.read(buf); }catch(Exception e) { return NC; } if(size==-1)return NC; bId=0; } return (char)buf[bId++]; } public int nextInt() { return (int)nextLong(); } public long nextLong() { num=1; boolean neg = false; if(c==NC)c=nextChar(); for(;(c<'0' || c>'9'); c = nextChar()) { if(c=='-')neg=true; } long res = 0; for(; c>='0' && c <='9'; c=nextChar()) { res = (res<<3)+(res<<1)+c-'0'; num*=10; } return neg?-res:res; } public double nextDouble() { double cur = nextLong(); return c!='.' ? cur:cur+nextLong()/num; } public String next() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c>32) { res.append(c); c=nextChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c!='\n') { res.append(c); c=nextChar(); } return res.toString(); } public boolean hasNext() { if(c>32)return true; while(true) { c=nextChar(); if(c==NC)return false; else if(c>32)return true; } } public int[] nextIntArray(int n) { int[] res = new int[n]; for(int i = 0; i < n; i++) res[i] = nextInt(); return res; } } }
Java
["3 1 100 100\n1 3 8", "3 100 1 100\n1 3 8", "3 100 100 1\n1 3 8", "5 1 2 4\n5 5 3 6 5", "5 1 2 2\n5 5 3 6 5"]
1 second
["12", "9", "4", "4", "3"]
null
Java 11
standard input
[ "greedy", "math", "sortings", "binary search", "ternary search" ]
c9c65223b8575676647fe943f8dff25a
The first line of input contains four integers $$$N$$$, $$$A$$$, $$$R$$$, $$$M$$$ ($$$1 \le N \le 10^{5}$$$, $$$0 \le A, R, M \le 10^{4}$$$)Β β€” the number of pillars and the costs of operations. The second line contains $$$N$$$ integers $$$h_{i}$$$ ($$$0 \le h_{i} \le 10^{9}$$$)Β β€” initial heights of pillars.
2,100
Print one integerΒ β€” the minimal cost of restoration.
standard output
PASSED
75362a252ebcd65b05092d0e51352f91
train_002.jsonl
1589628900
You have to restore the wall. The wall consists of $$$N$$$ pillars of bricks, the height of the $$$i$$$-th pillar is initially equal to $$$h_{i}$$$, the height is measured in number of bricks. After the restoration all the $$$N$$$ pillars should have equal heights.You are allowed the following operations: put a brick on top of one pillar, the cost of this operation is $$$A$$$; remove a brick from the top of one non-empty pillar, the cost of this operation is $$$R$$$; move a brick from the top of one non-empty pillar to the top of another pillar, the cost of this operation is $$$M$$$.You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes $$$0$$$.What is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.util.*; 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); Hola solver = new Hola(); solver.solve(1, in, out); out.close(); } static class Hola { PrintWriter out; InputReader in; long mod = (long)1e9 + 7; int MAXN = (int)2e5 + 5; long[] sum = new long[MAXN]; long[] cnts = new long[MAXN]; TreeSet<Long> tset = new TreeSet<>(); HashMap<Long, Integer> hmap = new HashMap<>(); long A, R, M; long f(long x, int n){ int left_id = hmap.get(tset.lower(x)); int right_id = hmap.get(tset.ceiling(x)); long left = x * cnts[left_id] - sum[left_id]; if(!tset.contains(x)) right_id--; long right = (sum[n] - sum[right_id]) - (x * (cnts[n] - cnts[right_id])); if(x == 4){ // pn(left_id +" "+right_id +" "+left +" "+right); } long min = left * A + right * R; if(left <= right) min = Math.min(min, left * M + (right - left) * R); else min = Math.min(min, right * M + (left - right) * A); if(x == 4){ // pn(min +" +hola"); } return min; } public void solve(int testNumber, InputReader in, PrintWriter out) { this.out = out; this.in = in; int n = ni(); A = nl(); R = nl(); M = nl(); long[] arr = new long[n]; int i = 0; for(i = 0; i < n; i++) tset.add(arr[i] = nl()); i = 1; long[] which = new long[n + 2]; for(long x : tset) { hmap.put(x, i); which[i++] = x; } tset.add(0L); tset.add((long)1e18); hmap.put(0L, 0); sort(arr); for(i = 0; i < n; i++) { sum[hmap.get(arr[i])] += arr[i]; cnts[hmap.get(arr[i])]++; } for(i = 1; i <= n + 1; i++) { sum[i] += sum[i - 1]; cnts[i] += cnts[i - 1]; } long lo = arr[0] - 1, hi = arr[n - 1]; while(hi - lo > 1){ long mid = (lo + hi) >> 1; //System.out.println(mid); if(f(mid, n) < f(mid + 1, n)) hi = mid; else lo = mid; } //pn(lo + 1); long min = f(lo + 1, n); //pn(f(4, n)); /*for(i = 1; i <= hmap.size(); i++){ long left = which[i] * cnts[i - 1] - sum[i - 1]; long right = sum[n] - sum[i] - which[i] * (cnts[n] - cnts[i]); min = Math.min(min, left * A + right * R); if(left <= right) min = Math.min(min, left * M + (right - left) * R); else min = Math.min(min, right * M + (left - right) * A); }*/ pn(min); } void sort(long[] A){ PriorityQueue<Long> pq = new PriorityQueue(); int i = 0; for(i = 0; i < A.length; i++) pq.add(A[i]); for(i = 0; i < A.length; i++) A[i] = pq.poll(); } int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } long mul(long a,long b){ if(a>=mod)a%=mod; if(b>=mod)b%=mod; a*=b; if(a>=mod)a%=mod; return a; } long modPow(long a, long p){ long o = 1; while(p>0){ if((p&1)==1)o = mul(o,a); a = mul(a,a); p>>=1; } return o; } final Comparator<Tuple> com = new Comparator<Tuple>() { @Override public int compare(Tuple t1, Tuple t2) { if(t1.x != t2.x) return Long.compare(t1.x, t2.x); else return Long.compare(t1.y, t2.y); } }; class Tuple{ int x, y, z; Tuple(int x, int y){ this.x = x; this.y = y; } } int ni() { return in.nextInt(); } long nl() { return in.nextLong(); } void pn(Object o) { out.println(o); } void p(Object o) { out.print(o); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new UnknownError(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new UnknownError(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
Java
["3 1 100 100\n1 3 8", "3 100 1 100\n1 3 8", "3 100 100 1\n1 3 8", "5 1 2 4\n5 5 3 6 5", "5 1 2 2\n5 5 3 6 5"]
1 second
["12", "9", "4", "4", "3"]
null
Java 11
standard input
[ "greedy", "math", "sortings", "binary search", "ternary search" ]
c9c65223b8575676647fe943f8dff25a
The first line of input contains four integers $$$N$$$, $$$A$$$, $$$R$$$, $$$M$$$ ($$$1 \le N \le 10^{5}$$$, $$$0 \le A, R, M \le 10^{4}$$$)Β β€” the number of pillars and the costs of operations. The second line contains $$$N$$$ integers $$$h_{i}$$$ ($$$0 \le h_{i} \le 10^{9}$$$)Β β€” initial heights of pillars.
2,100
Print one integerΒ β€” the minimal cost of restoration.
standard output
PASSED
4e1cc68c8ea2d93cbf4478a279445132
train_002.jsonl
1589628900
You have to restore the wall. The wall consists of $$$N$$$ pillars of bricks, the height of the $$$i$$$-th pillar is initially equal to $$$h_{i}$$$, the height is measured in number of bricks. After the restoration all the $$$N$$$ pillars should have equal heights.You are allowed the following operations: put a brick on top of one pillar, the cost of this operation is $$$A$$$; remove a brick from the top of one non-empty pillar, the cost of this operation is $$$R$$$; move a brick from the top of one non-empty pillar to the top of another pillar, the cost of this operation is $$$M$$$.You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes $$$0$$$.What is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.util.*; 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); Hola solver = new Hola(); solver.solve(1, in, out); out.close(); } static class Hola { PrintWriter out; InputReader in; long mod = (long)1e9 + 7; int MAXN = (int)2e5 + 5; long[] sum = new long[MAXN]; long[] cnts = new long[MAXN]; TreeSet<Long> tset = new TreeSet<>(); HashMap<Long, Integer> hmap = new HashMap<>(); long A, R, M; long f(long x, int n){ int left_id = hmap.get(tset.lower(x)); int right_id = hmap.get(tset.ceiling(x)); long left = x * cnts[left_id] - sum[left_id]; if(!tset.contains(x)) right_id--; long right = (sum[n] - sum[right_id]) - (x * (cnts[n] - cnts[right_id])); if(x == 4){ // pn(left_id +" "+right_id +" "+left +" "+right); } long min = left * A + right * R; if(left <= right) min = Math.min(min, left * M + (right - left) * R); else min = Math.min(min, right * M + (left - right) * A); if(x == 4){ // pn(min +" +hola"); } return min; } public void solve(int testNumber, InputReader in, PrintWriter out) { this.out = out; this.in = in; int n = ni(); A = nl(); R = nl(); M = nl(); long[] arr = new long[n]; int i = 0; for(i = 0; i < n; i++) tset.add(arr[i] = nl()); i = 1; long[] which = new long[n + 2]; for(long x : tset) { hmap.put(x, i); which[i++] = x; } tset.add(0L); tset.add((long)1e18); hmap.put(0L, 0); sort(arr); for(i = 0; i < n; i++) { sum[hmap.get(arr[i])] += arr[i]; cnts[hmap.get(arr[i])]++; } for(i = 1; i <= n + 1; i++) { sum[i] += sum[i - 1]; cnts[i] += cnts[i - 1]; } long lo = arr[0] - 1, hi = arr[n - 1]; while(hi - lo > 1){ long mid = (lo + hi) >> 1; //System.out.println(mid); if(f(mid, n) < f(mid + 1, n)) hi = mid; else lo = mid; } //pn(lo + 1); long min = f(lo + 1, n); //pn(f(4, n)); /*for(i = 1; i <= hmap.size(); i++){ long left = which[i] * cnts[i - 1] - sum[i - 1]; long right = sum[n] - sum[i] - which[i] * (cnts[n] - cnts[i]); min = Math.min(min, left * A + right * R); if(left <= right) min = Math.min(min, left * M + (right - left) * R); else min = Math.min(min, right * M + (left - right) * A); }*/ pn(min); } void sort(long[] A){ PriorityQueue<Long> pq = new PriorityQueue(); int i = 0; for(i = 0; i < A.length; i++) pq.add(A[i]); for(i = 0; i < A.length; i++) A[i] = pq.poll(); } int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } long mul(long a,long b){ if(a>=mod)a%=mod; if(b>=mod)b%=mod; a*=b; if(a>=mod)a%=mod; return a; } long modPow(long a, long p){ long o = 1; while(p>0){ if((p&1)==1)o = mul(o,a); a = mul(a,a); p>>=1; } return o; } final Comparator<Tuple> com = new Comparator<Tuple>() { @Override public int compare(Tuple t1, Tuple t2) { if(t1.x != t2.x) return Long.compare(t1.x, t2.x); else return Long.compare(t1.y, t2.y); } }; class Tuple{ int x, y, z; Tuple(int x, int y){ this.x = x; this.y = y; } } int ni() { return in.nextInt(); } long nl() { return in.nextLong(); } void pn(Object o) { out.println(o); } void p(Object o) { out.print(o); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new UnknownError(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new UnknownError(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
Java
["3 1 100 100\n1 3 8", "3 100 1 100\n1 3 8", "3 100 100 1\n1 3 8", "5 1 2 4\n5 5 3 6 5", "5 1 2 2\n5 5 3 6 5"]
1 second
["12", "9", "4", "4", "3"]
null
Java 11
standard input
[ "greedy", "math", "sortings", "binary search", "ternary search" ]
c9c65223b8575676647fe943f8dff25a
The first line of input contains four integers $$$N$$$, $$$A$$$, $$$R$$$, $$$M$$$ ($$$1 \le N \le 10^{5}$$$, $$$0 \le A, R, M \le 10^{4}$$$)Β β€” the number of pillars and the costs of operations. The second line contains $$$N$$$ integers $$$h_{i}$$$ ($$$0 \le h_{i} \le 10^{9}$$$)Β β€” initial heights of pillars.
2,100
Print one integerΒ β€” the minimal cost of restoration.
standard output
PASSED
90b7b6c85b5bc3f4edbcac1c8a56bd2c
train_002.jsonl
1589628900
You have to restore the wall. The wall consists of $$$N$$$ pillars of bricks, the height of the $$$i$$$-th pillar is initially equal to $$$h_{i}$$$, the height is measured in number of bricks. After the restoration all the $$$N$$$ pillars should have equal heights.You are allowed the following operations: put a brick on top of one pillar, the cost of this operation is $$$A$$$; remove a brick from the top of one non-empty pillar, the cost of this operation is $$$R$$$; move a brick from the top of one non-empty pillar to the top of another pillar, the cost of this operation is $$$M$$$.You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes $$$0$$$.What is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height?
256 megabytes
import java.io.*; import java.util.*; public class Main { private long cal(List<Integer> h, long rh, int A, int R, int M) { long top = 0, down = 0; for (int hv : h) { top += Math.max(0, hv - rh); down += Math.max(0, rh - hv); } if (top > down) { return (top - down) * R + M * down; } else { return (down - top) * A + M * top; } } private void solve() { int N = sc.nextInt(); int A = sc.nextInt(); int R = sc.nextInt(); int M = sc.nextInt(); M = Math.min(M, A + R); long top = 0; long down = 0; List<Integer> h = new ArrayList<>(); for (int i = 0; i < N; i++) { h.add(sc.nextInt()); top += h.get(i); } Collections.sort(h); long ans = Long.MAX_VALUE; ans = Math.min(ans, cal(h, top / N, A, R, M)); ans = Math.min(ans, cal(h, top / N + 1, A, R, M)); ans = Math.min(ans,top * R); for (int i = 0; i < N; i++) { long delta = h.get(i) - (i == 0 ? 0 : h.get(i - 1)); top -= delta * (N - i); down += delta * i; if (top > down) { ans = Math.min(ans, (top - down) * R + M * down); } else { ans = Math.min(ans, (down - top) * A + M * top); } } out.println(ans); } private static PrintWriter out; private static MyScanner sc; private static class MyScanner { BufferedReader br; StringTokenizer st; private 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()); } } public static void main(String[] args) { out = new PrintWriter(new BufferedOutputStream(System.out)); sc = new MyScanner(); new Main().solve(); out.close(); } }
Java
["3 1 100 100\n1 3 8", "3 100 1 100\n1 3 8", "3 100 100 1\n1 3 8", "5 1 2 4\n5 5 3 6 5", "5 1 2 2\n5 5 3 6 5"]
1 second
["12", "9", "4", "4", "3"]
null
Java 11
standard input
[ "greedy", "math", "sortings", "binary search", "ternary search" ]
c9c65223b8575676647fe943f8dff25a
The first line of input contains four integers $$$N$$$, $$$A$$$, $$$R$$$, $$$M$$$ ($$$1 \le N \le 10^{5}$$$, $$$0 \le A, R, M \le 10^{4}$$$)Β β€” the number of pillars and the costs of operations. The second line contains $$$N$$$ integers $$$h_{i}$$$ ($$$0 \le h_{i} \le 10^{9}$$$)Β β€” initial heights of pillars.
2,100
Print one integerΒ β€” the minimal cost of restoration.
standard output
PASSED
c2fcee1c67471039e6f34f3a0d196b55
train_002.jsonl
1589628900
You have to restore the wall. The wall consists of $$$N$$$ pillars of bricks, the height of the $$$i$$$-th pillar is initially equal to $$$h_{i}$$$, the height is measured in number of bricks. After the restoration all the $$$N$$$ pillars should have equal heights.You are allowed the following operations: put a brick on top of one pillar, the cost of this operation is $$$A$$$; remove a brick from the top of one non-empty pillar, the cost of this operation is $$$R$$$; move a brick from the top of one non-empty pillar to the top of another pillar, the cost of this operation is $$$M$$$.You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes $$$0$$$.What is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height?
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Random; import java.util.StringTokenizer; import java.util.TreeSet; public class Main { public static void main(String[] args){ new Thread(null, null, "Anshum Gupta", 99999999) { public void run() { try { solve(); } catch(Exception e) { e.printStackTrace(); System.exit(1); } } }.start(); } static final long mxx = (long)(1e18 + 5); static final int mxN = (int)(1e6); static final int mxV = (int)(1e6), log = 18; static final long MOD = 998244353; static final int INF = (int)1e9; static boolean[]vis; static ArrayList<ArrayList<Integer>> adj; static int n, m, q, k; static int N, M, R, A; static int[] h; static char[]str; public static void solve() throws Exception { // solve the problem here s = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out), true); // out = new PrintWriter("output.txt"); int tc = 1;//s.nextInt(); for(int i=1; i<=tc; i++) { // out.print("Case #" + i + ": "); testcase(); } out.flush(); out.close(); } static long find(int x) { long totalLess = 0, totalMore = 0; for(int i = 0; i < N; i++) { if(h[i] < x) totalLess += x - h[i]; if(h[i] > x) totalMore += h[i] - x; } if(totalLess < totalMore) return totalLess * M + (totalMore - totalLess) * R; else return totalMore * M + (totalLess - totalMore) * A; } static void testcase() { N = s.nextInt(); A = s.nextInt(); R = s.nextInt(); M = s.nextInt(); M = Math.min(M, A + R); h = s.nextIntArray(N); int left = 0, right = INF; while(right - left > 3) { int mid = (right + left) >> 1; if(find(mid) < find(mid + 1)) right = mid + 1; else left = mid; } long ans = mxx; for(int i = left; i <= right; i++) { ans = Math.min(ans, find(i)); } out.println(ans); } public static PrintWriter out; public static MyScanner s; static void shuffleArray(int[] a) { Random random = new Random(); for (int i = a.length-1; i > 0; i--) { int index = random.nextInt(i + 1); int tmp = a[index]; a[index] = a[i]; a[i] = tmp; } } static void shuffleSort(int[] a) { shuffleArray(a); Arrays.parallelSort(a); } static void shuffleArray(long[] a) { Random random = new Random(); for (int i = a.length-1; i > 0; i--) { int index = random.nextInt(i + 1); long tmp = a[index]; a[index] = a[i]; a[i] = tmp; } } static void shuffleSort(long[] a) { shuffleArray(a); Arrays.parallelSort(a); } static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public MyScanner(String fileName) { try { br = new BufferedReader(new FileReader(fileName)); } catch (FileNotFoundException e) { e.printStackTrace(); } } 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()); } int[] nextIntArray(int n){ int[]a = new int[n]; for(int i=0; i<n; i++) { a[i] = this.nextInt(); } return a; } long[] nextlongArray(int n) { long[]a = new long[n]; for(int i=0; i<n; i++) { a[i] = this.nextLong(); } return a; } Integer[] nextIntegerArray(int n){ Integer[]a = new Integer[n]; for(int i=0; i<n; i++) { a[i] = this.nextInt(); } return a; } Long[] nextLongArray(int n) { Long[]a = new Long[n]; for(int i=0; i<n; i++) { a[i] = this.nextLong(); } return a; } char[][] next2DCharArray(int n, int m){ char[][]arr = new char[n][m]; for(int i=0; i<n; i++) { arr[i] = this.next().toCharArray(); } return arr; } ArrayList<ArrayList<Integer>> readUndirectedUnweightedGraph(int n, int m) { ArrayList<ArrayList<Integer>>adj = new ArrayList<ArrayList<Integer>>(); for(int i=0; i<=n; i++)adj.add(new ArrayList<Integer>()); for(int i=0; i<m; i++) { int u = s.nextInt(); int v = s.nextInt(); adj.get(u).add(v); adj.get(v).add(u); } return adj; } ArrayList<ArrayList<Integer>> readDirectedUnweightedGraph(int n, int m) { ArrayList<ArrayList<Integer>>adj = new ArrayList<ArrayList<Integer>>(); for(int i=0; i<=n; i++)adj.add(new ArrayList<Integer>()); for(int i=0; i<m; i++) { int u = s.nextInt(); int v = s.nextInt(); adj.get(u).add(v); } return adj; } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3 1 100 100\n1 3 8", "3 100 1 100\n1 3 8", "3 100 100 1\n1 3 8", "5 1 2 4\n5 5 3 6 5", "5 1 2 2\n5 5 3 6 5"]
1 second
["12", "9", "4", "4", "3"]
null
Java 11
standard input
[ "greedy", "math", "sortings", "binary search", "ternary search" ]
c9c65223b8575676647fe943f8dff25a
The first line of input contains four integers $$$N$$$, $$$A$$$, $$$R$$$, $$$M$$$ ($$$1 \le N \le 10^{5}$$$, $$$0 \le A, R, M \le 10^{4}$$$)Β β€” the number of pillars and the costs of operations. The second line contains $$$N$$$ integers $$$h_{i}$$$ ($$$0 \le h_{i} \le 10^{9}$$$)Β β€” initial heights of pillars.
2,100
Print one integerΒ β€” the minimal cost of restoration.
standard output
PASSED
26b78ea984f6e5b27f131e0222aa9eec
train_002.jsonl
1589628900
You have to restore the wall. The wall consists of $$$N$$$ pillars of bricks, the height of the $$$i$$$-th pillar is initially equal to $$$h_{i}$$$, the height is measured in number of bricks. After the restoration all the $$$N$$$ pillars should have equal heights.You are allowed the following operations: put a brick on top of one pillar, the cost of this operation is $$$A$$$; remove a brick from the top of one non-empty pillar, the cost of this operation is $$$R$$$; move a brick from the top of one non-empty pillar to the top of another pillar, the cost of this operation is $$$M$$$.You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes $$$0$$$.What is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height?
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class Main { static Scanner sc = new Scanner(System.in); static int N; static long A, R, M; static int[] H; static long[] sum; public static void main(String[] args) { N = sc.nextInt(); A = sc.nextInt(); R = sc.nextInt(); M = sc.nextInt(); ArrayList<Integer> HA = new ArrayList<>(); for (int i = 0; i < N; i++) { HA.add(Integer.parseInt(sc.next())); } Collections.sort(HA); H = new int[N]; for (int i = 0; i < N; i++) { H[i] = HA.get(i); } long lo = H[0]; long hi = H[N - 1]; sum = new long[N + 1]; for (int i = 0; i < N; i++) { sum[i + 1] = sum[i] + H[i]; H[i] *= 2; } while(hi - lo > 10000) { long m1 = (lo * 2 + hi) / 3; long m2 = (lo + hi * 2) / 3; long c1 = calcCost(m1); long c2 = calcCost(m2); if (c1 < c2) { hi = m2; } else { lo = m1; } } long ans = Long.MAX_VALUE; for (long i = lo; i <= hi; i++) { ans = Math.min(ans, calcCost(i)); } System.out.println(ans); } static long calcCost(long h) { int pos = -Arrays.binarySearch(H, (int)h * 2 + 1) - 2; long lo = (long) (pos + 1) * h - sum[pos + 1]; long hi = (sum[N] - sum[pos + 1]) - (long) (N - pos - 1) * h; if (lo < hi) { return Math.min(A + R, M) * lo + (hi - lo) * R; } else { return Math.min(A + R, M) * hi + (lo - hi) * A; } // System.err.println("i:" + i + " pos:" + pos + " lo:" + lo + " hi:" + hi + " cost:" + cost[i]); } }
Java
["3 1 100 100\n1 3 8", "3 100 1 100\n1 3 8", "3 100 100 1\n1 3 8", "5 1 2 4\n5 5 3 6 5", "5 1 2 2\n5 5 3 6 5"]
1 second
["12", "9", "4", "4", "3"]
null
Java 11
standard input
[ "greedy", "math", "sortings", "binary search", "ternary search" ]
c9c65223b8575676647fe943f8dff25a
The first line of input contains four integers $$$N$$$, $$$A$$$, $$$R$$$, $$$M$$$ ($$$1 \le N \le 10^{5}$$$, $$$0 \le A, R, M \le 10^{4}$$$)Β β€” the number of pillars and the costs of operations. The second line contains $$$N$$$ integers $$$h_{i}$$$ ($$$0 \le h_{i} \le 10^{9}$$$)Β β€” initial heights of pillars.
2,100
Print one integerΒ β€” the minimal cost of restoration.
standard output
PASSED
c0c6e6f50cee213a6bd6b38805bbc20e
train_002.jsonl
1589628900
You have to restore the wall. The wall consists of $$$N$$$ pillars of bricks, the height of the $$$i$$$-th pillar is initially equal to $$$h_{i}$$$, the height is measured in number of bricks. After the restoration all the $$$N$$$ pillars should have equal heights.You are allowed the following operations: put a brick on top of one pillar, the cost of this operation is $$$A$$$; remove a brick from the top of one non-empty pillar, the cost of this operation is $$$R$$$; move a brick from the top of one non-empty pillar to the top of another pillar, the cost of this operation is $$$M$$$.You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes $$$0$$$.What is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height?
256 megabytes
import java.io.*; import java.util.*; public class Main { int n,a,r,m,h[]; long cum[]; final long inf=(long)(1e18+1); private void solve()throws IOException { n=nextInt(); a=nextInt(); r=nextInt(); m=nextInt(); h=new int[n+1]; cum=new long[n+1]; for(int i=1;i<=n;i++) h[i]=nextInt(); Arrays.sort(h,1,n+1); for(int i=1;i<=n;i++) cum[i]=cum[i-1]+h[i]; long ans=inf; for(int i=1;i<=n;i++) { int low=h[i],high=i==n?h[i]:h[i+1]-1; long curr=inf; while(high-low>=3) { int m1=low+(high-low)/3; int m2=high-(high-low)/3; long f1=f(m1,i); long f2=f(m2,i); if(f1<f2) high=m2; else low=m1; } for(int x=low;x<=high;x++) curr=Math.min(curr,f(x,i)); ans=Math.min(ans,curr); } out.println(ans); } long f(int x,int i) { long add=1l*h[i]*i-cum[i]; long sub=-1l*h[i]*(n-i)+(cum[n]-cum[i]); add+=1l*i*(x-h[i]); sub-=1l*(n-i)*(x-h[i]); if(m<a+r) { long common=Math.min(add,sub); return 1l*common*m+(add>sub?1l*(add-sub)*a:1l*(sub-add)*r); } else return 1l*add*a+1l*sub*r; } /////////////////////////////////////////////////////////// public void run()throws IOException { br=new BufferedReader(new InputStreamReader(System.in)); st=null; out=new PrintWriter(System.out); solve(); br.close(); out.close(); } public static void main(String args[])throws IOException{ new Main().run(); } BufferedReader br; StringTokenizer st; PrintWriter out; String nextToken()throws IOException{ while(st==null || !st.hasMoreTokens()) st=new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine()throws IOException{ return br.readLine(); } int nextInt()throws IOException{ return Integer.parseInt(nextToken()); } long nextLong()throws IOException{ return Long.parseLong(nextToken()); } double nextDouble()throws IOException{ return Double.parseDouble(nextToken()); } }
Java
["3 1 100 100\n1 3 8", "3 100 1 100\n1 3 8", "3 100 100 1\n1 3 8", "5 1 2 4\n5 5 3 6 5", "5 1 2 2\n5 5 3 6 5"]
1 second
["12", "9", "4", "4", "3"]
null
Java 11
standard input
[ "greedy", "math", "sortings", "binary search", "ternary search" ]
c9c65223b8575676647fe943f8dff25a
The first line of input contains four integers $$$N$$$, $$$A$$$, $$$R$$$, $$$M$$$ ($$$1 \le N \le 10^{5}$$$, $$$0 \le A, R, M \le 10^{4}$$$)Β β€” the number of pillars and the costs of operations. The second line contains $$$N$$$ integers $$$h_{i}$$$ ($$$0 \le h_{i} \le 10^{9}$$$)Β β€” initial heights of pillars.
2,100
Print one integerΒ β€” the minimal cost of restoration.
standard output
PASSED
2ea96d26410729040683acb831f4f24e
train_002.jsonl
1589628900
You have to restore the wall. The wall consists of $$$N$$$ pillars of bricks, the height of the $$$i$$$-th pillar is initially equal to $$$h_{i}$$$, the height is measured in number of bricks. After the restoration all the $$$N$$$ pillars should have equal heights.You are allowed the following operations: put a brick on top of one pillar, the cost of this operation is $$$A$$$; remove a brick from the top of one non-empty pillar, the cost of this operation is $$$R$$$; move a brick from the top of one non-empty pillar to the top of another pillar, the cost of this operation is $$$M$$$.You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes $$$0$$$.What is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.HashSet; import java.util.Iterator; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * 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; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); ERestorerDistance solver = new ERestorerDistance(); solver.solve(1, in, out); out.close(); } static class ERestorerDistance { public void solve(int testNumber, FastReader s, PrintWriter out) { int n = s.nextInt(); long a = s.nextLong(); long r = s.nextLong(); long m = s.nextLong(); long[] arr = s.nextLongArray(n); long[] sumTillNow = new long[n]; HashSet<Long> set = new HashSet<>(); for (int i = 0; i < n; i++) { set.add(arr[i]); } ERestorerDistance.arrays.sort(arr); sumTillNow[0] = arr[0]; for (int i = 1; i < n; i++) { sumTillNow[i] = sumTillNow[i - 1] + arr[i]; } Iterator<Long> iter = set.iterator(); long ans = Long.MAX_VALUE; while (iter.hasNext()) { long curr = iter.next(); int pos = ERestorerDistance.BS.binarySearchFirstOccurence(arr, curr); long sumLess = 0L; if (pos != 0) { sumLess = sumTillNow[pos - 1]; } int pos1 = ERestorerDistance.BS.binarySearchLastOccurence(arr, curr); long sumMore = 0L; if (pos1 != n - 1) { sumMore = sumTillNow[n - 1] - sumTillNow[pos1]; } long elementsLess = pos; long elementsMore = n - 1 - pos1; long a1 = (elementsLess * curr) - (sumLess); long a2 = sumMore - (elementsMore * curr); if (a1 > a2) { ans = Math.min(ans, Math.min((a1 * a) + (a2 * r), (a2 * m) + (a1 - a2) * a)); } else { ans = Math.min(ans, Math.min((a1 * a) + (a2 * r), (a1 * m) + (a2 - a1) * r)); } } long totalSum = sumTillNow[n - 1]; long reqd = (totalSum) / n - 1L; long surplus = 0L; long deficit = 0L; for (int i = 0; i < n; i++) { if (arr[i] < reqd) { deficit += (reqd - arr[i]); } else if (arr[i] > reqd) { surplus += (arr[i] - reqd); } } if (surplus <= deficit) { ans = Math.min(ans, (surplus * m) + (deficit - surplus) * a); } else { ans = Math.min(ans, (deficit * m) + (surplus - deficit) * r); } reqd = (totalSum) / n + 1L; surplus = 0L; deficit = 0L; for (int i = 0; i < n; i++) { if (arr[i] < reqd) { deficit += (reqd - arr[i]); } else if (arr[i] > reqd) { surplus += (arr[i] - reqd); } } if (surplus <= deficit) { ans = Math.min(ans, (surplus * m) + (deficit - surplus) * a); } else { ans = Math.min(ans, (deficit * m) + (surplus - deficit) * r); } reqd = (totalSum) / n; surplus = 0L; deficit = 0L; for (int i = 0; i < n; i++) { if (arr[i] < reqd) { deficit += (reqd - arr[i]); } else if (arr[i] > reqd) { surplus += (arr[i] - reqd); } } if (surplus <= deficit) { ans = Math.min(ans, (surplus * m) + (deficit - surplus) * a); } else { ans = Math.min(ans, (deficit * m) + (surplus - deficit) * r); } out.println(ans); } private static class BS { private static int binarySearchFirstOccurence(long[] arr, long ele) { int low = 0; int high = arr.length - 1; int ans = -1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] == ele) { ans = mid; high = mid - 1; } else if (ele < arr[mid]) { high = mid - 1; } else { low = mid + 1; } } return ans; } private static int binarySearchLastOccurence(long[] arr, long ele) { int low = 0; int high = arr.length - 1; int ans = -1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] == ele) { ans = mid; low = mid + 1; } else if (ele < arr[mid]) { high = mid - 1; } else { low = mid + 1; } } return ans; } } private static class arrays { static void merge(long arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; long L[] = new long[n1]; long R[] = new long[n2]; for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } static void sort(long arr[], int l, int r) { if (l < r) { int m = (l + r) / 2; sort(arr, l, m); sort(arr, m + 1, r); merge(arr, l, m, r); } } static void sort(long[] arr) { sort(arr, 0, arr.length - 1); } } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(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 nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public 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 long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; ++i) array[i] = nextLong(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["3 1 100 100\n1 3 8", "3 100 1 100\n1 3 8", "3 100 100 1\n1 3 8", "5 1 2 4\n5 5 3 6 5", "5 1 2 2\n5 5 3 6 5"]
1 second
["12", "9", "4", "4", "3"]
null
Java 11
standard input
[ "greedy", "math", "sortings", "binary search", "ternary search" ]
c9c65223b8575676647fe943f8dff25a
The first line of input contains four integers $$$N$$$, $$$A$$$, $$$R$$$, $$$M$$$ ($$$1 \le N \le 10^{5}$$$, $$$0 \le A, R, M \le 10^{4}$$$)Β β€” the number of pillars and the costs of operations. The second line contains $$$N$$$ integers $$$h_{i}$$$ ($$$0 \le h_{i} \le 10^{9}$$$)Β β€” initial heights of pillars.
2,100
Print one integerΒ β€” the minimal cost of restoration.
standard output
PASSED
87a0d60b4db63ef5fa347d8183d13d88
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.util.Scanner; public class Army { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int [] di= new int [n-1]; for(int i=0;i<di.length;i++) di[i]=sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); int sum=0; for(int i=a;i<=di.length&&i<b;i++) { sum+=di[i-1]; } System.out.print(sum); } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
82bb02c8989206b407a88f72cbf08bc5
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.util.Scanner; public class Two { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int n1=n-1; int [] tmp=new int [n1]; for(int i=0;i<n1;i++){ tmp[i]=sc.nextInt(); } int a=sc.nextInt(); int b=sc.nextInt(); int diff=b-a; int res=0; for (int i=a; i<b; i++) res+=tmp[i-1]; System.out.print(res); } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
b47e922e9a664ab99fdf3325353fea4e
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.util.*; public class fuchacm { public static void main(String []args){ int d=0; Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int arr[]=new int [n-1]; int s=0; for (int i = 0; i < n-1; i++) { arr[i]=sc.nextInt(); } int a=sc.nextInt();int b=sc.nextInt(); for(int i=a-1;i<b-1;i++){ s+=arr[i]; d++; } System.out.println(s); } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
a11ba1f8bbedd0811b6770ed18ea7a04
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Monty1{ public static void main(String[] args) { Scanner s=new Scanner(System.in); int a,b,sum=0,n,i; int[] z=new int[100]; n=s.nextInt(); z[0]=0; for (i = 1; i < n; i++) { z[i]=s.nextInt(); } a=s.nextInt(); b=s.nextInt(); for(i=a;i<=b;i++){ sum=sum+z[i-1]; } System.out.printf("%d",sum-z[a-1]); } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
330dde162c94a2d3acae414fdd04dd2a
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Atanas */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, Scanner in, PrintWriter out) { int N = in.nextInt(); int[] add = new int[N - 1]; for (int i = 0; i < N - 1; ++i) add[i] = in.nextInt(); int start = in.nextInt() - 1; int end = in.nextInt() - 1; int ret = 0; for (int j = start; j < Math.min(end, add.length); ++j) { ret += add[j]; } out.println(ret); } } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
b00da0073bb4afc273dd2c29f3043a98
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
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; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Nasko */ 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(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int N = in.nextInt(); int[] last = new int[N - 1]; for (int i = 0; i < N - 1; ++i) last[i] = in.nextInt(); int a = in.nextInt(); int b = in.nextInt(); int ret = 0; while (a < b) { ret += last[a - 1]; ++a; } out.println(ret); } } 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
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
56714bf4dc15e6e529ac354df4764409
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class CP { public static void main(String[] args) { InputStreamReader ir = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(ir); String line; String[] ar; try { int n = Integer.parseInt(br.readLine()); ar = br.readLine().split(" "); int[] a = new int[n]; a[0] = 0; for (int i = 1; i < n; i++) { a[i] = Integer.parseInt(ar[i - 1]); } ar = br.readLine().split(" "); int b = Integer.parseInt(ar[0]); int c = Integer.parseInt(ar[1]); int sum = 0; for (int i = b; i < c; i++) { sum += a[i]; } System.out.println(sum); } catch (Exception ex) { ex.printStackTrace(); } } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
f1c601f56997a0059fbaaa9a85ae8e32
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.util.Scanner; public class raef{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] arr=new int[n-1]; for(int i=0;i<n-1;i++){ int x=sc.nextInt(); arr[i]=x; } int a=sc.nextInt(); int b=sc.nextInt(); int years=0; for(int i=a-1;i<b-1;i++){ years+=arr[i]; } System.out.println(years); } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
d8f3890a7eb087a482f4dc4d34f0d65c
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.util.Scanner; public class Army { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int[]array = new int[n-1]; for (int i = 0; i < array.length; i++) { array[i] = scan.nextInt(); } int a = scan.nextInt(); int b = scan.nextInt(); int sum = 0; for (int i = a-1; i < b-1; i++) { sum+= array[i]; } System.out.println(sum); } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
dd89d652e6a669020b1922bd49132504
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.util.Scanner; public class Army { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int[]array = new int[n-1]; for (int i = 0; i < array.length; i++) { array[i] = scan.nextInt(); } int a = scan.nextInt(); int b = scan.nextInt(); int sum = 0; for (int i = a-1; i < b-1; i++) { sum+= array[i]; } System.out.println(sum); } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
973e332cfca67904aac7632b3c8dbdfd
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.lang.*; import java.util.*; import java.io.*; public class test { Scanner sc=new Scanner(System.in); PrintWriter pr=new PrintWriter(System.out,true); public static void main(String... args) { test c=new test(); c.prop(); } public void prop() { int n,x,y,sum=0; n=sc.nextInt(); int a[]=new int[n+3] ; a[0]=0 ; a[1]=0 ; for (int i=0;i<n-1 ;++i) { a[i+2]=sc.nextInt(); } x=sc.nextInt(); y=sc.nextInt(); for (int i=x+1;i<=y ;++i) { sum+=a[i] ; } pr.println(sum); } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
b1c05f467219350c79058ba7492313b6
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.util.Scanner; public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt() - 1; int[] d = new int[n]; for (int i = 0; i < n; i++) { d[i] = sc.nextInt(); } int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; int sum = 0; for (int i = a; i < b; i++) { sum += d[i]; } System.out.println(sum); } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
06b2ba463bffa94cf328ab26315bfff0
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.util.Scanner; public class Army { public static void main(String[] args) { Scanner scan= new Scanner(System.in); int n=scan.nextInt(); int[] a= new int[n]; int c=0; for(int i=0;i<n-1;i++) { a[i]=scan.nextInt(); } int a1= scan.nextInt(); int b=scan.nextInt(); int ans=0; for(int i=a1-1;i<b-1;i++) { ans+=a[i]; } System.out.println(ans); } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
ab73e38e037d9e31f9ecdf4dbd7245e5
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.io.IOException ; import java.io.BufferedReader ; import java.io.InputStreamReader ; import java.io.PrintWriter ; import java.util.StringTokenizer ; import java.util.*; public class MY { public static void main (String [] args) throws IOException { BufferedReader p = new BufferedReader (new InputStreamReader(System.in)) ; int n = Integer.parseInt(p.readLine()) ; StringTokenizer st = new StringTokenizer(p.readLine()) ; int [] r = new int [n ] ; r[0] = 0 ; for(int i = 1 ; i < n ; i ++ ) { r[i] = Integer.parseInt(st.nextToken()) ; } st = new StringTokenizer(p.readLine()) ; int r1 = Integer.parseInt(st.nextToken()) ; int r2 = Integer.parseInt(st.nextToken()) ; int a = 0 ; for(int i = r1 ; i <= r2-1 ; i ++ ) { a += r[i] ; } System.out.print(a) ; } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
17983c2bb6cc6edf496c8dd36eb487a2
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.util.*; import java.util.Map.Entry; import java.io.*; public class Q1 { static int[]d; static int[][]memo; public static void main(String[] args) throws Exception { Scanner sc= new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n=sc.nextInt(); d= new int [n-1]; memo = new int [n+1][n+1]; for (int i=0;i-1<n;i++) { Arrays.fill(memo[i], -1); } for(int i=0;i<d.length;i++) { d[i]=sc.nextInt(); } out.println(solve(sc.nextInt(),sc.nextInt())); out.flush(); } public static int solve(int a,int b) { if(memo[a][b]!=-1) return memo[a][b]; if (a==b) return 0; else memo[a][b]= d[a-1]+solve(a+1,b); return memo[a][b]; } public static int gcd(int a,int b) { return a%b==0?b:gcd(b,a%b); } public static List<List<String>> groupAnagrams(String[] strs) { List Solution = new ArrayList<ArrayList<String>>(); pair Sorted [] = new pair[strs.length]; for(int i=0;i<strs.length;i++) { char[] chars = strs[i].toCharArray(); Arrays.sort(chars); String sorted = new String(chars); Sorted[i]=new pair(strs[i],sorted); } Arrays.sort(Sorted); System.out.println(Arrays.toString(Sorted)); all: for(int i=0;i<strs.length;i++) { ArrayList<String> sol = new ArrayList<>(); sol.add(Sorted[i].org); if(strs.length==1||i==strs.length-1) { Solution.add(sol); break; } for(int j=i+1;j<strs.length;j++) { if (Sorted[i].sorted.equals(Sorted[j].sorted)) { sol.add(Sorted[j].org); if(j==strs.length-1) { Solution.add(sol); break all; } } else { i=j-1; Solution.add(sol); break; } } } System.out.println(Solution); return Solution; } public static void rotate(int[][] matrix) { int [][] res= new int [matrix.length][matrix.length]; for (int i=0;i<res.length;i++) { for(int j=0;j<res.length;j++) { res[j][matrix.length-1-i]=matrix[i][j]; } } for (int i=0;i<res.length;i++) { for(int j=0;j<res.length;j++) { matrix[i][j]=res[i][j]; } } } public static int lengthOfLongestSubstring(String s) { int sol=0; HashSet <Character> seenSoFar= new HashSet<Character>(); int maxsofar=0; int lastVisited = 0; for(int i=0;i<s.length();i++) { System.out.println("I am now visiting :"+s.charAt(i)); if (!seenSoFar.contains(s.charAt(i))) { System.out.println("i was not found in the hashset"); maxsofar++; sol=Math.max(sol,maxsofar); seenSoFar.add(s.charAt(i)); } else { System.out.println("I was found in the hashset"); sol=Math.max(maxsofar, sol); seenSoFar=new HashSet<>(); maxsofar=0; i=lastVisited; lastVisited++; } } return sol; } public static int[] findOrder(int numCourses, int[][] prerequisites) { int [] sol=new int [numCourses]; ArrayList<Integer>adjList[] = new ArrayList[numCourses]; for (int i = 0; i < adjList.length; i++) { adjList[i]=new ArrayList<>(); } HashSet<Integer> notroot = new HashSet<>(); for(int []p:prerequisites) { adjList[p[1]].add(p[0]); notroot.add(p[0]); } ArrayList<Integer> root= new ArrayList<>(); for (int i=0;i<numCourses;i++) { if (!notroot.contains(i)) { root.add(i); } } boolean v[]=new boolean[numCourses]; bfs(root,v,adjList,sol); System.out.println(Arrays.toString(sol)); return sol; } static void bfs(ArrayList<Integer> s,boolean visited [], ArrayList<Integer>adjList[],int ans[]) { int i=0; Queue<Integer> q = new LinkedList<Integer>(); for(int c:s) { q.add(c); ans[i++]=c; visited[c] = true; System.out.println(Arrays.toString(ans)); } while(!q.isEmpty()) { int u = q.remove(); for(int v: adjList[u]) if(!visited[v]) { System.out.println(Arrays.toString(ans)); visited[v] = true; ans[i++]=v; q.add(v); } } } } class pair implements Comparable<pair>{ String org; String sorted; pair(String n,String p){ org=n; sorted=p;} @Override public int compareTo(pair o) { return sorted.compareTo(o.sorted); } @Override public String toString() { return org+":"+sorted; } } class Scanner{ StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system));} public Scanner(String file) throws Exception{br = new BufferedReader(new FileReader (file));} public String next() throws IOException{ while (st==null|| !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException{ return Integer.parseInt(next()); } public long nextLong() throws IOException{ return Long.parseLong(next()); } public String nextLine() throws IOException{ return br.readLine(); } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
a8e65303c3671bea9da1f5364c77c4e4
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.util.*; import java.util.Map.Entry; import java.io.*; public class Q1 { static int[]d; public static void main(String[] args) throws Exception { Scanner sc= new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n=sc.nextInt(); d= new int [n-1]; for(int i=0;i<d.length;i++) { d[i]=sc.nextInt(); } out.println(solve(sc.nextInt(),sc.nextInt())); out.flush(); } public static int solve(int a,int b) { if (a==b) return 0; else return d[a-1]+solve(a+1,b); } public static int gcd(int a,int b) { return a%b==0?b:gcd(b,a%b); } public static List<List<String>> groupAnagrams(String[] strs) { List Solution = new ArrayList<ArrayList<String>>(); pair Sorted [] = new pair[strs.length]; for(int i=0;i<strs.length;i++) { char[] chars = strs[i].toCharArray(); Arrays.sort(chars); String sorted = new String(chars); Sorted[i]=new pair(strs[i],sorted); } Arrays.sort(Sorted); System.out.println(Arrays.toString(Sorted)); all: for(int i=0;i<strs.length;i++) { ArrayList<String> sol = new ArrayList<>(); sol.add(Sorted[i].org); if(strs.length==1||i==strs.length-1) { Solution.add(sol); break; } for(int j=i+1;j<strs.length;j++) { if (Sorted[i].sorted.equals(Sorted[j].sorted)) { sol.add(Sorted[j].org); if(j==strs.length-1) { Solution.add(sol); break all; } } else { i=j-1; Solution.add(sol); break; } } } System.out.println(Solution); return Solution; } public static void rotate(int[][] matrix) { int [][] res= new int [matrix.length][matrix.length]; for (int i=0;i<res.length;i++) { for(int j=0;j<res.length;j++) { res[j][matrix.length-1-i]=matrix[i][j]; } } for (int i=0;i<res.length;i++) { for(int j=0;j<res.length;j++) { matrix[i][j]=res[i][j]; } } } public static int lengthOfLongestSubstring(String s) { int sol=0; HashSet <Character> seenSoFar= new HashSet<Character>(); int maxsofar=0; int lastVisited = 0; for(int i=0;i<s.length();i++) { System.out.println("I am now visiting :"+s.charAt(i)); if (!seenSoFar.contains(s.charAt(i))) { System.out.println("i was not found in the hashset"); maxsofar++; sol=Math.max(sol,maxsofar); seenSoFar.add(s.charAt(i)); } else { System.out.println("I was found in the hashset"); sol=Math.max(maxsofar, sol); seenSoFar=new HashSet<>(); maxsofar=0; i=lastVisited; lastVisited++; } } return sol; } public static int[] findOrder(int numCourses, int[][] prerequisites) { int [] sol=new int [numCourses]; ArrayList<Integer>adjList[] = new ArrayList[numCourses]; for (int i = 0; i < adjList.length; i++) { adjList[i]=new ArrayList<>(); } HashSet<Integer> notroot = new HashSet<>(); for(int []p:prerequisites) { adjList[p[1]].add(p[0]); notroot.add(p[0]); } ArrayList<Integer> root= new ArrayList<>(); for (int i=0;i<numCourses;i++) { if (!notroot.contains(i)) { root.add(i); } } boolean v[]=new boolean[numCourses]; bfs(root,v,adjList,sol); System.out.println(Arrays.toString(sol)); return sol; } static void bfs(ArrayList<Integer> s,boolean visited [], ArrayList<Integer>adjList[],int ans[]) { int i=0; Queue<Integer> q = new LinkedList<Integer>(); for(int c:s) { q.add(c); ans[i++]=c; visited[c] = true; System.out.println(Arrays.toString(ans)); } while(!q.isEmpty()) { int u = q.remove(); for(int v: adjList[u]) if(!visited[v]) { System.out.println(Arrays.toString(ans)); visited[v] = true; ans[i++]=v; q.add(v); } } } } class pair implements Comparable<pair>{ String org; String sorted; pair(String n,String p){ org=n; sorted=p;} @Override public int compareTo(pair o) { return sorted.compareTo(o.sorted); } @Override public String toString() { return org+":"+sorted; } } class Scanner{ StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system));} public Scanner(String file) throws Exception{br = new BufferedReader(new FileReader (file));} public String next() throws IOException{ while (st==null|| !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException{ return Integer.parseInt(next()); } public long nextLong() throws IOException{ return Long.parseLong(next()); } public String nextLine() throws IOException{ return br.readLine(); } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
67ce3ee65f22a647a8b1145636b04ce0
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int arr[] = new int[n]; for (int i = 0; i < n - 1; i++) { arr[i] = in.nextInt(); } int a = in.nextInt(); int b = in.nextInt(); int sum = 0; for (; a < b; a++) { sum += arr[a - 1]; } System.out.println(sum); } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
21938edbaa688f823c0cac9598ce214a
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = in.nextInt(); int[] arr = new int[a - 1]; int sum = 0; for (int i = 0; i < arr.length; i++) { arr[i] = in.nextInt(); } int a1 = in.nextInt(); int b1 = in.nextInt(); for (; a1 <b1; a1++) { sum += arr[a1-1]; } System.out.println(sum); } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
eb5c96ef13b7d9c41df0971a26baf733
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scn=new Scanner(System.in); int n=Integer.parseInt(scn.nextLine()); int[] container=new int[n-1]; for(int i=0; i<n-1; i++) container[i]=scn.nextInt(); int a=scn.nextInt(); int b=scn.nextInt(); int sum=0; for(int i=a-1; i<b-1; i++) sum+=container[i]; System.out.println(sum); } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
58d3dd0f202eeed618cca3487cf89195
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.util.Scanner; public class bobosdf { public static void main(String[] args) { Scanner in = new Scanner(System.in); int x = in.nextInt(); int[] levels = new int[x]; for (int i = 0; i < x-1; i++) { levels[i] = in.nextInt(); } int a = in.nextInt(); int b = in.nextInt(); int result = b-a; int total = 0; for (int i = a-1; i < b-1;i++) { total+= levels[i]; } System.out.println(total); } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
15b8a507b4df26e34d46bc733681d8ec
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.util.Scanner; public class rank { public static void main(String[] args) { Scanner cin=new Scanner(System.in); int rank=cin.nextInt(); int []year=new int[rank-1]; int sum=0; for(int i=0;i<rank-1;i++) { year[i]=cin.nextInt(); } int a=cin.nextInt(); int b=cin.nextInt(); for (int i = a-1; i < b-1; i++) { sum+=year[i]; } System.out.println(sum); } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
e0b78d490c8b291c6f4bbfeb56404714
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.util.Scanner; public class chion { public static void main(String[] args) { Scanner cin=new Scanner(System.in); int arr[]=new int[cin.nextInt()]; arr[1]=cin.nextInt(); for (int i = 2; i < arr.length; i++) { arr[i]=cin.nextInt(); arr[i]+=arr[i-1]; } int i=cin.nextInt()-1; int j=cin.nextInt()-1; System.out.println(arr[j]-arr[i]); } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
b09ae88eb247dc850e98fe42fbfc47c6
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.util.*; public class Army { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int ar[]=new int[n-1]; for(int i=0;i<n-1;i++) ar[i]=sc.nextInt(); int a=sc.nextInt(); int b=sc.nextInt(); int sum=0; for(int i=a-1;i<b-1;i++) { sum=sum+ar[i]; } System.out.println(sum); } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
a78c5e0aa6300db5745b9457853097f5
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.io.*; import java.math.*; import java.text.*; import java.util.*; import java.util.regex.*; public class Solution{ InputStream is; PrintWriter out; String INPUT = ""; Random r = new Random(); Scanner in=new Scanner(System.in); long mod=1000000007l; public void solve(){ int n=ni(); int[] arr=new int[n]; for(int i=1;i<n;i++){ arr[i]=ni(); } for(int i=1;i<n;i++){ arr[i]+=arr[i-1]; } int a=ni()-1; int b=ni()-1; //out.println(Arrays.toString(arr)); out.println(Math.abs(arr[a]-arr[b])); } public void run(){ is = new DataInputStream(System.in); out = new PrintWriter(System.out); int t=1;while(t-- > 0)solve(); out.flush(); } public static void main(String[] args){new Solution().run();} //Fast I/O code is copied from uwi code. private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte(){ if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns(){ int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n){ char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } static int i(long n){ return (int)Math.round(n); } private char[][] nm(int n, int m){ char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n){ int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni(){ int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl(){ long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
f376f3ff3a81c161c6be9834008ab6b3
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Hello{ InputStream is; PrintWriter out; String INPUT = ""; Scanner in=new Scanner(System.in); public void solve(){ int n=ni(); int[] arr=new int[n+1]; for(int i=2;i<=n;i++){ arr[i]+=arr[i-1]+ni(); } int a=ni(); int b=ni(); out.println(arr[b]-arr[a]); } void run(){ is = new DataInputStream(System.in); out = new PrintWriter(System.out); solve(); out.flush(); } public static void main(String[] args){new Hello().run();} private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte(){ if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private int[] na(int n){ int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni(){ int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl(){ long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
99466c823e82e274beb5a4e67d071adb
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int [] year = new int [n-1]; for (int i = 0; i < year.length; i++) { year [i]= input.nextInt(); } int a = input.nextInt(); int b = input.nextInt(); int total = 0; for (int i = a-1; i < b-1; i++) { total += year[i]; } System.out.println(total); } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output
PASSED
3dac7c34236afdc3de7f21ab61cb5635
train_002.jsonl
1288440000
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
256 megabytes
import java.util.*; public class CodeForces { public static void main(String[] args){ //BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); Scanner input=new Scanner(System.in); int n=input.nextInt(); int sum=0; int []years=new int[n-1]; for(int i=0;i<years.length;i++){ years[i]=input.nextInt(); } int a=input.nextInt(); int b=input.nextInt(); for(;a<b;a++){ sum+=years[a-1]; } System.out.println(sum); } }
Java
["3\n5 6\n1 2", "3\n5 6\n1 3"]
2 seconds
["5", "11"]
null
Java 8
standard input
[ "implementation" ]
69850c2af99d60711bcff5870575e15e
The first input line contains an integer n (2 ≀ n ≀ 100). The second line contains n - 1 integers di (1 ≀ di ≀ 100). The third input line contains two integers a and b (1 ≀ a &lt; b ≀ n). The numbers on the lines are space-separated.
800
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
standard output