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
94aea1dd4a5ce2ad3d7cf57a9b580a1b
train_001.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.util.HashMap; import java.util.HashSet; import java.util.Scanner; public class RotationMatching { public static void main(String args[]){ Scanner s=new Scanner(System.in); int n=s.nextInt(); int a[]=new int[n]; int b[]=new int[n]; for(int i=0;i<n;i++){ a[i]=s.nextInt()-1; } for(int i=0;i<n;i++){ b[i]=s.nextInt()-1; } greed(a,b,n); } static void greed(int[]a,int []b,int n){ /////corresponding index int crr[]=new int [n]; for(int i=0;i<n;i++){ int val=b[i]; crr[val]=i; } ////////////////lr int dp[]=new int[n]; for(int i=0;i<n;i++){ int val=i-crr[a[i]]; if(val>=0){ dp[i]=val; //dp[1][i]=(n-val); } else{ val=Math.abs(val); // dp[1][i]=val; dp[i]=(n-val); } } /////////////////// HashMap<Integer,Integer>hm=new HashMap<Integer, Integer>(); int max=1; for(int i=0;i<n;i++){ if(!hm.containsKey(dp[i])){ hm.put(dp[i],1); } else { int val=hm.get(dp[i]); val=val+1; hm.remove(dp[i]); hm.put(dp[i],val); if(max<val){ max=val; } } } System.out.println(max); ////////////////// // for(int i=0;i<n;i++){ // //System.out.print(dp[0][i]+" "); // System.out.print(crr[i]+" "); // } //////// } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 8
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$Β β€” the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ β€” the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ β€” the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
43e4f2f3a524641a1f0792ac91f45bf1
train_001.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.io.*; import java.util.*; public class CF1365C { static class Element implements Comparable<Element>{ int value; int position; Element(int value,int position){ this.value = value; this.position = position; } public int compareTo(Element other){ if(this.value == other.value){ return 0; } else if(this.value > other.value) return 1; else return -1; } } public static void main(String[] args) throws IOException{ Reader input = new Reader(); PrintWriter pw = new PrintWriter(System.out); // code goes here int n = input.nextInt(); int[] first = new int[n+1]; int[] second = new int[n+1]; Element[] arr = new Element[n+1]; for(int i = 1;i <= n;i++){ first[i] = input.nextInt(); arr[i] = new Element(first[i],i); } Arrays.sort(arr,1,n+1); // for(int i = 1;i <= n;i++){ // pw.println(arr[i].value + " " + arr[i].position); // } Map<Integer,Integer> map = new HashMap<Integer,Integer>(); for(int i = 1;i <= n;i++){ second[i] = input.nextInt(); int value = second[i]; int pos1 = i; int pos2 = arr[value].position; int shiftNeeded = pos2 - pos1; if(shiftNeeded < 0){ shiftNeeded = shiftNeeded + n; } // if(shiftNeeded == 4){ // pw.println("For " + value + " pos1 " + pos1 + " " + "pos2 " + pos2); // } if(map.get(shiftNeeded) == null){ map.put(shiftNeeded,1); } else{ map.put(shiftNeeded,map.get(shiftNeeded) + 1); } } int max = Integer.MIN_VALUE; for(Map.Entry m : map.entrySet()){ // pw.println(m.getKey() + " " + m.getValue()); max = Math.max(max,(int)m.getValue()); } pw.println(max); pw.flush(); pw.close(); } 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(); } } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 8
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$Β β€” the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ β€” the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ β€” the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
7952c995136c2c564eb21e07a3e01fd8
train_001.jsonl
1591540500
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner; public class Task648C { public static void main(String[] args) { Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); int t = 1;//in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc. int n; int a[] = new int[200_001]; int d[] = new int[200_001]; n = in.nextInt(); for (int j = 0; j < n; j++) { a[in.nextInt()] = j; } for (int j = 0; j < n; j++) { final int i = in.nextInt(); a[i] -= j; if (a[i] < 0) { a[i] += n; } } for (int j = 1; j <= n; j++) { d[a[j]]++; } System.out.println(Arrays.stream(d).max().getAsInt()); } }
Java
["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"]
1 second
["5", "1", "2"]
NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.
Java 8
standard input
[ "data structures", "constructive algorithms", "implementation", "greedy" ]
eb1bb862dc2b0094383192f6998891c5
The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$Β β€” the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ β€” the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ β€” the elements of the second permutation.
1,400
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
standard output
PASSED
7f3bd21015e4a1388b6752d490b87188
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class Main { public static BufferedReader in; public static PrintWriter out; public static StringTokenizer tok; public static void main(String args[]) throws IOException { //in = new BufferedReader(new InputStreamReader(System.in)); //out = new PrintWriter(new OutputStreamWriter(System.out)); in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter(new FileWriter("output.txt")); solve(); in.close(); out.close(); } static void solve() throws IOException { int n, k; n = nextInt(); k = nextInt(); ArrayList < node > list = new ArrayList<>(); for(int i=0; i<n; i++) { list.add( new node( nextInt(), i + 1 ) ); } Collections.sort( list ); out.println( list.get( k - 1 ).getVal() ); for(int i=0; i<k; i++) { out.print( list.get( i ) );; } } public static int nextInt() throws IOException { return Integer.parseInt(next()); } public static long nextLong() throws IOException { return Long.parseLong(next()); } public static double nextDouble() throws IOException { return Double.parseDouble(next()); } public static BigInteger nextBigInteger() throws IOException { return new BigInteger(next()); } public static String next() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } public static String nextLine() throws IOException { tok = new StringTokenizer(""); return in.readLine(); } public static boolean hasNext() throws IOException { while (tok == null || !tok.hasMoreTokens()) { String s = in.readLine(); if (s == null) { return false; } tok = new StringTokenizer(s); } return true; } } class node implements Comparable < node > { private int val; private int pos; public node(int val, int pos) { this.val = val; this.pos = pos; } public int compareTo(node other) { return Integer.compare( -this.val, -other.val ); } public String toString() { return this.pos + " "; } public int getVal() { return this.val; } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
7d97e3ef8254665226c45d4489c93a95
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.StringTokenizer; public class Reading { static BufferedReader in; static PrintWriter out; static StringTokenizer tok; static void solve() throws Exception { int n = nextInt(), k = nextInt(); List<Light> list = new ArrayList<>(); for (int i = 1; i <= n; i++) list.add(new Light(i, nextInt())); Collections.sort(list); out.println(list.get(n - k).level); for (int i = n - k; i < n; i++) out.print(list.get(i).index + " "); } public static void main(String args[]) { try { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter(new FileWriter("output.txt")); solve(); in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); System.exit(1); } } static int nextInt() throws IOException { return Integer.parseInt(next()); } static int[] nextIntArray(int len, int start) throws IOException { int[] a = new int[len]; for (int i = start; i < len; i++) a[i] = nextInt(); return a; } static long nextLong() throws IOException { return Long.parseLong(next()); } static long[] nextLongArray(int len, int start) throws IOException { long[] a = new long[len]; for (int i = start; i < len; i++) a[i] = nextLong(); return a; } static String next() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } } class Light implements Comparable<Light> { int index; int level; Light(int a, int b) { index = a; level = b; } @Override public int compareTo(Light o) { return level - o.level; } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
82ef1792c1914b6e25b7b702a701608a
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.io.*; import java.util.*; public class Reading implements Closeable { private InputReader in; private PrintWriter out; private Reading() throws IOException { in = new InputReader(new FileInputStream(new File("input.txt"))); out = new PrintWriter(new FileOutputStream(new File("output.txt"))); } public void solve() { int n = in.ni(), k = in.ni(); class Hour { int idx, power; Hour(int idx, int power) { this.idx = idx; this.power = -power; } } List<Hour> list = new ArrayList<>(); for (int i = 1; i <= n; i++) list.add(new Hour(i, in.ni())); Collections.sort(list, Comparator.comparingInt(a -> a.power)); out.println(-list.get(k - 1).power); for (int i = 0; i < k; i++) { out.print(list.get(i).idx); out.print(' '); } } @Override public void close() throws IOException { in.close(); out.close(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int ni() { return Integer.parseInt(next()); } public long nl() { return Long.parseLong(next()); } public void close() throws IOException { reader.close(); } } public static void main(String[] args) throws IOException { try (Reading instance = new Reading()) { instance.solve(); } } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
cf7fbe34ca9606d3d499ca613b5220e2
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.util.*; import java.io.*; public class Reading { /************************ SOLUTION STARTS HERE ***********************/ static class Pair implements Comparable<Pair>{ int index,key; Pair(int k,int i){ index = i; key = k; } @Override public int compareTo(Pair o) { return Integer.compare(o.key, key); } } private static void solve(FastScanner s1, PrintWriter out){ int n = s1.nextInt() , k = s1.nextInt(); Pair arr[] = new Pair[n]; for(int i=0;i<n;i++) arr[i] = new Pair(s1.nextInt(), i + 1); Arrays.sort(arr); out.println(arr[k-1].key); for(int i=0;i<k;i++) out.print(arr[i].index + " "); } /************************ SOLUTION ENDS HERE ************************/ /************************ TEMPLATE STARTS HERE *********************/ public static void main(String []args) throws IOException { FastScanner in = new FastScanner(new FileInputStream("input.txt")); PrintWriter out = new PrintWriter("output.txt"); solve(in, out); in.close(); out.close(); } static class FastScanner{ BufferedReader reader; StringTokenizer st; FastScanner(InputStream stream){reader=new BufferedReader(new InputStreamReader(stream));st=null;} String next() {while(st == null || !st.hasMoreTokens()){try{String line = reader.readLine();if(line == null){return null;} st = new StringTokenizer(line);}catch (Exception e){throw new RuntimeException();}}return st.nextToken();} String nextLine() {String s=null;try{s=reader.readLine();}catch(IOException e){e.printStackTrace();}return s;} int nextInt() {return Integer.parseInt(next());} long nextLong() {return Long.parseLong(next());} double nextDouble(){return Double.parseDouble(next());} char nextChar() {return next().charAt(0);} int[] nextIntArray(int n) {int[] arr= new int[n]; int i=0;while(i<n){arr[i++]=nextInt();} return arr;} long[] nextLongArray(int n) {long[]arr= new long[n]; int i=0;while(i<n){arr[i++]=nextLong();} return arr;} int[] nextIntArrayOneBased(int n) {int[] arr= new int[n+1]; int i=1;while(i<=n){arr[i++]=nextInt();} return arr;} long[] nextLongArrayOneBased(int n){long[]arr= new long[n+1];int i=1;while(i<=n){arr[i++]=nextLong();}return arr;} void close(){try{reader.close();}catch(IOException e){e.printStackTrace();}} } /************************ TEMPLATE ENDS HERE ************************/ }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
98dff5478edcfc71f85d3cc5c255f505
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.util.*; import java.io.*; public class Reading { public static void main(String[] args) throws Exception { Scanner in = new Scanner(new File("input.txt")); PrintWriter out = new PrintWriter(new File("output.txt")); int n = in.nextInt(); int k = in.nextInt(); Hour[] hours = new Hour[n]; for(int x = 0; x < n; x++) { hours[x] = new Hour(in.nextInt(), x); } Arrays.sort(hours); out.println(hours[k - 1].light); for(int y = 0; y < k; y++) { if(y > 0) { out.print(" "); } out.print(hours[y].index + 1); } out.println(); out.close(); } static class Hour implements Comparable<Hour> { int light; int index; public Hour(int l, int i) { light = l; index = i; } public int compareTo(Hour o) { return o.light - light; } } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
bc87edef036af23a5fb78257b50cc537
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class CodeForces { public static void main(String[] args) throws Exception { // Scanner input = new Scanner(new BufferedReader(new // InputStreamReader(System.in))); Scanner input = new Scanner(new File("input.txt")); PrintWriter out = new PrintWriter(new File("output.txt")); int n = input.nextInt(); int k = input.nextInt(); int[][] array = new int[n][2]; for (int i = 0; i < n; i++) { array[i][0] = input.nextInt(); array[i][1] = i + 1; } Arrays.sort(array, (a, b) -> Double.compare(a[0], b[0])); out.println(array[array.length - k][0]); for (int i = 0; i < k; i++) { out.print(array[array.length - i - 1][1] + " "); } out.close(); } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
8d73d8fbf0fd467774dd63be0925e643
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
//package codeforces; import java.io.BufferedReader; import java.io.Closeable; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Set; import java.util.StringTokenizer; public class B implements Closeable { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(System.out); StringTokenizer stringTokenizer; B() throws IOException { reader = new BufferedReader(new FileReader("input.txt")); writer = new PrintWriter(new FileWriter("output.txt")); } String next() throws IOException { while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) { stringTokenizer = new StringTokenizer(reader.readLine()); } return stringTokenizer.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()); } final int MOD = 1000 * 1000 * 1000 + 7; int sum(int a, int b) { a += b; return a >= MOD ? a - MOD : a; } int product(int a, int b) { return (int) (1l * a * b % MOD); } int pow(int x, long k) { int result = 1; while (k > 0) { if (k % 2 == 1) { result = product(result, x); } x = product(x, x); k /= 2; } return result; } @SuppressWarnings("unchecked") void solve() throws IOException { int n = nextInt(), k = nextInt(); class Hour { int id, a; public Hour(int id, int a) { this.id = id; this.a = a; } } Queue<Hour> q = new PriorityQueue<>(n, new Comparator<Hour>() { @Override public int compare(Hour o1, Hour o2) { return Integer.compare(o1.a, o2.a); } }); for(int i = 0; i < n; i++) { q.offer(new Hour(i + 1, nextInt())); } while(q.size() > k) { q.poll(); } writer.println(q.peek().a); for (Hour hour : q) { writer.print(hour.id + " "); } } public static void main(String[] args) throws IOException { try (B b = new B()) { b.solve(); } } @Override public void close() throws IOException { reader.close(); writer.close(); } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
f37b6b8369c35a6278ff591a69ff0d62
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.util.*; import java.util.Scanner; import java.io.*; import javax.lang.model.util.ElementScanner6; public class B234 { public static void main(String args[]) throws Exception { Scanner in = new Scanner(new File("input.txt")); PrintWriter pr = new PrintWriter(new File("output.txt")); int tc=1; //tc=in.nextInt(); while(tc-->0) { int n=in.nextInt(); int k=in.nextInt(); Pair[] arr=new Pair[n]; for(int i=0;i<n;i++) { arr[i]=new Pair(in.nextInt(),i); } Arrays.sort(arr); pr.println(arr[n-k].val); for(int i=n-k;i<n;i++) { pr.print(arr[i].idx+" "); } pr.println(); } pr.flush(); pr.close(); } static class Pair implements Comparable<Pair> { int val, idx; public Pair(int val, int idx) { this.val = val; this.idx = idx + 1; } @Override public int compareTo(Pair o) { return val - o.val; } } static int gcd(int a,int b) { if(a==0) return b; return gcd(a%b,b); } static int lcm(int a,int b) { return (a*b)/gcd(a,b); } static boolean prime[]; static void sieveofe() { int n=1000000; prime=new boolean[n+1]; Arrays.fill(prime,true); prime[1]=false; for(int i=2;i*i<=n;i++) { if(prime[i]==true) { for(int j=i*i;j<=n;j+=i) { prime[j]=false; } } } } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class 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 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
a78ebad7bd198940412ceab5f7e21165
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.util.*; import java.io.*; public class Main234B { public static class set implements Comparable<set> { int a; int b; public set(int a,int b) { this.a=a; this.b=b; } public int compareTo(set that) { if(that.a>this.a) return 1; else if(that.a==this.a) return 0; else return -1; } } public static void main(String[] args) throws IOException { File in=new File("input.txt"); Writer wr=new FileWriter("output.txt"); Scanner sc=new Scanner(in); int n=sc.nextInt(); int k=sc.nextInt(); set[] a=new set[n]; for(int i=0;i<n;i++) { int x=sc.nextInt(); a[i]=new set(x,i); } Arrays.sort(a); wr.write(a[k-1].a+""); wr.write(System.getProperty("line.separator")); for(int i=0;i<k;i++) wr.write((a[i].b+1)+" "); wr.flush(); wr.close(); } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
c6b8edfa5e109e6bcf1209b353032069
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; /** * * @author Hartha */ public class Reading { private static class Arr implements Comparable<Arr> { int val; int index; public Arr(int val, int index) { this.val = val; this.index = index; } @Override public int compareTo(Arr o) { return o.val - this.val; } } public static void main(String[] args) throws IOException { Scanner Reader = new Scanner(new File("input.txt")); try(PrintWriter out = new PrintWriter(new File("output.txt"))){ int n = Reader.nextInt(), m = Reader.nextInt(); Arr[] arr = new Arr[n]; for(int i = 0; i < n; i++) arr[i] = new Arr(Reader.nextInt(), i+1); Arrays.sort(arr); out.println(arr[m-1].val); for(int i = 0; i < m; i++) out.print(arr[i].index + " "); out.flush(); } } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
039ca1a2da6de6b00454038281523d62
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.awt.Point; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class B234 { public static void main(String[] args) throws IOException { Scanner in = new Scanner(new File("input.txt")); int N = in.nextInt(); int K = in.nextInt(); Point[] P = new Point[N]; for (int n=1; n<=N; n++) { int light = in.nextInt(); P[n-1] = new Point(light, n); } Arrays.sort(P, new Comparator<Point>() { @Override public int compare(Point o1, Point o2) { return o2.x - o1.x; } }); PrintWriter out = new PrintWriter(new File("output.txt")); out.println(P[K-1].x); StringBuilder output = new StringBuilder(); for (int k=0; k<K; k++) { output.append(P[k].y).append(' '); } out.println(output); out.close(); } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
817e633061b9a76d0caf7d7cfe979c26
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; public class Main{ public static void main(String[] args) throws Exception{ FastScanner sc =new FastScanner(); PrintWriter out=new PrintWriter(new File("output.txt")); int n=sc.nextInt(); int k=sc.nextInt(); List<Pair> arr=new ArrayList<>(); for(int i=0;i<n;i++){ int x=sc.nextInt(); arr.add(new Pair(i+1,x)); } Collections.sort(arr,(a,b)->{ return b.val-a.val; }); out.println(arr.get(k-1).val); for(int i=0;i<k;i++){ out.print(arr.get(i).idx+" "); } out.close(); } } class Pair{ int idx=-1,val=-1; Pair(int idx,int val){ this.idx=idx; this.val=val; } } class FastScanner { BufferedReader br; StringTokenizer st =new StringTokenizer(""); FastScanner() throws Exception{ br=new BufferedReader(new FileReader("input.txt")); } String next(){ if(!st.hasMoreTokens()){ try{ st=new StringTokenizer(br.readLine()); } catch(Exception e){ } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
3f3f56da8c6450c101eb5f9d1fcf67f4
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; public class Main{ public static void main(String[] args) throws Exception{ Scanner sc = new Scanner((new FileReader("input.txt"))); FileWriter out = new FileWriter("output.txt"); int n=sc.nextInt(); int k=sc.nextInt(); List<Pair> arr=new ArrayList<>(); for(int i=0;i<n;i++){ int x=sc.nextInt(); arr.add(new Pair(i+1,x)); } Collections.sort(arr,(a,b)->{ return b.val-a.val; }); out.write(arr.get(k-1).val+"\n"); for(int i=0;i<k;i++){ out.write(arr.get(i).idx+" "); } out.close(); } } class Pair{ int idx=-1,val=-1; Pair(int idx,int val){ this.idx=idx; this.val=val; } } // class FastScanner { // BufferedReader br =new BufferedReader(new FileReader("input.txt")); // StringTokenizer st =new StringTokenizer(""); // String next(){ // if(!st.hasMoreTokens()){ // try{ // st=new StringTokenizer(br.readLine()); // } // catch(Exception e){ // } // } // return st.nextToken(); // } // int nextInt(){ // return Integer.parseInt(next()); // } // }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
b05e56a2c15d4dc117c9b15957943a8f
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
/** * Platform : Codeforces * Url : http://codeforces.com/problemset/problem/234/B * Verdict : Accepted * */ import java.util.Scanner; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.LinkedHashMap; import java.util.stream.Collectors; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.util.ArrayList; public class Reading{ public static void main(String args[]) throws FileNotFoundException,IOException{ Scanner scan = new Scanner(new File("input.txt")); int n = scan.nextInt(); int k = scan.nextInt(); HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>(); for(int i = 1; i <= n; i++){ hm.put(i, scan.nextInt()); } // System.out.println(hm); Map<Integer, Integer> sorted = hm .entrySet() .stream() .sorted(Collections.reverseOrder(Map.Entry.comparingByValue())) .collect( Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new)); ArrayList<Integer> res = new ArrayList<Integer>(); // System.out.println(sorted.get(k)); Writer wr = new FileWriter("output.txt"); for(Integer keys : sorted.keySet()){ if(k == 0) break; if(k == 1) wr.write(sorted.get(keys).toString()+"\n"); res.add(keys); k--; } for(Integer i : res) wr.write(i+" "); wr.close(); } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
d1f3d6e0b467f718325615c4084c0a16
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.awt.Point; import java.io.*; import java.util.*; import java.math.*; import static java.lang.Math.*; import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.Double.parseDouble; import static java.lang.String.*; import javafx.scene.paint.Color; public class Main { public static void main(String[] args) throws IOException { // BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); //(new FileReader("input.in")); StringBuilder out = new StringBuilder(); // StringTokenizer tk; // tk = new StringTokenizer(in.readLine()); Scanner Reader = new Scanner(new File("input.txt")); //Reader.init(System.in); PrintWriter p = new PrintWriter(new File("output.txt")); int n=Reader.nextInt(),k=Reader.nextInt(); point[]a=new point[n]; for (int i = 0; i < n; i++) { a[i]=new point(i+1, Reader.nextInt()); } Arrays.sort(a); int min=Integer.MAX_VALUE; for (int i = n-1,j=0; j<k; i--,j++) { min= min(min, a[i].v); } p.println(min); for (int i = n-1,j=0; j<k; i--,j++) { p.print(a[i].a); p.print(" "); } p.println(); p.close(); Reader.close(); } static class point implements Comparable<point> { int a, v; public point(int a, int b) { this.a = a; v = b; } @Override public int compareTo(point o) { if (v == o.v) { if (a < o.a) { return -1; } else if (a > o.a) { return 1; } return 0; } else if (v < o.v) { return -1; } else { return 1; } } } } class Reader { static StringTokenizer tokenizer; static BufferedReader reader; public static void init(InputStream input) throws UnsupportedEncodingException { reader = new BufferedReader(new InputStreamReader(input, "UTF-8")); tokenizer = new StringTokenizer(""); } public static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } public static String nextLine() throws IOException { return reader.readLine(); } public static int nextInt() throws IOException { return Integer.parseInt(next()); } public static double nextDouble() throws IOException { return Double.parseDouble(next()); } public static long nextLong() throws IOException { return Long.parseLong(next()); } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
a4f516dbb79dc20d7ce81693d2dd87fb
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.util.*; import java.io.*; import java.nio.file.Files; import java.nio.file.Paths; public class Watermelon { static int x; static PrintWriter out; public static void main(String[] args) { Scanner sc=new Scanner(System.in); ArrayList<Integer> list=new ArrayList<>(); // String content = new String(Files.readAllBytes(Paths.get(fileName))); InputStream inputStream; try { inputStream = new FileInputStream("input.txt"); } catch (IOException e) { throw new RuntimeException(e); } OutputStream outputStream; try { outputStream = new FileOutputStream("output.txt"); } catch (IOException e) { try { inputStream.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } throw new RuntimeException(e); } Scanner in = new Scanner(inputStream); out = new PrintWriter(outputStream); int n=in.nextInt(); int k=in.nextInt(); int[] arr=new int[n]; int[] carr=new int[n]; for(int i=0;i<n;i++) {arr[i]=in.nextInt();carr[i]=arr[i];} // String content = new String(Files.readAllBytes(Paths.get(fileName))); Arrays.sort(carr); // Watermelon.display1(carr,n-k); Count c=null; A:for(int i=n-k;i<n;i++){ if(i==n-k) c=new Count(carr[i]); for(int j=0;j<n;j++){ if(carr[i]==arr[j]) { arr[j]=-1; c.list.add(j+1); if(c.list.size()==k) break A; } } } c.display(); out.close(); in.close(); } private static int gcd(int a, int b) { while (b > 0) { int temp = b; b = a % b; // % is remainder a = temp; } return a; } private static int lcm(int a, int b) { return a * (b / gcd(a, b)); } static int[] toFractionPos(int x,int y){ int a=gcd(x,y); int[] arr={x/a,y/a}; //display(arr); return arr; } static void display(int[][] arr){ for(int i=0;i<arr.length;i++){ for(int j=0;j<arr[i].length;j++){ System.out.print(arr[i][j]+" "); } System.out.println(); } System.out.println(); } static void display(String[] arr){ for(int i=0;i<arr.length;i++){ System.out.println(arr[i]+" "); } // System.out.println(); } static void display(Integer[] arr){ // display1(arr); ArrayList<Integer> list=new ArrayList<>(); for(int i=0;i<arr.length;i++){ list.add(arr[i]); } for(int i=0;i<list.size();i++){ for(int j=0;j<list.size();j++){ if(i!=j) if(list.get(i)==list.get(j)) list.remove(j); } } //System.out.println(list+" "+list.size()); for(int i=0;i<list.size();i++) out.print(list.get(i)+" "); } static void display1(int[] arr,int x){ for(int i=x;i<arr.length;i++) System.out.print(arr[i]+" "); System.out.println(); } static void display(double[] arr){ // System.out.println(); for(int i=0;i<arr.length;i++){ System.out.println(arr[i]+" "); } // System.out.println(); } static String str(char[] carr){ String str=""; for(int i=0;i<carr.length;i++){ str=str+carr[i]; } return str; } } class Count{ int i; ArrayList<Integer> list=new ArrayList<>(); Count(int i,int c){ this.i=i; list.add(c); } Count(int i){ this.i=i; } void display(){ Watermelon.out.println(i); // System.out.println(list); Integer[] arr=list.toArray(new Integer[list.size()]); Arrays.sort(arr); Watermelon.display(arr); } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
7aa90d7565ebecda69c38466086941a6
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.StringTokenizer; public class Problem52 { public static class Thing implements Comparable<Thing>{ int pos; int light; public Thing(int pos,int light) { this.pos=pos; this.light=light; } public int compareTo(Thing thing) { return this.light-thing.light; } } public static void main(String[] args) throws IOException{ // TODO Auto-generated method stub out=new PrintWriter (new File("output.txt")); File in=new File("input.txt"); FastReader s=new FastReader (in); int n=s.nextInt(); int k=s.nextInt(); List<Thing> arr=new ArrayList<>(); for(int i=0;i<n;i++) { int x=s.nextInt(); Thing temp=new Thing(i+1,x); arr.add(temp); } Collections.sort(arr); Thing[] ansarr=new Thing[arr.size()]; arr.toArray(ansarr); int minimum=ansarr[n-k].light; int[] mainarr=new int[k]; int pos=0; while(k>0) { mainarr[pos]=ansarr[n-k].pos; pos++; k--; } out.println(minimum); for(int i=0;i<(mainarr.length);i++) { out.print(mainarr[i]+" "); } out.flush(); out.close(); } public static PrintWriter out; public static class FastReader { BufferedReader br; StringTokenizer st; //it reads the data about the specified point and divide the data about it ,it is quite fast //than using direct public FastReader(File in) throws IOException{ br = new BufferedReader(new FileReader(in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next());//converts string to integer } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (Exception e) { e.printStackTrace(); } return str; } } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
57af6a45badf37540e0bda9e51f1aa75
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class Main2 { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new FileReader("input.txt")); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); // BufferedReader br = new BufferedReader(new // InputStreamReader(System.in)); String[]in = br.readLine().split(" "); int n = Integer.parseInt(in[0]); int k = Integer.parseInt(in[1]); in = br.readLine().split(" "); int[]arr = new int[n]; int[]ind = new int[n]; for(int i = 0;i<n;i++){ arr[i] = Integer.parseInt(in[i]); ind[i] = i+1; } for(int i = 0;i<n-1;i++){ for(int j = 0;j<n-i-1;j++){ if(arr[j]<arr[j+1]){ int swap = arr[j]; arr[j] = arr[j+1]; arr[j+1] = swap; swap = ind[j]; ind[j] = ind[j+1]; ind[j+1] = swap; } } } out.println(arr[k-1]); int[]ans = new int[k]; for(int i = 0;i<k;i++){ ans[i] = ind[i]; } Arrays.sort(ans); for(int i = 0;i<k-1;i++){ out.print(ans[i]+" "); } out.println(ans[k-1]); br.close(); out.close(); } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
88d431d5a0b4f973f7e97fdb42d1e7af
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
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 javaapplication1; //import java.util.Scanner; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; 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.math.BigDecimal; import java.util.*; public class Main { final int N=(int)15e6+3; public static void main(String[] args) throws IOException { // Reader.init(System.in); Scanner in=new Scanner(new FileInputStream("input.txt")); //FastReader in=new FastReader(); //in=new FileReader("input.txt"); PrintWriter out=new PrintWriter(new FileOutputStream("output.txt")); Main ob=new Main(); int n=in.nextInt(),k=in.nextInt(); ArrayList<pair> arr=new ArrayList(); for(int i=0;i<n;i++) arr.add(new pair(in.nextInt(),i+1)); Collections.sort(arr,new pair()); out.println(arr.get(n-k).x); for(int i=n-k;i<n;i++) out.print(arr.get(i).y+" "); out.println(); out.close(); } String sort(String inputString) { ArrayList<Character> arr=new ArrayList(); for(int i=0;i<inputString.length();i++) arr.add(inputString.charAt(i)); Collections.sort(arr); StringBuilder sb=new StringBuilder(); for(int i=0;i<arr.size();i++) sb.append(arr.get(i)); return sb.toString(); } boolean next_permutation(int[] p, int len) { int a = len - 2; while (a >= 0 && p[a] >= p[a + 1]) { a--; } if (a == -1) { return false; } int b = len - 1; while (p[b] <= p[a]) { b--; } p[a] += p[b]; p[b] = p[a] - p[b]; p[a] -= p[b]; for (int i = a + 1, j = len - 1; i < j; i++, j--) { p[i] += p[j]; p[j] = p[i] - p[j]; p[i] -= p[j]; } return true; } int lower_bound(ArrayList<Integer> arr,int key) { int len = arr.size(); int lo = 0; int hi = len-1; int mid = (lo + hi)/2; while (true) { int cmp = (arr.get(mid)==key?1:0); if (cmp == 0 || cmp > 0) { hi = mid-1; if (hi < lo) return mid; } else { lo = mid+1; if (hi < lo) return mid<len-1?mid+1:-1; } mid = (lo + hi)/2; } } int upper_bound(ArrayList<Integer> arr, int key) { int len = arr.size(); int lo = 0; int hi = len-1; int mid = (lo + hi)/2; while (true) { int cmp = (arr.get(mid)==key?1:0); if (cmp == 0 || cmp < 0) { lo = mid+1; if (hi < lo) return mid<len-1?mid+1:-1; } else { hi = mid-1; if (hi < lo) return mid; } mid = (lo + hi)/2; } } int gcd(int a,int b){ return b==0?a:gcd(b,a%b); } double area(ArrayList<Point> arr){ double sum=0.0; for(int i=0;i<arr.size();i++){ sum+=cross(arr.get(i),arr.get(((i+1)%(int)arr.size()))); } return Math.abs(sum)/2.0; } double dot(Point p,Point q){ return (p.x*q.x+p.y*q.y); } double cross(Point p,Point q){ return (p.x*q.y-p.y*q.x); } Point computeLineIntersection(Point a,Point b,Point c,Point d){ b=b.sub(a);d=c.sub(d);c=c.sub(a); return a.add(b.mulCon(cross(c,d)).devideCon(cross(b,d))); } 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 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()); } } class Point{ double x,y; Point(double _x,double _y){ x=_x; y=_y; } Point add(final Point p){ return new Point(x+p.x,y+p.y); } Point sub(final Point p){ return new Point(x-p.x,y-p.y); } Point mulCon(final double c){ return new Point(c*x,c*y); } Point devideCon(final double c){ return new Point(x/c,y/c); } } class pair implements Comparator<pair>{ int x,y; pair(){} pair(int a,int b){ x=a; y=b; } @Override public int compare(pair a,pair b){ return a.x-b.x; } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
a9fbe808bf078669bd19a0f596289dfa
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.io.*; import java.util.*; /** * * @author mohanad */ public class x { public static void main(String[] args) throws IOException { Scanner scan = new Scanner(System.in); StringBuilder ou = new StringBuilder(); BufferedReader bf = new BufferedReader(new FileReader("input.txt")); StringTokenizer st=new StringTokenizer(bf.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); int arr[]= new int [n]; int index[]= new int [n]; st=new StringTokenizer(bf.readLine()); for (int i = 0 ; i < n ; ++i ){ arr[i]=Integer.parseInt(st.nextToken()); index[i]=(i+1); } //sort and keep original index for (int i = 1 ; i < arr.length ; ++i){ int c= i ; while ( c > 0 && arr[c]>arr[c-1] ){ int tmp = arr[c]; arr[c]=arr[c-1]; arr[c-1] = tmp; tmp = index[c]; index[c]=index[c-1]; index[c-1]=tmp; --c; } } ou.append(arr[k-1]+"\n"); Arrays.sort(index , 0 , k); for (int i = 0 ; i < k ; ++i){ ou.append(index[i]).append(" "); } PrintWriter pw=new PrintWriter("output.txt"); //System.out.println(ou.toString()); pw.println(ou.toString()); pw.close(); } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
b8c5d719fa45c986b51cf90a47f5236f
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.Comparator; import java.util.StringTokenizer; /** * @author artyom */ public class Reading implements Runnable { private BufferedReader in; private PrintWriter out; private StringTokenizer tok; private void solve() throws IOException { int n = nextInt(), k = nextInt(); SortedArray<Short> a = new SortedArray<>(readShortArray(n)); StringBuilder sb = new StringBuilder(); for (int i = n - 1, lim = n - k; i >= lim; i--) { sb.append(a.indices[i] + 1); if (i > lim) { sb.append(' '); } else { sb.insert(0, '\n'); sb.insert(0, a.get(i)); } } out.println(sb.toString()); } //-------------------------------------------------------------- public static void main(String[] args) { new Reading().run(); } @Override public void run() { try { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter(new FileWriter("output.txt")); tok = null; solve(); in.close(); out.close(); } catch (IOException e) { System.exit(0); } } private Short[] readShortArray(int n) throws IOException { Short[] arr = new Short[n]; for (int i = 0; i < n; i++) { arr[i] = nextShort(); } return arr; } private String nextToken() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } private short nextShort() throws IOException { return Short.parseShort(nextToken()); } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private static final class SortedArray<E extends Comparable<E>> { private final E[] array; private final Integer[] indices; SortedArray(final E[] array) { if (array == null) { throw new NullPointerException(); } this.array = array; indices = createIndexArray(array); Arrays.sort(indices, new ArrayComparator()); } private static <T> Integer[] createIndexArray(T[] arr) { Integer[] indices = new Integer[arr.length]; for (int i = 0; i < arr.length; i++) { indices[i] = i; } return indices; } private int length() { return indices.length; } private E get(int index) { return array[indices[index]]; } private void set(int index, E val) { array[indices[index]] = val; } private class ArrayComparator implements Comparator<Integer> { @Override public int compare(Integer index1, Integer index2) { return array[index1].compareTo(array[index2]); } } } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
986e7372fa81b0c66b9ecb544cffd4b8
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main { static class Reader { private InputStream mIs;private byte[] buf = new byte[1024];private int curChar,numChars;public Reader() { this(System.in); }public Reader(InputStream is) { mIs = is;} public int read() {if (numChars == -1) throw new InputMismatchException();if (curChar >= numChars) {curChar = 0;try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();}if (numChars <= 0) return -1; }return buf[curChar++];} 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 String s(){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 long l(){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 int i(){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 double d() throws IOException {return Double.parseDouble(s()) ;} public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } /////////////////////////////////////////////////////////////////////////////////////////// // RRRRRRRRR AAA HHH HHH IIIIIIIIIIIII LLL // // RR RRR AAAAA HHH HHH IIIIIIIIIII LLL // // RR RRR AAAAAAA HHH HHH III LLL // // RR RRR AAA AAA HHHHHHHHHHH III LLL // // RRRRRR AAA AAA HHHHHHHHHHH III LLL // // RR RRR AAAAAAAAAAAAA HHH HHH III LLL // // RR RRR AAA AAA HHH HHH IIIIIIIIIII LLLLLLLLLLLL // // RR RRR AAA AAA HHH HHH IIIIIIIIIIIII LLLLLLLLLLLL // /////////////////////////////////////////////////////////////////////////////////////////// static class B implements Comparable<B> { int x;int y; public B(int a,int b) { x=a; y=b; } public int compareTo(B num) { return num.x-x; } } public static void main(String[] args)throws IOException { /*PrintWriter out= new PrintWriter(System.out); Reader sc=new Reader();*/ Scanner sc=new Scanner(new File("input.txt")); PrintWriter out=new PrintWriter(new File("output.txt")); int n=sc.nextInt(); int r=sc.nextInt(); B arr[]=new B[n]; for(int i=0;i<n;i++) arr[i]=new B(sc.nextInt(),i+1); Arrays.sort(arr); out.println(arr[r-1].x+" "); for(int i=0;i<r;i++) out.print(arr[i].y+" "); out.flush(); } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
c9ea40aa57d4e765ec344ab784702c5d
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; import java.util.StringTokenizer; public class Solution { public static void main(String[] args) throws IOException { //BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); Scanner scrn = new Scanner(new File("input.txt")); PrintWriter pw = new PrintWriter(new File("output.txt")); String str = scrn.nextLine(); StringTokenizer st = new StringTokenizer(str, " "); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); String s = scrn.nextLine(); st = new StringTokenizer(s, " "); Pair[] arr = new Pair[n]; for (int i = 0; i < n; i++) { arr[i] = new Pair(Integer.parseInt(st.nextToken()), i + 1); } Arrays.sort(arr, Collections.reverseOrder()); int min = arr[k - 1].value; pw.write(min + "\n"); int index = 0; for (int i = 0; i < n; i++) { if (arr[i].value >= min && index < k) { pw.write(arr[i].index + " "); index++; } } pw.close(); } public static class Pair implements Comparable<Pair> { int value; int index; public Pair(int v, int index) { this.value = v; this.index = index; } @Override public int compareTo(Pair o) { return this.value - o.value; } } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
51c3d40abc94ad5035465c210c597f17
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.util.Arrays; import java.util.Scanner; public class PA { public static void main(String[] args) throws Exception { new PA().run(); } private void run() throws Exception { Scanner sc = new Scanner(new FileReader("input.txt")); FileWriter out = new FileWriter("output.txt"); int n = sc.nextInt(); int k = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); int[] ac = Arrays.copyOf(a, a.length); Arrays.sort(ac); int minLum = ac[ac.length - k]; out.write(minLum + "\n"); for (int i = 0; i < n && k > 0; i++) { if (a[i] >= minLum) { out.write(i + 1 + " "); k--; } } sc.close(); out.close(); } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
2c265b20e391aa76a72137b0723728a1
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.math.BigInteger; import java.util.*; import java.io.*; public class test { public static class list{ int index; int item; public list(int item,int index){ this.index=index; this.item=item; } } public static void main(String[] args) throws IOException { File inFile = new File ("input.txt"); Scanner sc = new Scanner (inFile); File outFile = new File ("output.txt"); FileWriter fWriter = new FileWriter (outFile); PrintWriter pWriter = new PrintWriter (fWriter); int n=sc.nextInt(); int k=sc.nextInt(); list a[]=new list[n]; for(int i=0;i<n;i++){ a[i]=new list(sc.nextInt(),i); } sort(a,n); pWriter.println(a[n-k].item); for(int i=n-1;i>=n-k;i--){ pWriter.print((a[i].index+1)+" "); } pWriter.close(); sc.close(); } public static void sort(list a[],int n){ for(int i=0;i<n-1;i++){ for(int j=0;j<n-1-i;j++){ if(a[j].item>a[j+1].item){ list t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } } 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 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
41e5dbe4d2435945a0a58096d11c23ae
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
//package Practice2.CF234; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class CF234B { public static void main(String[] args) throws Exception{ Scanner s = new Scanner(new File("input.txt")); BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt")); int n = s.nextInt(); int k = s.nextInt(); pair[] arr = new pair[n]; for (int i = 0; i < n; i++) { arr[i] = new pair(s.nextInt(),i+1); } Arrays.sort(arr); int i = 0; ArrayList<Integer> ans = new ArrayList<>(); while(i < k){ ans.add(arr[i].ind); i++; } writer.write(arr[i-1].val + "\n"); // System.out.println(ans); for (int m:ans) { writer.write(m + " "); // System.out.println(m + " "); } writer.flush(); writer.close(); } private static class pair implements Comparable<pair>{ int val; int ind; public pair(int val, int ind) { this.val = val; this.ind = ind; } @Override public int compareTo(pair p){ return Integer.compare(p.val,this.val); } } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 8
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
5f3c5239ef8bda231a06518900746303
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class B { int n; char[] v; public B(int n, String s) { this.n = n; v = s.toCharArray(); } private static void add(int res[], char[] v, int i, int j, int k) { if (v[i] == 'R' && v[j] == 'L') { res[k] = j + 1; res[k + 1] = i + 1; } else { res[k] = i + 1; res[k + 1] = j + 1; } } public int[] solve() { int[] res = new int[n]; int k = 0; for (int i = 0; i < n; i++) { int j = n - i - 1; if (n - k == 4) { add(res, v, i, i + 2, k); k += 2; add(res, v, i + 1, i + 3, k); } else { add(res, v, i, j, k); } k += 2; if(k == n){ break; } } return res; } /** * @param args */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub boolean isOnline = false; InputStream inputStream = isOnline ? System.in : new FileInputStream( "input.txt"); OutputStream outputStream = isOnline ? System.out : new FileOutputStream("output.txt"); Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); int n = in.nextInt(); int k = in.nextInt(); Hour[] hours = new Hour[n]; for(int i=0;i<n;i++){ int v = in.nextInt(); hours[i] = new Hour(i+1, v); } Arrays.sort(hours); int min = hours[n-k].value; out.write(min+"\n"); for(int i=0;i<k;i++){ out.write(hours[n-i-1].index+""); if(i < k-1){ out.write(" "); } } out.close(); in.close(); } static class Hour implements Comparable<Hour>{ int index, value; public Hour(int index, int value) { this.index = index; this.value = value; } @Override public int compareTo(Hour o) { if(o.value < value) return 1; if(o.value > value) return -1; if(o.index < index) return 1; if(o.index > index) return -1; return 0; } } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 6
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
57bccc8646b6ee29943fe8ced2fdc0cf
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.io.*; import java.util.*; public class B { public static void main(String[] args) throws IOException { new B().run(); } InputReader in; PrintWriter out; public void run() throws IOException { in = new InputReader( new FileInputStream("input.txt") ); out = new PrintWriter( new FileOutputStream("output.txt") ); solve(); out.close(); } private void solve() throws IOException { int n = in.nextInt(); int k = in.nextInt(); pair[] a = new pair[ n ]; for (int i=0; i<n; i++) a[i] = new pair( i+1, in.nextInt() ); Arrays.sort( a ); out.println( a[k-1].val ); for (int i=0; i<k; i++) out.print( a[i].pos + " " ); out.println(); } } class pair implements Comparable<pair> { int pos, val; pair(int pos, int val) { this.pos = pos; this.val = val; } @Override public int compareTo(pair P) { return P.val - val; } } class InputReader { BufferedReader br; StringTokenizer st; public InputReader(InputStream in) { br = new BufferedReader(new InputStreamReader( in )); st = null; } 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()); } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 6
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
8736b2a3eb5439badf7b2f47408bb139
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.util.*; import java.io.*; public class cf145a { static BufferedReader br; cf145a(){ //System.out.println("wew"); init(); } static void init() { try { br = new BufferedReader(new FileReader("input.txt")); } catch (Exception e) { System.exit(-1); } } public static Integer[] getIntArr() { try { StringTokenizer st = new StringTokenizer(br.readLine()); Integer a[] = new Integer[st.countTokens()]; for (int i = 0; i < a.length; i++) { a[i] = Integer.parseInt(st.nextToken()); } return a; } catch (Exception e) { } return null; } public static void main(String[] arg) { init(); Integer a[] = getIntArr(); Integer b[] = getIntArr(); Integer c[][] = new Integer[b.length][2]; for (int i = 0; i < b.length; i++) { c[i][0] = b[i]; c[i][1] = i + 1; } Arrays.sort(c, new Comparator<Integer[]>() { public int compare(Integer[] a, Integer[] b) { return a[0] - b[0]; } }); int res = Integer.MAX_VALUE; String ou = ""; for (int i = b.length - 1; i >= b.length - a[1]; i--) { ou += c[i][1] + " "; } print(c[b.length - a[1]][0]); print(ou.trim()); } public static void print(Object o) { try { PrintWriter out = new PrintWriter(new FileOutputStream(new File("output.txt"),true)); out.println(o); out.flush(); } catch (Exception e) { } } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 6
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
45d2d7bdc06582eb830cc8436f3133c1
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
//package Round_145; import java.util.*; import java.io.*; public class b { class p implements Comparable<p>{ int a; int id; p(int a, int id){ this.a = a; this.id = id; } @Override public int compareTo(p o) { return o.a - this.a; } } String input = "input.txt"; String output = "output.txt"; FastScanner in; PrintWriter out; p [] shuffle(p a[]){ for (int i = 0; i<a.length; i++){ int t = (int)(Math.random()*i); p swap = a[i]; a[i] = a[t]; a[t] = swap; } return a; } void solve() throws Exception { int n = in.nextInt(); int k =in.nextInt(); p a[] = new p[n]; for (int i = 0;i<n; i++) a[i] = new p(in.nextInt(), i+1); a = shuffle(a); Arrays.sort(a); long sum = Integer.MAX_VALUE; for (int i=0; i<k; i++) sum = Math.min(a[i].a, sum); out.println(sum); for (int i=0; i<k; i++) out.print(a[i].id + " "); } void run() { try { if (input.length() == 0) { InputStreamReader ins = new InputStreamReader(System.in); in = new FastScanner(new BufferedReader(ins)); } else { FileReader f = new FileReader(input); in = new FastScanner(new BufferedReader(f)); } if (output.length() == 0) { out = new PrintWriter(System.out); } else out = new PrintWriter(new File(output)); solve(); out.flush(); out.close(); } catch (Exception ex) { ex.printStackTrace(); out.close(); } } public static void main(String args[]) { new b().run(); } class FastScanner { BufferedReader bf; StringTokenizer st; FastScanner(BufferedReader bf) { this.bf = bf; } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(bf.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 6
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
57650eedb8237d235c6fb4ceb0a145fd
train_001.jsonl
1350370800
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip β€” n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class P234B { public void run() throws Exception { int n = nextInt(); int k = nextInt(); int [] a = new int [n]; boolean [] u = new boolean [n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } StringBuffer res = new StringBuffer(); int min = Integer.MAX_VALUE; for (int j = 0; j < k; j++) { int idx = 0; int m = Integer.MIN_VALUE; for (int i = 0; i < n; i++) { if (!u[i]) { if (a[i] > m) { m = a[i]; idx = i; } } } min = Math.min(min, m); res = res.append(idx + 1).append(' '); u[idx] = true; } println(min); println(res); } public static void main(String... args) throws Exception { br = new BufferedReader(new FileReader("input.txt")); pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream("output.txt"))); new P234B().run(); br.close(); pw.close(); } static BufferedReader br; static PrintWriter pw; StringTokenizer stok; String nextToken() throws IOException { while (stok == null || !stok.hasMoreTokens()) { String s = br.readLine(); if (s == null) { return null; } stok = new StringTokenizer(s); } return stok.nextToken(); } void print(byte b) { print("" + b); } void print(int i) { print("" + i); } void print(long l) { print("" + l); } void print(double d) { print("" + d); } void print(char c) { print("" + c); } void print(StringBuffer sb) { print("" + sb); } void print(String s) { pw.print(s); } void println(byte b) { println("" + b); } void println(int i) { println("" + i); } void println(long l) { println("" + l); } void println(double d) { println("" + d); } void println(char c) { println("" + c); } void println(StringBuffer sb) { println("" + sb); } void println(String s) { pw.println(s); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } char nextChar() throws IOException { return (char) (br.read()); } String next() throws IOException { return br.readLine(); } }
Java
["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"]
1 second
["20\n1 3 4", "35\n1 3 4 5 6"]
NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
Java 6
input.txt
[ "sortings" ]
a585045a9a6f62bfe1c0618c2ee02f48
The first input line contains two integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ n) β€” the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≀ ai ≀ 100), ai is the light level at the i-th hour.
1,000
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, β€” the indexes of hours Vasya will read at (1 ≀ bi ≀ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.
output.txt
PASSED
264b152c159564d87459c32ac45cefd7
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.InputMismatchException; public class e { static class Solver { int K, N, towers[][], memo[], nxt[]; int dp(int idx) { if(idx >= N) return 0; if(memo[idx] != -1) return memo[idx]; // continue bringing this cover forward (from behind) int res = 1 + dp(idx + 1); // make use of a later tower for(int k = nxt[idx]; k < K; k++) { int del = stretch(towers[k], idx); res = min(res, del + dp(towers[k][0] + towers[k][1] + del + 1)); } return memo[idx] = res; } void solve(FastScanner s, PrintWriter out) { K = s.nextInt(); N = s.nextInt(); for(int[] a : towers = s.next2DIntArray(K, 2)) a[0]--; Arrays.sort(towers, (a, b) -> a[0] - b[0]); nxt = new int[N + 1]; nxt[N] = K; for(int j = K - 1, i = N - 1; i >= 0; i--) { nxt[i] = towers[j][0] == i ? j : nxt[i] + 1; } Arrays.fill(memo = new int[N], -1); int best = 987654321; for(int i = 0; i < K; i++) { // start with tower i int del = stretch(towers[i], 0); best = min(best, del + dp(towers[i][0] + towers[i][1] + del + 1)); } out.println(best); } int min(int a, int b) { return a < b ? a : b; } int stretch(int[] tow, int x) { int d = Math.abs(tow[0] - x); if(d <= tow[1]) return 0; return Math.abs(tow[1] - d); } } public static void main(String[] args) { FastScanner s = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); new Solver().solve(s, out); out.close(); s.close(); } static class FastScanner { private InputStream stream; private byte[] buf = new byte[1 << 22]; private int curChar; private int numChars; public FastScanner(InputStream stream) { this.stream = stream; } public FastScanner(File f) throws FileNotFoundException { this(new FileInputStream(f)); } public FastScanner(String s) { this.stream = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8)); } void close() { try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } 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(); } public int[] nextIntArray(int N) { int[] ret = new int[N]; for (int i = 0; i < N; i++) ret[i] = this.nextInt(); return ret; } public int[][] next2DIntArray(int N, int M) { int[][] ret = new int[N][]; for (int i = 0; i < N; i++) ret[i] = this.nextIntArray(M); return ret; } public long[] nextLongArray(int N) { long[] ret = new long[N]; for (int i = 0; i < N; i++) ret[i] = this.nextLong(); return ret; } public long[][] next2DLongArray(int N, int M) { long[][] ret = new long[N][]; for (int i = 0; i < N; i++) ret[i] = nextLongArray(M); return ret; } public double[] nextDoubleArray(int N) { double[] ret = new double[N]; for (int i = 0; i < N; i++) ret[i] = this.nextDouble(); return ret; } public double[][] next2DDoubleArray(int N, int M) { double[][] ret = new double[N][]; for (int i = 0; i < N; i++) ret[i] = this.nextDoubleArray(M); return ret; } } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
0b84f0bf8f97ebee30900b472f6b7e6a
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
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.StringTokenizer; /* * 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. */ /** * * @author Andy Phan */ public class e { public static void main(String[] args) { FS in = new FS(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int m = in.nextInt(); int[] mid = new int[n]; int[] range = new int[n]; for(int i = 0; i < n; i++) { mid[i] = in.nextInt()-1; range[i] = in.nextInt(); } int[] dp = new int[m+1]; for(int i = m-1; i >= 0; i--) { dp[i] = dp[i+1]+1; for(int j = 0; j < n; j++) { if(mid[j]+range[j] >= i && mid[j]-range[j] <= i) { dp[i] = dp[i+1]; break; } if(mid[j] > i) { dp[i] = Math.min(dp[i], dp[Math.min(m, mid[j]+(mid[j]-i)+1)]+(mid[j]-i-range[j])); } } } System.out.println(dp[0]); out.close(); } static class FS { BufferedReader in; StringTokenizer token; public FS(InputStream str) { in = new BufferedReader(new InputStreamReader(str)); } public String next() { if (token == null || !token.hasMoreElements()) { try { token = new StringTokenizer(in.readLine()); } catch (IOException ex) { } return next(); } return token.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
20ec74c062e0ab3c842fdd787242ae90
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String args[]) { new Main().run(); } FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); void run() { work(); out.flush(); } long mod = 998244353; long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } void work() { int n = in.nextInt(); int m = in.nextInt(); int[] dp = new int[m + 2]; int[] rec = new int[m + 2]; int[][] A = new int[n][]; int min = 999999999; for (int i = 0; i < n; i++) { int x = in.nextInt(); int s = in.nextInt(); A[i] = new int[] { x - s, x + s }; min = Math.min(min, x - s); rec[Math.max(0, x - s)]++; rec[Math.min(m + 1, x + s + 1)]--; } for (int i = 1; i <= m + 1; i++) rec[i] += rec[i - 1]; Arrays.sort(A, new Comparator<int[]>() { public int compare(int[] a1, int[] a2) { return a1[1] - a2[1]; } }); for (int i = 1; i <= m; i++) { if (rec[i] > 0) { dp[i] = dp[i - 1]; } else if (i < min) { dp[i] = i; } else { dp[i] = 999999999; for (int j = 0; j < n; j++) { if (A[j][1] > i) break; int s = A[j][0]; int e = A[j][1]; int ns = s - (i - e); int v = (i - e); dp[i] = Math.min(dp[i], v + (ns - 1 < 0 ? 0 : dp[ns - 1])); } } } out.println(dp[m]); } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { if (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
db3a2d1234b8b2e5212af53a5f71dd08
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class TaskA { private static class Antenna { int position; int range; public Antenna(int position, int range) { this.position = position; this.range = range; } int rightBorder() { return position + range; } int leftBorder() { return position - range; } } private static int N; private static int M; private static int[][] cache; private static Antenna[] antennas; private static int solve() { return dp(N, M); } private static int dp(int i, int reach) { if (reach < 0) return 0; if (i < 0) return reach; if (cache[i][reach] == -1) { int cost = Math.max(reach - antennas[i].rightBorder(), 0); int answer = Math.min( dp(i - 1, reach), cost + dp(i - 1, antennas[i].leftBorder() - cost - 1)); cache[i][reach] = answer; } return cache[i][reach]; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); N = scanner.nextInt(); M = scanner.nextInt(); antennas = new Antenna[N + 1]; cache = new int[N + 1][M + 1]; antennas[0] = new Antenna(0, 0); for (int i = 0; i <= N; i++) Arrays.fill(cache[i], -1); for (int n = 1; n <= N; n++) antennas[n] = new Antenna(scanner.nextInt(), scanner.nextInt()); Arrays.sort(antennas, Comparator.comparingInt(a -> a.position)); System.out.println(solve()); } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
c8216f6a29f1024d1c34317ef249e4a3
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.util.*; import java.io.*; public class Main { static int[]dp; static boolean[] cov; static int[] pos; static int[] s; static int n,m; static int solve(int i){ if(i>m) return 0; if(dp[i]!=-1) return dp[i]; int ans = (int) 1e9; for(int j=0; j<n; j++){ int s1 = Math.max(s[j],Math.abs(pos[j]-i)); int cost = s1-s[j]; int tmp = cost+solve(pos[j]+s1+1); ans = Math.min(ans,tmp); } if (i > 1) ans = Math.min(ans, 1 + solve(i + 1)); return dp[i] = ans; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); // String s = br.readLine(); // char[] arr=s.toCharArray(); // ArrayList<Integer> arrl = new ArrayList<Integer>(); // TreeSet<Integer> ts1 = new TreeSet<Integer>(); // HashSet<Integer> h = new HashSet<Integer>(); // HashMap<Integer, Integer> map= new HashMap<>(); // PriorityQueue<String> pQueue = new PriorityQueue<String>(); // LinkedList<String> object = new LinkedList<String>(); // StringBuilder str = new StringBuilder(); StringTokenizer st = new StringTokenizer(br.readLine()); n = Integer.parseInt(st.nextToken()); m = Integer.parseInt(st.nextToken()); pos = new int[n]; s = new int[n]; for(int i=0; i<n; i++){ st = new StringTokenizer(br.readLine()); pos[i] = Integer.parseInt(st.nextToken()); s[i] = Integer.parseInt(st.nextToken()); } dp = new int[m+5]; Arrays.fill(dp,-1); out.println(solve(1)); out.flush(); }}
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
b76f977466c48a9c65ac4d56d75982af
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; public class E { public static void main(String[] args) throws Exception { // StringTokenizer stok = new StringTokenizer(new Scanner(new File("F:/books/input.txt")).useDelimiter("\\A").next()); StringTokenizer stok = new StringTokenizer(new Scanner(System.in).useDelimiter("\\A").next()); StringBuilder sb = new StringBuilder(); Integer n = Integer.parseInt(stok.nextToken()); Integer m = Integer.parseInt(stok.nextToken()); Integer[][] a = new Integer[n][2]; for(int i=0;i<n;i++) { a[i][0] = Integer.parseInt(stok.nextToken()); a[i][1] = Integer.parseInt(stok.nextToken()); } Arrays.sort(a,(x,y)->Integer.compare(x[0], y[0])); boolean[] v = new boolean[m+2]; for(int i=0;i<n;i++) { for(int j=Math.max(1, a[i][0]-a[i][1]);j<=Math.min(m, a[i][0]+a[i][1]);j++) { v[j]=true; } } int[] p = new int[m+2]; int i=m+1; while(i>0) { p[i]=i; int j=i; while(--j>0 && v[j]) p[j]=i; i=j; } int[] dp = new int[m+2]; Arrays.fill(dp, Integer.MAX_VALUE); dp[p[1]]=0; i=p[1]; while(i<m+1) { if(!v[i] && dp[i]!=Integer.MAX_VALUE) { int pos = Arrays.binarySearch(a, new Integer[] {i, 0},(x,y)->Integer.compare(x[0],y[0])); pos=-pos-1; if(pos==a.length) { int d=m+1-i; dp[m+1]=Math.min(dp[m+1], dp[i]+d); } for(int j=pos;j<a.length;j++) { if(a[j][0]-a[j][1]<=i) continue; int d = a[j][0]-a[j][1]-i; int idx = Math.min(m+1, a[j][0]+a[j][1]+d+1); idx=p[idx]; dp[idx] = Math.min(dp[idx], dp[i]+d); } } i++; } System.out.println(dp[m+1]); } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
cdd6cbf2a6a5bddc8a5fb71c4bf32a63
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.io.*; import java.util.*; public class Main { static FastReader in; static PrintWriter out; static final int INF = (int) 1e9; static void solve() { int n = in.nextInt(); int m = in.nextInt(); int[] x = new int[n]; int[] s = new int[n]; for (int i = 0; i < n; i++) { x[i] = in.nextInt(); s[i] = in.nextInt(); } int[] dp = new int[m + 1]; for (int i = 1; i <= m; i++) { dp[i] = i; for (int j = 0; j < n; j++) { if (x[j] - s[j] <= i) { int d = Math.max(0, i - (x[j] + s[j])); dp[i] = Math.min(dp[i], d + dp[Math.max(0, x[j] - s[j] - d - 1)]); } } } out.println(dp[m]); } public static void main(String[] args) { in = new FastReader(System.in); // in = new FastReader(new FileInputStream("input.txt")); out = new PrintWriter(System.out); // out = new PrintWriter(new FileOutputStream("output.txt")); int t = 1; while (t-- > 0) solve(); out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; FastReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } Integer nextInt() { return Integer.parseInt(next()); } Long nextLong() { return Long.parseLong(next()); } Double nextDouble() { return Double.parseDouble(next()); } String next() { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(nextLine()); } return st.nextToken(); } String nextLine() { String s = ""; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
36fcc576da97051e4c4cc643b0904c8f
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import static java.lang.Math.*; import static java.util.Arrays.*; import java.util.*; import java.util.stream.*; public class E { public Object solve () { int N = sc.nextInt(), M = sc.nextInt(); int [][] A = sc.nextInts(N); sort(A, by(0)); int [] C = new int [M+1]; fill(C, INF); for (int i : rep(N)) { int X = A[i][0], S = A[i][1], L = X - S, R = X + S; for (int t : req(M)) { int c = C[t], q = max(0, L - (t + 1)); t = max(t, R + q); t = min(t, M); c += q; C[t] = min(C[t], c); } int Q = max(0, L - 1), T = min(R + Q, M); C[T] = min(C[T], Q); } int res = INF; for (int T : req(M)) { int Q = max(0, M - T); res = min(res, C[T] + Q); } return res; } private static final int CONTEST_TYPE = 1; private static void init () { } private static final int INF = (int) 1e9 + 10; private static Comparator<int[]> by (int j, int ... J) { Comparator<int[]> res = (x, y) -> x[j] - y[j]; for (int i : J) res = res.thenComparing((x, y) -> x[i] - y[i]); return res; } private static int [] rep (int N) { return rep(0, N); } private static int [] rep (int S, int T) { if (S >= T) return new int [0]; int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; } private static int [] req (int N) { return rep(0, N+1); } private static <T> T [] sort(T [] A, Comparator<T> C) { Arrays.sort(A, C); return A; } //////////////////////////////////////////////////////////////////////////////////// OFF private static IOUtils.MyScanner sc = new IOUtils.MyScanner(); private static class IOUtils { public static class MyScanner { public String next () { newLine(); return line[index++]; } public int nextInt () { return Integer.parseInt(next()); } public String nextLine () { line = null; return readLine(); } public String [] nextStrings () { return split(nextLine()); } public int[] nextInts () { return nextStream().mapToInt(Integer::parseInt).toArray(); } public int[][] nextInts (int N) { return IntStream.range(0, N).mapToObj(i -> nextInts()).toArray(int[][]::new); } ////////////////////////////////////////////// private boolean eol () { return index == line.length; } private String readLine () { try { return r.readLine(); } catch (Exception e) { throw new Error (e); } } private final java.io.BufferedReader r; private MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); } private MyScanner (java.io.BufferedReader r) { try { this.r = r; while (!r.ready()) Thread.sleep(1); start(); } catch (Exception e) { throw new Error(e); } } private String [] line; private int index; private void newLine () { if (line == null || eol()) { line = split(readLine()); index = 0; } } private java.util.stream.Stream<String> nextStream () { return java.util.Arrays.stream(nextStrings()); } private String [] split (String s) { return s.length() > 0 ? s.split(" ") : new String [0]; } } private static String build (Object o, Object ... A) { return buildDelim(" ", o, A); } private static String buildDelim (String delim, Object o, Object ... A) { StringBuilder b = new StringBuilder(); append(b, o, delim); for (Object p : A) append(b, p, delim); return b.substring(delim.length()); } ////////////////////////////////////////////////////////////////////////////////// private static java.text.DecimalFormat formatter = new java.text.DecimalFormat("#.#########"); private static void start () { if (t == 0) t = millis(); } private static void append (java.util.function.Consumer<Object> f, java.util.function.Consumer<Object> g, final Object o) { if (o.getClass().isArray()) { int len = java.lang.reflect.Array.getLength(o); for (int i = 0; i < len; ++i) f.accept(java.lang.reflect.Array.get(o, i)); } else if (o instanceof Iterable<?>) ((Iterable<?>)o).forEach(f::accept); else g.accept(o instanceof Double ? formatter.format(o) : o); } private static void append (final StringBuilder b, Object o, final String delim) { append(x -> { append(b, x, delim); }, x -> b.append(delim).append(x), o); } private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out); private static void print (Object o, Object ... A) { String res = build(o, A); if (DEBUG == 2) err(res, '(', time(), ')'); pw.println(res); if (DEBUG == 1) { pw.flush(); System.out.flush(); } } private static void err (Object o, Object ... A) { System.err.println(build(o, A)); } private static int DEBUG; private static void exit () { String end = "------------------" + System.lineSeparator() + time(); switch(DEBUG) { case 1: print(end); break; case 2: err(end); break; } IOUtils.pw.close(); System.out.flush(); System.exit(0); } private static long t; private static long millis () { return System.currentTimeMillis(); } private static String time () { return "Time: " + (millis() - t) / 1000.0; } private static void run (int N) { try { DEBUG = Integer.parseInt(System.getProperties().get("DEBUG").toString()); } catch (Throwable t) {} for (int n = 1; n <= N; ++n) { Object res = new E().solve(); if (res != null) { @SuppressWarnings("all") Object o = CONTEST_TYPE == 0 ? "Case #" + n + ": " + build(res) : res; print(o); } } exit(); } } //////////////////////////////////////////////////////////////////////////////////// public static void main (String[] args) { init(); @SuppressWarnings("all") int N = CONTEST_TYPE == 1 ? 1 : sc.nextInt(); IOUtils.run(N); } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
941a26f684e3a97a8f8cf176e2fa818a
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import static java.lang.Math.*; import static java.util.Arrays.*; import java.util.*; import java.util.stream.*; public class E { public Object solve () { int N = sc.nextInt(), M = sc.nextInt(); int [][] A = sc.nextInts(N); sort(A, by(0)); int [] C = new int [M+1]; fill(C, INF); for (int i : rep(N)) { int X = A[i][0], S = A[i][1], L = X - S, R = X + S; for (int t : req(M)) { int c = C[t], q = max(0, L - (t + 1)); t = max(t, R + q); t = min(t, M); c += q; C[t] = min(C[t], c); } int q = max(0, L - 1), t = min(R + q, M); C[t] = min(C[t], q); } for (int i : rep(M)) C[i+1] = min(C[i+1], C[i] + 1); int res = C[M]; return res; } private static final int CONTEST_TYPE = 1; private static void init () { } private static final int INF = (int) 1e9 + 10; private static Comparator<int[]> by (int j, int ... J) { Comparator<int[]> res = (x, y) -> x[j] - y[j]; for (int i : J) res = res.thenComparing((x, y) -> x[i] - y[i]); return res; } private static int [] rep (int N) { return rep(0, N); } private static int [] rep (int S, int T) { if (S >= T) return new int [0]; int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; } private static int [] req (int N) { return rep(0, N+1); } private static <T> T [] sort(T [] A, Comparator<T> C) { Arrays.sort(A, C); return A; } //////////////////////////////////////////////////////////////////////////////////// OFF private static IOUtils.MyScanner sc = new IOUtils.MyScanner(); private static class IOUtils { public static class MyScanner { public String next () { newLine(); return line[index++]; } public int nextInt () { return Integer.parseInt(next()); } public String nextLine () { line = null; return readLine(); } public String [] nextStrings () { return split(nextLine()); } public int[] nextInts () { return nextStream().mapToInt(Integer::parseInt).toArray(); } public int[][] nextInts (int N) { return IntStream.range(0, N).mapToObj(i -> nextInts()).toArray(int[][]::new); } ////////////////////////////////////////////// private boolean eol () { return index == line.length; } private String readLine () { try { return r.readLine(); } catch (Exception e) { throw new Error (e); } } private final java.io.BufferedReader r; private MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); } private MyScanner (java.io.BufferedReader r) { try { this.r = r; while (!r.ready()) Thread.sleep(1); start(); } catch (Exception e) { throw new Error(e); } } private String [] line; private int index; private void newLine () { if (line == null || eol()) { line = split(readLine()); index = 0; } } private java.util.stream.Stream<String> nextStream () { return java.util.Arrays.stream(nextStrings()); } private String [] split (String s) { return s.length() > 0 ? s.split(" ") : new String [0]; } } private static String build (Object o, Object ... A) { return buildDelim(" ", o, A); } private static String buildDelim (String delim, Object o, Object ... A) { StringBuilder b = new StringBuilder(); append(b, o, delim); for (Object p : A) append(b, p, delim); return b.substring(delim.length()); } ////////////////////////////////////////////////////////////////////////////////// private static java.text.DecimalFormat formatter = new java.text.DecimalFormat("#.#########"); private static void start () { if (t == 0) t = millis(); } private static void append (java.util.function.Consumer<Object> f, java.util.function.Consumer<Object> g, final Object o) { if (o.getClass().isArray()) { int len = java.lang.reflect.Array.getLength(o); for (int i = 0; i < len; ++i) f.accept(java.lang.reflect.Array.get(o, i)); } else if (o instanceof Iterable<?>) ((Iterable<?>)o).forEach(f::accept); else g.accept(o instanceof Double ? formatter.format(o) : o); } private static void append (final StringBuilder b, Object o, final String delim) { append(x -> { append(b, x, delim); }, x -> b.append(delim).append(x), o); } private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out); private static void print (Object o, Object ... A) { String res = build(o, A); if (DEBUG == 2) err(res, '(', time(), ')'); pw.println(res); if (DEBUG == 1) { pw.flush(); System.out.flush(); } } private static void err (Object o, Object ... A) { System.err.println(build(o, A)); } private static int DEBUG; private static void exit () { String end = "------------------" + System.lineSeparator() + time(); switch(DEBUG) { case 1: print(end); break; case 2: err(end); break; } IOUtils.pw.close(); System.out.flush(); System.exit(0); } private static long t; private static long millis () { return System.currentTimeMillis(); } private static String time () { return "Time: " + (millis() - t) / 1000.0; } private static void run (int N) { try { DEBUG = Integer.parseInt(System.getProperties().get("DEBUG").toString()); } catch (Throwable t) {} for (int n = 1; n <= N; ++n) { Object res = new E().solve(); if (res != null) { @SuppressWarnings("all") Object o = CONTEST_TYPE == 0 ? "Case #" + n + ": " + build(res) : res; print(o); } } exit(); } } //////////////////////////////////////////////////////////////////////////////////// public static void main (String[] args) { init(); @SuppressWarnings("all") int N = CONTEST_TYPE == 1 ? 1 : sc.nextInt(); IOUtils.run(N); } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
c5d4f707ab8828b03495817470ae2296
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import static java.lang.Math.*; import java.util.*; import java.util.stream.*; public class E { public Object solve () { int N = sc.nextInt(), M = sc.nextInt(); int [][] A = sc.nextInts(N); sort(A, by(0)); Map<Integer, Integer> Z = new HashMap<>(); for (int i : rep(N)) { int X = A[i][0], S = A[i][1], L = X - S, R = X + S; for (int T : Z.keySet().toArray(new Integer [0])) { int C = Z.get(T); int Q = max(0, L - (T + 1)); T = max(T, R + Q); T = min(T, M); C += Q; int D = Z.getOrDefault(T, INF); if (C < D) Z.put(T, C); } int Q = max(0, L - 1), T = min(R + Q, M); int D = Z.getOrDefault(T, INF); if (Q < D) Z.put(T, Q); } int res = INF; for (int T : Z.keySet()) { int C = Z.get(T); int Q = max(0, M - T); res = min(res, C + Q); } return res; } private static final int CONTEST_TYPE = 1; private static void init () { } private static final int INF = (int) 1e9 + 10; private static Comparator<int[]> by (int j, int ... J) { Comparator<int[]> res = (x, y) -> x[j] - y[j]; for (int i : J) res = res.thenComparing((x, y) -> x[i] - y[i]); return res; } private static int [] rep (int N) { return rep(0, N); } private static int [] rep (int S, int T) { if (S >= T) return new int [0]; int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; } private static <T> T [] sort(T [] A, Comparator<T> C) { Arrays.sort(A, C); return A; } //////////////////////////////////////////////////////////////////////////////////// OFF private static IOUtils.MyScanner sc = new IOUtils.MyScanner(); private static class IOUtils { public static class MyScanner { public String next () { newLine(); return line[index++]; } public int nextInt () { return Integer.parseInt(next()); } public String nextLine () { line = null; return readLine(); } public String [] nextStrings () { return split(nextLine()); } public int[] nextInts () { return nextStream().mapToInt(Integer::parseInt).toArray(); } public int[][] nextInts (int N) { return IntStream.range(0, N).mapToObj(i -> nextInts()).toArray(int[][]::new); } ////////////////////////////////////////////// private boolean eol () { return index == line.length; } private String readLine () { try { return r.readLine(); } catch (Exception e) { throw new Error (e); } } private final java.io.BufferedReader r; private MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); } private MyScanner (java.io.BufferedReader r) { try { this.r = r; while (!r.ready()) Thread.sleep(1); start(); } catch (Exception e) { throw new Error(e); } } private String [] line; private int index; private void newLine () { if (line == null || eol()) { line = split(readLine()); index = 0; } } private java.util.stream.Stream<String> nextStream () { return java.util.Arrays.stream(nextStrings()); } private String [] split (String s) { return s.length() > 0 ? s.split(" ") : new String [0]; } } private static String build (Object o, Object ... A) { return buildDelim(" ", o, A); } private static String buildDelim (String delim, Object o, Object ... A) { StringBuilder b = new StringBuilder(); append(b, o, delim); for (Object p : A) append(b, p, delim); return b.substring(delim.length()); } ////////////////////////////////////////////////////////////////////////////////// private static java.text.DecimalFormat formatter = new java.text.DecimalFormat("#.#########"); private static void start () { if (t == 0) t = millis(); } private static void append (java.util.function.Consumer<Object> f, java.util.function.Consumer<Object> g, final Object o) { if (o.getClass().isArray()) { int len = java.lang.reflect.Array.getLength(o); for (int i = 0; i < len; ++i) f.accept(java.lang.reflect.Array.get(o, i)); } else if (o instanceof Iterable<?>) ((Iterable<?>)o).forEach(f::accept); else g.accept(o instanceof Double ? formatter.format(o) : o); } private static void append (final StringBuilder b, Object o, final String delim) { append(x -> { append(b, x, delim); }, x -> b.append(delim).append(x), o); } private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out); private static void print (Object o, Object ... A) { String res = build(o, A); if (DEBUG == 2) err(res, '(', time(), ')'); pw.println(res); if (DEBUG == 1) { pw.flush(); System.out.flush(); } } private static void err (Object o, Object ... A) { System.err.println(build(o, A)); } private static int DEBUG; private static void exit () { String end = "------------------" + System.lineSeparator() + time(); switch(DEBUG) { case 1: print(end); break; case 2: err(end); break; } IOUtils.pw.close(); System.out.flush(); System.exit(0); } private static long t; private static long millis () { return System.currentTimeMillis(); } private static String time () { return "Time: " + (millis() - t) / 1000.0; } private static void run (int N) { try { DEBUG = Integer.parseInt(System.getProperties().get("DEBUG").toString()); } catch (Throwable t) {} for (int n = 1; n <= N; ++n) { Object res = new E().solve(); if (res != null) { @SuppressWarnings("all") Object o = CONTEST_TYPE == 0 ? "Case #" + n + ": " + build(res) : res; print(o); } } exit(); } } //////////////////////////////////////////////////////////////////////////////////// public static void main (String[] args) { init(); @SuppressWarnings("all") int N = CONTEST_TYPE == 1 ? 1 : sc.nextInt(); IOUtils.run(N); } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
e52369aa4feefdb301c06391b3b434e8
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
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 htvu */ 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); EAntennaCoverage solver = new EAntennaCoverage(); solver.solve(1, in, out); out.close(); } static class EAntennaCoverage { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(), m = in.nextInt(); int[] p = new int[n]; int[] s = new int[n]; boolean[] covered = new boolean[m + 1]; for (int i = 0; i < n; ++i) { p[i] = in.nextInt(); s[i] = in.nextInt(); for (int j = Math.max(p[i] - s[i], 0); j <= Math.min(m, p[i] + s[i]); ++j) covered[j] = true; } int[] dp = new int[m + 1]; dp[0] = 0; for (int x = 1; x <= m; ++x) { dp[x] = x; if (covered[x]) { dp[x] = dp[x - 1]; continue; } for (int i = 0; i < n; ++i) if (p[i] < x) { int d = Math.max(x - p[i] - s[i], 0); dp[x] = Math.min(dp[x], dp[Math.max(0, p[i] - s[i] - d - 1)] + d); } } out.println(dp[m]); } } 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 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
8749ae44424d504a9f8fb7bec2b1b96a
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.util.*; import java.io.*; import java.io.FileWriter; // Solution public class Main { public static void main (String[] argv) { new Main(); } boolean test = false; final int MOD = 998244353; //1000000007; public Main() { FastReader in = new FastReader(new BufferedReader(new InputStreamReader(System.in))); // FastReader in = new FastReader(new BufferedReader(new FileReader("Main.in"))); //int nt = in.nextInt(); int nt = 1; for (int it = 0; it < nt; it++) { int n = in.nextInt(); int m = in.nextInt(); int[][] ants = new int[n][2]; for (int i = 0; i < n; i++) { int xi = in.nextInt(); int ci = in.nextInt(); ants[i][0] = xi; ants[i][1] = ci; } Arrays.sort(ants, (x,y)->(x[0] - y[0])); int[] fmin = new int[m+1]; boolean[] covered = new boolean[m+1]; int INF = Integer.MAX_VALUE; for (int i = 1; i <= m; i++) { fmin[i] = INF; } for (int[] ant : ants) { int l = ant[0] - ant[1], r = ant[0] + ant[1]; l = Math.max(1, l); r = Math.min(r, m); for (int j = l; j <= r; j++) covered[j] = true; } for (int i = 1; i <= m; i++) { if (covered[i]) { break; } fmin[i] = i; } for (int i = 1; i <= m; i++){ if (covered[i]) { fmin[i] = fmin[i-1]; continue; } int dp = INF; for (int j = 1; j <= n; j++) { if (ants[j-1][0] > i) break; int cost = Math.max(0, i - ants[j-1][0] - ants[j-1][1]); int lo = 2 * ants[j-1][0] - i - 1; if (lo <= 0) lo = 0; dp = cost + fmin[lo]; fmin[i] = Math.min(fmin[i], dp); } } System.out.println(fmin[m]); // } } private int inv(int v) { return pow(v, MOD-2); } private int pow(long v, int p) { long ans = 1; while (p > 0) { if (p % 2 == 1) ans = ans * v % MOD; v = v * v % MOD; p = p / 2; } return (int)ans; } private double dist(double x, double y, double xx, double yy) { return Math.sqrt((xx - x) * (xx - x) + (yy - y) * (yy - y)); } private int mod_add(int a, int b) { int v = a + b; if (v >= MOD) v -= MOD; return v; } private long overlap(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) { if (x1 > x3) return overlap(x3, y3, x4, y4, x1, y1, x2, y2); if (x3 > x2 || y4 < y1 || y3 > y2) return 0L; //(x3, ?, x2, ?) int yL = Math.max(y1, y3); int yH = Math.min(y2, y4); int xH = Math.min(x2, x4); return f(x3, yL, xH, yH); } //return #black cells in rectangle private long f(int x1, int y1, int x2, int y2) { long dx = 1L + x2 - x1; long dy = 1L + y2 - y1; if (dx % 2 == 0 || dy % 2 == 0 || (x1 + y1) % 2 == 0) return 1L * dx * dy / 2; return 1L * dx * dy / 2 + 1; } private int distM(int x, int y, int xx, int yy) { return abs(x - xx) + abs(y - yy); } private boolean less(int x, int y, int xx, int yy) { return x < xx || y > yy; } private int mul(int x, int y) { return (int)(1L * x * y % MOD); } private int nBit1(int v) { int v0 = v; int c = 0; while (v != 0) { ++c; v = v & (v - 1); } return c; } private long abs(long v) { return v > 0 ? v : -v; } private int abs(int v) { return v > 0 ? v : -v; } private int common(int v) { int c = 0; while (v != 1) { v = (v >>> 1); ++c; } return c; } private void reverse(char[] a, int i, int j) { while (i < j) { swap(a, i++, j--); } } private void swap(char[] a, int i, int j) { char t = a[i]; a[i] = a[j]; a[j] = t; } private int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } private long gcd(long x, long y) { if (y == 0) return x; return gcd(y, x % y); } private int max(int a, int b) { return a > b ? a : b; } private long max(long a, long b) { return a > b ? a : b; } private int min(int a, int b) { return a > b ? b : a; } private long min(long a, long b) { return a > b ? b : a; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(BufferedReader in) { br = in; } String next() { while (st == null || !st.hasMoreElements()) { try { String line = br.readLine(); if (line == null || line.length() == 0) return ""; st = new StringTokenizer(line); } catch (IOException e) { return ""; //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) { return null; //e.printStackTrace(); } return str; } } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
72124eaf8a37ef3e898825e2dfbed918
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.util.*; import java.io.*; public class E { public static void main(String[] args) { FastScanner scanner = new FastScanner(); PrintWriter out = new PrintWriter(System.out, false); int n = scanner.nextInt(); int m = scanner.nextInt(); int[][] d = new int[n][2]; for(int i = 0; i < n; i++) { d[i][0]= scanner.nextInt(); d[i][1]= scanner.nextInt(); } ArrayList<Pair>[] lists = new ArrayList[m+2]; for(int i = 0; i <= m + 1; i++) lists[i] = new ArrayList<>(); for(int i = 0; i < n; i++) { int curl= d[i][0] - d[i][1]; int curr= d[i][0] + d[i][1]; for(int j = 0; j <= m; j++) { if (curl < 1) curl = 1; if (curr > m) curr = m; lists[curr].add(new Pair(curl, j)); curl--; curr++; } } SegTree sg = new SegTree(m+1, 0, m); int[] best = new int[m+2]; Arrays.fill(best, Integer.MAX_VALUE/3); best[0] =0; for(int i = 1; i <= m; i++) sg.update(1,i,Integer.MAX_VALUE/3); for(int j = 1; j <= m; j++) { for(Pair pp: lists[j]) { int min = sg.query(1, pp.a-1, j); if (min + pp.b < best[j]) { best[j] = min+pp.b; } } sg.update(1,j,best[j]); } out.println(best[m]); out.flush(); } static class SegTree { int[] hi, lo, min; public SegTree(int ss, int l, int r) { hi = new int[4*ss + 1]; lo = new int[4*ss + 1]; min = new int[4*ss + 1]; init(1, l, r); } void init(int cur, int l, int r){ hi[cur] = r; lo[cur] = l; if (l ==r)return; int mid = (l + r)/2; init(cur*2, l, mid); init(cur*2 +1, mid + 1, r); } void update(int cur, int t, int amt) { if (t == hi[cur] && lo[cur] == t) { min[cur] = amt; return; } int mid = (hi[cur] + lo[cur]) /2; if (t<=mid) update(cur*2, t, amt); else update(cur*2 + 1, t, amt); min[cur] = Math.min(min[cur*2], min[cur*2+1]); } int query(int cur, int l, int r) { if (l > hi[cur] || r < lo[cur]) return Integer.MAX_VALUE; if (l <= lo[cur] && r >= hi[cur]) { return min[cur]; } int ll = query(cur*2, l, r); int rr = query(cur*2+1, l, r); return Math.min(rr, ll); } } static class Pair { int a,b; public Pair(int aa, int bb) { a = aa; b = bb; } } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader in) { br = new BufferedReader(in); } public FastScanner() { 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()); } String readNextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
61e38e16c1977a6dcb759057c9a5960d
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
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.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author ijxjdjd */ 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); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } static class TaskE { int INF = (int) (1e9); int M; public void solve(int testNumber, InputReader in, PrintWriter out) { ArrayList<Ant> arr = new ArrayList<>(); int N = in.nextInt(); M = in.nextInt(); int[] dp = new int[M + 1]; Arrays.fill(dp, INF); dp[0] = 0; for (int i = 0; i < N; i++) { arr.add(new Ant(in.nextInt(), in.nextInt())); } arr.add(new Ant(0, 0)); for (int i = 1; i <= M; i++) { for (Ant a : arr) { if (a.li <= i && a.ri >= i) { dp[i] = dp[i - 1]; } else { if (a.ri <= i) { dp[i] = Math.min(dp[i], dp[Math.max(0, a.li - (i - a.ri) - 1)] + i - a.ri); } } } } out.println(dp[M]); } class Ant { int li; int ri; Ant(int x, int si) { li = Math.max(0, x - si); ri = Math.min(M, x + si); } } } 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 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
39a6d90f7034ebaab191a869b5365ff9
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.awt.Point; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class thing { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); Point[] x_s = new Point[n]; for(int i = 0; i < n; i++) { x_s[i] = new Point(in.nextInt(), in.nextInt()); } int[] dp = new int[m+1]; for(int i = 1; i <= m; i++) { dp[i] = i; } for(int i = 1; i <= m; i++) { for(Point cur : x_s) { int l = Math.max(cur.x - cur.y, 0); int r = Math.min(cur.x + cur.y, m); if(r < i) { dp[i] = Math.min(dp[i], dp[Math.max(0, l-(i-r)-1)] + i-r); } else { dp[i] = Math.min(dp[i], dp[Math.max(0, l-1)]); } } } System.out.println(dp[m]); } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
af530058bacaef57a59e40c5e0854462
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; import java.util.TreeMap; /* 2 50 25 24 40 2 */ public class E{ static int N,M; static Ant ar[]; static int dp[][][]; static int covered[]; static int nextNotCovered[]; static int INF = 10000000; static final boolean debug = false; public static void main(String args[]){ FS in = new FS(); N = in.nextInt(); M = in.nextInt(); ar = new Ant[N]; covered = new int[M]; for(int i = 0; i < N; i++) { ar[i] = new Ant(in.nextInt()-1, in.nextInt()); int l = ar[i].x - ar[i].s; int left = 2*ar[i].s + 1; if(l < 0) { int dif = 0-l; l = 0; left -= dif; } covered[l] = left; } int cur = 0; for(int i = 0; i < M; i++) { cur = Math.max(cur-1, covered[i]); cur = Math.max(cur, 0); covered[i] = cur; } nextNotCovered = new int[M]; Arrays.fill(nextNotCovered, -1); for(int i = M-2; i >= 0; i--) { if(covered[i+1] > 0) nextNotCovered[i] = nextNotCovered[i+1]; else nextNotCovered[i] = i+1; } int firstNotCovered = (covered[0] == 0 ? 0 : nextNotCovered[0]); if(firstNotCovered == -1) { System.out.println(0); return; } Arrays.sort(ar); if(debug) { System.out.println(Arrays.toString(ar)); System.out.println("FirstNotCovered: "+firstNotCovered); } dp = new int[2][N][M+1]; for(int a[][] : dp) for(int b[] : a) Arrays.fill(b, -1); int best = M+2; for(int st = 0; st < N; st++) { //We are starting here int l = ar[st].x - ar[st].s; int res = 0; if(l > firstNotCovered) { res += (l-firstNotCovered); res += go(1, st, ar[st].x + ar[st].s + (l-firstNotCovered)); } else { res += go(1, st, ar[st].x + ar[st].s); } best = min(best, res); } System.out.println(best); } static int go(int finalized, int ant, int lastCoveredByThisGuy) { if(lastCoveredByThisGuy >= M-1) return 0; if(ant >= N) return INF; if(dp[finalized][ant][lastCoveredByThisGuy] != -1) return dp[finalized][ant][lastCoveredByThisGuy]; int res = INF; //if finalized == 1, then we are fine. We are taking from this guy //if finalized == 0, then lastCoveredByThisGuy is equal to the last covered from who was left over //expand this guy by one if(finalized == 1) res = min(res, 1 + go(finalized, ant, lastCoveredByThisGuy+1)); //skip to next guy res = min(res, go(0, ant+1, lastCoveredByThisGuy)); //finalize by decision if(finalized == 0) { int next = nextNotCovered[lastCoveredByThisGuy]; if(next == -1) res = min(res, 0); else { int l = ar[ant].x - ar[ant].s; if(l > next) { int use = (l-next); res = min(res, use + go(1, ant, ar[ant].x + ar[ant].s + use)); } else { res = min(res, go(1, ant, ar[ant].x + ar[ant].s)); } } } return dp[finalized][ant][lastCoveredByThisGuy] = res; } static int min(int a, int b) { return a < b ? a : b;} static class Ant implements Comparable<Ant>{ int x,s; public Ant(int xx, int ss) { x=xx; s=ss; } @Override public int compareTo(Ant o) { return x-o.x; } @Override public String toString() { return "x="+x+" s="+s; } } static class FS{ BufferedReader br; StringTokenizer st; public FS(String s) throws Exception { try{ br = new BufferedReader(new FileReader(s)); } catch(Exception e) { throw new Exception(); } } public FS(){ br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while(st == null ||!st.hasMoreElements()){ try { st = new StringTokenizer(br.readLine());} catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } String next() { return nextToken(); } } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
8c0b2ea7b650fd245ae44ae17ad2d261
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.io.BufferedInputStream; import java.io.InputStream; import java.io.PrintStream; import java.util.Arrays; import java.util.Comparator; import java.util.Locale; import java.util.Scanner; public class ETask { private static final String QUICK_ANSWER = "NO"; private final Scanner in; private final StringBuilder out; public ETask(Scanner in, StringBuilder out) { this.in = in; this.out = out; } public void solve() throws QuickAnswer { int m = nextInt(); int n = nextInt(); int[][] ints = nextInts(m, 2); int[] l = new int[m]; int[] r = new int[m]; for (int i = 0; i < m; i++) { l[i] = ints[0][i] - ints[1][i] - 1; r[i] = ints[0][i] + ints[1][i] - 1; } int[] dp = new int[n]; for (int pos = 0; pos < n; ++pos) { dp[pos] = pos + 1; boolean inside = false; for (int i = 0; i < m; i++) { if (l[i] <= pos && pos <= r[i]) { inside = true; break; } } if(inside){ dp[pos] = pos == 0 ? 0 : dp[pos - 1]; continue; } for (int i = 0; i < m; i++) { if(pos < l[i]) continue; int d = pos - r[i]; dp[pos] = Math.min(dp[pos], d + (l[i] - d <= 0 ? 0 : dp[l[i] - d - 1])); } } print(dp[n - 1]); } // Common functions void quickAnswer(String answer) throws QuickAnswer { throw new QuickAnswer(answer); } void quickAnswer() throws QuickAnswer { quickAnswer(QUICK_ANSWER); } static class QuickAnswer extends Exception { private String answer; public QuickAnswer(String answer) { this.answer = answer; } } void print(Object... args) { String prefix = ""; for (Object arg : args) { out.append(prefix); out.append(arg); prefix = " "; } } void println(Object... args) { print(args); out.append("\n"); } void printsp(Object... args) { print(args); out.append(" "); } int nextInt() { return in.nextInt(); } long nextLong() { return in.nextLong(); } String nextString() { String res = in.nextLine(); return res.trim().isEmpty() ? in.nextLine() : res; } int[] nextInts(int count) { int[] res = new int[count]; for (int i = 0; i < count; ++i) { res[i] = in.nextInt(); } return res; } int[][] nextInts(int count, int n) { int[][] res = new int[n][count]; for (int i = 0; i < count; ++i) { for (int j = 0; j < n; j++) { res[j][i] = in.nextInt(); } } return res; } long[] nextLongs(int count) { long[] res = new long[count]; for (int i = 0; i < count; ++i) { res[i] = in.nextLong(); } return res; } long[][] nextLongs(int count, int n) { long[][] res = new long[n][count]; for (int i = 0; i < count; ++i) { for (int j = 0; j < n; j++) { res[j][i] = in.nextLong(); } } return res; } public static void main(String[] args) { doMain(System.in, System.out); } static void doMain(InputStream inStream, PrintStream outStream) { Scanner in = new Scanner(new BufferedInputStream(inStream)).useLocale(Locale.ENGLISH); StringBuilder totalOut = new StringBuilder(); int count = 1; //count = in.nextInt(); while (count-- > 0) { try { StringBuilder out = new StringBuilder(); new ETask(in, out).solve(); totalOut.append(out.toString()); } catch (QuickAnswer e) { totalOut.append(e.answer); } if (count > 0) { totalOut.append("\n"); } } outStream.print(totalOut.toString()); } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
1fc5dce2ee33f3353f0350627fd393c3
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.IOException; import java.io.InputStreamReader; 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 */ 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); EAntennaCoverage solver = new EAntennaCoverage(); solver.solve(1, in, out); out.close(); } static class EAntennaCoverage { static int n; static int m; static EAntennaCoverage.antenna[] arr; static Integer[][] memo; public void solve(int testNumber, Scanner sc, PrintWriter pw) { n = sc.nextInt(); m = sc.nextInt(); arr = new EAntennaCoverage.antenna[n]; for (int i = 0; i < n; i++) arr[i] = new EAntennaCoverage.antenna(sc.nextInt(), sc.nextInt()); Arrays.sort(arr); memo = new Integer[n + 1][m + 1]; pw.println(dp(0, 0)); } private int dp(int idx, int covered) { if (covered >= m) return 0; if (idx == n) { return Math.max(m - covered, 0); } if (memo[idx][covered] != null) return memo[idx][covered]; int take = (int) 1e9; if (covered <= arr[idx].pos + arr[idx].scope) take = Math.max(arr[idx].pos - arr[idx].scope - covered - 1, 0) + dp(idx + 1, arr[idx].pos + Math.max(arr[idx].pos - arr[idx].scope - covered - 1, 0) + arr[idx].scope); int leave = dp(idx + 1, covered); return memo[idx][covered] = Math.min(take, leave); } static class antenna implements Comparable<EAntennaCoverage.antenna> { int pos; int scope; antenna(int pos, int scope) { this.pos = pos; this.scope = scope; } public int compareTo(EAntennaCoverage.antenna antenna) { return pos == antenna.pos ? scope - antenna.scope : pos - antenna.pos; } public String toString() { return pos + " " + scope; } } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
8842c85bb0227d98f63dd8ec4ee34cc5
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
/* Rajkin Hossain */ import java.io.*; import java.util.*; import static java.lang.Math.*; public class E { FastInput k = new FastInput(System.in); //FastInput k = new FastInput("/home/rajkin/Desktop/input.txt"); FastOutput z = new FastOutput(); int n, m; Pair [] antenas; int [][] dp; int rec(int index, int completeRange) { if(completeRange > m) { return 0; } if(index >= n) { return m - completeRange + 1; } if(dp[index][completeRange] != -1) { return dp[index][completeRange]; } if(m == completeRange) { return 1; } int left = antenas[index].x - antenas[index].s; int right = antenas[index].x + antenas[index].s; if(completeRange < left) { int amount = left - completeRange; return dp[index][completeRange] = Math.min(amount + rec(index+1, right+amount+1), rec(index+1, completeRange)); } else{ return dp[index][completeRange] = rec(index+1, Math.max(completeRange, right+1)); } } void startAlgo() { if(m == 1) z.println(0); else z.println(rec(0, 1)); } void startProgram() { while(k.hasNext()) { n = k.nextInt(); m = k.nextInt(); antenas = new Pair[n]; dp = new int[n][m+1]; k.fill2DIntArray(dp, -1); for(int i = 0; i<n; i++) { antenas[i] = new Pair(k.nextInt(), k.nextInt()); } Arrays.sort(antenas); startAlgo(); } z.flush(); System.exit(0); } class Pair implements Comparable<Pair> { int x, s; public Pair(int x, int s) { this.x = x; this.s = s; } @Override public int compareTo(Pair p) { return this.x - p.x; } } public static void main(String [] args) throws IOException { new Thread(null, new Runnable(){ public void run(){ try{ new E().startProgram(); } catch(Exception e){ e.printStackTrace(); } } },"Main",1<<28).start(); } /* MARK: FastInput and FastOutput */ class FastInput { BufferedReader reader; StringTokenizer tokenizer; FastInput(InputStream stream){ reader = new BufferedReader(new InputStreamReader(stream)); } FastInput(String path){ try { reader = new BufferedReader(new FileReader(path)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } tokenizer = null; } String next() { return nextToken(); } String nextLine() { try { return reader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } boolean hasNext(){ try { return reader.ready(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } String nextToken() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { String line = null; try { line = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } if (line == null) { return null; } tokenizer = new StringTokenizer(line); } return tokenizer.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.valueOf(nextToken()); } int [] getInputIntegerArray(int n) { int [] input = new int[n]; for(int i = 0; i<n; i++) { input[i] = nextInt(); } return input; } long [] getInputLongArray(int n) { long [] input = new long[n]; for(int i = 0; i<n; i++) { input[i] = nextLong(); } return input; } void fill2DIntArray(int [][] array, int value) { for(int i = 0; i<array.length; i++) { for(int j = 0; j<array[0].length; j++) { array[i][j] = value; } } } void fill2DLongArray(long [][] array, long value) { for(int i = 0; i<array.length; i++) { for(int j = 0; j<array[0].length; j++) { array[i][j] = value; } } } int [] getInputIntegerArrayOneBasedIndex(int n) { int [] input = new int[n+1]; for(int i = 1; i<=n; i++) { input[i] = nextInt(); } return input; } long [] getInputLongArrayOneBasedIndex(int n) { long [] input = new long[n+1]; for(int i = 1; i<=n; i++) { input[i] = nextLong(); } return input; } } class FastOutput extends PrintWriter { FastOutput() { super(new BufferedOutputStream(System.out)); } public void debug(Object...obj) { System.err.println(Arrays.deepToString(obj)); } } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
f0e9c725f78377b378c36cacc827a2e7
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.io.*; import java.util.*; public class AntennaCoverage { public static long INF = Long.MAX_VALUE/2; public static void main(String[] args) throws IOException{ BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter((new OutputStreamWriter(System.out))); StringTokenizer st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); long[] dp = new long[m+1]; Interval[] intervals = new Interval[n+1]; intervals[0] = new Interval(0, 0); for(int i = 1; i <= n; i++){ st = new StringTokenizer(f.readLine()); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); intervals[i] = new Interval(Math.max(1, a-b), Math.min(m, a+b)); } Arrays.sort(intervals); for(int i = 0; i < m+1; i++){ dp[i] = INF; } dp[0] = 0; for(int i = 0; i < m; i++){ for(int j = 0; j < n+1; j++) { if (intervals[j].left <= i) { if(intervals[j].right > i){ dp[Math.min(intervals[j].right, m)] = Math.min(dp[Math.min(intervals[j].right, m)], dp[i]); } continue; } else { int val = intervals[j].left - i - 1; dp[Math.min(intervals[j].right + val, m)] = Math.min(dp[Math.min(intervals[j].right + val, m)], dp[i] + val); } } } long minVal = INF; for(int i = m; i >= 0; i--){ if(dp[i] < INF){ minVal = Math.min(minVal, dp[i]+(m-i)); } } out.println(minVal); out.close(); }static class Interval implements Comparable<Interval>{ public int left; public int right; public Interval(int l, int r){ left = l; right = r; } public int compareTo(Interval i){ if(left < i.left) return -1; else if(left > i.left) return 1; else if(right < i.right) return -1; else if(right > i.right) return 1; return 0; } } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
0ac6ce232bf5e96123b97250eb47fed2
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.io.BufferedInputStream; import java.io.InputStream; import java.io.PrintStream; import java.util.Arrays; import java.util.Comparator; import java.util.Locale; import java.util.Scanner; public class ETask { private static final String QUICK_ANSWER = "NO"; private final Scanner in; private final StringBuilder out; public ETask(Scanner in, StringBuilder out) { this.in = in; this.out = out; } public void solve() throws QuickAnswer { int m = nextInt(); int n = nextInt(); int[][] ints = nextInts(m, 2); int[] l = new int[m]; int[] r = new int[m]; for (int i = 0; i < m; i++) { l[i] = ints[0][i] - ints[1][i] - 1; r[i] = ints[0][i] + ints[1][i] - 1; } int[] dp = new int[n]; for (int pos = 0; pos < n; ++pos) { dp[pos] = pos + 1; boolean inside = false; for (int i = 0; i < m; i++) { if (l[i] <= pos && pos <= r[i]) { inside = true; break; } } if(inside){ dp[pos] = pos == 0 ? 0 : dp[pos - 1]; continue; } for (int i = 0; i < m; i++) { if(pos < l[i]) continue; int d = pos - r[i]; dp[pos] = Math.min(dp[pos], d + (l[i] - d <= 0 ? 0 : dp[l[i] - d - 1])); } } print(dp[n - 1]); } // Common functions void quickAnswer(String answer) throws QuickAnswer { throw new QuickAnswer(answer); } void quickAnswer() throws QuickAnswer { quickAnswer(QUICK_ANSWER); } static class QuickAnswer extends Exception { private String answer; public QuickAnswer(String answer) { this.answer = answer; } } void print(Object... args) { String prefix = ""; for (Object arg : args) { out.append(prefix); out.append(arg); prefix = " "; } } void println(Object... args) { print(args); out.append("\n"); } void printsp(Object... args) { print(args); out.append(" "); } int nextInt() { return in.nextInt(); } long nextLong() { return in.nextLong(); } String nextString() { String res = in.nextLine(); return res.trim().isEmpty() ? in.nextLine() : res; } int[] nextInts(int count) { int[] res = new int[count]; for (int i = 0; i < count; ++i) { res[i] = in.nextInt(); } return res; } int[][] nextInts(int count, int n) { int[][] res = new int[n][count]; for (int i = 0; i < count; ++i) { for (int j = 0; j < n; j++) { res[j][i] = in.nextInt(); } } return res; } long[] nextLongs(int count) { long[] res = new long[count]; for (int i = 0; i < count; ++i) { res[i] = in.nextLong(); } return res; } long[][] nextLongs(int count, int n) { long[][] res = new long[n][count]; for (int i = 0; i < count; ++i) { for (int j = 0; j < n; j++) { res[j][i] = in.nextLong(); } } return res; } public static void main(String[] args) { doMain(System.in, System.out); } static void doMain(InputStream inStream, PrintStream outStream) { Scanner in = new Scanner(new BufferedInputStream(inStream)).useLocale(Locale.ENGLISH); StringBuilder totalOut = new StringBuilder(); int count = 1; //count = in.nextInt(); while (count-- > 0) { try { StringBuilder out = new StringBuilder(); new ETask(in, out).solve(); totalOut.append(out.toString()); } catch (QuickAnswer e) { totalOut.append(e.answer); } if (count > 0) { totalOut.append("\n"); } } outStream.print(totalOut.toString()); } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
2e4da7f0902e0cbf7d881aceed9998c9
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.util.*; import java.io.*; public class E { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); n = Integer.parseInt(st.nextToken()); m = Integer.parseInt(st.nextToken()); ants = new Ant[n]; int[] s = new int[m+1]; Arrays.fill(s, -1); for(int i = 0; i < n; i++) { st = new StringTokenizer(br.readLine()); int p = Integer.parseInt(st.nextToken()); int sc = Integer.parseInt(st.nextToken()); ants[i] = new Ant(p, sc); } Arrays.sort(ants); dp = new int[m+1][n]; for(int i = 0; i < m+1; i++) { Arrays.fill(dp[i], -1); } int min = Integer.MAX_VALUE; for(int i = 0; i < n; i++) { min = Integer.min(min, dp(1, i)); } System.out.println(min); } static int n, m; static Ant[] ants; static int[][] dp; static int dp(int i, int ant) { if(i > m) return 0; if(dp[i][ant] >= 0) { return dp[i][ant]; } int dif = ants[ant].p - ants[ant].s - i; int reach = ants[ant].p + ants[ant].s + Integer.max(0, dif); int rest = Integer.max(0, m - reach); for(int a = ant+1; a < n; a++) { rest = Integer.min(rest, dp(reach + 1, a)); } return dp[i][ant] = Integer.max(0, dif) + Integer.max(0, rest); } static class Ant implements Comparable<Ant>{ int p, s; public Ant(int p, int s) { this.p = p; this.s = s; } @Override public int compareTo(Ant arg0) { return p - arg0.p; } public String toString() { return p+" "+s; } } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
6e4d53fadc0b5eb80741655ef2bc50d3
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import org.omg.CORBA.MARSHAL; import java.math.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; import java.util.*; import java.io.*; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////// ///////// //////// ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOOOOO SSSSSSS EEEEEEEEEEEEE ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMMMM MMMMMM OOO OOO SSSS SSS EEEEEEEEEEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMM MMM MMMM OOO OOO SSSS SSS EEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMMMM MMMM OOO OOO SSSS EEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSSSSSS EEEEE ///////// //////// HHHHHHHHHHHHHHHH EEEEEEEEEEE MMMM MMMM OOO OOO SSSSSS EEEEEEEEEEE ///////// //////// HHHHHHHHHHHHHHHH EEEEEEEEEEE MMMM MMMM OOO OOO SSSSSSS EEEEEEEEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSSS EEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSS SSSS EEEEE ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOO OOO SSS SSSS EEEEEEEEEEEEE ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOOOOO SSSSSSS EEEEEEEEEEEEE ///////// //////// ///////// //////// ///////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public class Contest1 { static class Antenna{ int l,r; Antenna(int a,int b){ l=a; r=b; } } static int n,m; static Antenna[]antennas; static int[][]memo; static int dp(int pre,int point){ if (point>m)return 0; if (memo[pre][point]!=-1)return memo[pre][point]; int ans= (int) 1e9; if (pre!=n){ ans=dp(pre,point+1)+1; } for (int i =0;i<n;i++){ if (i==pre||antennas[i].r<point)continue; int inc =Math.max(0,antennas[i].l-point); ans=Math.min(ans,dp(i,Math.max(antennas[i].r+inc+1,point+1))+inc); } return memo[pre][point]=ans; } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); n = sc.nextInt(); m = sc.nextInt(); antennas=new Antenna[n]; for (int i =0;i<n;i++){ int x=sc.nextInt(); int s= sc.nextInt(); antennas[i]=new Antenna(Math.max(x-s,1),Math.min(x+s,m)); } memo=new int[n+1][m+1]; for (int []x:memo)Arrays.fill(x,-1); pw.println(dp(n,1)); pw.flush(); } static class UnionFind { int[] p, rank, setSize; int numSets; TreeSet<Integer>[]ele; public UnionFind(int N) { ele=new TreeSet[N]; p = new int[numSets = N]; rank = new int[N]; setSize = new int[N]; for (int i = 0; i < N; i++) { ele[i]=new TreeSet<>(); p[i] = i; setSize[i] = 1; ele[i].add(i); } } public int findSet(int i) { return p[i] == i ? i : (p[i] = findSet(p[i])); } public boolean isSameSet(int i, int j) { return findSet(i) == findSet(j); } public void unionSet(int i, int j) { if (isSameSet(i, j)) return; numSets--; int x = findSet(i), y = findSet(j); if(rank[x] > rank[y]) { for (int zz:ele[y])ele[x].add(zz); p[y] = x; setSize[x] += setSize[y]; } else { for (int zz:ele[x])ele[y].add(zz); p[x] = y; setSize[y] += setSize[x]; if(rank[x] == rank[y]) rank[y]++; } } public int numDisjointSets() { return numSets; } public int sizeOfSet(int i) { return setSize[findSet(i)]; } } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() 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
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
a28cde8de7a82d4a41bb76515bf137df
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
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.io.Writer; import java.io.OutputStreamWriter; 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; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } static class TaskE { static final int INF = 1 << 30; public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int m = in.nextInt(); int[] x = new int[n]; int[] s = new int[n]; for (int i = 0; i < n; i++) { x[i] = in.nextInt(); s[i] = in.nextInt(); } int[] perm = new int[n]; int[] X = new int[n]; int[] S = new int[n]; for (int i = 0; i < n; i++) perm[i] = i; for (int i = 0; i < n; i++) { int j; int v = perm[i]; for (j = 0; j < i && x[perm[j]] < x[perm[i]]; j++) { } for (int k = i; k > j; k--) perm[k] = perm[k - 1]; perm[j] = v; } for (int i = 0; i < n; i++) { X[i] = x[perm[i]]; S[i] = s[perm[i]]; } int[] dp = new int[m + 2]; int[] suffixMin = new int[m + 2]; Arrays.fill(dp, INF); Arrays.fill(suffixMin, m + 1); dp[m + 1] = 0; for (int i = n - 1; i >= 0; i--) { int lftEnd = X[i] - S[i]; int rgtEnd = X[i] + S[i]; for (int j = 1; j <= m; j++) { int minExtend = 0; if (lftEnd > j) minExtend = lftEnd - j; if (rgtEnd < j) minExtend = j - rgtEnd; int pos = rgtEnd + minExtend; if (pos >= m) dp[j] = Integer.min(dp[j], minExtend); else dp[j] = Integer.min(dp[j], minExtend + suffixMin[pos + 1] - pos - 1); } for (int j = m; j >= 1; j--) { suffixMin[j] = Integer.min(suffixMin[j + 1], dp[j] + j); } } out.println(dp[1]); } } 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 println(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } writer.print('\n'); } 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 interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
c87ccfaa73b36db380b578908a45f6e2
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.util.Scanner; import static java.lang.Math.*; public class E { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt() + 1; int p[] = new int[n]; int s[] = new int[n]; int pokr[] = new int[m]; for (int i = 0; i < n; i++) { p[i] = in.nextInt(); s[i] = in.nextInt(); for (int j = max(p[i] - s[i], 1); j < min(m, p[i] + s[i]); j++) { pokr[j] = 1; } } int dp[] = new int[m]; for (int i = 1; i < m; i++) { dp[i] = i; } for (int i = 0; i < m; i++) { if (pokr[i] == 1) dp[i] = dp[i - 1]; for (int j = 0; j < n; j++) { if (p[j] <= i) { int k = max(i - (p[j] + s[j]), 0); dp[i] = min(dp[i], k + dp[max(0, p[j] - s[j] - k - 1)]); } } } System.out.println(dp[m - 1]); } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
00932e8e7692eea90ffcdb5adbd4b34d
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; public class Main { static int inf = (int) 1e9 + 7; public static void main(String[] args) throws IOException { sc = new Scanner(System.in); pw = new PrintWriter(System.out); int n = sc.nextInt(); int m = sc.nextInt(); int a[] = new int [n]; int r[] = new int [n]; for(int i = 0;i < n;i++) { a[i] = sc.nextInt(); r[i] = sc.nextInt(); } int dp[] = new int [m + 1]; dp[0] = 0; for(int i = 1;i <= m;i++) { dp[i] = inf; for(int j = 0;j < n;j++) { if (a[j] - r[j] > i) continue; dp[i] = Math.min(dp[i], dp[Math.max(a[j] - r[j] - Math.max(i - a[j] - r[j], 0) - 1, 0)] + Math.max(i - a[j] - r[j], 0)); dp[i] = Math.min(dp[i], Math.max(Math.max(i - a[j] - r[j], 0), Math.max(a[j] - r[j] - 1, 0))); } } //for(int i = 0;i <= m;i++) pw.println(i + " " + dp[i]); pw.println(dp[m]); pw.close(); } static Scanner sc; static PrintWriter pw; static class Scanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); Scanner(InputStream in) throws FileNotFoundException { br = new BufferedReader(new InputStreamReader(in)); } Scanner(String in) throws FileNotFoundException { br = new BufferedReader(new FileReader(in)); } String next() throws IOException { while (!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()); } } } class pair implements Comparator<pair>{ int a, b; pair(int a, int b) { this.a = a; this.b = b; } pair() {} @Override public int compare(pair o1, pair o2) { return Integer.compare(o1.a - o1.b, o2.a - o2.b); } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
51b391a296401165b65b64ad08b0f490
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Arrays; import java.io.IOException; import java.io.UncheckedIOException; import java.io.Closeable; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27); thread.start(); thread.join(); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInput in = new FastInput(inputStream); FastOutput out = new FastOutput(outputStream); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } } static class TaskE { public void solve(int testNumber, FastInput in, FastOutput out) { int n = in.readInt(); int m = in.readInt(); TaskE.Node[] nodes = new TaskE.Node[1 + n]; for (int i = 1; i <= n; i++) { nodes[i] = new TaskE.Node(); int x = in.readInt(); int s = in.readInt(); nodes[i].l = x - s; nodes[i].r = x + s; } Arrays.sort(nodes, 1, n + 1, (a, b) -> a.l - b.l); int[][] dp = new int[n + 1][m + 1]; SequenceUtils.deepFill(dp, (int) 1e8); dp[0][0] = 0; int[] minUntil = new int[m + 1]; for (int i = 0; i <= m; i++) { minUntil[i] = i; } for (int i = 1; i <= n; i++) { for (int j = m; j >= 0; j--) { int ext = Math.max(0, j - nodes[i].r); int l = Math.max(0, nodes[i].l - ext - 1); dp[i][j] = minUntil[l] + ext; if (j < m) { dp[i][j] = Math.min(dp[i][j], dp[i][j + 1]); } } for (int j = 0; j <= m; j++) { minUntil[j] = Math.min(minUntil[j], dp[i][j]); } } out.println(minUntil[m]); } static class Node { int l; int r; } } static class FastOutput implements AutoCloseable, Closeable { private StringBuilder cache = new StringBuilder(10 << 20); private final Writer os; public FastOutput(Writer os) { this.os = os; } public FastOutput(OutputStream os) { this(new OutputStreamWriter(os)); } public FastOutput println(int c) { cache.append(c).append('\n'); return this; } public FastOutput flush() { try { os.append(cache); os.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { os.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } static class FastInput { private final InputStream is; private byte[] buf = new byte[1 << 13]; private int bufLen; private int bufOffset; private int next; public FastInput(InputStream is) { this.is = is; } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { bufLen = -1; } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public int readInt() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } int val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } } static class SequenceUtils { public static void deepFill(Object array, int val) { if (!array.getClass().isArray()) { throw new IllegalArgumentException(); } if (array instanceof int[]) { int[] intArray = (int[]) array; Arrays.fill(intArray, val); } else { Object[] objArray = (Object[]) array; for (Object obj : objArray) { deepFill(obj, val); } } } } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
7e6130268eda22d094789f8777d0c0d6
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
//package codeforces.round600div2; import java.io.*; import java.util.StringTokenizer; public class AntennaCoverage { public static void main(String[] args) { // try { // FastScanner in = new FastScanner(new FileInputStream("src/input.in")); // PrintWriter out = new PrintWriter(new FileOutputStream("src/output.out")); FastScanner in = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); solve(1, in, out); // } // catch (IOException e) { // e.printStackTrace(); // } } private static void solve(int q, FastScanner in, PrintWriter out) { for(int c = 0; c < q; c++) { int n = in.nextInt(), m = in.nextInt(); int[][] intervals = new int[n][2]; for(int i = 0; i < n; i++) { int start = in.nextInt() - 1; int diff = in.nextInt(); intervals[i][0] = start - diff; intervals[i][1] = start + diff; } int[] dp = new int[m]; for(int i = 0; i < m; i++) { dp[i] = i + 1; boolean covered = false; for(int j = 0; j < n; j++) { if(intervals[j][0] <= i && intervals[j][1] >= i) { covered = true; break; } } if(covered) { dp[i] = i == 0 ? 0 : dp[i - 1]; } else { for(int j = 0; j < n; j++) { if(intervals[j][0] > i) { continue; } int diff = i - intervals[j][1]; dp[i] = Math.min(dp[i], diff + (intervals[j][0] <= diff ? 0 : dp[intervals[j][0] - diff - 1])); } } } out.println(dp[m - 1]); } out.close(); } static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream)); } catch (Exception e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
b3f2c5e4d691c2bcd221a292de8245e9
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.io.*; import java.util.*; public class tr7 { public static void main(String[] args) throws Throwable { Scanner sc = new Scanner(); PrintWriter pw = new PrintWriter(System.out); n = sc.nextInt(); m = sc.nextInt(); x = new int[n]; s = new int[n]; for (int i = 0; i < n; i++) { x[i] = sc.nextInt(); s[i] = sc.nextInt(); } mem = new Integer[m + 2]; pw.println(dp(1)); pw.close(); } static int n, m; static Integer[] mem; static int[] x, s; static int dp(int i) { if (i > m) return 0; if (mem[i] != null) return mem[i]; int ans = m; for (int j = 0; j < n; j++) { int newS = Math.max(s[j], Math.abs(x[j] - i)); int cost = newS - s[j]; ans = Math.min(ans, cost + dp(x[j] + newS + 1)); } ans = Math.min(ans, 1 + dp(i + 1)); return mem[i] = ans; } static class Scanner { StringTokenizer st; BufferedReader br; Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } Scanner(String s) throws Throwable { br = new BufferedReader(new FileReader(new File(s))); } String next() throws Throwable { if (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int nextInt() throws Throwable { return Integer.parseInt(next()); } long nextLong() throws Throwable { return Long.parseLong(next()); } } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
60cef1e1849e1ee32df444caaeba9a22
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.awt.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.List; public class Main { public static void main(String[] args) throws IOException { FastReader sc = new FastReader(); int n=sc.nextInt(),m=sc.nextInt(); int interval[][]=new int[n][2]; for (int i=0;i<n;i++){ int a=sc.nextInt(),s=sc.nextInt(); interval[i][0]=a-s; interval[i][1]=a+s; } int dp[]=new int[m+1]; for (int pos=1;pos<=m;pos++){ dp[pos]=pos; boolean covered=false; for (int i=0;i<n;i++){ if (interval[i][0]<=pos && interval[i][1]>=pos){ covered=true; dp[pos]=dp[pos-1]; break; } } if (!covered){ for (int i=0;i<n;i++){ if (interval[i][0]>pos)continue; int diff=pos-interval[i][1]; // System.out.println(diff+"hi"); dp[pos]=Math.min(dp[pos],(interval[i][0]-diff<=0?0:dp[interval[i][0]-diff-1])+diff); } } // System.out.println(dp[pos]); } System.out.println(dp[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 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
7359158a89c5b09d4aeb42a4a2e0de5d
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.io.*; import java.util.*; /** * A simple template for competitive programming problems. */ public class Banana { final InputReader in = new InputReader(System.in); final PrintWriter out = new PrintWriter(System.out); boolean DEBUG = false; int n; int m; int[] dp; int[] x; int[] s; void solve() { n = in.nextInt(); m = in.nextInt(); dp = new int[m]; Arrays.fill(dp, -1); x = new int[n]; s = new int[n]; for(int i=0; i<n; i++) { x[i] = in.nextInt()-1; s[i] = in.nextInt(); } out.println(recurse(0)); } int recurse(int i) { if(i>=m) return 0; if (dp[i] != -1) return dp[i]; int ans = (int) 1e9; for (int j = 0; j < n; j++) { int s1 = Math.max(s[j], Math.abs(x[j] - i)); int cost = s1 - s[j]; int tmp = cost + recurse(x[j] + s1 + 1); ans = Math.min(ans, tmp); } if (i > 0) ans = Math.min(ans, 1 + recurse(i + 1)); dp[i] = ans; return dp[i]; } class A { int x; int s; int id; A(int x, int s, int id) {this.x = x; this.s = s; this.id = id;} } public static void main(String[] args) throws FileNotFoundException { Banana s = new Banana(); s.solve(); s.out.close(); } public Banana() throws FileNotFoundException { } private static class InputReader { private final InputStream stream; private final byte[] buf = new byte[1024]; private int curChar; private int numChars; InputReader(InputStream stream) { this.stream = stream; } InputReader(String fileName) { InputStream stream = null; try { stream = new FileInputStream(fileName); } catch (FileNotFoundException e) { e.printStackTrace(); } this.stream = stream; } int[] nextArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } int[] nextRandomArray(int n, int lim) { int[] arr = new int[n]; Random r = new Random(); for(int i=0; i<n; i++) { arr[i] = r.nextInt(lim); } return arr; } int[] nextRandomArray(int n) { int[] arr = new int[n]; Random r = new Random(); for(int i=0; i<n; i++) { arr[i] = r.nextInt(); } return arr; } int[] nextRandomArray(int n, int low, int lim) { int[] arr = new int[n]; Random r = new Random(); for(int i=0; i<n; i++) { arr[i] = low+r.nextInt(lim-low+1); } return arr; } int[][] nextMatrix(int n, int m) { int[][] matrix = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) matrix[i][j] = nextInt(); return matrix; } 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(); } String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } 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; } 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; } double nextDouble() { return Double.parseDouble(nextString()); } private 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++]; } private 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; } } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
68b4ccf21cbfcb653da220ce7805fb5e
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.io.*; import java.util.*; /** * A simple template for competitive programming problems. */ public class Banana { final InputReader in = new InputReader(System.in); final PrintWriter out = new PrintWriter(System.out); boolean DEBUG = false; void solve() { int n = in.nextInt(); int m =in.nextInt(); A[] as = new A[n]; for(int i=0; i<n; i++) { as[i] = new A(in.nextInt()-1, in.nextInt(), i); } A[] lps = Arrays.copyOf(as, n); Arrays.sort(lps, (A a1, A a2) -> a1.x-a1.s-(a2.x-a2.s)); A[] rps = Arrays.copyOf(as, n); Arrays.sort(rps, (A a1, A a2) -> a1.x+a1.s-(a2.x+a2.s)); int[] dp = new int[m]; int untilCovered = m; boolean f = true; int r = n-1; for(int i=m-1; i>=0; i--) { while(r>=0 && rps[r].x+rps[r].s>=i) { untilCovered = Math.min(untilCovered, rps[r].x-rps[r].s); r--; } if(untilCovered<=i) { if(f) { dp[i] = m-i-1; f = false; } else { dp[i] = dp[i+1]; } } else { int min = Integer.MAX_VALUE; int mrp = Math.min(m-1, rps[n-1].x+rps[n-1].s); for(int j=r+1; j<=n-1; j++) { int cost = rps[j].x-rps[j].s-i; int newRp = rps[j].x+rps[j].s+cost; int cand; if(newRp>=m-1) { cand = cost; } else if(newRp<mrp) { cand = cost + dp[newRp+1]; } else { cand = cost + m-1-newRp; } min = Math.min(min, cand); } dp[i] = min; } } out.println(dp[0]); } class A { int x; int s; int id; A(int x, int s, int id) {this.x = x; this.s = s; this.id = id;} } public static void main(String[] args) throws FileNotFoundException { Banana s = new Banana(); s.solve(); s.out.close(); } public Banana() throws FileNotFoundException { } private static class InputReader { private final InputStream stream; private final byte[] buf = new byte[1024]; private int curChar; private int numChars; InputReader(InputStream stream) { this.stream = stream; } InputReader(String fileName) { InputStream stream = null; try { stream = new FileInputStream(fileName); } catch (FileNotFoundException e) { e.printStackTrace(); } this.stream = stream; } int[] nextArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } int[] nextRandomArray(int n, int lim) { int[] arr = new int[n]; Random r = new Random(); for(int i=0; i<n; i++) { arr[i] = r.nextInt(lim); } return arr; } int[] nextRandomArray(int n) { int[] arr = new int[n]; Random r = new Random(); for(int i=0; i<n; i++) { arr[i] = r.nextInt(); } return arr; } int[] nextRandomArray(int n, int low, int lim) { int[] arr = new int[n]; Random r = new Random(); for(int i=0; i<n; i++) { arr[i] = low+r.nextInt(lim-low+1); } return arr; } int[][] nextMatrix(int n, int m) { int[][] matrix = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) matrix[i][j] = nextInt(); return matrix; } 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(); } String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } 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; } 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; } double nextDouble() { return Double.parseDouble(nextString()); } private 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++]; } private 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; } } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
97bf81f33c57ffac0f0609ad597d76e6
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
/*Author: Satyajeet Singh, Delhi Technological University*/ import java.io.*; import java.util.*; import java.text.*; import java.lang.*; import java.math.*; public class Main{ /*********************************************Constants******************************************/ static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out)); static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static long mod=(long)1e9+7; static long mod1=998244353; static boolean sieve[]; static ArrayList<Integer> primes; static long factorial[],invFactorial[]; static ArrayList<Pair> graph[]; static int pptr=0; static String st[]; /****************************************Solutions Begins***************************************/ static class segmentTree{ int n=0; int[] lo,hi; long[] value; segmentTree(int n){ this.n=n; lo=new int[4*n+1]; hi=new int[4*n+1]; value=new long[4*n+1]; init(1,1,n); } void init(int i,int left,int right){ lo[i]=left; hi[i]=right; if(left==right){ return; } int mid=(left+right)/2; init(2*i,left,mid); init(2*i+1,mid+1,right); } void update(int left,int right,long val){ update(1,left,right,val); } long query(int left,int right){ return query(1,left,right); } void update(int i,int left,int right,long val){ if(left>hi[i]||right<lo[i]){ return; } if(lo[i]==hi[i]){ value[i]=val; return ; } update(2*i,left,right,val); update(2*i+1,left,right,val); value[i]=Math.min(value[2*i],value[2*i+1]); } long query(int i,int left,int right){ if(left>hi[i]||right<lo[i]){ return Long.MAX_VALUE>>1; } if(lo[i]>=left&&hi[i]<=right){ return value[i]; } long l=query(2*i,left,right); long r=query(2*i+1,left,right); return Math.min(l,r); } } public static void main(String[] args) throws Exception{ nl(); int n=pi(); int m=pi(); int sz[]=new int[m+1]; int queries[][]=new int[m+1][]; for(int i=0;i<=m-1;i++){ queries[i]=new int[2*n+2]; } queries[m]=new int[2*n*m+5]; for(int i=0;i<n;i++){ nl(); int x=pi(); int d=pi(); int y=0; int idx=Math.min(m,y+x+d); while(y+x+d<=m||(x-(y+d))>=1){ idx=Math.min(m,y+x+d); queries[idx][sz[idx]++]=Math.max(1,x-(d+y)); queries[idx][sz[idx]++]=y; y++; } if(y==0){ queries[idx][sz[idx]++]=Math.max(1,x-(d+y)); queries[idx][sz[idx]++]=y; } } // debug(queries); long dp[]=new long[m+1]; Arrays.fill(dp,Long.MAX_VALUE>>1); dp[0]=0; segmentTree seg=new segmentTree(m); for(int j=1;j<=m;j++){ for(int i=0;i<sz[j];i+=2){ int l=queries[j][i]; int cost=queries[j][i+1]; long mn=Math.min(dp[l-1],seg.query(l,j-1)); dp[j]=Math.min(dp[j],mn+cost); } seg.update(j,j,dp[j]); } // debug(dp); out.println(dp[m]); /****************************************Solutions Ends**************************************************/ out.flush(); out.close(); } /****************************************Template Begins************************************************/ static void nl() throws Exception{ pptr=0; st=br.readLine().split(" "); } static void nls() throws Exception{ pptr=0; st=br.readLine().split(""); } static int pi(){ return Integer.parseInt(st[pptr++]); } static long pl(){ return Long.parseLong(st[pptr++]); } static double pd(){ return Double.parseDouble(st[pptr++]); } static String ps(){ return st[pptr++]; } /***************************************Precision Printing**********************************************/ static void printPrecision(double d){ DecimalFormat ft = new DecimalFormat("0.000000000000000000000"); out.print(ft.format(d)); } /**************************************Bit Manipulation**************************************************/ static void printMask(long mask){ System.out.println(Long.toBinaryString(mask)); } static int countBit(long mask){ int ans=0; while(mask!=0){ mask&=(mask-1); ans++; } return ans; } /******************************************Graph*********************************************************/ static void Makegraph(int n){ graph=new ArrayList[n]; for(int i=0;i<n;i++){ graph[i]=new ArrayList<>(); } } static void addEdge(int a,int b){ graph[a].add(new Pair(b,1)); } static void addEdge(int a,int b,int c){ graph[a].add(new Pair(b,c)); } /*********************************************PAIR********************************************************/ static class Pair implements Comparable<Pair> { int u; int v; int index=-1; public Pair(int u, int v) { this.u = u; this.v = v; } public int hashCode() { int hu = (int) (u ^ (u >>> 32)); int hv = (int) (v ^ (v >>> 32)); return 31 * hu + hv; } public boolean equals(Object o) { Pair other = (Pair) o; return u == other.u && v == other.v; } public int compareTo(Pair other) { if(index!=other.index) return Long.compare(index, other.index); return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u); } public String toString() { return "[u=" + u + ", v=" + v + "]"; } } /******************************************Long Pair*******************************************************/ static class Pairl implements Comparable<Pairl> { long u; long v; int index=-1; public Pairl(long u, long v) { this.u = u; this.v = v; } public int hashCode() { int hu = (int) (u ^ (u >>> 32)); int hv = (int) (v ^ (v >>> 32)); return 31 * hu + hv; } public boolean equals(Object o) { Pairl other = (Pairl) o; return u == other.u && v == other.v; } public int compareTo(Pairl other) { if(index!=other.index) return Long.compare(index, other.index); return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u); } public String toString() { return "[u=" + u + ", v=" + v + "]"; } } /*****************************************DEBUG***********************************************************/ public static void debug(Object... o){ System.out.println(Arrays.deepToString(o)); } /************************************MODULAR EXPONENTIATION***********************************************/ static long modulo(long a,long b,long c){ long x=1; long y=a%c; while(b > 0){ if(b%2 == 1){ x=(x*y)%c; } y = (y*y)%c; // squaring the base b /= 2; } return x%c; } /********************************************GCD**********************************************************/ static long gcd(long x, long y){ if(x==0) return y; if(y==0) return x; long r=0, a, b; a = (x > y) ? x : y; // a is greater number b = (x < y) ? x : y; // b is smaller number r = b; while(a % b != 0){ r = a % b; a = b; b = r; } return r; } /******************************************SIEVE**********************************************************/ static void sieveMake(int n){ sieve=new boolean[n]; Arrays.fill(sieve,true); sieve[0]=false; sieve[1]=false; for(int i=2;i*i<n;i++){ if(sieve[i]){ for(int j=i*i;j<n;j+=i){ sieve[j]=false; } } } primes=new ArrayList<Integer>(); for(int i=0;i<n;i++){ if(sieve[i]){ primes.add(i); } } } /********************************************End***********************************************************/ }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
256bb8f6958d47081cde40a5ba3cc962
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
/** * BaZ :D */ import java.util.*; import java.io.*; import static java.lang.Math.*; public class Main { static MyScanner scan; static PrintWriter pw; static long MOD = 1_000_000_007; static long INF = 1_000_000_000_000_000_000L; static long inf = 2_000_000_000; public static void main(String[] args) { new Thread(null, null, "BaZ", 1 << 27) { public void run() { try { solve(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } }.start(); } static int n,m,dp[][],x[],s[]; static void solve() throws IOException { //initIo(true); initIo(false); StringBuilder sb = new StringBuilder(); n = ni(); m = ni(); dp = new int[m+1][2]; x = new int[n]; s = new int[n]; for(int i=0;i<n;++i) { x[i] = ni(); s[i] = ni(); } for(int i=0;i<=m;++i) { dp[i][0] = dp[i][1] = -1; } pl(f(1, 0)); pw.flush(); pw.close(); } static int f(int idx, int prev_covered) { if(idx>m) { return 0; } if(dp[idx][prev_covered]!=-1) { return dp[idx][prev_covered]; } int ans = 1000000; if(prev_covered == 1) { ans = min(ans, f(idx+1, 1) + 1); } for(int i=0;i<n;++i) { if(x[i]<idx) { if(x[i]+s[i]>=idx) { ans = min(ans, f(idx+1, 1)); } else { int need = idx - (x[i] + s[i]); ans = min(ans, need + f(idx+1, 1)); } } else { if(x[i] - s[i] <= idx) { ans = min(ans, f(x[i]+s[i]+1, 1)); } else { int need = (x[i] - s[i]) - idx; ans = min(ans, need + f(x[i]+s[i] + need+1, 1)); } } } //pl("ret : "+ans+ " in : "+idx); return dp[idx][prev_covered] = ans; } static void initIo(boolean isFileIO) throws IOException { scan = new MyScanner(isFileIO); if(isFileIO) { pw = new PrintWriter("/Users/amandeep/Desktop/output.txt"); } else { pw = new PrintWriter(System.out, true); } } static int ni() throws IOException { return scan.nextInt(); } static long nl() throws IOException { return scan.nextLong(); } static double nd() throws IOException { return scan.nextDouble(); } static String ne() throws IOException { return scan.next(); } static String nel() throws IOException { return scan.nextLine(); } static void pl() { pw.println(); } static void p(Object o) { pw.print(o+" "); } static void pl(Object o) { pw.println(o); } static void psb(StringBuilder sb) { pw.print(sb); } static void pa(String arrayName, Object arr[]) { pl(arrayName+" : "); for(Object o : arr) p(o); pl(); } static void pa(String arrayName, int arr[]) { pl(arrayName+" : "); for(int o : arr) p(o); pl(); } static void pa(String arrayName, long arr[]) { pl(arrayName+" : "); for(long o : arr) p(o); pl(); } static void pa(String arrayName, double arr[]) { pl(arrayName+" : "); for(double o : arr) p(o); pl(); } static void pa(String arrayName, char arr[]) { pl(arrayName+" : "); for(char o : arr) p(o); pl(); } static void pa(String listName, List list) { pl(listName+" : "); for(Object o : list) p(o); pl(); } static void pa(String arrayName, Object[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(Object o : arr[i]) p(o); pl(); } } static void pa(String arrayName, int[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(int o : arr[i]) p(o); pl(); } } static void pa(String arrayName, long[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(long o : arr[i]) p(o); pl(); } } static void pa(String arrayName, char[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(char o : arr[i]) p(o); pl(); } } static void pa(String arrayName, double[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(double o : arr[i]) p(o); pl(); } } static class MyScanner { BufferedReader br; StringTokenizer st; MyScanner(boolean readingFromFile) throws IOException { if(readingFromFile) { br = new BufferedReader(new FileReader("/Users/amandeep/Desktop/input.txt")); } else { br = new BufferedReader(new InputStreamReader(System.in)); } } String nextLine()throws IOException { return br.readLine(); } String next() throws IOException { if(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
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
02e4fd671ed2b23778352f4d5244b0bb
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
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.util.Comparator; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Jeel Vaishnav */ 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); EAntennaCoverage solver = new EAntennaCoverage(); solver.solve(1, in, out); out.close(); } static class EAntennaCoverage { int[] segTree; int querySegTree(int low, int high, int pos, int l, int r) { if (low > r || high < l) return Integer.MAX_VALUE; if (low >= l && high <= r) return segTree[pos]; int mid = (low + high) >> 1; int val1 = querySegTree(low, mid, 2 * pos + 1, l, r); int val2 = querySegTree(mid + 1, high, 2 * pos + 2, l, r); return Math.min(val1, val2); } void updateSegTree(int low, int high, int pos, int ind, int val) { if (low > ind || high < ind) return; if (low == high) { segTree[pos] = val; return; } int mid = (low + high) >> 1; updateSegTree(low, mid, 2 * pos + 1, ind, val); updateSegTree(mid + 1, high, 2 * pos + 2, ind, val); segTree[pos] = Math.min(segTree[2 * pos + 1], segTree[2 * pos + 2]); } public void solve(int testNumber, InputReader sc, PrintWriter out) { int n = sc.nextInt(); int m = sc.nextInt(); Pair p[] = new Pair[n]; for (int i = 0; i < n; ++i) p[i] = new Pair(sc.nextInt(), sc.nextInt()); Arrays.sort(p, new Comparator<Pair>() { public int compare(Pair o1, Pair o2) { if (o1.ind < o2.ind) return -1; if (o1.ind > o2.ind) return 1; return 0; } }); int dp[] = new int[2 * m]; segTree = new int[4 * 2 * m]; int ans = Integer.MAX_VALUE; Arrays.fill(segTree, Integer.MAX_VALUE); for (int i = 0; i < 2 * m; ++i) { dp[i] = Integer.MAX_VALUE; for (int j = 0; j < n; ++j) { if (p[j].ind <= i) { int reqDist = i - p[j].ind; int cost = Math.max(0, reqDist - p[j].scope); int lastInd = p[j].ind - reqDist - 1; if (lastInd < 0) { dp[i] = Math.min(dp[i], cost); } else { int val = querySegTree(0, 2 * m - 1, 0, lastInd, i - 1); if (val != Integer.MAX_VALUE) dp[i] = Math.min(dp[i], val + cost); } } } updateSegTree(0, 2 * m - 1, 0, i, dp[i]); if (i >= m - 1) ans = Math.min(ans, dp[i]); } out.print(ans); } class Pair { int ind; int scope; Pair(int a, int b) { ind = a - 1; scope = b; } } } 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
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
0be4faa2edf9cd8299c79464a9786d47
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
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.util.Comparator; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Sparsh Sanchorawala */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); EAntennaCoverage solver = new EAntennaCoverage(); solver.solve(1, in, out); out.close(); } static class EAntennaCoverage { public void solve(int testNumber, InputReader s, PrintWriter w) { int n = s.nextInt(), m = s.nextInt(); int[] pos = new int[n]; int[] scope = new int[n]; for (int i = 0; i < n; i++) { pos[i] = s.nextInt(); scope[i] = s.nextInt(); } Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = i; Arrays.sort(a, new Comparator<Integer>() { public int compare(Integer o1, Integer o2) { if (pos[o1] < pos[o2]) return -1; if (pos[o1] > pos[o2]) return 1; return 0; } }); long[] dp = new long[m + 1]; Arrays.fill(dp, Integer.MAX_VALUE); dp[0] = 0; for (int i = 0; i < n; i++) { int p = pos[a[i]]; int sc = scope[a[i]]; long[] dp0 = new long[m + 1]; for (int j = 0; j <= m; j++) { dp0[j] = dp[j]; } Arrays.fill(dp, Integer.MAX_VALUE); dp[0] = 0; for (int j = m; j >= 1; j--) { int left = Math.max(Math.min(p - sc - 1, p - (j - p) - 1), 0); dp[j] = dp0[left] + Math.max(0, (j - (p + sc))); dp[j] = Math.min(dp0[j], dp[j]); dp[j] = Math.min(Math.max(0, Math.max(p - sc - 1, j - (p + sc))), dp[j]); } } w.println(dp[m]); } } 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 interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
ce07f40d078823779bd11c6b7daaac8e
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
/* If you want to aim high, aim high Don't let that studying and grades consume you Just live life young ****************************** If I'm the sun, you're the moon Because when I go up, you go down ******************************* I'm working for the day I will surpass you https://www.a2oj.com/Ladder16.html */ import java.util.*; import java.io.*; import java.math.*; public class x1253E { static int M; static Range[] arr; public static void main(String omkar[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); M = Integer.parseInt(st.nextToken()); arr = new Range[N]; for(int i=0; i < N; i++) { st = new StringTokenizer(infile.readLine()); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); arr[i] = new Range(a,b); } dp = new long[M+1]; Arrays.fill(dp, Long.MAX_VALUE); System.out.println(dfs(1)); } static long[] dp; public static long dfs(int i) { if(i > M) return 0; if(dp[i] != Long.MAX_VALUE) return dp[i]; long res = Long.MAX_VALUE; for(Range curr: arr) { if(i >= curr.x-curr.r && i <= curr.x+curr.r) res = Math.min(res, dfs(curr.x+curr.r+1)); else if(i < curr.x-curr.r) { int left = curr.x-curr.r; res = Math.min(res, left-i+dfs(curr.x+curr.r+left-i+1)); } } res = Math.min(res, 1+dfs(i+1)); return dp[i] = res; } } class Range { public int x; public int r; public Range(int a, int b) { x = a; r = b; } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
9b8a4fb0ecc597e2bebe529eb75f9ea0
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class C { public static void main(String[] args) { FastReader scan = new FastReader(); PrintWriter out = new PrintWriter(System.out); int n = scan.nextInt(), m = scan.nextInt(); int[][] interval = new int[n][2]; long[] minCost = new long[m+1]; for(int i = 0; i < n; i++) { int pos = scan.nextInt(), range = scan.nextInt(); interval[i][0] = Math.max(1, pos-range); interval[i][1] = Math.min(m, pos+range); } for(int i = 1; i <= m; i++) { minCost[i] = i; for(int j = 0; j < n; j++) { if(i >= interval[j][0] && i <= interval[j][1]) minCost[i] = Math.min(minCost[i], minCost[i-1]); else if(i > interval[j][1]) { int currCost = i - interval[j][1], left = Math.max(0, interval[j][0] - currCost - 1); minCost[i] = Math.min(minCost[i], minCost[left] + currCost); } } } out.println(minCost[m]); out.close(); } 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 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
6f5caeb1ba538421f0f498594f331e81
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
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.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author KharYusuf */ 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); EAntennaCoverage solver = new EAntennaCoverage(); solver.solve(1, in, out); out.close(); } static class EAntennaCoverage { public void solve(int testNumber, FastReader s, PrintWriter w) { int n = s.nextInt(), m = s.nextInt(); pair<Integer, Integer>[] p = new pair[n]; int[] cover = new int[m + 2], cost = new int[m + 1]; for (int i = 0; i < n; i++) { p[i] = new pair<>(s.nextInt(), s.nextInt()); cover[Math.max(1, p[i].x - p[i].y)]++; cover[Math.min(m, p[i].x + p[i].y) + 1]--; } for (int i = 2; i <= m; i++) cover[i] += cover[i - 1]; for (int i = m - 1; i >= 0; i--) { if (cover[i + 1] != 0) { cost[i] = cost[i + 1]; continue; } int mn = m - i; for (int j = 0; j < n; j++) { if (p[j].x - p[j].y <= i) continue; int to = p[j].x - p[j].y - (i + 1); mn = Math.min(mn, to + cost[Math.min(m, p[j].x + p[j].y + to)]); } cost[i] = mn; } w.println(cost[0]); } } static class pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<pair<U, V>> { public U x; public V y; public pair(U x, V y) { this.x = x; this.y = y; } public int compareTo(pair<U, V> other) { int i = x.compareTo(other.x); if (i != 0) return i; return y.compareTo(other.y); } public String toString() { return x.toString() + " " + y.toString(); } public boolean equals(Object obj) { if (this.getClass() != obj.getClass()) return false; pair<U, V> other = (pair<U, V>) obj; return x.equals(other.x) && y.equals(other.y); } public int hashCode() { return 31 * x.hashCode() + y.hashCode(); } } 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 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
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
dd1e2a7b549a86deaa93aa77010b56bd
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; import java.util.TreeSet; public class Main { static ArrayList<Integer>adj[]; static PrintWriter out=new PrintWriter(System.out); public static int mod = 1000000007; static Boolean notmemo[][][]; static int k; static int a[]; static long b[]; static int m; public static void main(String[] args) throws Exception { Scanner sc=new Scanner(System.in); n=sc.nextInt(); m=sc.nextInt(); x=new long[n]; s=new long[n]; for (int i = 0; i < s.length; i++) { x[i]=sc.nextLong(); s[i]=sc.nextLong(); } mini=new long[m+1]; Arrays.fill(mini,-1); System.out.println(dp(1)); // System.out.println(count); out.flush(); } static long mini[]; static long x[]; static long s[]; static long count=0; static long dp(int covered) { if(covered>m) { return 0; } long ans=INF; if(mini[covered]!=-1) { return mini[covered]; } //System.out.println(covered); for(int i=0 ; i<n ; i++) { long maxx=Math.max(s[i], Math.abs(x[i]-covered)); ans=Math.min(ans, 1l*Math.abs((maxx-s[i]))+dp((int) (maxx+x[i]+1))); } ans=Math.min(ans, 1+dp(covered+1)); return mini[covered]=ans; } static class incpair implements Comparable<incpair> { int a; long b; int idx; incpair(int a, long dirg,int i) {this.a = a; b = dirg; idx=i; } public int compareTo(incpair e){ return (int) (b - e.b); } } static class decpair implements Comparable<decpair> { int a; long b;int idx; decpair(int a, long dirg,int i) {this.a = a; b = dirg; idx=i;} public int compareTo(decpair e){ return (int) (e.b- b); } } static long allpowers[]; static class Quad implements Comparable<Quad>{ int u; int v; char state; int turns; public Quad(int i, int j, char c, int k) { u=i; v=j; state=c; turns=k; } public int compareTo(Quad e){ return (int) (turns - e.turns); } } static long dirg[][]; static Edge[] driver; static int n; static class Edge implements Comparable<Edge> { int node; long cost; Edge(int a, long dirg) { node = a; cost = dirg; } public int compareTo(Edge e){ return (int) (cost - e.cost); } } static long manhatandistance(long x,long x2,long y,long y2) { return Math.abs(x-x2)+Math.abs(y-y2); } static long fib[]; static long fib(int n) { if(n==1||n==0) { return 1; } if(fib[n]!=-1) { return fib[n]; } else return fib[n]= ((fib(n-2)%mod+fib(n-1)%mod)%mod); } static class Pair implements Comparable<Pair>{ int skill; int idx; Pair(int a, int b){ skill=a; idx=b; } public int compareTo(Pair p) { return p.skill-this.skill; } } static long[][] comb; static long nCr1(int n , int k) { if(n < k) return 0; if(k == 0 || k == n) return 1; if(comb[n][k] != -1) return comb[n][k]; if(n - k < k) return comb[n][k] = nCr1(n, n - k); return comb[n][k] = (nCr1(n - 1, k - 1) + nCr1(n - 1, k))%mod; } static class Triple implements Comparable<Triple>{ int u; int v; long totalcost; public Triple(int a,int b,long l) { u=a; v=b; totalcost=l; } @Override public int compareTo(Triple o) { return Long.compare( totalcost,o.totalcost); } } static TreeSet<Long> primeFactors(long N) // O(sqrt(N) / ln sqrt(N)) { TreeSet<Long> factors = new TreeSet<Long>(); //take abs(N) in case of -ve integers int idx = 0, p = primes.get(idx); while(p * p <= N) { while(N % p == 0) { factors.add((long) p); N /= p; } if(primes.size()>idx+1) p = primes.get(++idx); else break; } if(N != 1) // last prime factor may be > sqrt(N) factors.add(N); // for integers whose largest prime factor has a power of 1 return factors; } static boolean visited[]; /**static int bfs(int s) { Queue<Integer> q = new LinkedList<Integer>(); q.add(s); int count=0; int maxcost=0; int dist[]=new int[n]; dist[s]=0; while(!q.isEmpty()) { int u = q.remove(); if(dist[u]==k) { break; } for(Pair v: adj[u]) { maxcost=Math.max(maxcost, v.cost); if(!visited[v.v]) { visited[v.v]=true; q.add(v.v); dist[v.v]=dist[u]+1; maxcost=Math.max(maxcost, v.cost); } } } return maxcost; }**/ public static boolean FindAllElements(int n, int k) { int sum = k; int[] A = new int[k]; Arrays.fill(A, 0, k, 1); for (int i = k - 1; i >= 0; --i) { while (sum + A[i] <= n) { sum += A[i]; A[i] *= 2; } } if(sum==n) { return true; } else return false; } static long[][] memo; static boolean vis2[][]; static class SegmentTree { // 1-based DS, OOP int N; //the number of elements in the array as a power of 2 (i.e. after padding) int[] array, sTree, lazy; SegmentTree(int[] in) { array = in; N = in.length - 1; sTree = new int[N<<1]; //no. of nodes = 2*N - 1, we add one to cross out index zero lazy = new int[N<<1]; build(1,1,N); } void build(int node, int b, int e) // O(n) { if(b == e) sTree[node] = array[b]; else { int mid = b + e >> 1; build(node<<1,b,mid); build(node<<1|1,mid+1,e); sTree[node] = sTree[node<<1]+sTree[node<<1|1]; } } void update_point(int index, int val) // O(log n) { index += N - 1; sTree[index] += val; while(index>1) { index >>= 1; sTree[index] = sTree[index<<1] + sTree[index<<1|1]; } } void update_range(int i, int j, int val) // O(log n) { update_range(1,1,N,i,j,val); } void update_range(int node, int b, int e, int i, int j, int val) { if(i > e || j < b) return; if(b >= i && e <= j) { sTree[node] += (e-b+1)*val; lazy[node] += val; } else { int mid = b + e >> 1; propagate(node, b, mid, e); update_range(node<<1,b,mid,i,j,val); update_range(node<<1|1,mid+1,e,i,j,val); sTree[node] = sTree[node<<1] + sTree[node<<1|1]; } } void propagate(int node, int b, int mid, int e) { lazy[node<<1] += lazy[node]; lazy[node<<1|1] += lazy[node]; sTree[node<<1] += (mid-b+1)*lazy[node]; sTree[node<<1|1] += (e-mid)*lazy[node]; lazy[node] = 0; } int query(int i, int j) { return query(1,1,N,i,j); } int query(int node, int b, int e, int i, int j) // O(log n) { if(i>e || j <b) return 0; if(b>= i && e <= j) return sTree[node]; int mid = b + e >> 1; propagate(node, b, mid, e); int q1 = query(node<<1,b,mid,i,j); int q2 = query(node<<1|1,mid+1,e,i,j); return q1 + q2; } } static boolean f2=false; static long[][] matMul(long[][] a2, long[][] b, int p, int q, int r) //C(p x r) = A(p x q) x (q x r) -- O(p x q x r) { long[][] C = new long[p][r]; for(int i = 0; i < p; ++i) { for(int j = 0; j < r; ++j) { for(int k = 0; k < q; ++k) { C[i][j] = (C[i][j]+(a2[i][k]%mod * b[k][j]%mod))%mod; C[i][j]%=mod; } } } return C; } static ArrayList<Pair> a1[]; static int memo1[]; static boolean vis[]; static TreeSet<Integer> set=new TreeSet<Integer>(); static long modPow(long ways, long count, long mod) // O(log e) { ways %= mod; long res = 1; while(count > 0) { if((count & 1) == 1) res = (res * ways) % mod; ways = (ways * ways) % mod; count >>= 1; } return res%mod; } static long gcd(long ans,long b) { if(b==0) { return ans; } return gcd(b,ans%b); } static int[] isComposite; static int[] valid; static ArrayList<Integer> primes; static ArrayList<Integer> l; static void sieve(int N) // O(N log log N) { isComposite = new int[N+1]; isComposite[0] = isComposite[1] = 1; // 0 indicates a prime number primes = new ArrayList<Integer>(); l=new ArrayList<>(); valid=new int[N+1]; for (int i = 2; i <= N; ++i) //can loop till i*i <= N if primes array is not needed O(N log log sqrt(N)) if (isComposite[i] == 0) //can loop in 2 and odd integers for slightly better performance { primes.add(i); l.add(i); valid[i]=1; for (int j = i*2; j <=N; j +=i) { // j = i * 2 will not affect performance too much, may alter in modified sieve isComposite[j] = 1; } } for(int i=0 ; i<primes.size() ; i++) { for(int j:primes) { if(primes.get(i)*j>N) { break; } valid[primes.get(i)*j]=1; } } } public static long[] schuffle(long[] a2) { for (int i = 0; i < a2.length; i++) { int x=(int)(Math.random()*a2.length); long temp=a2[x]; a2[x]=a2[i]; a2[i]=temp; } return a2; } static int V; static long INF=(long) 1E18; static class Edge2 { int node; long cost; long next; Edge2(int a, int c,Long long1) { node = a; cost = long1; next=c;} } static 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 String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
96df3e8f746a1faeae9afced36586dcf
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.io.*; import java.util.*; public class ProblemE { public static InputStream inputStream = System.in; public static OutputStream outputStream = System.out; public static void main(String[] args) { MyScanner scanner = new MyScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); int n = scanner.nextInt(); int m = scanner.nextInt(); List<Pair<Integer, Integer>> list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(new Pair<>(scanner.nextInt(), scanner.nextInt())); } list.sort(Comparator.comparingInt(t -> t.first)); int[][] dp = new int[n][m + 1]; for (int i = 0; i < n; i++) { for (int k = 1; k <= m; k++) { Pair<Integer, Integer> pair = list.get(i); int a = pair.first - 1 - pair.second; int b = k - pair.first - pair.second; int x = Math.max(a, b); x = Math.max(0, x); dp[i][k] = x; if (i != 0) { dp[i][k] = Math.min(dp[i - 1][k], dp[i][k]); x = Math.max(0, k - pair.first - pair.second); int pk = pair.first - pair.second - x; if (pk > 0) { dp[i][k] = Math.min(dp[i][k], dp[i - 1][pk - 1] + x); } } } } out.println(dp[n - 1][m]); out.flush(); } private static class MyScanner { private BufferedReader bufferedReader; private StringTokenizer stringTokenizer; private MyScanner(InputStream inputStream) { bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); } private String next() { while (stringTokenizer == null || !stringTokenizer.hasMoreElements()) { try { stringTokenizer = new StringTokenizer(bufferedReader.readLine()); } catch (IOException e) { e.printStackTrace(); } } return stringTokenizer.nextToken(); } private int nextInt() { return Integer.parseInt(next()); } private long nextLong() { return Long.parseLong(next()); } private double nextDouble() { return Double.parseDouble(next()); } private String nextLine() { String str = ""; try { str = bufferedReader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } private static class Pair<F, S> { private F first; private S second; public Pair() { } public Pair(F first, S second) { this.first = first; this.second = second; } } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
583ad01c7047351b5cefb9de4ff1a6b4
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; public class antena { private static List<lamp> lamps = new ArrayList<>(); public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer token = new StringTokenizer(br.readLine()); int n = Integer.parseInt(token.nextToken()); int m = Integer.parseInt(token.nextToken()); for (int i = 0; i < n; i++) { token = new StringTokenizer(br.readLine()); int center = Integer.parseInt(token.nextToken()); int radio = Integer.parseInt(token.nextToken()); int left = center - radio; int right = center + radio; lamps.add(new lamp(left, right, center)); } int [] dp = new int [m + 1]; dp [m] = 0; for (int i = m - 1; i >= 0; i--){ dp[i] = m - i; for (lamp theLamp : lamps){ if ( i+1 >= theLamp.left && i+1 <= theLamp.right){ dp [i] = dp [i+1]; break; } if (i < theLamp.left){ int toComplete = theLamp.left - i - 1; dp [i] = Math.min(dp[i], toComplete + dp[Math.min(m, theLamp.right + toComplete)]); } } } bw.write(dp[0] + "\n"); bw.close(); } } class lamp { int left; int right; int center; lamp(int a, int b, int c) { left = a; right = b; center = c; } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
3f921a95ff3ac7c69175aa508204ea56
train_001.jsonl
1573914900
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis.On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$.It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String args[]) {new Main().run();} FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); void run(){ work(); out.flush(); } long mod=998244353; long gcd(long a,long b) { return b==0?a:gcd(b,a%b); } void work() { int n=in.nextInt(); int m=in.nextInt(); int[] dp=new int[m+2]; int[] rec=new int[m+2]; int[][] A=new int[n][]; int min=999999999; for(int i=0;i<n;i++) { int x=in.nextInt(); int s=in.nextInt(); A[i]=new int[] {x-s,x+s}; min=Math.min(min, x-s); rec[Math.max(0,x-s)]++; rec[Math.min(m+1, x+s+1)]--; } for(int i=1;i<=m+1;i++)rec[i]+=rec[i-1]; Arrays.sort(A,new Comparator<int[]>() { public int compare(int[] a1,int[] a2) { return a1[1]-a2[1]; } }); for(int i=1;i<=m;i++) { if(rec[i]>0) { dp[i]=dp[i-1]; }else if(i<min) { dp[i]=i; }else { dp[i]=999999999; for(int j=0;j<n;j++) { if(A[j][1]>i)break; int s=A[j][0]; int e=A[j][1]; int ns=s-(i-e); int v=(i-e); dp[i]=Math.min(dp[i], v+(ns-1<0?0:dp[ns-1])); } } } out.println(dp[m]); } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } public String next() { if(st==null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } }
Java
["3 595\n43 2\n300 4\n554 10", "1 1\n1 1", "2 50\n20 0\n3 1", "5 240\n13 0\n50 25\n60 5\n155 70\n165 70"]
3 seconds
["281", "0", "30", "26"]
NoteIn the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna.Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important.β€”In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do.Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
Java 8
standard input
[ "dp", "sortings", "greedy", "data structures" ]
fccb8049e7b0f0bcd1dcd93620a86b5c
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 80$$$ and $$$n \le m \le 100\ 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 \le x_i \le m$$$ and $$$0 \le s_i \le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct).
2,200
You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna.
standard output
PASSED
91bd6fb0ba00925fb909768afce6dc20
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
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 code.force; import java.util.*; /** * * @author Abdo Harby */ public class Cupboards { public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int r=0,l=0; if(n<=10000&&n>=2){ for (int i=0;i<n;i++){ int left=in.nextInt(); l+=left; int right=in.nextInt(); r+=right; } System.out.println(Math.min(l, n-l)+Math.min(r, n-r)); } } }
Java
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output
PASSED
888653c1522fd636ae5d6a50262ad175
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
256 megabytes
import java.io.*; import java.util.*; public class CF { public static void main(String[] args) throws IOException { FastScanner in = new FastScanner(System.in); int n = in.nextInt(); int l = 0; int r = 0; for (int i = 0; i < n; i++) { l += in.nextInt(); r += in.nextInt(); } int min = Integer.MAX_VALUE; min = Math.min(min, r+l); min = Math.min(min, n-Math.max(l, r)+Math.min(l, r)); min = Math.min(min, 2*n-l-r); System.out.println(min); } 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; } private static class FastScanner { private final InputStream stream; private final 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 int[] nextArrayInt(int n) { int tab[] = new int[n]; for (int i = 0; i < n; i++) { tab[i] = nextInt(); } return tab; } public String[] nextArrayString(int n) { String tab[] = new String[n]; for (int i = 0; i < n; i++) { tab[i] = next(); } return tab; } 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
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output
PASSED
6bb684dff8f97e4366e60e9c2d345aa1
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
256 megabytes
import java.util.Scanner; public class Cupboards { public static void main(String args[]) { Scanner in = new Scanner(System.in); int n, i, sum_r = 0, sum_l = 0, t = 0; n = in.nextInt(); int l[] = new int[n]; int r[] = new int[n]; for(i = 0; i < n; i++) { l[i] = in.nextInt(); sum_l += l[i]; r[i] = in.nextInt(); sum_r += r[i]; } if(sum_l > n/2) t = t + (n - sum_l); else t = t + sum_l; if(sum_r > n/2) t = t + (n - sum_r); else t = t + sum_r; System.out.println(t); } }
Java
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output
PASSED
0b69b6fedfd85aa031a4508406f4d474
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
256 megabytes
import java.util.Scanner; public class que17 { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); int l = 0, r = 0; for (int i = 0; i < n; i++) { int li = scn.nextInt(); int ri = scn.nextInt(); if (li == 0) { l++; } if (ri == 0) { r++; } } System.out.println(Math.min(l, n - l) + Math.min(r, n - r)); } }
Java
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output
PASSED
487e4020ab108b08970a16ef10ed2e2f
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
256 megabytes
import java.util.Scanner; public class codeforces { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int al = 0; int ar = 0; int t = 0; for (int i = 0; i < n; i++) { al += sc.nextInt(); ar += sc.nextInt(); } if(al<n-al){ t+=al; }else{ t+=n-al; } if(ar<n-ar){ t+=ar; } else{ t+=n-ar; } System.out.println(t); } }
Java
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output
PASSED
1f760bb45392fbffce1a9d3a8a88af08
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
256 megabytes
import java.util.Scanner; public class cupboards{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); int lZ =0, lO = 0, rZ = 0, rO = 0; for(int i =0; i< n; i++) { int in1 = sc.nextInt(); int in2 = sc.nextInt(); if(in1 == 0) lZ++; if(in1 == 1) lO++; if(in2 == 0) rZ++; if(in2 ==1) rO++; } //System.out.println(lZ + " " + lO + " " + rZ + " " + rO); int minL = 0, minR = 0; if(lZ < lO) { minL = lZ; if(rZ < rO) { minR = rZ; } else { minR = rO; } } else { minL = lO; if(rZ < rO) { minR = rZ; } else { minR = rO; } } System.out.println((minL+minR)); } }
Java
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output
PASSED
565f6982c60624b82e1147ee92268e6f
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
256 megabytes
import java.util.*; import java.lang.*; import java.util.Scanner; import java.io.*; public class prac11 { public static void main (String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int n01=0; int n02=0; int n11=0; int n12=0; int arr[]=new int[n]; for(int i=0;i<n;i++) { int a=in.nextInt(); int b=in.nextInt(); if(a==0) n01++; else n11++; if(b==0) n02++; else n12++; } int ans=Math.min(n01,n11)+Math.min(n02,n12); System.out.println(ans); } }
Java
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output
PASSED
ed72a9214f1abf2942f7bddef56ac0ed
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
256 megabytes
import java.util.Scanner; public class Cupboards { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int ls = 0; int rs = 0; for(int i=0; i<n; i++){ ls += sc.nextInt(); rs += sc.nextInt(); } int sum = 0; if(ls<=n/2) sum += ls; else sum += (n-ls); if(rs<=n/2) sum += rs; else sum += (n-rs); System.out.println(sum); } }
Java
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output
PASSED
f2a904af2e2a37d3aa070222e7209bc6
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
256 megabytes
import java.util.*; import java.io.*; //267630EY public class Main238A2 { static PrintWriter out=new PrintWriter(System.out); public static void main(String[] args) throws IOException { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int minl=Integer.MAX_VALUE; int minr=Integer.MAX_VALUE; int l0=0; int l1=0; int r0=0; int r1=0; for(int i=0;i<n;i++) { int x=sc.nextInt(); if(x==1) l1++; else l0++; x=sc.nextInt(); if(x==1) r1++; else r0++; } out.println(Math.min(l0,l1)+Math.min(r0,r1)); out.flush(); } static class Scanner { BufferedReader br; StringTokenizer tk=new StringTokenizer(""); public Scanner(InputStream is) { br=new BufferedReader(new InputStreamReader(is)); } public boolean hasNext() { boolean result=false; try { result = br.ready(); } catch (IOException e) { System.err.println(e); } return result; } public int nextInt() throws IOException { if(tk.hasMoreTokens()) return Integer.parseInt(tk.nextToken()); tk=new StringTokenizer(br.readLine()); return nextInt(); } public long nextLong() throws IOException { if(tk.hasMoreTokens()) return Long.parseLong(tk.nextToken()); tk=new StringTokenizer(br.readLine()); return nextLong(); } public String next() throws IOException { if(tk.hasMoreTokens()) return (tk.nextToken()); tk=new StringTokenizer(br.readLine()); return next(); } public String nextLine() throws IOException { tk=new StringTokenizer(""); return br.readLine(); } public double nextDouble() throws IOException { if(tk.hasMoreTokens()) return Double.parseDouble(tk.nextToken()); tk=new StringTokenizer(br.readLine()); return nextDouble(); } public char nextChar() throws IOException { if(tk.hasMoreTokens()) return (tk.nextToken().charAt(0)); tk=new StringTokenizer(br.readLine()); return nextChar(); } public int[] nextIntArray(int n) throws IOException { int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=nextInt(); return a; } public long[] nextLongArray(int n) throws IOException { long a[]=new long[n]; for(int i=0;i<n;i++) a[i]=nextLong(); return a; } public int[] nextIntArrayOneBased(int n) throws IOException { int a[]=new int[n+1]; for(int i=1;i<=n;i++) a[i]=nextInt(); return a; } public long[] nextLongArrayOneBased(int n) throws IOException { long a[]=new long[n+1]; for(int i=1;i<=n;i++) a[i]=nextLong(); return a; } } }
Java
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output
PASSED
55bcc1c5609e4506e3c57185839008d0
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
256 megabytes
import java.util.*; import java.io.*; public class Main248A { static PrintWriter out=new PrintWriter(System.out); public static void main(String[] args) throws IOException { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int ro=0; int r1=0; int lo=0; int l1=0; for(int i=0;i<n;i++) { int l=sc.nextInt(); int r=sc.nextInt(); if(l==0) lo++; else if(l==1) l1++; if(r==0) ro++; else if(r==1) r1++; } int allC=l1+r1; int allO=lo+ro; int co=l1+ro; int oc=lo+r1; int ans=Math.min(allC,Math.min(allO,Math.min(co,oc))); out.println(ans); out.flush(); } static class Scanner { BufferedReader br; StringTokenizer tk=new StringTokenizer(""); public Scanner(InputStream is) { br=new BufferedReader(new InputStreamReader(is)); } public int nextInt() throws IOException { if(tk.hasMoreTokens()) return Integer.parseInt(tk.nextToken()); tk=new StringTokenizer(br.readLine()); return nextInt(); } public long nextLong() throws IOException { if(tk.hasMoreTokens()) return Long.parseLong(tk.nextToken()); tk=new StringTokenizer(br.readLine()); return nextLong(); } public String next() throws IOException { if(tk.hasMoreTokens()) return (tk.nextToken()); tk=new StringTokenizer(br.readLine()); return next(); } public String nextLine() throws IOException { tk=new StringTokenizer(""); return br.readLine(); } public double nextDouble() throws IOException { if(tk.hasMoreTokens()) return Double.parseDouble(tk.nextToken()); tk=new StringTokenizer(br.readLine()); return nextDouble(); } public char nextChar() throws IOException { if(tk.hasMoreTokens()) return (tk.nextToken().charAt(0)); tk=new StringTokenizer(br.readLine()); return nextChar(); } public int[] nextIntArray(int n) throws IOException { int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=nextInt(); return a; } public long[] nextLongArray(int n) throws IOException { long a[]=new long[n]; for(int i=0;i<n;i++) a[i]=nextLong(); return a; } public int[] nextIntArrayOneBased(int n) throws IOException { int a[]=new int[n+1]; for(int i=1;i<=n;i++) a[i]=nextInt(); return a; } public long[] nextLongArrayOneBased(int n) throws IOException { long a[]=new long[n+1]; for(int i=1;i<=n;i++) a[i]=nextLong(); return a; } } }
Java
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output
PASSED
574996c8035f7d66ab8c22e5cd7fe815
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
256 megabytes
import java.util.Scanner; public class Cupboards{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int testCases = sc.nextInt(); int countLeft1 = 0; int countRight1 = 0; int countLeft0 = 0; int countRight0 = 0; int result = 0; while(testCases-- > 0){ int left = sc.nextInt(); int right = sc.nextInt(); if(left == 1){ countLeft1++; }else if(left == 0){ countLeft0++; } if(right == 1){ countRight1++; }else if(right == 0){ countRight0++; } } if(countRight0 <= countRight1){ result += countRight0; }else if(countRight0 > countRight1){ result += countRight1; } if(countLeft0 <= countLeft1){ result += countLeft0; }else if(countLeft0 > countLeft1){ result += countLeft1; } System.out.println(result); } }
Java
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output
PASSED
f488fd3993ed6c690af7bb1c4905bc7d
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
256 megabytes
import java.util.*; public class insight { public static void main(String... ar) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] left = new int[n]; int[] right = new int[n]; int leftZero = 0; int rightZero = 0; for (int i = 0; i < n; i++) { left[i] = sc.nextInt(); right[i] = sc.nextInt(); if (left[i] == 0) { leftZero++; } if (right[i] == 0) { rightZero++; } } int ans = 0; if (leftZero > n / 2) { ans += n - leftZero; } else { ans += leftZero; } // System.out.println(ans); if (rightZero >= n / 2) { ans += n - rightZero; } else { ans += rightZero; } System.out.println(ans); } }
Java
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output
PASSED
a82d0779ab1419bc44dd69c73e86b78f
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
256 megabytes
import java.util.Scanner; public class Main2 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int[] left = new int[n]; int[] right = new int[n]; int onesLeft = 0; int zeroesLeft = 0; int onesRight = 0; int zeroesRight = 0; for(int i=0;i<n;i++) { left[i] = scan.nextInt(); right[i] = scan.nextInt(); if(left[i]==0) { zeroesLeft++; }else { onesLeft++; } if(right[i]==0) { zeroesRight++; }else { onesRight++; } } scan.close(); int sum=0; if(zeroesLeft==0 || onesLeft==0) { sum+=0; }else if(zeroesLeft>onesLeft) { sum+=onesLeft; }else { sum+=zeroesLeft; } if(zeroesRight==0 || onesRight==0) { sum+=0; }else if(zeroesRight>onesRight) { sum+=onesRight; }else { sum+=zeroesRight; } System.out.print(sum); } }
Java
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output
PASSED
1aae9837b113d798fb899492860d9330
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner (System.in); int z=in.nextInt(); int x,y,sum=0,sum2=0; for (int i=0;i<z;i++) { x=in.nextInt(); y=in.nextInt(); if (x==1) { sum++; } if(y==1) { sum2++; } } int xx=(Math.min(sum, z-sum)+Math.min(sum2, z-sum2)); System.out.print(xx); } }
Java
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output
PASSED
d2cc5e9715868b88e6f73deabdeb6bab
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
256 megabytes
import java.util.*; import java.lang.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] l = new int[n]; int[] r = new int[n]; for(int i=0;i<n;i++){ l[i] = sc.nextInt(); r[i] = sc.nextInt(); } int l0 = 0,l1=0; int r0 = 0,r1=0; for(int i=0;i<n;i++) { if(l[i]==0) l0++; else l1++; if(r[i]==0) r0++; else r1++; } int c = 0; if(l0<=l1) c+=l0; else if(l0>l1) c+=l1; if(r0<r1) c+=r0; else if(r0>=r1) c+=r1; System.out.println(c); } }
Java
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output
PASSED
2007a648420b803ac64cea79088b6583
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
256 megabytes
import java.util.*; public class Cupboards{ public static void main(String[]args){ Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int [] Left = new int [N]; int [] Right = new int [N]; int L0 = 0; int L1 = 0; int R1 = 0; int R0 = 0; int timeR = 0; int timeL = 0; for(int i =0; i < N;i++){ int l = sc.nextInt(); int r = sc.nextInt(); if(l == 0){ L0++; } if(l == 1){ L1++; } if(r == 1){ R1++; } if(r == 0){ R0++; } } if(L0 > L1){ timeL = N - L0; } else{ if(L0 < L1){ timeL = N - L1; } else{ timeL = N - L1; } } if(R0 > R1){ timeR = N - R0; } else{ if(R0 < R1){ timeR = N - R1; } else{ timeR = N - R1; } } System.out.println(timeL + timeR); } }
Java
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output
PASSED
6f8932ddbf87a0b2f8720d7a3e1b2760
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
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. */ import java.util.Scanner; /** * * @author pc */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { int n, left1 = 0, right1 = 0, total = 0; int[][] doors; Scanner in = new Scanner(System.in); n = in.nextInt(); doors = new int[n][2]; for (int i = 0; i < n; i++) { for (int j = 0; j < 2; j++) { doors[i][j] = in.nextInt(); if (doors[i][j] == 1 && j == 0) { left1 += 1; } else if (doors[i][j] == 1 && j == 1) { right1 += 1; } } } if (left1 < (n - left1)) { total += left1; } else { total += (n - left1); } if (right1 < (n - right1)) { total += right1; } else { total += (n - right1); } System.out.println(total); } }
Java
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output
PASSED
a6c0c46332f1303774b5a0bc776429a2
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class Main{ public static void main(String[] args) throws IOException { BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(bf.readLine()); int R_Z=0; int R_O=0; int L_Z=0; int L_O=0; for(int i=0;i<n;i++){ String in[] = bf.readLine().split(" "); int x=Integer.parseInt(in[0]); if(x==0){L_Z++;}else{L_O++;} x=Integer.parseInt(in[1]); if(x==0){R_Z++;}else{R_O++;} } System.out.println(Math.min(R_O, R_Z)+Math.min(L_O, L_Z)); } }
Java
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output
PASSED
f495972d38455a710f7dbea9c34cc40b
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
256 megabytes
import java.util.Scanner; public class Cupboards { public static void main (String [] args) { Scanner sc = new Scanner (System.in); int number = sc.nextInt(); int l1=0,l0=0,r1=0,r0=0; int result =0 ; for( int i = 0 ; i < number; i++) { int l = sc.nextInt(); if(l == 0 ) { l0++; }else { l1++; } int r = sc.nextInt(); if(r==0) { r0++; }else { r1++; } } if(l1>l0) { result = result+l0; }else { result = result+l1; } if(r1>r0) { result = result+r0; } else { result = result+r1; } System.out.println(result); } }
Java
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output
PASSED
8ab28367521e62ca1248147ca33cd549
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args) { Scanner scan=new Scanner(System.in); int n=scan.nextInt(); int l[]=new int[n]; int r[]=new int[n]; for(int i=0;i<n;++i){ l[i]=scan.nextInt(); r[i]=scan.nextInt(); } int left=0; int right=0; int ans=0; for(int i=0;i<n;++i){ if(l[i]==1){ ++left; } if(r[i]==1){ ++right; } } ans=Math.min(n-left,left); ans+=Math.min(n-right,right); System.out.println(ans); } }
Java
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output
PASSED
1dc7f2c4a4f8de65851276544d0a99dc
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(), left = 0, right = 0; for(int i = 0; i<n; i++) { if(input.nextInt() == 1) left ++; if(input.nextInt() == 1) right ++; } System.out.println(Math.min(left, n - left) + Math.min(right, n - right)); } }
Java
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output
PASSED
41a84fb5d9e57b11928dc6a3bdc5b015
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
256 megabytes
import java.util.Scanner; public class practice { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int lc = 0, rc = 0; for (int i = 0; i < n; i++) { int a = input.nextInt(); int b = input.nextInt(); lc += a; rc += b; } System.out.println(Math.min(rc, n - rc) + Math.min(n - lc, lc)); } }
Java
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output
PASSED
d8d58989889a6ab025a40297661c1fb4
train_001.jsonl
1353857400
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
256 megabytes
import java.util.Scanner; public class practice { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int lc = 0, lo = 0, rc = 0, ro = 0; for (int i = 0; i < n; i++) { int a = input.nextInt(); int b = input.nextInt(); if (a == 0) lc++; else lo++; if (b == 0) rc++; else ro++; } System.out.println(Math.min(rc, ro) + Math.min(lo, lc)); } }
Java
["5\n0 1\n1 0\n0 1\n1 1\n0 1"]
2 seconds
["3"]
null
Java 8
standard input
[ "implementation" ]
2052b0b4abdcf7d613b56842b1267f60
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.
800
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
standard output