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
b07af9abf4186627d8c4c9435190f224
train_002.jsonl
1518861900
Dima has a hamsters farm. Soon N hamsters will grow up on it and Dima will sell them in a city nearby.Hamsters should be transported in boxes. If some box is not completely full, the hamsters in it are bored, that's why each box should be completely full with hamsters.Dima can buy boxes at a factory. The factory produces boxes of K kinds, boxes of the i-th kind can contain in themselves ai hamsters. Dima can buy any amount of boxes, but he should buy boxes of only one kind to get a wholesale discount.Of course, Dima would buy boxes in such a way that each box can be completely filled with hamsters and transported to the city. If there is no place for some hamsters, Dima will leave them on the farm.Find out how many boxes and of which type should Dima buy to transport maximum number of hamsters.
256 megabytes
import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class ar { 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 (final 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 (final IOException e) { e.printStackTrace(); } return str; } } static boolean[] seen; static Map<Integer, Integer> map; static Set<Character> charset; static Set<Integer> set; static int[] arr; static int[][] dp; static int rem = (int) 1e9 + 7; static List<List<Integer>> graph; static int[][] kmv ={{2,1},{1,2},{2,-1},{-1,2},{-2,1},{1,-2},{-1,-2},{-2,-1}}; static char[]car; static boolean[] primes; static int MAX=(int)1e4+1; static double[] dar; static void Sieve(){ primes=new boolean[MAX]; primes[0]=true; primes[1]=true; for(int i=2;i*i<MAX;i++){ if(!primes[i]){ for(int j=i*i;j<MAX;j+=i){ primes[j]=true; } } } } public static void main( String[] args) throws java.lang.Exception { FastReader scn = new FastReader(); long n=scn.nextLong(); long k=scn.nextLong(); long num; long ans=-1; long nob=0,boxn=0; for(long i=0;i<k;i++){ num=scn.nextLong(); if((n/num)*num>ans){ nob=i+1; ans=(n/num)*num; boxn=num; } } System.out.print(nob+" "+ans/boxn); return; } }
Java
["19 3\n5 4 10", "28 3\n5 6 30"]
2 seconds
["2 4", "1 5"]
null
Java 11
standard input
[ "implementation" ]
8e36566b5e0c74730ea118e23b030778
The first line contains two integers N and K (0 ≤ N ≤ 1018, 1 ≤ K ≤ 105) — the number of hamsters that will grow up on Dima's farm and the number of types of boxes that the factory produces. The second line contains K integers a1, a2, ..., aK (1 ≤ ai ≤ 1018 for all i) — the capacities of boxes.
1,000
Output two integers: the type of boxes that Dima should buy and the number of boxes of that type Dima should buy. Types of boxes are numbered from 1 to K in the order they are given in input. If there are many correct answers, output any of them.
standard output
PASSED
f902f94540f43270c51f79ef72d3ce65
train_002.jsonl
1518861900
Dima has a hamsters farm. Soon N hamsters will grow up on it and Dima will sell them in a city nearby.Hamsters should be transported in boxes. If some box is not completely full, the hamsters in it are bored, that's why each box should be completely full with hamsters.Dima can buy boxes at a factory. The factory produces boxes of K kinds, boxes of the i-th kind can contain in themselves ai hamsters. Dima can buy any amount of boxes, but he should buy boxes of only one kind to get a wholesale discount.Of course, Dima would buy boxes in such a way that each box can be completely filled with hamsters and transported to the city. If there is no place for some hamsters, Dima will leave them on the farm.Find out how many boxes and of which type should Dima buy to transport maximum number of hamsters.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; import java.util.stream.IntStream; public class CF939B { public static void main(String[] args) throws Exception { InputReader in = new InputReader(System.in); long N = in.nextLong(); int k = in.nextInt(); long[] a = new long[k]; for (int i = 0; i < a.length; i++) { a[i] = in.nextLong(); } System.out.println(solve(N, a)); } static String solve(long N, long[] a) { return IntStream.range(0, a.length).boxed().min((index1, index2) -> Long.compare(N % a[index1], N % a[index2])) .map(i -> String.format("%d %d", i + 1, N / a[i])).get(); } static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); 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 long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["19 3\n5 4 10", "28 3\n5 6 30"]
2 seconds
["2 4", "1 5"]
null
Java 11
standard input
[ "implementation" ]
8e36566b5e0c74730ea118e23b030778
The first line contains two integers N and K (0 ≤ N ≤ 1018, 1 ≤ K ≤ 105) — the number of hamsters that will grow up on Dima's farm and the number of types of boxes that the factory produces. The second line contains K integers a1, a2, ..., aK (1 ≤ ai ≤ 1018 for all i) — the capacities of boxes.
1,000
Output two integers: the type of boxes that Dima should buy and the number of boxes of that type Dima should buy. Types of boxes are numbered from 1 to K in the order they are given in input. If there are many correct answers, output any of them.
standard output
PASSED
00ddab95ee795f45f1f4235fa38ac73f
train_002.jsonl
1518861900
Dima has a hamsters farm. Soon N hamsters will grow up on it and Dima will sell them in a city nearby.Hamsters should be transported in boxes. If some box is not completely full, the hamsters in it are bored, that's why each box should be completely full with hamsters.Dima can buy boxes at a factory. The factory produces boxes of K kinds, boxes of the i-th kind can contain in themselves ai hamsters. Dima can buy any amount of boxes, but he should buy boxes of only one kind to get a wholesale discount.Of course, Dima would buy boxes in such a way that each box can be completely filled with hamsters and transported to the city. If there is no place for some hamsters, Dima will leave them on the farm.Find out how many boxes and of which type should Dima buy to transport maximum number of hamsters.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); long n = scanner.nextLong(); long k = scanner.nextInt(); long min = Long.MAX_VALUE; long count =0; long amount =0; if(n==0){ System.out.println(1); System.out.println(0); }else { for (long i = 1; i <= k; i++) { long a = scanner.nextLong(); long mod = n%a; if (mod < min && a <= n) { amount = n / a; min = mod; count = i; } } if(count==0&&amount==0){ System.out.println(1); System.out.println(0); }else { System.out.println(count); System.out.println(amount); } } } }
Java
["19 3\n5 4 10", "28 3\n5 6 30"]
2 seconds
["2 4", "1 5"]
null
Java 11
standard input
[ "implementation" ]
8e36566b5e0c74730ea118e23b030778
The first line contains two integers N and K (0 ≤ N ≤ 1018, 1 ≤ K ≤ 105) — the number of hamsters that will grow up on Dima's farm and the number of types of boxes that the factory produces. The second line contains K integers a1, a2, ..., aK (1 ≤ ai ≤ 1018 for all i) — the capacities of boxes.
1,000
Output two integers: the type of boxes that Dima should buy and the number of boxes of that type Dima should buy. Types of boxes are numbered from 1 to K in the order they are given in input. If there are many correct answers, output any of them.
standard output
PASSED
14138b51bdbe3b986db88a2ae9a800e3
train_002.jsonl
1518861900
Dima has a hamsters farm. Soon N hamsters will grow up on it and Dima will sell them in a city nearby.Hamsters should be transported in boxes. If some box is not completely full, the hamsters in it are bored, that's why each box should be completely full with hamsters.Dima can buy boxes at a factory. The factory produces boxes of K kinds, boxes of the i-th kind can contain in themselves ai hamsters. Dima can buy any amount of boxes, but he should buy boxes of only one kind to get a wholesale discount.Of course, Dima would buy boxes in such a way that each box can be completely filled with hamsters and transported to the city. If there is no place for some hamsters, Dima will leave them on the farm.Find out how many boxes and of which type should Dima buy to transport maximum number of hamsters.
256 megabytes
import java.util.*; import java.io.*; public class Solution { public static void main(String[] args){ Scanner scan = new Scanner(System.in); long n = scan.nextLong(); int k = scan.nextInt(); long min = n + 1, ans = -1, box = 0; for (int i = 0; i < k; i++) { long x = scan.nextLong(); if (n % x < min) { min = n % x; ans = i + 1; box = n / x; } } System.out.println(ans + " " + box); scan.close(); } }
Java
["19 3\n5 4 10", "28 3\n5 6 30"]
2 seconds
["2 4", "1 5"]
null
Java 11
standard input
[ "implementation" ]
8e36566b5e0c74730ea118e23b030778
The first line contains two integers N and K (0 ≤ N ≤ 1018, 1 ≤ K ≤ 105) — the number of hamsters that will grow up on Dima's farm and the number of types of boxes that the factory produces. The second line contains K integers a1, a2, ..., aK (1 ≤ ai ≤ 1018 for all i) — the capacities of boxes.
1,000
Output two integers: the type of boxes that Dima should buy and the number of boxes of that type Dima should buy. Types of boxes are numbered from 1 to K in the order they are given in input. If there are many correct answers, output any of them.
standard output
PASSED
05fd4ed59a53f94a7c1e246c621da551
train_002.jsonl
1518861900
Dima has a hamsters farm. Soon N hamsters will grow up on it and Dima will sell them in a city nearby.Hamsters should be transported in boxes. If some box is not completely full, the hamsters in it are bored, that's why each box should be completely full with hamsters.Dima can buy boxes at a factory. The factory produces boxes of K kinds, boxes of the i-th kind can contain in themselves ai hamsters. Dima can buy any amount of boxes, but he should buy boxes of only one kind to get a wholesale discount.Of course, Dima would buy boxes in such a way that each box can be completely filled with hamsters and transported to the city. If there is no place for some hamsters, Dima will leave them on the farm.Find out how many boxes and of which type should Dima buy to transport maximum number of hamsters.
256 megabytes
///package codeforces; import java.util.*; public class HamsterFarm { public static void main(String[] args) { Scanner in = new Scanner (System.in); long N,K,X,P=0,n=0; long Rem; Rem=1000000000000000005L; N=in.nextLong(); K=in.nextLong(); long [] A = new long[100005]; for(int i=0;i<K;i++){ A[i]=in.nextLong(); } for(int i=0;i<K;i++){ X=N%A[i]; if(X==0) { P=i+1; n=N/A[i]; break; } else if(X<Rem) { P=i+1; n=N/A[i]; Rem=X; } } System.out.println(P+" "+n); in.close(); } }
Java
["19 3\n5 4 10", "28 3\n5 6 30"]
2 seconds
["2 4", "1 5"]
null
Java 11
standard input
[ "implementation" ]
8e36566b5e0c74730ea118e23b030778
The first line contains two integers N and K (0 ≤ N ≤ 1018, 1 ≤ K ≤ 105) — the number of hamsters that will grow up on Dima's farm and the number of types of boxes that the factory produces. The second line contains K integers a1, a2, ..., aK (1 ≤ ai ≤ 1018 for all i) — the capacities of boxes.
1,000
Output two integers: the type of boxes that Dima should buy and the number of boxes of that type Dima should buy. Types of boxes are numbered from 1 to K in the order they are given in input. If there are many correct answers, output any of them.
standard output
PASSED
12f288b45fc3be77ca9ad27d8cdfd3c7
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import javafx.util.*; import java.util.*; import java.io.*; import java.math.*; public class Test5 { Random rnd = new Random(); PrintWriter pw = new PrintWriter(System.out); static int[] n; int a, mod=998244353; void run(){ a = ni(); int[] l = na(a), r = na(a); boolean fl = true; int[] ans = new int[a]; int max = 0; for (int q = 0; q < a; q++) { max = Math.max(max, l[q]+r[q]); } max++; for (int q = 0; q < a; q++){ ans[q] = (max - l[q] - r[q]); if(ans[q]>a) fl = false; } for (int q = 0; q < a; q++) { int cl = 0, cr = 0; for (int w = 0; w < q; w++) if (ans[w] > ans[q]) cl++; for (int w = q + 1; w < a; w++) if (ans[w] > ans[q]) cr++; if (cl != l[q] || cr != r[q]) fl = false; } if (fl) { pw.println("YES"); for (int i : ans) pw.print(i + " "); } else pw.println("NO"); pw.flush(); pw.flush(); } static class PyraSort { private static int heapSize; public static void sort(int[] a) { buildHeap(a); while (heapSize > 1) { swap(a, 0, heapSize - 1); heapSize--; heapify(a, 0); } } private static void buildHeap(int[] a) { heapSize = a.length; for (int i = a.length / 2; i >= 0; i--) { heapify(a, i); } } private static void heapify(int[] a, int i) { int l = 2 * i + 2; int r = 2 * i + 1; int largest = i; if (l < heapSize && a[i] < a[l]) { largest = l; } if (r < heapSize && a[largest] < a[r]) { largest = r; } if (i != largest) { swap(a, i, largest); heapify(a, largest); } } private static void swap(int[] a, int i, int j) { a[i] ^= a[j] ^= a[i]; a[j] ^= a[i]; } } public static void main(String[] args) { new Test5().run(); } InputStream is = System.in; private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private String nline(){ int b = readByte(); StringBuilder sb = new StringBuilder(); while (b!=10) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
b7742f4ff3aa0601338a96c9f03d8005
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; import java.util.stream.*; import static java.lang.Math.abs; import static java.lang.Math.min; import static java.lang.Math.max; import static java.lang.Math.sqrt; @SuppressWarnings("unchecked") public class P1054C { public void run() throws Exception { int n = nextInt(), l [] = readInt(n), r [] = readInt(n), a [] = new int [n]; for (int i = 0; i < n; a[i] = n - l[i] - r[i], i++); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; r[i] -= (a[j] > a[i]) ? 1 : 0, j++); for (int j = i - 1; j >= 0; l[i] -= (a[j] > a[i]) ? 1 : 0, j--); if ((l[i] != 0) || (r[i] != 0)) { println("NO"); return; } } println("YES"); IntStream.of(a).forEach(this::println); } public static void main(String... args) throws Exception { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedOutputStream(System.out)); new P1054C().run(); br.close(); pw.close(); System.err.println("\n[Time : " + (System.currentTimeMillis() - startTime) + " ms]"); } static long startTime = System.currentTimeMillis(); 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(Object o) { if (o instanceof int[]) { print(Arrays.toString((int [])o)); } else if (o instanceof long[]) { print(Arrays.toString((long [])o)); } else if (o instanceof char[]) { print(Arrays.toString((char [])o)); } else if (o instanceof byte[]) { print(Arrays.toString((byte [])o)); } else if (o instanceof short[]) { print(Arrays.toString((short [])o)); } else if (o instanceof boolean[]) { print(Arrays.toString((boolean [])o)); } else if (o instanceof float[]) { print(Arrays.toString((float [])o)); } else if (o instanceof double[]) { print(Arrays.toString((double [])o)); } else if (o instanceof Object[]) { print(Arrays.toString((Object [])o)); } else { print("" + o); } } void print(String s) { pw.print(s); } void println() { println(""); } 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(Object o) { print(o); println(); } 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 nextToken(); } String nextLine() throws IOException { return br.readLine(); } int [] readInt(int size) throws IOException { int [] array = new int [size]; for (int i = 0; i < size; i++) { array[i] = nextInt(); } return array; } long [] readLong(int size) throws IOException { long [] array = new long [size]; for (int i = 0; i < size; i++) { array[i] = nextLong(); } return array; } double [] readDouble(int size) throws IOException { double [] array = new double [size]; for (int i = 0; i < size; i++) { array[i] = nextDouble(); } return array; } String [] readLines(int size) throws IOException { String [] array = new String [size]; for (int i = 0; i < size; i++) { array[i] = nextLine(); } return array; } int gcd(int a, int b) { if (a == 0) return Math.abs(b); if (b == 0) return Math.abs(a); a = Math.abs(a); b = Math.abs(b); int az = Integer.numberOfTrailingZeros(a), bz = Integer.numberOfTrailingZeros(b); a >>>= az; b >>>= bz; while (a != b) { if (a > b) { a -= b; a >>>= Integer.numberOfTrailingZeros(a); } else { b -= a; b >>>= Integer.numberOfTrailingZeros(b); } } return (a << Math.min(az, bz)); } long gcd(long a, long b) { if (a == 0) return Math.abs(b); if (b == 0) return Math.abs(a); a = Math.abs(a); b = Math.abs(b); int az = Long.numberOfTrailingZeros(a), bz = Long.numberOfTrailingZeros(b); a >>>= az; b >>>= bz; while (a != b) { if (a > b) { a -= b; a >>>= Long.numberOfTrailingZeros(a); } else { b -= a; b >>>= Long.numberOfTrailingZeros(b); } } return (a << Math.min(az, bz)); } void shuffle(int [] a) { Random r = new Random(); for (int i = a.length - 1; i >= 0; i--) { int j = r.nextInt(a.length); int t = a[i]; a[i] = a[j]; a[j] = t; } } void shuffle(long [] a) { Random r = new Random(); for (int i = a.length - 1; i >= 0; i--) { int j = r.nextInt(a.length); long t = a[i]; a[i] = a[j]; a[j] = t; } } void shuffle(Object [] a) { Random r = new Random(); for (int i = a.length - 1; i >= 0; i--) { int j = r.nextInt(a.length); Object t = a[i]; a[i] = a[j]; a[j] = t; } } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
3392377d1c6d4856e5920546b909f20f
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.util.*; import java.io.*; public class Solution { public static PrintWriter out; public static void main(String args[]) { MyScanner sc=new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out), true); int n=sc.nextInt(); int l[]=new int[n]; int r[]=new int[n]; int rank[]=new int [n]; for(int i=0;i<n;i++) l[i]=sc.nextInt(); for(int i=0;i<n;i++) r[i]=sc.nextInt(); boolean pos=true; int maxr=1; for(int i=0;i<n;i++) { if(l[i]<=i&&r[i]<=(n-1-i)) rank[i]=l[i]+r[i]+1; else { pos=false; break; } if(rank[i]>maxr) maxr=rank[i]; } for(int i=0;i<n;i++) { int lr=0; for(int j=0;j<i;j++) { if(rank[j]<rank[i]) lr++; } int rr=0; for(int j=i+1;j<n;j++) { if(rank[j]<rank[i]) rr++; } if(lr!=l[i]||rr!=r[i]) { pos=false; break; } } if(!pos||maxr>n) System.out.println("NO"); else { int npr=0; int pr=0; int rb[]=new int [n]; for(int i=0;i<n;i++) rb[i]=rank[i]; Arrays.sort(rb); for(int i=0;i<n;i++) { if(i==0&&rb[i]!=1) { pos=false; break; } else if(i==0) { npr=2; pr=1; } else if(rb[i]==pr) npr++; else if(rb[i]==npr) { pr=rb[i]; npr++; } else { pos=false; break; } } if(!pos) System.out.println("NO"); else { maxr++; System.out.println("YES"); for(int i=0;i<n;i++) { System.out.print((maxr-rank[i])+" "); } } } } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
d1e15a2c6eff0c74d0d516ccd9dcb686
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class Main { public static void main (String[] args) { BufferedReader bi = new BufferedReader(new InputStreamReader(System.in)); String str=""; try { //str = bi.readLine(); //int t = Integer.parseInt(str); //for (int j=1;j<=t;j++) { str = bi.readLine(); int n = Integer.parseInt(str); str = bi.readLine(); String [] len = str.split(" "); int[] l = new int [len.length]; for (int i=0;i<n;i++) { l[i] = Integer.parseInt(len[i]); } str = bi.readLine(); len = str.split(" "); int[] r = new int [len.length]; for (int i=0;i<n;i++) { r[i] = Integer.parseInt(len[i]); } int []arr = new int[n]; if (l[0]>0 || r[n-1]>0) { System.out.println("NO"); return; } for (int i=0;i<n;i++) { if (l[i]==0 && r[i]==0) { arr[i] = n; } else { arr[i] = -1; } } int done = n-1; while (done>0 && numberOfNumbersGreaterThanThisToLeft(n, arr)<n) { boolean found = false; List<Integer> list = new ArrayList<>(); for (int i=0;i<n;i++) { if (arr[i]==-1 && l[i]==numberOfNumbersGreaterThanThisToLeft(i, arr) && r[i]==numberOfNumbersGreaterThanThisToRigth(i, arr)) { list.add(i); found = true; } } if (!found) { System.out.println("NO"); return; } else { for (int j:list) { arr[j] = done; } } done--; } System.out.println("YES"); StringBuffer buf = new StringBuffer(); for (int i=0;i<n;i++) { buf.append(arr[i]).append(" "); } System.out.println(buf.toString()); } } catch (Exception e) { } } public static int numberOfNumbersGreaterThanThisToRigth (int index , int arr[]) { int count=0; for (int i=index+1;i<arr.length;i++) { if (arr[i]>0) { count++; } } return count; } public static int numberOfNumbersGreaterThanThisToLeft (int index , int arr[]) { int count=0; for (int i=0;i<index;i++) { if (arr[i]>0) { count++; } } return count; } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
c3a180ce55d8009f7aa443ac159657d1
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
//Code by Sounak, IIEST import java.io.*; import java.math.*; import java.util.*; import java.util.Arrays; public class Test1{ public static void main(String args[])throws IOException{ FastReader sc = new FastReader(); StringBuilder sb = new StringBuilder(); long mod=1000000007l; int n=sc.nextInt(); int l[]=new int[n]; int r[]=new int[n]; int a[]=new int[n]; Arrays.fill(a,1); int i,j,count=0; boolean ch=true; for(i=0;i<n;i++) { l[i]=sc.nextInt(); if(l[i]>i) ch=false; } for(i=0;i<n;i++) { r[i]=sc.nextInt(); if(r[i]>n-i-1) ch=false; } if(!ch) { System.out.println("NO"); return; } for(i=n-1;i>=0;i--) { a[i]=n-l[i]-r[i]; //System.out.println(count+" "+a[i]); } for(i=0;i<n;i++) { count=0; for(j=0;j<i;j++) { if(a[j]>a[i]) count++; } if(count!=l[i]) ch=false; count=0; for(j=i+1;j<n;j++) { if(a[j]>a[i]) count++; } if(count!=r[i]) ch=false; } if(!ch) { System.out.println("NO"); return; } System.out.println("YES"); for(i=0;i<n;i++) System.out.println(a[i]); } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } } 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\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
658319174e272800e477bb0cd80470a7
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import javax.sound.midi.SysexMessage; import java.util.*; public class C { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); int[] L = new int[N]; int[] R = new int[N]; for(int i = 0;i < N; i++) { L[i] = scanner.nextInt(); if (L[i] > i) { System.out.println("NO"); System.exit(0); } } for(int i = 0; i < N; i++) { R[i] = scanner.nextInt(); if (R[i] > N-i-1) { System.out.println("NO"); System.exit(0); } } int[] cand = new int[N]; for(int i = 0; i < N; i++) { cand[i] = N-R[i]-L[i]; int ind = i-1; while (ind >= 0) { if (cand[i] > cand[ind]) { R[ind]--; if (R[ind] < 0) { System.out.println("NO"); System.exit(0); } } if (cand[i] < cand[ind]) { L[i]--; if (L[i] < 0) { System.out.println("NO"); System.exit(0); } } ind--; } } for(int i = 0; i < N; i++) { if (L[i] != 0 || R[i] != 0) { System.out.println("NO"); System.exit(0); } } //output StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("YES\n"); for(int i = 0; i < N; i++) { stringBuilder.append((cand[i])); stringBuilder.append(" "); } System.out.println(stringBuilder.toString()); } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
e37da835133fd484bdc838f8f03dad3c
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class A { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] l = new int[n]; int[] r = new int[n]; for(int i=0; i<n; i++) { l[i] = in.nextInt(); } for(int i=0; i<n; i++) { r[i] = in.nextInt(); } int[] pot = new int[n]; for(int i=0; i<n; i++) { pot[i] = n - (l[i] + r[i]); } // if pot satisfies left and right for(int i=0; i<n; i++) { if(i == 0) { if(l[i] != 0) { System.out.println("NO"); return; } else { continue; } } int cnt = 0; for(int j=0; j<i; j++) { if(pot[j] > pot[i]) { cnt += 1; } } if(cnt != l[i]) { System.out.println("NO"); return; } } for(int i=n-1; i>=0; i--) { if(i == n-1) { if(r[i] != 0) { System.out.println("NO"); return; } else { continue; } } int cnt = 0; for(int j=i+1; j<n; j++) { if(pot[j] > pot[i]) { cnt += 1; } } if(cnt != r[i]) { System.out.println("NO"); return; } } System.out.println("YES"); for(int i : pot) { System.out.print(i + " "); } } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
445ad52e7b8f7f3afe293a02cc8ac5aa
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.util.Scanner; import java.util.Stack; public class CandiesDistribution { static class lr{ int l,r, index; } public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); lr[] kids = new lr[n]; int[] lefttoright = new int[n]; for (int k = 0; k < n; k++){ kids[k] = new lr(); kids[k].l = sc.nextInt(); kids[k].index = k; } for (int k = 0; k < n; k++){ kids[k].r = sc.nextInt(); } int cur = n; boolean nosol = false; for (int i = 0; i < n; i++){ Stack<Integer> multindex = new Stack<Integer>(); boolean found = false; for (int p = 0; p < n; p++){ if (kids[p].r == 0 && kids[p].l == 0){ found = true; multindex.add(p); i++; } } i--; if(!found){ nosol = true; } if (cur < 1){ nosol = true; break; } while (!multindex.isEmpty()) { int index = multindex.pop(); lefttoright[index] = cur; for (int j = 0; j < n; j++) { if (kids[j].index <= index) { kids[j].r--; } else { kids[j].l--; } } } cur--; } if (!nosol) { System.out.println("YES"); for (int p = 0; p < n; p++) { System.out.print(lefttoright[p] + " "); } }else{ System.out.println("NO"); } } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
a7c0706287315eae61e3aba6c34c33e1
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.io.*; import java.util.Arrays; public class CandiesDistribution { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(reader.readLine()); int[] l = new int[n]; int[] r = new int[n]; String[] lStrs = reader.readLine().split(" "); String[] rStrs = reader.readLine().split(" "); for (int i = 0; i < n; i++) { l[i] = Integer.parseInt(lStrs[i]); r[i] = Integer.parseInt(rStrs[i]); } int[] res = new int[n]; Arrays.fill(res, -1); int[] leftCount = new int[n]; int[] rightCount = new int[n]; int num = n; for (int sum = 0; sum < n; sum++) { for (int i = 0; i < n; i++){ if (l[i] + r[i] == sum && leftCount[i] == l[i] && rightCount[i] == r[i]) { res[i] = num; } } num--; leftCount[0] = res[0] == -1 ? 0 : 1; for (int i = 1; i < n; i++) { leftCount[i] = leftCount[i - 1] + (res[i] == -1 ? 0 : 1); } rightCount[n - 1] = res[n - 1] == -1 ? 0 : 1; for (int i = n - 2; i >= 0; i--) { rightCount[i] = rightCount[i + 1] + (res[i] == -1 ? 0 : 1); } } for (int i = 0; i < n; i++) { if (res[i] == -1) { System.out.println("NO"); return; } } System.out.print("YES\n" + res[0]); for (int i = 1; i < n; i++) { System.out.print(" " + res[i]); } System.out.println(); } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
a123da5abf236ffb18a65d79ccf738bb
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.io.*; import java.util.*; public class Main implements Runnable { int maxn = (int)1e3+11; long inf = (long)1e18; long mod = (long)1e9+7; int n,m,k; int left[] = new int[maxn]; int right[] = new int[maxn]; int leftCopy[] = new int[maxn]; int rightCopy[] = new int[maxn]; boolean used[] = new boolean[maxn]; private boolean no; void solve() throws Exception { n = in.nextInt(); for (int i = 1; i <= n; i++) { left[i] = in.nextInt(); leftCopy[i] = left[i]; } for (int i = 1; i <= n; i++) { right[i] = in.nextInt(); rightCopy[i] = right[i]; } int ans[] = new int[n + 1]; int next = n; while (true) { ArrayList<Integer> list = getZeroZero(); if (isAllRemainZero()) { for (int i = 1; i <= n; i++) { if (ans[i] == 0) { ans[i] = 1; } } break; } else if (list.isEmpty()) { out.println("NO"); return; } else { for (int val : list) { ans[val] = next; used[val] = true; } next--; for (int pos : list) { for (int i = pos + 1; i <= n; i++) { left[i] = Math.max(left[i] - 1, 0); } for (int i = 1; i < pos; i++) { right[i] = Math.max(right[i] - 1, 0); } } } } if (isNo(ans)) { out.println("NO"); return; } out.println("YES"); for (int i = 1; i <= n; i++) { out.print(ans[i] + " "); } } public boolean isNo(int a[]) { for (int i=1; i<=n; i++) { int cnt = 0; for (int j=i+1; j<=n; j++) { if (a[j]>a[i]) { cnt++; } } if (rightCopy[i]!=cnt) { return true; } cnt = 0; for (int j=i-1; j>=1; j--) { if (a[j]>a[i]) { cnt++; } } if (leftCopy[i]!=cnt) { return true; } } return false; } public ArrayList<Integer> getZeroZero() { ArrayList<Integer> list = new ArrayList<>(); for (int i=1; i<=n; i++) { if (!used[i]) { if (left[i]==0 && right[i]==0) { list.add(i); } } } return list; } public boolean isAllRemainZero() { for (int i=1; i<=n; i++) { if (!used[i] && (left[i]!=0 || right[i]!=0)) { return false; } } return true; } String fileInName = ""; static Throwable throwable; public static void main (String [] args) throws Throwable { Thread thread = new Thread(null, new Main(), "", (1 << 26)); thread.start(); thread.join(); thread.run(); if (throwable != null) throw throwable; } FastReader in; PrintWriter out; public void run() { try { if (!fileInName.isEmpty()) { in = new FastReader(new BufferedReader(new FileReader(fileInName+".in"))); out = new PrintWriter(new BufferedWriter(new FileWriter(fileInName+".out"))); } else { in = new FastReader(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(System.out); } solve(); } catch(Exception e) { throwable = e; } finally { out.close(); } } class FastReader { BufferedReader bf; StringTokenizer tk = null; public FastReader(BufferedReader bf) { this.bf = bf; } public Integer nextInt() throws Exception { return Integer.parseInt(nextToken()); } public Long nextLong() throws Exception { return Long.parseLong(nextToken()); } public Double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } public String nextToken () throws Exception { if (tk==null || !tk.hasMoreTokens()) { tk = new StringTokenizer(bf.readLine()); } if (!tk.hasMoreTokens()) return nextToken(); else return tk.nextToken(); } } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
ed3bd11c25d4ec8a98073467b0b5bbb1
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class A implements Runnable { public void run() { long startTime = System.nanoTime(); int n = nextInt(); int[] a = new int[n]; int[] b = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } for (int i = 0; i < n; i++) { b[i] = nextInt(); } boolean ok = true; int[] p = new int[n]; for (int i = 0; i < n; i++) { p[i] = a[i] + b[i]; ok &= p[i] < n; } int[] ret = new int[n]; if (ok) { for (int i = 0; i < n; i++) { ret[i] = n - p[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (ret[i] < ret[j]) { if (j < i) { a[i]--; } else { b[i]--; } } } if (a[i] != 0 || b[i] != 0) { ok = false; } } } if (ok) { println("YES"); StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(' ').append(ret[i]); } println(sb.substring(1)); } else { println("NO"); } if (fileIOMode) { System.out.println((System.nanoTime() - startTime) / 1e9); } out.close(); } //----------------------------------------------------------------------------------- private static boolean fileIOMode; private static BufferedReader in; private static PrintWriter out; private static StringTokenizer tokenizer; public static void main(String[] args) throws Exception { fileIOMode = args.length > 0 && args[0].equals("!"); if (fileIOMode) { in = new BufferedReader(new FileReader("a.in")); out = new PrintWriter("a.out"); } else { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } tokenizer = new StringTokenizer(""); new Thread(new A()).start(); } private static String nextLine() { try { return in.readLine(); } catch (IOException e) { return null; } } private static String nextToken() { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(nextLine()); } return tokenizer.nextToken(); } private static int nextInt() { return Integer.parseInt(nextToken()); } private static long nextLong() { return Long.parseLong(nextToken()); } private static double nextDouble() { return Double.parseDouble(nextToken()); } private static BigInteger nextBigInteger() { return new BigInteger(nextToken()); } private static void print(Object o) { if (fileIOMode) { System.out.print(o); } out.print(o); } private static void println(Object o) { if (fileIOMode) { System.out.println(o); } out.println(o); } private static void printf(String s, Object... o) { if (fileIOMode) { System.out.printf(s, o); } out.printf(s, o); } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
0a2983c74d0e405cd467c1a92546135a
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] l = new int[n]; int[] r = new int[n]; for (int i = 0; i < n; i++) { l[i] = scanner.nextInt(); } for (int i = 0; i < n; i++) { r[i] = scanner.nextInt(); } int[] ans = new int[n]; for (int i = 0; i < n; i++) { ans[i] = n - l[i] - r[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j < i && ans[j] > ans[i]) { l[i]--; } if (j > i && ans[j] > ans[i]) { r[i]--; } } if (l[i] != 0 || r[i] != 0) { System.out.println("NO"); return; } } System.out.println("YES"); for (int i = 0; i < n; i++) { System.out.print(ans[i] + " "); } } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
2bc7f07ec8afd276cc1a2f6bb6a56f56
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import javafx.util.Pair; import java.lang.reflect.Array; import java.util.*; public class Triangl { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] l = new int[n]; int[] r = new int[n]; int[] ch = new int[n]; for (int i = 0; i < n; i++) l[i] = sc.nextInt(); for (int i = 0; i < n; i++) r[i] = sc.nextInt(); for (int i = 0; i < n; i++) { ch[i] = (n-l[i]-r[i]); } for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if (ch[j] > ch[i]) l[i] -= 1; } } for (int i = 0; i < n; i++) { for (int j = i+1; j < n; j++) { if (ch[j] > ch[i]) r[i] -= 1; } } for (int i = 0; i < n; i++) { if (l[i] != 0 || r[i] != 0){ System.out.println("NO"); return; } } System.out.println("YES"); for (int i = 0; i < n; i++) { System.out.print(ch[i] + " "); } } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
0c4a4ca31e13c5c0159c07cd7247fa74
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; public class Main{ public static void main(String[] args) throws Exception{ BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); boolean ans= true; int n = Integer.parseInt(br.readLine()); String[] strs = br.readLine().split(" "); int[] l=new int[n]; for(int i=0;i<n;i++){ l[i]=Integer.parseInt(strs[i]); if(l[i]>i){ ans=false; } } strs = br.readLine().split(" "); int[] r=new int[n]; for(int i=0;i<n;i++){ r[i]=Integer.parseInt(strs[i]); if(r[i]>(n-i-1)){ ans=false; } } int[] ar=new int[n]; if(ans){ //System.out.println("YES"); for(int i=0;i<n;i++){ ar[i] = n-(l[i]+r[i]); } HashMap<Integer, Integer> left = new HashMap<>(); for(int i=0;i<n;i++){ int v = l[i]; int c = 0; for(int j=i-1;j>=0;j--){ if(ar[j]>ar[i]){ c++; } } if(c!=v) ans=false; } HashMap<Integer, Integer> right = new HashMap<>(); for(int i=0;i<n;i++){ int v = r[i]; int c = 0; for(int j=i+1;j<n;j++){ if(ar[j]>ar[i]){ c++; } } if(c!=v) ans=false; } // System.out.println("YES"); } if(ans){ System.out.println("YES"); for(int i=0;i<n;i++){ System.out.print(ar[i]+" "); } } else{ System.out.println("NO"); } } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
2a43935492e2af9b5b6700ac182899bf
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; import java.util.Iterator; import java.util.Stack; public class Main implements Runnable { static final long MOD = 1000000007L; static final long MOD2 = 1000000009L; static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 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 String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Main(),"Main",1<<26).start(); } int MAXN = 1000001; ArrayList<Integer> adj[]; int[] dx = {1,-1,0,0}; int[] dy = {0,0,1,-1}; public void run() { PrintWriter w= new PrintWriter(System.out); InputReader sc = new InputReader(System.in); int n = sc.nextInt(); int[] arr = new int[n]; int[] arr2 = new int[n]; for(int i = 0;i < n;i++) { arr[i] = sc.nextInt(); } for(int i = 0;i < n;i++) { arr2[i] = sc.nextInt(); } adj = new ArrayList[n]; for(int i = 0;i < n;i++) { adj[i] = new ArrayList(); } for(int i = 0;i < n;i++) { int countl = 0; int countr = 0; for(int j = 0;j < n;j++) { if(i == j) { continue; } if(arr[j] + arr2[j] < arr[i] + arr2[i]) { if(j < i) { countl++; }else { countr++; } adj[i].add(j); } } if(countl != arr[i] || countr != arr2[i]) { w.println("NO"); w.close(); System.exit(0); } } w.println("YES"); val = new int[n]; Arrays.fill(val,1); topo(); while(!st.isEmpty()) { visited = new boolean[n]; int x = st.pop(); dfs(x,-1); } for(int i = 0;i < n;i++) { w.print(val[i] + " "); } w.close(); } boolean[] visited; int[] val; Stack<Integer> st = new Stack(); void dfs(int curr,int prev) { if(prev == -1) { }else { val[curr] = val[prev]+1; } Iterator<Integer> it = adj[curr].iterator(); while(it.hasNext()) { int x = it.next(); if(!visited[x]) { visited[x] = true; dfs(x,curr); } } } void topo() { visited = new boolean[adj.length]; for(int i = 0;i < visited.length;i++) { if(!visited[i]) { dfs(i); } } } void dfs(int a) { visited[a] = true; Iterator<Integer> it = adj[a].iterator(); while(it.hasNext()) { int x = it.next(); if(!visited[x]) { dfs(x); } } st.push(a); } static class Pair implements Comparable<Pair>{ int a; int b; Pair(int a, int b){ this.a = a; this.b = b; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub return Long.compare(this.a,o.a); } public boolean equals(Object o) { Pair p = (Pair)o; return this.a == p.a && this.b == p.b; } public int hashCode(){ return Long.hashCode(a)*27 + Long.hashCode(b)* 31; } } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
a82426dbdef3021ca4b476a4dd4145b1
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.util.*; public class codeforces { public static void main(String args[] ) { Scanner in = new Scanner (System.in); int flag=0; int n = in.nextInt(); int[] leftg = new int[n]; int[] rightg = new int[n]; int[] rank = new int[10000]; for(int i=0;i<n;i++) { leftg[i]=in.nextInt(); } for(int i=0;i<n;i++) { rightg[i]=in.nextInt(); rank[i]=n-leftg[i]-rightg[i]; } for(int i=0;i<n;i++) { int left =0; for(int j=0;j<i;j++) { if(rank[j]>rank[i]) { left+=1; } } int right=0; for(int j=i+1;j<n;j++) { if(rank[j]>rank[i]) { right=right+1; } } if(left!=leftg[i]||right!=rightg[i]) { System.out.println("NO"); flag=1; break; } } if(flag==0) { System.out.println("YES"); for(int i=0;i<n;i++) { System.out.print(rank[i]+" "); } } in.close(); } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
c680a60488f194d4bc10d5ef9d2306f6
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
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 TO_NOOB */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] l = new int[n]; for (int i = 0; i < n; i++) { l[i] = in.nextInt(); } int[] r = new int[n]; for (int i = 0; i < n; i++) { r[i] = in.nextInt(); } int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = n - l[i] - r[i]; } for (int i = 0; i < n; i++) { int li = 0; int ri = 0; for (int j = 0; j < i; j++) { if (res[j] > res[i]) li++; } for (int j = i + 1; j < n; j++) { if (res[j] > res[i]) ri++; } if (li != l[i] || ri != r[i]) { out.println("NO"); return; } } out.println("YES"); for (int a : res) out.print(a + " "); } } 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
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
83f2171d5652709368fbb089f5489e4a
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.util.Scanner; /** * Contest: Mail.ru cup, Round 1 * Problem: C * * @author Arturs Licis */ public class ProblemC { public static void main(String[] args) { 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(); } for (int i = 0; i < n; i++) { r[i] = sc.nextInt(); } int[] t = new int[n]; int max = 0; for (int i = 0; i < n; i++) { t[i] = l[i] + r[i]; max = Math.max(t[i], max); } max++; int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = max - t[i]; } for (int i = 0; i < n; i++) { int lCnt = 0; int rCnt = 0; for (int ll = 0; ll < i; ll++) { if (res[ll] > res[i]) lCnt++; } for (int rr = i + 1; rr < n; rr++) { if (res[rr] > res[i]) rCnt++; } if (l[i] != lCnt || r[i] != rCnt) { System.out.println("NO"); return; } } System.out.println("YES"); for (int c : res) System.out.print(c + " "); } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
03282bbd2ef851313c2061aac1059ca8
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class OoziTestOne { static Scanner scanner = new Scanner(System.in); static long MAX = 0; public static void main(String[] args) { int n = scanner.nextInt(); int[] left = new int[n]; int[] right = new int[n]; long[] result = new long[n]; MAX = n; for (int i = 0; i < n; i++) { int l = scanner.nextInt(); if (l != 0 && i == 0) { System.out.println("NO"); return; } left[i] = l; } for (int i = 0; i < n; i++) { int r = scanner.nextInt(); if (r != 0 && i == n - 1) { System.out.println("NO"); return; } right[i] = r; } for (int i = 0; i < n; i++) { if (left[i] + right[i] == 0) { result[i] = MAX; continue; } if (Math.abs(left[i] - right[i]) == 0) { result[i] = MAX - (left[i] + right[i]) > 0 ? MAX - (left[i] + right[i]) : 0; continue; } if (Math.abs(left[i] - right[i]) > 0) { result[i] = MAX - (left[i] + right[i]) > 0 ? MAX - (left[i] + right[i]) : 0; } } for (int i = 0; i < n; i++) { if (left[i] + right[i] == 0) { if (result[i] != MAX) { System.out.println("NO"); return; } } if (!(doCheckL(result, result[i], left[i], i) && doCheckR(result, result[i], right[i], i))) { System.out.println("NO"); return; } } System.out.println("YES"); for (int i = 0; i < n; i++) { System.out.print(result[i] + " "); } } static boolean doCheckR(long[] result, long value, int number, int pos) { int counter = 0; for (int i = pos + 1; i < result.length; i++) { if (result[i] > value) { counter++; } } return number == counter; } static boolean doCheckL(long[] result, long value, int number, int pos) { int counter = 0; for (int i = pos; i >= 0; i--) { if (result[i] > value) { counter++; } } return number == counter; } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
28984016735921e97ec9b47dfdd43fa6
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Vadim */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.ni(); int[] l = in.na(n); int[] r = in.na(n); int[] s = new int[n]; int maxS = 0; for (int i = 0; i < n; i++) { s[i] = l[i] + r[i]; maxS = Math.max(s[i], maxS); } int[] values = new int[n]; for (int i = 0; i < n; i++) { values[i] = maxS + 1 - s[i]; } boolean ans = true; for (int i = 0; i < n; i++) { int lc = 0; for (int j = 0; j < i; j++) { if (values[j] > values[i]) lc++; } int rc = 0; for (int j = i + 1; j < n; j++) { if (values[j] > values[i]) rc++; } if (lc != l[i] || rc != r[i]) { ans = false; break; } } if (ans) { out.println("YES"); for (int i = 0; i < n; i++) { out.print(values[i]); if (i != n - 1) out.print(' '); } out.println(); } else { out.println("NO"); } } } static class InputReader extends BufferedReader { public InputReader(InputStream st) { super(new InputStreamReader(st)); } public String readLine() { try { return super.readLine(); } catch (IOException e) { return null; } } private int readByte() { try { return read(); } catch (IOException e) { throw new RuntimeException(); } } public int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } public int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
3cefe27251f6b9bc7f35b939f911f596
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] l = new int[n]; int[] r = new int[n]; int[] f = new int[n]; for (int i = 0; i < n; i ++) l[i] = sc.nextInt(); for (int i = 0; i < n; i ++) r[i] = sc.nextInt(); sc.close(); int now = n; while (true) { int ll = 0, tot = 0; for (int i = 0; i < n; i ++) if (f[i] == 0 && l[i] == 0 && r[i] == 0) { f[i] = now; tot ++; } for (int i = 0; i < n; i ++) if (f[i] == now) { ll ++; } else { l[i] -= ll; r[i] -= tot - ll; } now -= tot; if (tot == 0 || now == 0) break; } if (now != 0) System.out.println("NO"); else { System.out.println("YES"); for (int i = 0; i < n - 1; i ++) System.out.print(f[i] + " "); System.out.println(f[n - 1]); } } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
95e21b95943d1985943faf453bb2f5c3
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.util.Scanner; public class C { static final Scanner sc = new Scanner(System.in); static final int n = sc.nextInt(); static final int a[] = new int[n]; static final int l[] = new int[n]; static final int r[] = new int[n]; public static void main(String[] args) { for (int i = 0; i < n; i++) { l[i] = sc.nextInt(); if (l[i] > i) { System.out.println("NO"); return; } } for (int i = 0; i < n; i++) { r[i] = sc.nextInt(); if (r[i] >= n - i) { System.out.println("NO"); return; } } if (l[0] != 0 || r[n - 1] != 0){ System.out.println("NO"); return; } int max = 0; for (int i = 0; i < n; i++) { a[i] = l[i] + r[i]; if (a[i] > max) max = a[i]; } ++max; for (int i = 0; i < n; i++) { a[i] = max - a[i]; } for (int i = 0; i < n; i++) { final int m = a[i]; int lk = 0; int rk = 0; for (int j = i + 1; j < n; j++) { if (a[j] > m) ++rk; } if (rk != r[i]){ System.out.println("NO"); return; } for (int j = 0; j < i; j++) { if (a[j] > m) ++lk; } if (lk != l[i]){ System.out.println("NO"); return; } } System.out.println("YES"); for (int i : a) { System.out.print(i + " "); } } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
14b9c082a5104333731684ccecc7fb79
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.io.*; import java.util.*; import java.util.regex.*; public class vk18 { public static void main(String[]stp) throws Exception { Scanner scan=new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(br.readLine()); //String []s; int n=Integer.parseInt(st.nextToken()),i,j; st=new StringTokenizer(br.readLine()); Integer l[]=new Integer[n]; Integer r[]=new Integer[n]; for(i=0;i<n;i++) l[i]=Integer.parseInt(st.nextToken()); st=new StringTokenizer(br.readLine()); for(i=0;i<n;i++) r[i]=Integer.parseInt(st.nextToken()); Integer ans[]=new Integer[n]; for(i=0;i<n;i++) ans[i]=n-l[i]-r[i]; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(j < i && ans[j] > ans[i]) l[i]--; else if(j > i && ans[j] > ans[i]) r[i]--; } if(l[i]!=0 || r[i] !=0 ) { pw.println("NO"); pw.flush(); System.exit(0); } } pw.println("YES"); for(i=0;i<n;i++) pw.print(ans[i]+" "); pw.flush(); } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
3563d723b74e1eaac1ac7979a971d3f4
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
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 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] l = new int[n]; for (int i = 0; i < n; i++) { l[i] = in.nextInt(); } int[] r = new int[n]; for (int i = 0; i < n; i++) { r[i] = in.nextInt(); } int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = n - (l[i] + r[i]); } for (int i = 0; i < n; i++) { int L = 0; int R = 0; for (int j = 0; j < i; j++) { if (a[j] > a[i]) ++L; } for (int j = i + 1; j < n; j++) { if (a[j] > a[i]) ++R; } if (a[i] < 1 || L != l[i] || R != r[i]) { out.println("NO"); return; } } out.println("YES"); for (int i = 0; i < n; i++) { if (i > 0) out.print(' '); out.print(a[i]); } out.println(); } } static class InputReader { final InputStream is; final byte[] buf = new byte[1024]; int pos; int size; public InputReader(InputStream is) { this.is = is; } public int nextInt() { int c = read(); while (isWhitespace(c)) c = read(); int sign = 1; if (c == '-') { sign = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = read(); } while (!isWhitespace(c)); return res * sign; } int read() { if (size == -1) throw new InputMismatchException(); if (pos >= size) { pos = 0; try { size = is.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (size <= 0) return -1; } return buf[pos++] & 255; } static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
77b5d7b66a604e4a994aaaeea00ff9b2
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.util.Arrays; import java.util.HashSet; import java.util.Scanner; public class q40 { public static void main(String[] args) { 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(); for(int i=0;i<n;i++) r[i]=sc.nextInt(); if(!(r[n-1]==0&&l[0]==0)) { System.out.println("NO"); return; } pair res [] = new pair [n]; for(int i=0;i<n;i++) { res[i]=new pair(i,r[i]+l[i]); } Arrays.sort(res); int N = 1; while(N < n) N <<= 1; //padding int[] in = new int[N + 1]; // in[res[0].idx]=1; // System.out.println(Arrays.toString(in)); SegmentTree st = new SegmentTree(in); st.update_point(res[0].idx+1, 1); // System.out.println(res[0].idx); int k=1; while(true) { if(k>=n||res[k].val!=res[0].val) { break; }else { // System.out.println(22); // System.out.println(res[k].idx); st.update_point(res[k].idx+1, 1); k++; } } // System.out.println(st.query(1, 3)); for(int i=k;i<n;i++) { int u =i; while(true) { int j =i; int leftstart =1; int leftend = (res[i].idx+1)-1; int rightstart =(res[i].idx+1)+1; int rightend = n+1; int leftamount = st.query(leftstart, leftend); int rightamount = st.query(rightstart, rightend); // System.out.println(leftstart+" "+leftend); // System.out.println(rightstart+" "+rightend); // System.out.println(st.query(0, res[i].idx+1)); // System.out.println(res[i].idx-1); // System.out.println(res[i].idx+1); // System.out.println(st.query(1, 2)); // System.out.println(st.query(2, 3)); // System.out.println(st.query(3, 3)); // System.out.println(res[i].idx); // System.out.println(res[i].idx+" "+leftamount+" "+rightamount+" "+l[res[i].idx]+" "+r[res[i].idx]); if(!(leftamount==l[res[i].idx]&&rightamount==r[res[i].idx])) { System.out.println("NO"); return ; } if(i+1>=n||res[j+1].val!=res[i].val) break; else i++; } for(int j=u;j<=i;j++) { st.update_point(res[j].idx+1, 1); // in[res[u].idx]=1; } } System.out.println("YES"); int ans [] = new int [n]; for(int i=0;i<n;i++) { ans[res[i].idx]=n-res[i].val; } for(int i=0;i<n;i++) { System.out.print(ans[i]+" "); } } static class pair implements Comparable<pair>{ int idx, val; pair(int idx, int val){ this.idx=idx; this.val=val; } public int compareTo(pair o) { if(this.val<o.val) return -1; else if(this.val>o.val) return 1; else return 0; } } public 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; } } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
93d0eea7d406f03a85abb19dae1df087
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
public class Main { private static void solve() { int n = ni(); int[] l = na(n); int[] r = na(n); if (l[0] != 0 || r[n - 1] != 0) { System.out.println("NO"); return; } int[] ret = new int[n]; for (int k = n; k >= 1; k --) { int[] rc = new int[n]; int[] lc = new int[n]; for (int i = 0; i < n; i ++) { if (ret[i] > k) { lc[i] ++; } if (i > 0) lc[i] += lc[i - 1]; } for (int i = n - 1; i >= 0; i --) { if (ret[i] > k) { rc[i] ++; } if (i < n - 1) rc[i] += rc[i + 1]; } for (int i = 0; i < n; i ++) { if (ret[i] == 0 && (i == 0 || lc[i - 1] == l[i]) && (i == n -1 || rc[i + 1] == r[i])) { ret[i] = k; } } } StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i ++) { if (ret[i] == 0) { System.out.println("NO"); return; } sb.append(ret[i]); if (i < n - 1) { sb.append(" "); } } System.out.println("YES"); System.out.println(sb); } public static void main(String[] args) { new Thread(null, new Runnable() { @Override public void run() { long start = System.currentTimeMillis(); String debug = args.length > 0 ? args[0] : null; if (debug != null) { try { is = java.nio.file.Files.newInputStream(java.nio.file.Paths.get(debug)); } catch (Exception e) { throw new RuntimeException(e); } } reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768); solve(); out.flush(); tr((System.currentTimeMillis() - start) + "ms"); } }, "", 64000000).start(); } private static java.io.InputStream is = System.in; private static java.io.PrintWriter out = new java.io.PrintWriter(System.out); private static java.util.StringTokenizer tokenizer = null; private static java.io.BufferedReader reader; public static String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new java.util.StringTokenizer(reader.readLine()); } catch (Exception e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } private static double nd() { return Double.parseDouble(next()); } private static long nl() { return Long.parseLong(next()); } private static int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private static char[] ns() { return next().toCharArray(); } private static long[] nal(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private static int[][] ntable(int n, int m) { int[][] table = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table[i][j] = ni(); } } return table; } private static int[][] nlist(int n, int m) { int[][] table = new int[m][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table[j][i] = ni(); } } return table; } private static int ni() { return Integer.parseInt(next()); } private static void tr(Object... o) { if (is != System.in) System.out.println(java.util.Arrays.deepToString(o)); } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
9e69f8409526de116a701ae624f3243d
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.util.*; public class Main { static Main sc = new Main(); public class heap implements Comparable<heap> { int idx; int sum; public heap(int idx, int sum) { this.idx = idx; this.sum = sum; } public int compareTo(heap ob) { return ob.sum - this.sum; } } public static void main(String[] args) { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); int[] l = new int[n]; int[] r = new int[n]; for (int i = 0; i < n; i++) { l[i] = scn.nextInt(); } for (int i = 0; i < n; i++) { r[i] = scn.nextInt(); } ArrayList<heap> al = new ArrayList<heap>(); for (int i = 0; i < n; i++) { heap ob = sc.new heap(i, l[i] + r[i]); al.add(ob); } Collections.sort(al); // for (int i = 0; i < n; i++) { // heap ob = al.get(i); // System.out.println(ob.idx + " " + ob.sum); // } int[] ans = new int[n]; for (int i = 0; i < n; i++){ ans[i] = 1; } int count = 1; int presum = al.get(0).sum; ans[al.get(0).idx] = 1; for (int i = 1; i < n; i++) { heap ob = al.get(i); // ans[ob.idx] = count; // System.out.print(ans[i]+" "); if (presum > ob.sum) { presum = ob.sum; count++; ans[ob.idx] = count; } else{ ans[ob.idx] = count; } } // for(int i=0;i<n;i++){ // System.out.print(ans[i]+" "); // } int flag=0; for (int i = 0; i < n; i++) { int count1 = 0, count2 = 0; for (int j = 0; j <= i - 1; j++) { if (ans[j] > ans[i]) { count1++; } } for (int j = i + 1; j < n; j++) { if (ans[j] > ans[i]) { count2++; } } if (count1 != l[i] || count2 != r[i]) { flag = 1; break; } } if (flag == 1) { System.out.println("NO"); } else { System.out.println("YES"); for (int i = 0; i < ans.length; i++) { System.out.print(ans[i] + " "); } } } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
4920d658cf3b676ea652eb1c6bf81578
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.util.*; public class Mail_C { 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]; for (int i = 0; i < n; i++){ left[i] = scan.nextInt(); } for (int i = 0; i < n; i++){ right[i] = scan.nextInt(); } // int n = 5; // int[] left = new int[]{0,0,1,1,2}; // int[] right = new int[]{2,0,1,0,0}; helper(left, right, n); } private static void helper(int[] left, int[] right, int n){ int count = n; boolean[] visited = new boolean[n]; int[] res = new int[n]; int level = n; while (count != 0){ Set<Integer> set = new HashSet<>(); for (int i = 0; i < n; i++){ if (!visited[i] && left[i] == 0 && right[i] == 0){ visited[i] = true; count--; set.add(i); } } if (set.isEmpty()){ //no max find break; } for (int index: set){ res[index] = level; //give the value for (int i = index + 1; i < n; i++){ if (!visited[i]) left[i]--; //if (left[i] > 0) left[i]--; } for (int i = 0; i < index; i++){ if (!visited[i]) right[i]--; //if (right[i] > 0) right[i]--; may not valid which means right[i] could be negative after minus } } level--; } if (count != 0){ System.out.println("NO"); } else { System.out.println("YES"); for (int i = 0; i < n; i++){ System.out.print(res[i] + " "); } } } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
f5fefba5d89db3c5e78554d36d358950
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.io.*; import java.util.*; public class Main { FastReader scn; PrintWriter out; String INPUT = ""; void solve() { int n = scn.nextInt(); int[] l = scn.nextIntArray(n), r = scn.nextIntArray(n); int[] arr = new int[n]; for(int i = 0; i < n; i++) { arr[i] = n - (l[i] + r[i]); } for(int i = 0; i < n; i++) { int x = 0; for(int j = 0; j < i; j++) { if(arr[j] > arr[i]) { x++; } } if(x != l[i]) { out.println("NO"); return; } x = 0; for(int j = n - 1; j > i; j--) { if(arr[j] > arr[i]) { x++; } } if(x != r[i]) { out.println("NO"); return; } } out.println("YES"); for(int x : arr) { out.print(x + " "); } out.println(); } void run() throws Exception { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null; out = new PrintWriter(System.out); scn = new FastReader(oj); solve(); out.flush(); if (!oj) { System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } } public static void main(String[] args) throws Exception { new Main().run(); } class FastReader { InputStream is; public FastReader(boolean onlineJudge) { is = onlineJudge ? System.in : new ByteArrayInputStream(INPUT.getBytes()); } byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return (char) skip(); } String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nextLine() { int b = skip(); StringBuilder sb = new StringBuilder(); while ((!isSpaceChar(b) || b == ' ')) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } char[] next(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } char[][] nextMatrix(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = next(m); return map; } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } int[][] next2DInt(int n, int m) { int[][] arr = new int[n][]; for (int i = 0; i < n; i++) { arr[i] = nextIntArray(m); } return arr; } long[][] next2DLong(int n, int m) { long[][] arr = new long[n][]; for (int i = 0; i < n; i++) { arr[i] = nextLongArray(m); } return arr; } int[] shuffle(int[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); arr[i] = arr[i] ^ arr[j]; arr[j] = arr[i] ^ arr[j]; arr[i] = arr[i] ^ arr[j]; } return arr; } } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
4208b8142b51f096c2b192cbe72f6512
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Try { static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int ni() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nl() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nia(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public String rs() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } static long mod=1000000007; static BigInteger bigInteger = new BigInteger("1000000007"); static int n = (int)1e6; static boolean[] prime; static ArrayList<Integer> as; static HashSet<Integer> hs; static void sieve() { Arrays.fill(prime , true); prime[0] = prime[1] = false; for(int i = 2 ; i * i <= n ; ++i) { if(prime[i]) { for(int k = i * i; k< n ; k+=i) { prime[k] = false; } } } } static PrintWriter w = new PrintWriter(System.out); static char [][]sol; static int t2 = 0; static int t3 = 0; public static void main(String[] args) { InputReader sc = new InputReader(System.in); //PrintWriter w = new PrintWriter(System.out); prime = new boolean[n + 1]; sieve(); prime[1] = false; /* as = new ArrayList<>(); for(int i=2;i<=1000000;i++) { if(prime[i]) as.add(i); } */ /* long a = sc.nl(); BigInteger ans = new BigInteger("1"); for (long i = 1; i < Math.sqrt(a); i++) { if (a % i == 0) { if (a / i == i) { ans = ans.multiply(BigInteger.valueOf(phi(i))); } else { ans = ans.multiply(BigInteger.valueOf(phi(i))); ans = ans.multiply(BigInteger.valueOf(phi(a / i))); } } } w.println(ans.mod(bigInteger)); */ // MergeSort ob = new MergeSort(); // ob.sort(arr, 0, arr.length-1); // Student []st = new Student[x]; // st[i] = new Student(i,d[i]); //Arrays.sort(st,(p,q)->p.diff-q.diff); int x = sc.ni(); int []l = sc.nia(x); int []r = sc.nia(x); int f = 0; int []res = new int[x]; for(int i=0;i<x;i++) { res[i] = x - l[i] - r[i]; } for(int i=0;i<x;i++) { for(int j=0;j<x;j++) { if(j < i && res[j] > res[i]) l[i]--; if(j > i && res[j] > res[i]) r[i]--; } if(l[i] !=0 || r[i] !=0) { f = 1; break; } } if(f==1) w.println("NO"); else { w.println("YES"); for(int i=0;i<x;i++) { w.print(res[i] + " "); } } w.close(); } static int digit(long x) { int p = 0; while(x > 0) { x /= 10; p++; } return p; } public static String f(String a,int b){ if(b==0) return ""; else if(b==1) return a; else{ if(b%2==0) return f(a,b/2)+f(a,b/2); else return a+f(a,b/2)+f(a,b/2); } } static class Student { int first; int sec; Student(int first,int sec) { this.first = first; this.sec = sec; } } public static long modMultiply(long one, long two) { return (one % mod * two % mod) % mod; } static long fx(int m) { long re = 0; for(int i=1;i<=m;i++) { re += (long) (i / gcd(i,m)); } return re; } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } static long phi(long nx) { // Initialize result as n double result = nx; // Consider all prime factors of n and for // every prime factor p, multiply result // with (1 - 1/p) for (int p = 0; as.get(p) * as.get(p) <= nx; ++p) { // Check if p is a prime factor. if (nx % as.get(p) == 0) { // If yes, then update n and result while (nx % as.get(p) == 0) nx /= as.get(p); result *= (1.0 - (1.0 / (double) as.get(p))); } } // If n has a prime factor greater than sqrt(n) // (There can be at-most one such prime factor) if (nx > 1) result *= (1.0 - (1.0 / (double) nx)); return (long)result; //return(phi((long)result,k-1)); } public static void primeFactors(int n) { //hs = new HashSet<>(); // Print the number of 2s that divide n while (n%2==0) { hs.add(2); //System.out.print(2 + " "); n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i+= 2) { // While i divides n, print i and divide n while (n%i == 0) { // System.out.print(i + " "); hs.add(i); n /= i; } } // This condition is to handle the case whien // n is a prime number greater than 2 if (n >= 2) { hs.add(n); } } static int digitsum(int x) { int sum = 0; while(x > 0) { int temp = x % 10; sum += temp; x /= 10; } return sum; } static int countDivisors(int n) { int cnt = 0; for (int i = 1; i*i <=n; i++) { if (n % i == 0 && i<=1000000) { // If divisors are equal, // count only one if (n / i == i) cnt++; else // Otherwise count both cnt = cnt + 2; } } return cnt; } static boolean isprime(long n) { if(n == 2) return true; if(n == 3) return true; if(n % 2 == 0) return false; if(n % 3 == 0) return false; long i = 5; long w = 2; while(i * i <= n) { if(n % i == 0) return false; i += w; w = 6 - w; } return true; } static long log2(long value) { return Long.SIZE-Long.numberOfLeadingZeros(value); } static boolean binarysearch(int []arr,int p,int n) { //ArrayList<Integer> as = new ArrayList<>(); //as.addAll(0,at); //Collections.sort(as); boolean re = false; int st = 0; int end = n-1; while(st <= end) { int mid = st + (end-st)/2; if(p > arr[mid]) { st = mid+1; } else if(p < arr[mid]) { end = mid-1; } else if(p == arr[mid]) { re = true; break; } } return re; } /* Java program for Merge Sort */ static class MergeSort { // Merges two subarrays of arr[]. // First subarray is arr[l..m] // Second subarray is arr[m+1..r] void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int [n1]; int R[] = new int [n2]; /*Copy data to temp arrays*/ for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } /* A utility function to print array of size n */ } public static int ip(String s){ return Integer.parseInt(s); } public static String ips(int s){ return Integer.toString(s); } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
a52be1a7a6c3977772acd124d9e837db
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int l[] = new int[n]; int r[] = new int[n]; int p[] = new int[n]; for(int i = 0; i< n; i++) l[i] = sc.nextInt(); for(int i = 0; i< n; i++) r[i] = sc.nextInt(); if(l[0]> 0 || r[n-1]> 0) { System.out.println("NO"); return; } int min = Integer.MAX_VALUE; for(int i = 0; i< n; i++) { p[i] = n-l[i]+n-r[i]; if(p[i]< min) min = p[i]; } for(int i = 0; i < n; i++) { p[i] = p[i]- min+1; } //right check for(int i = 0; i< n-1; i++) { int count = 0; for(int j = i+1; j < n; j++) { if(p[i] < p[j]) count++; } if(r[i] != count) { System.out.println("NO"); return; } } //left check for(int i = n-1; i> 0 ; i--) { int count = 0; for(int j = i-1; j >= 0; j--) { if(p[i] < p[j]) count++; } if(l[i] != count) { System.out.println("NO"); return; } } System.out.println("YES"); for(int i = 0; i< n; i++) { System.out.print(p[i]+" "); } } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
bf154012f996edaaf6c7a2bb6bc7a4e9
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String args[]) throws Exception { Scanner sc = new Scanner(System.in); int n; n = sc.nextInt(); int arr[][] = new int[n][4]; // int arr2[] = new int[n]; for (int i = 0; i < n; i++) { arr[i][0] = sc.nextInt(); } int count1 = 0; for (int i = 0; i < n; i++) { arr[i][1] = sc.nextInt(); if (arr[i][0] == arr[i][1] && arr[i][0] == 0) { arr[i][3] = n; count1++; } } int flag = 0; int arr2[] = new int[n]; int arr3[] = new int[n]; if (arr[0][1] == 0) { arr2[0] = 1; } for (int i = 1; i < n; i++) { arr2[i] = (arr2[i - 1] + (arr[i][3] == n ? 1 : 0)); } if (arr[n - 1][0] == 0) { arr3[n - 1] = 1; } for (int i = n - 2; i >= 0; i--) { arr3[i] = (arr3[i + 1] + (arr[i][3] == n ? 1 : 0)); } int count = n - count1; int c = 1; while (count > 0 && c < n) { for (int i = 0; i < n; i++) { if (arr[i][3] == 0 && arr2[i] == arr[i][0] && arr3[i] == arr[i][1]) { arr[i][3] = n - c; count--; } } int countm = 0; for (int j = 0; j < n; j++) { if (arr[j][3] == n - c) { arr2[j] += (++countm); } else { arr2[j] += (countm); } } countm = 0; for (int j = n - 1; j >= 0; j--) { if (arr[j][3] == n - c) { arr3[j] += (++countm); } else { arr3[j] += (countm); } } c++; } for (int i = 0; i < n; i++) { if (arr[i][3] == 0) { flag = 1; break; } } if (flag == 1) { System.out.println("NO"); } else { System.out.println("YES"); for (int i = 0; i < n; i++) { System.out.print(arr[i][3] + " "); } } } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
89c35b5d408c9653455d1b70ad048f09
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
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.util.Iterator; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.NoSuchElementException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Egor Kulikov (egor@egork.net) */ 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); C solver = new C(); solver.solve(1, in, out); out.close(); } static class C { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(); int[] l = in.readIntArray(n); int[] r = in.readIntArray(n); int[] answer = new int[n]; int[] sum = new int[n]; for (int i = 0; i < n; i++) { sum[i] = l[i] + r[i]; } int[] order = ArrayUtils.order(sum); int last = 3 * n; int value = n + 1; for (int i : order) { if (sum[i] != last) { last = sum[i]; value--; } answer[i] = value; } for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if (answer[i] > answer[j]) { r[j]--; } else if (answer[i] < answer[j]) { l[i]--; } } } if (ArrayUtils.count(l, 0) == n && ArrayUtils.count(r, 0) == n) { out.printLine("YES"); out.printLine(answer); return; } out.printLine("NO"); } } static class ArrayUtils { public static int[] range(int from, int to) { return Range.range(from, to).toArray(); } public static int[] createOrder(int size) { return range(0, size); } public static int[] sort(int[] array, IntComparator comparator) { return sort(array, 0, array.length, comparator); } public static int[] sort(int[] array, int from, int to, IntComparator comparator) { if (from == 0 && to == array.length) { new IntArray(array).sort(comparator); } else { new IntArray(array).subList(from, to).sort(comparator); } return array; } public static int[] order(final int[] array) { return sort(createOrder(array.length), new IntComparator() { public int compare(int first, int second) { if (array[first] < array[second]) { return -1; } if (array[first] > array[second]) { return 1; } return 0; } }); } public static int count(int[] array, int value) { return new IntArray(array).count(value); } } static interface IntCollection extends IntStream { public int size(); default public void add(int value) { throw new UnsupportedOperationException(); } default public int[] toArray() { int size = size(); int[] array = new int[size]; int i = 0; for (IntIterator it = intIterator(); it.isValid(); it.advance()) { array[i++] = it.value(); } return array; } default public IntCollection addAll(IntStream values) { for (IntIterator it = values.intIterator(); it.isValid(); it.advance()) { add(it.value()); } return this; } } static class IntArray extends IntAbstractStream implements IntList { private int[] data; public IntArray(int[] arr) { data = arr; } public int size() { return data.length; } public int get(int at) { return data[at]; } public void addAt(int index, int value) { throw new UnsupportedOperationException(); } public void removeAt(int index) { throw new UnsupportedOperationException(); } public void set(int index, int value) { data[index] = value; } } static class Range { public static IntList range(int from, int to) { int[] result = new int[Math.abs(from - to)]; int current = from; if (from <= to) { for (int i = 0; i < result.length; i++) { result[i] = current++; } } else { for (int i = 0; i < result.length; i++) { result[i] = current--; } } return new IntArray(result); } } static interface IntComparator { public int compare(int first, int second); } static class IntArrayList extends IntAbstractStream implements IntList { private int size; private int[] data; public IntArrayList() { this(3); } public IntArrayList(int capacity) { data = new int[capacity]; } public IntArrayList(IntCollection c) { this(c.size()); addAll(c); } public IntArrayList(IntStream c) { this(); if (c instanceof IntCollection) { ensureCapacity(((IntCollection) c).size()); } addAll(c); } public IntArrayList(IntArrayList c) { size = c.size(); data = c.data.clone(); } public IntArrayList(int[] arr) { size = arr.length; data = arr.clone(); } public int size() { return size; } public int get(int at) { if (at >= size) { throw new IndexOutOfBoundsException("at = " + at + ", size = " + size); } return data[at]; } private void ensureCapacity(int capacity) { if (data.length >= capacity) { return; } capacity = Math.max(2 * data.length, capacity); data = Arrays.copyOf(data, capacity); } public void addAt(int index, int value) { ensureCapacity(size + 1); if (index > size || index < 0) { throw new IndexOutOfBoundsException("at = " + index + ", size = " + size); } if (index != size) { System.arraycopy(data, index, data, index + 1, size - index); } data[index] = value; size++; } public void removeAt(int index) { if (index >= size || index < 0) { throw new IndexOutOfBoundsException("at = " + index + ", size = " + size); } if (index != size - 1) { System.arraycopy(data, index + 1, data, index, size - index - 1); } size--; } public void set(int index, int value) { if (index >= size) { throw new IndexOutOfBoundsException("at = " + index + ", size = " + size); } data[index] = value; } public int[] toArray() { return Arrays.copyOf(data, size); } } 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[] readIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = readInt(); } return array; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static abstract class IntAbstractStream implements IntStream { public String toString() { StringBuilder builder = new StringBuilder(); boolean first = true; for (IntIterator it = intIterator(); it.isValid(); it.advance()) { if (first) { first = false; } else { builder.append(' '); } builder.append(it.value()); } return builder.toString(); } public boolean equals(Object o) { if (!(o instanceof IntStream)) { return false; } IntStream c = (IntStream) o; IntIterator it = intIterator(); IntIterator jt = c.intIterator(); while (it.isValid() && jt.isValid()) { if (it.value() != jt.value()) { return false; } it.advance(); jt.advance(); } return !it.isValid() && !jt.isValid(); } public int hashCode() { int result = 0; for (IntIterator it = intIterator(); it.isValid(); it.advance()) { result *= 31; result += it.value(); } return result; } } static interface IntReversableCollection extends IntCollection { } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) { writer.print(' '); } writer.print(array[i]); } } public void printLine(int[] array) { print(array); writer.println(); } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } static interface IntList extends IntReversableCollection { public abstract int get(int index); public abstract void set(int index, int value); public abstract void addAt(int index, int value); public abstract void removeAt(int index); default public void swap(int first, int second) { if (first == second) { return; } int temp = get(first); set(first, get(second)); set(second, temp); } default public IntIterator intIterator() { return new IntIterator() { private int at; private boolean removed; public int value() { if (removed) { throw new IllegalStateException(); } return get(at); } public boolean advance() { at++; removed = false; return isValid(); } public boolean isValid() { return !removed && at < size(); } public void remove() { removeAt(at); at--; removed = true; } }; } default public void add(int value) { addAt(size(), value); } default public IntList sort(IntComparator comparator) { Sorter.sort(this, comparator); return this; } default public IntList subList(final int from, final int to) { return new IntList() { private final int shift; private final int size; { if (from < 0 || from > to || to > IntList.this.size()) { throw new IndexOutOfBoundsException("from = " + from + ", to = " + to + ", size = " + size()); } shift = from; size = to - from; } public int size() { return size; } public int get(int at) { if (at < 0 || at >= size) { throw new IndexOutOfBoundsException("at = " + at + ", size = " + size()); } return IntList.this.get(at + shift); } public void addAt(int index, int value) { throw new UnsupportedOperationException(); } public void removeAt(int index) { throw new UnsupportedOperationException(); } public void set(int at, int value) { if (at < 0 || at >= size) { throw new IndexOutOfBoundsException("at = " + at + ", size = " + size()); } IntList.this.set(at + shift, value); } public IntList compute() { return new IntArrayList(this); } }; } } static class Sorter { private static final int INSERTION_THRESHOLD = 16; private Sorter() { } public static void sort(IntList list, IntComparator comparator) { quickSort(list, 0, list.size() - 1, (Integer.bitCount(Integer.highestOneBit(list.size()) - 1) * 5) >> 1, comparator); } private static void quickSort(IntList list, int from, int to, int remaining, IntComparator comparator) { if (to - from < INSERTION_THRESHOLD) { insertionSort(list, from, to, comparator); return; } if (remaining == 0) { heapSort(list, from, to, comparator); return; } remaining--; int pivotIndex = (from + to) >> 1; int pivot = list.get(pivotIndex); list.swap(pivotIndex, to); int storeIndex = from; int equalIndex = to; for (int i = from; i < equalIndex; i++) { int value = comparator.compare(list.get(i), pivot); if (value < 0) { list.swap(storeIndex++, i); } else if (value == 0) { list.swap(--equalIndex, i--); } } quickSort(list, from, storeIndex - 1, remaining, comparator); for (int i = equalIndex; i <= to; i++) { list.swap(storeIndex++, i); } quickSort(list, storeIndex, to, remaining, comparator); } private static void heapSort(IntList list, int from, int to, IntComparator comparator) { for (int i = (to + from - 1) >> 1; i >= from; i--) { siftDown(list, i, to, comparator, from); } for (int i = to; i > from; i--) { list.swap(from, i); siftDown(list, from, i - 1, comparator, from); } } private static void siftDown(IntList list, int start, int end, IntComparator comparator, int delta) { int value = list.get(start); while (true) { int child = ((start - delta) << 1) + 1 + delta; if (child > end) { return; } int childValue = list.get(child); if (child + 1 <= end) { int otherValue = list.get(child + 1); if (comparator.compare(otherValue, childValue) > 0) { child++; childValue = otherValue; } } if (comparator.compare(value, childValue) >= 0) { return; } list.swap(start, child); start = child; } } private static void insertionSort(IntList list, int from, int to, IntComparator comparator) { for (int i = from + 1; i <= to; i++) { int value = list.get(i); for (int j = i - 1; j >= from; j--) { if (comparator.compare(list.get(j), value) <= 0) { break; } list.swap(j, j + 1); } } } } static interface IntStream extends Iterable<Integer>, Comparable<IntStream> { public IntIterator intIterator(); default public Iterator<Integer> iterator() { return new Iterator<Integer>() { private IntIterator it = intIterator(); public boolean hasNext() { return it.isValid(); } public Integer next() { int result = it.value(); it.advance(); return result; } }; } default public int compareTo(IntStream c) { IntIterator it = intIterator(); IntIterator jt = c.intIterator(); while (it.isValid() && jt.isValid()) { int i = it.value(); int j = jt.value(); if (i < j) { return -1; } else if (i > j) { return 1; } it.advance(); jt.advance(); } if (it.isValid()) { return 1; } if (jt.isValid()) { return -1; } return 0; } default public int count(int value) { int result = 0; for (IntIterator it = intIterator(); it.isValid(); it.advance()) { if (it.value() == value) { result++; } } return result; } } static interface IntIterator { public int value() throws NoSuchElementException; public boolean advance(); public boolean isValid(); } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
d99627779b01d433d8163d152b1dec8f
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.util.*; import java.io.*; public class b{ public static void main(String [] args){ 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(); } for(int i=0;i<n;i++){ r[i]=sc.nextInt(); } int [] ans=new int[n]; for(int i=0;i<n;i++){ ans[i]=n-l[i]-r[i]; } boolean valid=true; for(int i=0;i<n;i++){ int leftMax=0; for(int j=0;j<i;j++){ if(ans[j]>ans[i])leftMax++; } int rightMax=0; for(int j=i+1;j<n;j++){ if(ans[j]>ans[i])rightMax++; } if(leftMax!=l[i] || rightMax!=r[i]){ valid=false; } if(!valid)break; } if(!valid)System.out.println("NO"); else{ System.out.println("YES"); for(int i : ans){ System.out.print(i+" "); } System.out.println(""); } sc.close(); } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
0c89349deba4bb49e626d18bceb75060
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
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 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] l = new int[n]; for (int i = 0; i < n; i++) { l[i] = in.nextInt(); } int[] r = new int[n]; int[] ans = new int[n]; for (int i = 0; i < n; i++) { r[i] = in.nextInt(); ans[i] = n - r[i] - l[i]; } boolean eq = true; int[] l2 = left(ans); for (int i = 0; i < n; i++) { if (l2[i] != l[i]) { eq = false; } } int[] r2 = right(ans); for (int i = 0; i < n; i++) { if (r2[i] != r[i]) { eq = false; } } if (eq) { out.println("YES"); for (int i : ans) { out.print(i + " "); } } else { out.println("NO"); } } int[] left(int[] a) { int[] res = new int[a.length]; for (int i = 0; i < a.length; i++) { int cnt = 0; for (int j = 0; j < i; j++) { if (a[j] > a[i]) { cnt++; } } res[i] = cnt; } return res; } int[] right(int[] a) { int[] res = new int[a.length]; for (int i = a.length - 1; i >= 0; i--) { int cnt = 0; for (int j = a.length - 1; j > i; j--) { if (a[j] > a[i]) { cnt++; } } res[i] = cnt; } return res; } } static class InputReader { private BufferedReader reader; private StringTokenizer stt; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { return null; } } public String next() { while (stt == null || !stt.hasMoreTokens()) { stt = new StringTokenizer(nextLine()); } return stt.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
8cc17a63cbdd70eda4767f768de8d2cc
train_002.jsonl
1539880500
There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j &lt; i$$$), such that $$$a_i &lt; a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i &lt; j \leq n$$$), such that $$$a_i &lt; a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
256 megabytes
import java.util.ArrayList; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; public class mailru1{ public static void main(String args[]) { InputReader sc = new InputReader(System.in); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(), l[] = new int[n], r[] = new int[n], ans[] = new int[n], visited[] = new int[n]; ArrayList<Integer> list = new ArrayList<>(); boolean ps = true; for(int i=0; i<n; i++) l[i] = sc.nextInt(); for(int i=0; i<n; i++) r[i] = sc.nextInt(); for(int i=0; i<n; i++){ if(l[i] == 0 && r[i] == 0) { list.add(i); visited[i] = 1; ans[i] = n; } } if(list.isEmpty()) pw.println("NO"); else{ int k = n-1; loop:while(!list.isEmpty()) { for (Integer j : list) { for (int i = 0; i < j; i++) { if(visited[i] == 1) continue; if(r[i] > 0) r[i]--; else{ ps = false; break loop; } } for (int i = j + 1; i < n; i++) { if(visited[i] == 1) continue; if(l[i] > 0) l[i]--; else{ ps = false; break loop; } } } list.clear(); for(int i=0; i<n; i++){ if(l[i] == 0 && r[i] == 0 && visited[i] == 0) { visited[i] = 1; list.add(i); ans[i] = k; } } k--; } for(int i=0; i<n; i++){ if(ans[i] == 0) ps = false; } if(ps) { pw.println("YES"); for (int i : ans) pw.print(i + " "); } else pw.println("NO"); } pw.flush(); pw.close(); } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; InputReader(InputStream stream) { this.stream = stream; } int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { boolean isSpaceChar(int ch); } } }
Java
["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"]
1 second
["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"]
NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy.
Java 8
standard input
[ "constructive algorithms", "implementation" ]
fa531c38833907d619f1102505ddbb6a
On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces.
1,500
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them.
standard output
PASSED
ca3adaa25bc5ad85a68c8a251504c3ec
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; public class Main { public static int dp[][]=new int[202][402]; static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() throws Exception { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static int func(int []arr,int index,int val,int max) { //System.out.println(index+" "+val); if(index<0 || val<0) return 1000000000; else if(index==0 && val==0) return 0; else if(Main.dp[index][val]!=-1) return Main.dp[index][val]; else { dp[index][val]=Math.min(Math.abs(val-arr[index])+func(arr, index-1, val-1,max),func(arr, index, val-1,max)); return dp[index][val]; } } public static void solve(FastReader s) { for(int i=0;i<202;i++) for(int j=0;j<402;j++) Main.dp[i][j]=-1; int n=s.nextInt(); int arr[]=new int[n+1]; for(int i=1;i<=n;i++) { arr[i]=s.nextInt(); } Arrays.sort(arr); System.out.println(func(arr,n,401,n)); } public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); int testcases = sc.nextInt(); for (int i = 0; i < testcases; ++i) { Main.solve(sc); } } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
9c4744c2185e096a9077b74eeeae2b38
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; public class Main { public static int dp[][]=new int[202][402]; public static int MAX=(int)Math.pow(10,9); static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() throws Exception { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static int func(int []arr,int index,int val,int max) { //System.out.println(index+" "+val); if(index==max) return 0; else if(val==402) return Main.MAX; else if(Main.dp[index][val]!=-1) return Main.dp[index][val]; else { dp[index][val]=Math.min(Math.abs(val-arr[index])+func(arr, index+1, val+1,max),func(arr, index, val+1,max)); return dp[index][val]; } } public static void solve(FastReader s) { for(int i=0;i<202;i++) for(int j=0;j<402;j++) Main.dp[i][j]=-1; int n=s.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) { arr[i]=s.nextInt(); } Arrays.sort(arr); System.out.println(func(arr,0,1,n)); } public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); int testcases = sc.nextInt(); for (int i = 0; i < testcases; ++i) { Main.solve(sc); } } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
898796eb75b973f1d49ea6c6aaae10a3
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { PrintWriter out = new PrintWriter(System.out); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(""); String next() throws IOException { if (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int ni() throws IOException { return Integer.parseInt(next()); } long nl() throws IOException { return Long.parseLong(next()); } long mod=1000000007; int n; int[]A; long[][]D; void solve() throws IOException { for (int tc=ni();tc>0;tc--) { n=ni(); A=new int[n]; D=new long[n][400]; for (int i=0;i<n;i++) { A[i]=ni(); Arrays.fill(D[i],-1); } Arrays.sort(A); out.println(dp(0,1)); } out.flush(); } long dp(int u,int t) { if (u==n) return 0; if (D[u][t]>-1) return D[u][t]; long ret=Long.MAX_VALUE; if (t>=A[u]) ret=t-A[u]+dp(u+1,t+1); else { for (int i=t;i<=A[u];i++) { ret=Math.min(ret,A[u]-i+dp(u+1,i+1)); } } //out.println(t+" "+u+" "+A[u]+" "+ret); return D[u][t]=ret; } int gcd(int a,int b) { return(b==0?a:gcd(b,a%b)); } long gcd(long a,long b) { return(b==0?a:gcd(b,a%b)); } long mp(long a,long p) { long r=1; while(p>0) { if ((p&1)==1) r=(r*a)%mod; p>>=1; a=(a*a)%mod; } return r; } public static void main(String[] args) throws IOException { new Main().solve(); } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
7ab068ac494d42c6bc12cf48c0a25cea
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; public class C { public static void main(String[] args) { FastReader scan = new FastReader(); PrintWriter out = new PrintWriter(System.out); Task solver = new Task(); int t = scan.nextInt(); for(int tt = 1; tt <= t; tt++) solver.solve(tt, scan, out); out.close(); } static class Task { int n; int[] a; long[][] dp; public void solve(int testNumber, FastReader scan, PrintWriter out) { n = scan.nextInt(); a = new int[n]; dp = new long[n][2 * n + 1]; for(int i = 0; i < n; i++) { a[i] = scan.nextInt(); Arrays.fill(dp[i], -1); } ruffleSort(a); //out.println(Arrays.toString(a)); out.println(go(0, 1)); } long go(int at, int time) { if(at >= n) return 0; if(time >= 2 * n + 1) return Integer.MAX_VALUE / 10; if(dp[at][time] != -1) return dp[at][time]; long res = go(at, time + 1); res = Math.min(res, Math.abs(a[at] - time) + go(at + 1, time + 1)); return dp[at][time] = res; } } static void ruffleSort(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 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
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
583402df75032634b5572c2573d3fcfc
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
import java.io.*; import java.util.*; public class C { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static FastReader s = new FastReader(); static PrintWriter out = new PrintWriter(System.out); private static int[] rai(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextInt(); } return arr; } private static int[][] rai(int n, int m) { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = s.nextInt(); } } return arr; } private static long[] ral(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextLong(); } return arr; } private static long[][] ral(int n, int m) { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = s.nextLong(); } } return arr; } private static int ri() { return s.nextInt(); } private static long rl() { return s.nextLong(); } private static String rs() { return s.next(); } static int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } static boolean isPrime(int n) { //check if n is a multiple of 2 if (n % 2 == 0) return false; //if not, then just check the odds for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static int MOD=1000000007; static int[][] dp; static int findTime(List<Integer> list,int i,int n,int curr) { if(i==n) { return 0; } if(curr>2*n) { return 100000000; } if(dp[i][curr]!=-1) { return dp[i][curr]; } int res=Math.abs(list.get(i)-curr)+findTime(list,i+1,n,curr+1); res=Math.min(res,findTime(list,i,n,curr+1)); dp[i][curr]=res; return res; } public static void main(String[] args) { StringBuilder ans = new StringBuilder(); int t = ri(); // int t = 1; while (t-- > 0){ int n=ri(); List<Integer> list=new ArrayList<>(); for(int i=0;i<n;i++) { list.add(ri()); } Collections.sort(list); dp=new int[n][2*n+1]; for(int i=0;i<n;i++) { Arrays.fill(dp[i],-1); } ans.append(findTime(list,0,n,1)).append("\n"); } out.print(ans.toString()); out.flush(); } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
70ecb4cd68191d6ee363b13790f4ca30
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.Stack; import java.util.StringTokenizer; import static java.lang.Math.*; public class Main { public static void main(String[] args) { FastScanner sc =new FastScanner(); PrintWriter pw = new PrintWriter(System.out); int T=sc.nextInt(); while(T-- > 0) { int n = sc.nextInt(); int[] time = new int[n]; for(int i = 0; i < n; i++) { Arrays.fill(dp[i], -1); time[i] = sc.nextInt(); } sort(time); pw.println(solve(0, n, 1, time)); } pw.close(); } static int[][] dp = new int[210][410]; static int solve(int ind, int n, int curr, int[] time) { if(curr > 401) return 40000; if(ind == n) return 0; if(dp[ind][curr] != -1) return dp[ind][curr]; dp[ind][curr] = min(abs(curr-time[ind]) + solve(ind+1, n, curr+1, time), solve(ind, n, curr+1, time)); return dp[ind][curr]; } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
20e5f076a87015875f333431e80180a8
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class ChefMonocarp { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int q = sc.nextInt(); while (q-- > 0) { int n = sc.nextInt(); int[] t = new int[n]; for (int i = 0; i < n; i++) { t[i] = sc.nextInt(); } Arrays.sort(t); memo = new HashMap<>(); int ans = solve(t, 0, 1); System.out.println(ans); } } static Map<String, Integer> memo; private static int solve(int[] t, int i, int ct) { // TODO Auto-generated method stub String key = i + "_" + ct; if (i == t.length && ct != 2 * t.length + 1) return 0; if (ct == 2 * t.length + 1) return 100000; if (memo.containsKey(key)) return memo.get(key); int use = Math.abs(ct - t[i]) + solve(t, i + 1, ct + 1); int ignore = solve(t, i, ct + 1); memo.put(key, Math.min(use, ignore)); return Math.min(use, ignore); } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
72c596382754588b1327f1efad3c05c2
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class ChefMonocarp { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int q = sc.nextInt(); while (q-- > 0) { int n = sc.nextInt(); int[] t = new int[n]; for (int i = 0; i < n; i++) { t[i] = sc.nextInt(); } Arrays.sort(t); memo = new HashMap<>(); int ans = solve(t, 0, 1); System.out.println(ans); } } static Map<String, Integer> memo; private static int solve(int[] t, int i, int ct) { // TODO Auto-generated method stub String key = i + "_" + ct; if (i == t.length && ct != 2 * t.length + 1) return 0; if (ct == 2 * t.length + 1) return (int) 1e9; if (memo.containsKey(key)) return memo.get(key); int use = Math.abs(ct - t[i]) + solve(t, i + 1, ct + 1); int ignore = solve(t, i, ct + 1); memo.put(key, Math.min(use, ignore)); return Math.min(use, ignore); } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
42036c580f6bdbc574c2fbfa0183a670
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
import java.util.*; import java.io.*; public class CFC { BufferedReader br; PrintWriter out; StringTokenizer st; boolean eof; private static final long MOD = 1000L * 1000L * 1000L + 7; private static final int[] dx = {0, -1, 0, 1}; private static final int[] dy = {1, 0, -1, 0}; private static final String yes = "Yes"; private static final String no = "No"; void solve() { int T = nextInt(); // int T = 1; for (int i = 0; i < T; i++) { helper(); } } void helper() { int n = nextInt(); int[] arr = nextIntArr(n); int LIMIT = 400 + 10; long[][] dp = new long[1 + n][LIMIT]; for (int i = 0; i <= n; i++) { Arrays.fill(dp[i], Long.MAX_VALUE); } Arrays.sort(arr); dp[0][0] = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j < LIMIT; j++) { // Put arr[i - 1] to j. for (int k = 0; k < j; k++) { if (dp[i - 1][k] != Long.MAX_VALUE) { long diff = Math.abs(arr[i - 1] - j); dp[i][j] = Math.min(dp[i][j], dp[i - 1][k] + diff); } } } } long res = Long.MAX_VALUE; for (int j = 0; j < LIMIT; j++) { res = Math.min(res, dp[n][j]); } outln(res); } void shuffle(int[] a) { int n = a.length; for(int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); int tmp = a[i]; a[i] = a[r]; a[r] = tmp; } } long gcd(long a, long b) { while(a != 0 && b != 0) { long c = b; b = a % b; a = c; } return a + b; } private void outln(Object o) { out.println(o); } private void out(Object o) { out.print(o); } private void formatPrint(double val) { outln(String.format("%.9f%n", val)); } public CFC() { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } public static void main(String[] args) { new CFC(); } public long[] nextLongArr(int n) { long[] res = new long[n]; for(int i = 0; i < n; i++) res[i] = nextLong(); return res; } public int[] nextIntArr(int n) { int[] res = new int[n]; for(int i = 0; i < n; i++) res[i] = nextInt(); return res; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; return null; } } return st.nextToken(); } public String nextString() { try { return br.readLine(); } catch (IOException e) { eof = true; return null; } } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
afbf11e824322a3882e516bc4ba87f7e
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
import java.io.*; import java.util.*; public class eduprac implements Runnable { private boolean console=false; public void solve(int t) { int i; int n=in.ni(); long dp[][]=new long[n+1][405]; int a[]=new int[n]; for(i=0;i<n;i++) a[i]=in.ni(); for(i=0;i<=n;i++) Arrays.fill(dp[i],1000000); Arrays.sort(a); Arrays.fill(dp[0], 0); for(i=1;i<=n;i++) { for(int j=1;j<=400;j++) { dp[i][j]=Math.min(dp[i][j], dp[i-1][j-1]+Math.abs(j-a[i-1])); dp[i][j+1]=Math.min(dp[i][j+1], dp[i][j]); } } out.println(dp[n][400]); } @Override public void run() { try { init(); } catch (FileNotFoundException e) { e.printStackTrace(); } int t= in.ni(); for(int i=1;i<=t;i++) { solve(i); out.flush(); } } private FastInput in; private PrintWriter out; public static void main(String[] args) throws Exception { new eduprac().run(); } private void init() throws FileNotFoundException { InputStream inputStream = System.in; OutputStream outputStream = System.out; try { if (!console && System.getProperty("user.name").equals("sachan")) { outputStream = new FileOutputStream("/home/sachan/Desktop/output.txt"); inputStream = new FileInputStream("/home/sachan/Desktop/input.txt"); } } catch (Exception ignored) { } out = new PrintWriter(outputStream); in = new FastInput(inputStream); } static class FastInput { InputStream obj; public FastInput(InputStream obj) { this.obj = obj; } byte inbuffer[] = new byte[1024]; int lenbuffer = 0, ptrbuffer = 0; int readByte() { if (lenbuffer == -1) throw new InputMismatchException(); if (ptrbuffer >= lenbuffer) { ptrbuffer = 0; try { lenbuffer = obj.read(inbuffer); } catch (IOException e) { throw new InputMismatchException(); } } if (lenbuffer <= 0) return -1;return inbuffer[ptrbuffer++]; } String ns() { int b = skip();StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { sb.appendCodePoint(b);b = readByte(); }return sb.toString();} int ni() { int num = 0, b;boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true;b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; }b = readByte(); }} long nl() { long num = 0;int b;boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true;b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10L + (b - '0'); } else { return minus ? -num : num; }b = readByte(); } } boolean isSpaceChar(int c) { return (!(c >= 33 && c <= 126)); } int skip() { int b;while ((b = readByte()) != -1 && isSpaceChar(b)) ;return b; } float nf() {return Float.parseFloat(ns());} double nd() {return Double.parseDouble(ns());} char nc() {return (char) skip();} } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
07e84253df4a4b22f6032110be49567d
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.PriorityQueue; import java.util.Random; import java.util.StringTokenizer; import java.util.TreeSet; public class C { static int n; static int[] a; static long[][] dp; static long go(int at, int time) { if(at >= n) return 0; if(time >= 2 * n + 1) return Integer.MAX_VALUE / 10; if(dp[at][time] != -1) return dp[at][time]; long res = go(at, time + 1); res = Math.min(res, Math.abs(a[at] - time) + go(at + 1, time + 1)); return dp[at][time] = res; } public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); int t = sc.nextInt(); first: while (t-- != 0) { n = sc.nextInt(); a = sc.readArray(n); Arrays.sort(a); dp = new long[n][2 * n + 1]; for(int i = 0; i < n; i++) { Arrays.fill(dp[i], -1); } System.out.println(go(0, 1)); } } static void shuffleSort(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } } static void shuffleSort(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readArrayLong(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } static int max(int x, int y) { return Math.max(x, y); } static int min(int x, int y) { return Math.min(x, y); } static long max(long x, long y) { return Math.max(x, y); } static long min(long x, long y) { return Math.min(x, y); } static int min(int a[]) { int x = 1_000_000_00_9; for (int i = 0; i < a.length; i++) x = min(x, a[i]); return x; } static int max(int a[]) { int x = -1_000_000_00_9; for (int i = 0; i < a.length; i++) x = max(x, a[i]); return x; } static long min(long a[]) { long x = (long) 3e18; for (int i = 0; i < a.length; i++) x = min(x, a[i]); return x; } static long max(long a[]) { long x = -(long) 3e18; for (int i = 0; i < a.length; i++) x = max(x, a[i]); return x; } static int power(int x, int y) { int res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y >>= 1; x = (x * x); } return res; } static long power(long x, long y) { long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y >>= 1; x = (x * x); } return res; } static long power(long x, long y, long mod) { long res = 1; x %= mod; while (y > 0) { if (y % 2 == 1) res = (res * x) % mod; y >>= 1; x = (x * x) % mod; } return res; } static void intsort(int[] a) { List<Integer> temp = new ArrayList<Integer>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static void longsort(long[] a) { List<Long> temp = new ArrayList<Long>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static void reverseintsort(int[] a) { List<Integer> temp = new ArrayList<Integer>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); Collections.reverseOrder(); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static void reverselongsort(long[] a) { List<Long> temp = new ArrayList<Long>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); Collections.reverseOrder(); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static class longpair implements Comparable<longpair> { long x, y; longpair(long x, long y) { this.x = x; this.y = y; } public int compareTo(longpair p) { return Long.compare(this.x, p.x); } } static class intpair implements Comparable<intpair> { int x, y; intpair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(intpair o) { return Integer.compare(this.x, o.x); } // a = new pair [n]; // a[i] = new pair(coo,cost); } public static int gcd(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } public static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
37cc20915d78a383be869853b5cd3b89
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.PriorityQueue; import java.util.Random; import java.util.StringTokenizer; import java.util.TreeSet; public class C { static int n; static int[] a; static long[][] dp; static long calc(int p, int t) { if(p == n) return 0; if(t == 2 * n + 1) return Integer.MAX_VALUE; if(dp[p][t] != -1) return dp[p][t]; return dp[p][t] = min(calc(p,t+1),calc(p+1,t+1)+Math.abs(a[p] - t)); } public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); int t = sc.nextInt(); first: while (t-- != 0) { n = sc.nextInt(); a = sc.readArray(n); Arrays.sort(a); dp = new long[n][2 * n + 1]; for(int i = 0; i < n; i++) { Arrays.fill(dp[i], -1); } System.out.println(calc(0, 1)); } } static void shuffleSort(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } } static void shuffleSort(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readArrayLong(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } static int max(int x, int y) { return Math.max(x, y); } static int min(int x, int y) { return Math.min(x, y); } static long max(long x, long y) { return Math.max(x, y); } static long min(long x, long y) { return Math.min(x, y); } static int min(int a[]) { int x = 1_000_000_00_9; for (int i = 0; i < a.length; i++) x = min(x, a[i]); return x; } static int max(int a[]) { int x = -1_000_000_00_9; for (int i = 0; i < a.length; i++) x = max(x, a[i]); return x; } static long min(long a[]) { long x = (long) 3e18; for (int i = 0; i < a.length; i++) x = min(x, a[i]); return x; } static long max(long a[]) { long x = -(long) 3e18; for (int i = 0; i < a.length; i++) x = max(x, a[i]); return x; } static int power(int x, int y) { int res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y >>= 1; x = (x * x); } return res; } static long power(long x, long y) { long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y >>= 1; x = (x * x); } return res; } static long power(long x, long y, long mod) { long res = 1; x %= mod; while (y > 0) { if (y % 2 == 1) res = (res * x) % mod; y >>= 1; x = (x * x) % mod; } return res; } static void intsort(int[] a) { List<Integer> temp = new ArrayList<Integer>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static void longsort(long[] a) { List<Long> temp = new ArrayList<Long>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static void reverseintsort(int[] a) { List<Integer> temp = new ArrayList<Integer>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); Collections.reverseOrder(); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static void reverselongsort(long[] a) { List<Long> temp = new ArrayList<Long>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); Collections.reverseOrder(); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static class longpair implements Comparable<longpair> { long x, y; longpair(long x, long y) { this.x = x; this.y = y; } public int compareTo(longpair p) { return Long.compare(this.x, p.x); } } static class intpair implements Comparable<intpair> { int x, y; intpair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(intpair o) { return Integer.compare(this.x, o.x); } // a = new pair [n]; // a[i] = new pair(coo,cost); } public static int gcd(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } public static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
503c32c1feb0d95dc9ff23050f9cd652
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class NewSolution { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(reader.readLine()); StringBuilder ans = new StringBuilder(); while (t-->0) { int n = Integer.parseInt(reader.readLine()); StringTokenizer sToken = new StringTokenizer(reader.readLine()); HashMap<Integer, Integer> map = new HashMap<>(); ArrayList<Integer> list = new ArrayList<>(); for (int i=0; i<n; i++) { int x = Integer.parseInt(sToken.nextToken()); if (map.get(x)==null) { map.put(x, 0); list.add(x); } map.replace(x, map.get(x) + 1); } int[] res = new int[2*n+1]; Arrays.fill(res, Integer.MAX_VALUE); Collections.sort(list); int start = 1; //System.out.println(list+" "+map); int a = 0; for (int elem:list) { int amount = map.get(elem); for (int i=2*n-amount+1; i>=start; i--) { int x = res[i-1]; if (x==Integer.MAX_VALUE) x = 0; int diff = 0; for (int j=i; j<i+amount; j++) diff+=Math.abs(j - elem); //if (a==5) System.out.println(i+" "+diff); res[i+amount-1] = diff + x; //System.out.println(elem+" "+i+" "+diff); } //System.out.println(start+" "+amount); //System.out.println(Arrays.toString(res)); for (int i=start+amount; i<=2*n; i++) { if (res[i]==Integer.MAX_VALUE) break; res[i] = Math.min(res[i], res[i-1]); } start+=amount; //System.out.println(start); //System.out.println(Arrays.toString(res)); a++; //if (a==4) break; } //System.out.println(Arrays.toString(res)); int answer = 0; for (int i=2*n; i>0; i--) { if (res[i]!=Integer.MAX_VALUE) { answer = res[i]; break; } } ans.append(answer).append("\n"); } System.out.println(ans); } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
8432fbcd9625a06b6a8fd28f0f152b54
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class NewSolution { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(reader.readLine()); StringBuilder ans = new StringBuilder(); while (t-->0) { int n = Integer.parseInt(reader.readLine()); StringTokenizer sToken = new StringTokenizer(reader.readLine()); HashMap<Integer, Integer> map = new HashMap<>(); ArrayList<Integer> list = new ArrayList<>(); for (int i=0; i<n; i++) { int x = Integer.parseInt(sToken.nextToken()); if (map.get(x)==null) { map.put(x, 0); list.add(x); } map.replace(x, map.get(x) + 1); } int[] res = new int[2*n+1]; Arrays.fill(res, Integer.MAX_VALUE); Collections.sort(list); int start = 1; for (int elem:list) { int amount = map.get(elem); for (int i=2*n-amount+1; i>=start; i--) { int x = res[i-1]; if (x==Integer.MAX_VALUE) x = 0; int diff = 0; for (int j=i; j<i+amount; j++) diff+=Math.abs(j - elem); res[i+amount-1] = diff + x; } for (int i=start+amount; i<=2*n; i++) { if (res[i]==Integer.MAX_VALUE) break; res[i] = Math.min(res[i], res[i-1]); } start+=amount; } int answer = 0; for (int i=2*n; i>0; i--) { if (res[i]!=Integer.MAX_VALUE) { answer = res[i]; break; } } ans.append(answer).append("\n"); } System.out.println(ans); } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
b454bdc0feb59a23ebc05b89f05dfd75
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
// No sorcery shall prevail. // import java.util.*; import java.io.*; public class InVoker { //Variables static long mod = 1000000007; static long mod2 = 998244353; static FastReader inp= new FastReader(); static PrintWriter out= new PrintWriter(System.out); public static void main(String args[]) { InVoker g=new InVoker(); g.main(); out.close(); } int dp[][],n,a[]; //Main void main() { int t=inp.nextInt(); while(t-->0) { n=inp.nextInt(); a=new int[n]; input(a,n); sort(a); // can't hack now can you? XD dp=new int[n*2+1][n]; for(int i=0;i<=2*n;i++) Arrays.fill(dp[i], -1); out.println(go(1,0)); } } int go(int pos, int idx) { if(idx==n) { return 0; } if(pos==2*n+1) { return 100000; } if(dp[pos][idx]!=-1) return dp[pos][idx]; return dp[pos][idx]= Math.min( go(pos+1,idx+1)+Math.abs(a[idx]-pos), go(pos+1,idx)); } /********************************************************************************************************************************************************************************************************* * ti;. .:,:i: :,;;itt;. fDDEDDEEEEEEKEEEEEKEEEEEEEEEEEEEEEEE###WKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWW#WWWWWKKKKKEE :,:. f::::. .,ijLGDDDDDDDEEEEEEE* *ti;. .:,:i: .:,;itt;: GLDEEGEEEEEEEEEEEEEEEEEEDEEEEEEEEEEE#W#WEKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWWWWWKKKKKKG. .::. f:,...,ijLGDDDDDDDDEEEEEE * *ti;. .:,:i: :,;;iti, :fDDEEEEEEEEEEEEEEEKEEEEDEEEEEEEEEEEW##WEEEKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWWWWWWKKKKKKEG .::. .f,::,ijLGDDDDDDDDEEEEEE * *ti;. .:,:i: .,,;iti;. LDDEEEEEEEEEEKEEEEWEEEDDEEEEEEEEEEE#WWWEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWWKKKKKEDj .::. .:L;;ijfGDDDDDDDDDEEEEE * *ti;. .:,:i: .:,;;iii:LLDEEEEEEEEEEEKEEEEEEEEDEEEEEEEEEEEW#WWEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKWWKWWWWWWWWWWWWWWKKKKKKKEL .::. .:;LijLGGDDDDDDDDEEEEE * *ti;. .:,:;: :,;;ittfDEEEEEEEEEEEEEEEEKEEEGEEEEEEEEEEEKWWWEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWKKKKKKKELj .::. :,;jffGGDDDDDDDDDEEEE * *ti;. .:,:i: .,;;tGGDEEEEEEEEEEEKEEEKEEEDEEEEEEEEEEEEWWWEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWKWWWWWWKKKKKKKEEL .::. .:;itDGGDDDDDDDDDEEEE * *ti;. .:::;: :;ifDEEEEEEEEEEEEKEEEKEEEEEEEEEEEEEEEWWWEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKW#WWWKKKKKKKKEEf .::. :,itfGEDDDDDDDDDDDEE * *ti;. .:::;: :GGEEEEEEEEEEEKEKEEKEEEEEEEEEEEEEEEEWWEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKW#WKKKKKKKKKEEDG .::. .,;jfLGKDLDDDDDDEEDD * *ti;. .:::;: fDEEEEEEKKKKKKKKKEKEEEEEEEEEEEEEEE#WEEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKW#KKKKKKKKKKEEf .:::. .,;tfLGDEDDDDDDDDEEE * *ti;. :::;: fDEEEEEEKKKKKKKKKKWKEEEEEEEEEEEEEEEWKEEEEEEEEEEEEEEEEEEEEKEKKKKKKKKKKKKKKKKKKKKKKKKKKKKW##KKKKKKKKKEEft :::. .,;tfLGDDDKDDDDDDDDD * *ti;. .::;: fDEEEEEEKKKKKKKWKKKKKEEEEEEEEEEEEE#WEEWEEEEEDEEDEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKW#WKKKKKKKKEEGG :,:. .,;tfLGGDDDKDDDDDDDD * *ti;. .:.;: tGDEEEEKKKKKKKKKKKKKKKKKEEEEEEEEEEEWEEKWEEEEEEEDEEEEEEEEEEEEEEKEKKKKKKKKKKKKKKKKKKKKKKKKKKWWWKKKKKKKEEDf :::. .,;tfLGGDDDDEDDDDDDD * *ti;. .::;: fDEEEEEKKKKKKKKKKKWKKKKKKKKEEEEEEEWWEEWEEEEEEEEEEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKW##KKKKKKKEEEft.::. .,;tfLGGDDDDDDEDDDDD * *ti;. .:.;: tGDEEEKKKKKKKKKKKKKKKKKKKKKKEKEEEEE#EEWWEEEEEEEEEEEEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKW#WKKKKKKEEEGD:::. .,;tfLGGDDDDDDDEDDDD * *ti;. .:.,. LDEEEEKKKKKKKKKKWKWKKKKKKKKKKKKEEEKWEKW#EEEEEEEEEEEEEEEEKEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKW##KKKKKKEEEEf,,:. .,;tfLGGDDDDDDDDEDDD * *ti;. ..:.,. LGDEEEEKKKKKKKKKKWKKKKKKKKKKKKKKKKKWEEW#WEEEEEEEEEEEEEEEKEEEEEEEEEEEEEEEEEEEKEKKKKKKKKKKKKKKKK##KKKKKEEEEEfi;,. .,;tfLGGDDDDDDDDDKDD * *tt;. .:.,: jDEEEEKKKKKKKKKKWWKKKKKKKKKKKKKKKKKWKE#WWEEEEEEEEEEEEEEWEEEEEEEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKWWKKKKEEEEDfG;,: .,;tfLGGDDDDDDDDDDKD * *tii,. .:.,. tGDEEEEKKKKKKKKKKWWWKKKKKKKKKKKKKKKWKKWWWKEEEEEEEEEEEEEKEEEEEEEEEEEEEEEEEEEEEEEEEEEEEKKKKKKKKKKKW#KKKKEEEEDGGi;,. .,;tfLGGDDDDDDDDDDDE * *ti;;,:. .:.,: fDEEEEKKKKKKKKKKKWKKKKKKKKKKKKKKKKKWEK#WWKEEEEEEEEEEEEDEEEEEEEEEEEEEEGEEEEEEEEEEEEEEEEEEEKKKKKKKWWKKEEEEEEDDf;;;,. .,;tfLGGDDDDDDDDDDDD * *tii;,,:.. ...,. ;LEEEEEKKKKKKWKKKKWKKKKKKKKKKKKKKKKKEKKW#WEEEEEEEEEEEEEjEEEEEEEEEKEEEEGEEEEEEEEEKEEEEEEEEEEEEEEEEE#WKEEEEEEDDf;;;;,: .,itfLGGDDDDDDDDDDDD * *ti;,,,,,:. ...,. LDEEEEKKKKKKKKKKKWWWKKKKKKKKKKKKKKKWKK#W#WEEEEEEEEEEEDDLEEEEEEEEEWEEEEDEEEEEEEEEKEEEEEEEEEEEEEEEEEWWEEEEEEEDDfj,,,,,:. .,itfGGGDDDDDDDDDDDD * *tii,,,,::::. ...,: .fDEEEEKKKKKKWKKKKWWWKKKKKKKKKKKKKKKEKKW#WWEEEEEEEEEEEKiKEEKEEEEEEWEEEEDEEEEEEEEEEEEEEEEEEEEEEEEEEEWWEEEEEEEDDLD:::,,,:. .,ijfGGGDDDDDDDDDDDD * *ti;:::::::::.. .:.,: LDEEEEKKKKKKKWKKKKWWKKKKKKKKKKKKKKKKtKKWWWWKEEEEEEEEEDiiDEEEEEEEEWWEEEEEEDEEEEEEEEEEEEEEEEEEEEEEEEEEWKEEEEEDDDGL:. .:,,,: .,ijLGGGDDDDDDDDDDDD * *tt;. .::::::::.. ...,: :fDEEEKKKKKKKKKKKKWW#KKKKKKKKKKKKKKKKfKKWWWWKEEEEEEEEDti,DEKEEEEEEWWEEEDEEEEEEEEEKEEEEEEEEEEEEEDEEEEE#WEEEEEGGDGf:. .:,;,:. .,ijLGGDDDDDDDDDDDDD * *tt;. .:::::::.. ...,: GDEEEKKKKKKKKWKKKKWWWKKKWKKKKKKKWWWKDEKLWWWWKKEEEEEEDEi,LDEEEEEEEEWWEEEEEEEEEEEEEEEEEEEEEEEEEDEDEEEEEW#EEEEDDDDGf,. :,,,:...,ijLGGGDDDDDDDDDDDD * *tt;. .....::::.. ...,: fDEEEKKKKKKKKWKKKKWWWWKKKWKKKKKKKKKKfWKiWWW#KKEEEEEEEi;.EDfEEEDEEiWWEEEEEEEEEEEEDGKEEEEEEEEEEDEEEEEEEWWEEEEDDDGGLi. .,;,:::,ijLGGGDDDDDDDDDDDD * *tt;. ....:::::. ...,. iDEEEEKKKKKKKKWKKWKWWWWWKKWWWKKKKKKKKtWKt#WWWKKEEEEEDji..DDKDDEDEGiWKEEEEEEEEEEDDEjEEEEEEEEEEEDEEEEEEEKWKEEDDDDGGff. .:,;,,;ijLGGGDDDDDDDDDDDD * *tt;. ....::::.. .:.,: .LDEEEKKKKKKKKKKKKWWWWKWWWWWWWWWWKKKKWtKKiDWWWKKKEEEEKi:..DEDDDDDDiiWKEEEEEEEEEEDDEijDEEEEEKEEEEEEEEEEEEWWEEGDDDGGLG. .:,;;iijLGGGDDDDDDDDDDDD * *tt;. .....:::.. ...,. .fEEEEKKKKKKKKWKKKKWWWWWWWWWWWWWWKWKKKiKDiLWWWWKEEEEEi,..fD:DDDDDti;WEEEEEEEEEEKDDi:iDDEEEEWEEEEEEEEEEEE#WEEGDDDDGGG. :,iitjLGGGDDDDDDDDDDDD * *tti. .....:::.. ...,. GDEEEKKKKKKKKKWKKKWWW#WWWWWWWWWWWKWKKjiEjitWWWKKWEEEDi...DDLDDDDji;;WEEEEEEEEEEEDEj.iDDEEEEWEEEEEEEEEEEEWWEEDDDDDDGf. .,;tjfLGGDDDDDDDDDDDD * *tti. ....::::.. ...,. fEEEKKKKKKKKKKKKKKKW#WWWWWWWWWWWWWWWWtiEiiiWWWKKEWKEi....D.EDDDEi;.fWEEEEEEEEEEDDfL.;EDDEEEWEEEEEEEEEEEEWWEEEDDDDDGf. :;ijfLGGDDDDDDDDDDDD * *tti. ....::::.. ...,. LDEEEKKKKKKKKKKKKKKWWWWWWWWWWWWWWWW####WKiiiWWWKKKEEK,...:E:DDDEii..GWEEEEEEEEDWDDiL.,KDDEEEWEEEEEEEEEEEEWWKEEDDDDDGf: .,itfLGGDDDDDDDDDDDD * *tti. .....:::.. ...,. fDEEEKKKKKKKKKWKKKKWWWWWWWWWWWWW########WLiiWWWKKKEEjD...G,DDDDi;...EWEEEEEEEEDKDEii..LDDEEEWEEEEEEEEEEEEWWWEEDDDDDGfi .,;tfLGGGDDDDDDDDDDD * *tti. .....:::.. ...,. iGEEEKKKKKKKKKKWKKKKWWWWWWWWWWWW###########KiWWWKKEEE,.D..D.DDDii:...KKEEEEEEEEEDDj:...tEDEEEWEEEEEEEEEEEEWWWEEEDDDDDLL .,;tjLLGGDDDDDDDDDDD * *tti. ....::::......:. LEEEKKKKKKKKKKWWKKKWWW#KWWWWWWWW#####W####W##KWWKKEEL..:D.jjDDi;,....KKEEEEEEEDfDDi...:iKDEEEWKEEEEEEEEEEEWWWEEEEDDDDLG .,;tjLLGGDDDDDDDDDDD * *tti. ...::::::..,. :GEEEKKKKKKKKKKKKWWWWW##WWWWWWWWW##WKWK#W#W####WWKEEK.....G.DDti,.....KKEEEEEEDWGDf.,...iKDEEEWWEEEEEEEEEEEW#WEEEEEDDDGL .,;tjLLGGDDDDDDDDDDD * *tti. ....::::::,. GDEEKKKKKKKKKKKKKWWWW###WWWWWWWWWW#WWWK###W#####WKEKK.....jDDL;;......KKEEEEEEEEEDi.f...;KDEEEWWEEEEEEEEEEEWWWWEEEEEDDGf .,;tjLLGGDDDDDDDDDDD * *tti. ....:::,,. .LEEEKKKKKWKKKKKWWWWWW###WWWWWWWWWW#WWKW#WW##W#WWWKEKD:....:DD:;......;KEEEEEEEKiDD..f...,KKEEEWWEEEEEEEEEEEWWWWEEEEEDDDf .:;tjLLGGGDDDDDDDDDD * *tti. ...::,,,:. GDEEKKKKKKKKKKKKWWWWWWW#WWWWWWWWWWW#KjKWWWWWWWWWWWWEK.j,..;fD.;.......fKEEEEEDKG:Di..,....DKEEEWWEEEEEEKEKKKWWWWEEEEEEDDf .:;tjLLGGDDDDDDDDDDD * *jti. ...::,,,,:. .fEEEKKKKKWKKKKKKWWWWWWW#WWWWWWWWWWK#KKKWWWWWWWWWWWWWK..f:.:G.,:.......EKEEEEEKK;:E:.......fKEEEWWKEKEKKKKKKKW#WWEEEEEEDDf: .,;tfLLGGDDDDDDDDDDD * *tti. ...:,,,;;,: iDEEKKKKKWKKKKKKKWWWWWWW#WWWWWWWWWWK#WDKWWKKWWWWWWWWWE..;G:G..,........KKEEEEEKi.Gi..:.....tKEEKWWWKKKKKKKKKKW##WKEEEEEEDfi .,;tfLLGGGDDDDDDDDDD * *tti. ....::,,;;;,LEEKKKKKKWKKKKKWWWWWWW###WWWWWWWWWWKWWDKWEEEWKKWWWWWKKj.:LG..;.........EKEEEEKG;.G...;.....;KKEKWWWKKKKKKKKKKW##WWKEEEEEDfL .,;tfLGGGDDDDDDDDDDD * *jti. ...::::,;ijDEEKKKKKWKKKKKKWKWWWWW##WWWWWWWWWWWKK#KKGDGDWEEWKKWKKGE,.i;.:.........:EKEEEKE;.:L...j.....,KWEKWWWKKKKKKKKKK####WKKEEEEDLG .,;tfLGGGGDDDDDDDDDD * *jti. ...:...,,;GEEKKKKKWWKKKKKWWWWWWWW###WWWWWWWWWKKKWWKiLGGEDEDEKGKKiEG..;...........jKEEEKK;:.G....,.....:KKEWWWWKKKKKKKWKK####WKKKKEEEGL .,;tfLGGGGDDDDDDDDDD * *jti. ...:. .:,GEEKKKKKWKKKKKWWWWWWWW####WWWWWWWWWKKKWWKii;fDLGDK: EEi:E:.............EKEEKK;;..L...........KKKWWWWKKKKKKKWKK####WKKKWKEEDf .,;tfGGGGDDDDDDDDDDD * *jti. ...:. ,EEKKKKKWWKKKKKWWWWWWWWW###WWWWWWWWKKKKfWWLt;i,. fi EG..D:.............EKEKK;;..t....:.......KWKWWWWKKKKKKKWKK####WKKKWEEEDf:. .,;tfGGGGDDDDDDDDDDD * *jti. ...:. GEEKKKKKWKKKKKWWWWWWWWW####WWWWWWWKKKKKt;KKEfff .;t.................KKKKi;:..GtGGfG.......KWWWWWWKKKKKKKWKK###WWWKKKKEEEf,,: .,;tfGGGGDDDDDDDDDDD * *jti. ...:. GEKKKKKWWKKKKKWWWWWWWWWW##WWWWWWWKKKKKKt;EiKKKK, ...t................jEKKG;;..,.....,LGi....KWWWWWWKKKKKKWKKKW####WKKKKKEEL,,,:. .,;tfGGGDDDDDDDDDDDD * *jti. ...:. .GEEKKKKKWKKKKKWWWWWWWWWW###WWWWWWWKKKKKKtiE::tGG........................EEEj;;...,.........:D..DKWWWWWWKKKKK#KKW###W#WKKKKKEEfj:,,,:. .,;tfGGGDDDDDDDDDDDD * *jti. ...:. DEKKKKKWWKKKKKWWWWWWWWW####WWWWWWWKKKKKKiiE:::.::.......................EEi;;...j.....f......:iDKWWWWWWKKKKK#WW######WKKKKKEELG :,,,,:. .,;tfGGGDDDDDDDDDDDD * *jti. ...:. fEEKKKKWWKKKKWWWWWWWWWWW###WWWWWWWWKKKKKK;tE::..........................DD;.;,.::......;........EWWWWWWWKKKKW#WW#####WWKKKWKKELG .:,,,:::,;tfGGGDDDDDDDDDDDD * *jti. ...:. .DEKEKKKWWKKKKWWWWWWWWWWW###WWWWWWWWKKKKKE,iD::..........................D..,;.,;tLffi...........DWDWWWW#KKKWWWWW#####W#KKKWKEEGL .:,;,,,;tfGGGDDDDDDDDDDDD * *jti. ...:. ;EEKKKKWWKKKKKWWWWWW#WWWW####WWWWWWKKKKKEL:iD:..........................j ..;..;;:.....i,........DKtWWWWWKKWWWWWW#####WWWKKKEKEDf .:,;;;itfGGGDDDDDDDDDDDD * *jti. ...:. DEKKKKKWWKKKKWWWWWWW#WWWW####WWWWWWKKKKKEj:iG...............................:....................GKiWWWWWKKWW#WWW######WWKKKKKEEf .,;iitfGGGDDDDDDDDDDDD * *jti. ...:.:EKKKKKWWKKKKKWWWWWWW#WWW#####WWWWWKWKKKKEi:if:.................................iEKEKKKKKKDj......DKiWWWWWKWK##WW#######WWKKK:KEEL .:;itfGGGDDDDDDDDDDDD * *jji. ...:,DEEKKKWWWKWKKWWWWWWWWWWWW#####WWWWWWWKKKKEi:it..................................j. KKKKKKKKKKKf..DKiWWWWWKWW##WW#######WWKKK,KEEf .,;tfGGGDDDDDDDDDDDD * *jji. ..L:iDEEKKKWWKKKKKWWWWWWWWWWWW#####WWWWWKWKKKKKi.i;.................................. . KKKWWWWWWWWK..DGiWWWWWKK##WWW#####W#WWKKKjEKEL, .:;tfGGGDDDDDDDDDDDD * *jji. .f:::EEEKKKWWWKKKKKWWWWWWWWWWWW#####WWWWWKWKKKKK;.i,.................................:: KKEKWWWWWWfWK..EiiWWWWWKWW#WW##########KKKD,KELj .:;tfGGDDDDDDDDDDDDD * *jji. .t::::,DEEKKKWWKKKKWWWWWWWWW#WWWW#####WWWWKKWKKKEK;.i:.................................GDDEEEKKKWWWWWtWWD.E;iWWWWWW###WW#########WWKKK.EEDG .:;tfGGGDDDDDDDDDDDD * *jji. . j..::::EKEKKKWWWKKKKWWWWWWWWW#WWW######WWWWKKWKKKEK;.t:.................................ELLEDDEEEWWWWEtWK,.KiiWWWWWW###W##########WWKKK:EEEG .;tjfLLGDDDDDDDDDDDDDDD * *jji. i.::::::,EEEKKWWWKKKKKWWWWWWWWW#WWW#####WWWWWKWKKKKEE,.t..................................DfiEGDDDEEKKKttKWG.KiiWWWWW##WWW##########WWKKK:fEEL ,fGGGDDDDDDDDEEEDDDDDDDDDD * *jji. .;:..:::::DEEEKKWWWKKKKKWWWWWWWWW#WWWW####WWWWWWWKKKKED,.t..................................ifjDDGGEGDKK.ttKKE.DiWWWWW###WW##########WWWKKK:.KELiLGGGGDDDDDDDDDDDDEEEDDDDDDD * *jji. i.:.::::::,KEEKKWWWKKKKKKWWWWWWWWW#WWWW####WWWWWWWKKKKEL:.j..................................GGf,;ifLLED .iiKKi:fWWWWWW##W#W##########WWWKKK:.KKLGGDDDDDDDDDDDDDDDDEDDEEDDDDD * *jji. .j:.::::::::EEEKKKWWWKKKKKKWWWWWWWW##WWW#####WWWWWWWKKKKKf:.f..................................:EEfftf .,. ;iE,..jWWWWWWW###W############WWKKK,:KKGDDDDDDDDDDDDDDDDDDDDDDDEDDDD * *jji. .:.::::::::,,EEEKKWWWKKKKKKKWWWWWWWW##WWW#####WWWWWWWKKKKKt..G....................................EEELL; .j....tKWWWWWWW################WWWKKtfGKGEDDDDDDDDDDDDDDDDDDDDDDDEEDD * *jji. :...:::::::,,jEEKKWWWWKKKKKKWWWWWWWWW##KWW#####KWWWWWWKKKKEi..D....................................:jEEE.........;KKWWWWWWWW#WW##W##########WWKKDLGKEKDDDDDDDDDDDDDDDDDDDDDDDDDED * *jji. i:.::::::::,,,EEEKKWWWWKKKKKWWWWWWWWWW##WWW#####WWWWWWWKKKKKi..D......................................:::::......,KKKWWWWWWWWW#####W########WWWKKKGGKKEGGGGGGGGDDDDDDDDDDDDDDDDDDE * *jji. i..:::::::::,,tEEKKWWWWKKKKKWWWWWWWWWWW##WW######WWWWWWWKKKKKi..D......................................::::......:EKKKWWWWWWWWWWW##WW########W#WKKWGGKKGGGGGGGGGGGGGGGDDDDDDDDDDDDD * *jji. .:::::::::::,,,EEEKKWWWWKKKKKWWWWWWWWWWW##WW#####WWWWWWWWKKKKKi..D....................................:::::::::..tELii;KWWWWWWWWWW##WW######WWWWWWKWGGGKGGGGGGGGGGGGGGGGGGGGGGGGGGDG * *jjt. :.::::::::,,,,fEEKKWWWWKKKKKKWWWWWWWWWW###WW####WWWWWWW#WKKKKKi..D....................................:::::::.:.,;;;;;;,KKWWWWWWWWW#WW########WWWKKWGGGKGGGGGGGGGGGGGGGGGGGGGGGGGGGG * *jji. ;.::::::::,,,,;EEEKWWWWWKKKKKWWWWWWWWWWWW##WW###WKWWWWWK#WKKKKKi..G......................................:::::::,;;;;:...KKKWWWWWWWWWKWW#######WWWWKKGLGKDGGGGGGLLGGGGGGGGGGGGGGGGGGG * *jjt. f.:::::::::,,,,fEEKKWWWWWKKKKKWWWWWWWWWWW###WW##WKKWWWWWW#WKKKKK;.jt........i.............................:::::::;j;;....:E.KKKWWWWWWWKWW#####W#WWWWKKLLGWEEGGGGGLGGGGGGGGGGGGGGGGGGGG * *jjt. ...:::::::,,,,,;DEEKWWWWWKKKKKWWWWWWWWWWWW####WWWKKKWWWWWWWWKKKKK;.E;.........t.............................:::::ii;;.....D...KKWWWWWWWKWW#####WWEWWWKKGGGEKKGGGGGLGGGGGGGGGGGGGLGGGGGG * *fji. ;.:::::::,,,,,;LEEKKWWWWWKKKKKWWWWWWWWWWWW####KWKKKKWWWWWWWWKKKKKi.D;..........j.............................:::tt;,.....:.....KKWWWWWWKWWWW##WWWGWWWKKGGGGKEGGGGGGGGGGGGGGGGGGGLLGGGGL * *fji. t::::::::,,,,,,;EEEKWWWWWKKKKKKKWWWWWWWWWWW##WKWKKKKKWWWWWWWWKKKKKi:D;............j...........................::LL;,.............KKWWWWWKWWWWWWWWWGWWWKKGGGGKGGGGGGGGGGGGGGGGGGGGLLGGGGL * *fjt: .:::::::,,,,,,,DEEKWWWWWWKKKKKKKWWWWWWWWWWWWKKWKKKKKKWWWWK#WWKKKKWitE;........... ............................:G;;:...............KKKWWKKWWWWWWWWWGWWWKKGGGGWGGGGGGGGGGGGGGGGGGGGGGGGGGL * *fjji;:. .f:::::::,,,,,,,;EEEKWWWWWWKKKKKKWWWWWWWWWWWKKKKKKKKKKKWWKWWWWWKKKKWGKD;........................................L;;..................DKKWKKWWWWWWWWWGWWWKKDGGGKDGGGGGGGGGGGGGGGGGGGGGGGGGG * *fjjtii;,:. :::::::,,,,,,,;EEEKWWWWWWKKKKKKWWWWWWWWWWKKKKKKKKKKKKWWWWWW#WWKKKKWiEj;......................................:i,;....,...............;KKEKWWWWWWWWWGKWWKKDDGGDEGGGDGGGGGDGGGGGGGGGGGGGGGG * *fjtiiiii;;:. j::::::,,,,,,,;;EEEKWWWWW#KKKKKWWWWWWWWWKKKKKKWKKKKKKKWWWWWWWWWKKKKWtEL;:....................................;;;:...,;j................:KEEWWWWWWWWWDDWWKKDDDDDKDDDDDDDDDDDDDDDGGGGGGGGGGG * *fjti;;iiii;;,:::::::,,,,,,,,;EEEKWWWWWWWKKKKWWWWWWWWKKKKKKKWKKKKKKKWWWWWWW#W#KKKKWEEii;...................................f;:....,;L...................EEKWWWWWWWWDDWWKKDDDDDKEDDDDDDDDDDDDDDDDDDDDDGGGG * *fjt,,,;;;;ii;f::::::,,,,,,,;;EEKWWWWWWWKKKKKWWWKWWKKKKKKKKKKKKKKKKKWWWWWWW#W#KKKKWKEij;:...............................:G;,.....,;f....................:tKKWWWWWWWDDWWKKDDDDDKKDDDDDDDDDDDDDDDDDDDDDDDDD * *jjt. ..:,;;;;,::::,,,,,,,,;;GEEWWWWWWWWKKKKWKKWKKKKKKKKKKKKKKKKKKKKWWWWWWW#W#KKKKWEDi;j;............................,Li;L;;;..,;;f........................KKKKWWWKDDWWKKDDDGDKKGGGGGGGGDGDDDDDDDDDDDDDDD * *fjt. .:,,,:::::,,,,,,,;;;EEKWWWWWWWKKKKKKWKKKKKKKKKKKKKKKKKKKKKWKKKWKW##W#KKKKWEti;;G;........................tEEEL;;;;;;;;;;L..........................DKKKKKEDDWWKEDGftiLE;;;;itjLGGGGGGDDDDDDDDDDD * *fjt. .j::::,,,,,,,;;;DEEWWWWWWWWKKKKWKKKKKKKKKKKKKKKKKKKKKKKWKKWWWK##W#KKKKKEii;;;L;...................iDEEEEEEKKi;j;;;;jD.....:......................,KKKKDGGEKKE:::::;E::::::::::,tLGGDDDDDDDDDD * *fjt. .;:::,,,,,,,;;;;EEKWWWWWWWWKWKKKKKKKKKKKKKKKWKKKKKKKKKKWKKWWWW#WW#KKKKKKii;;;;f;.............:tDEEEEEKKKKKKKKEti;;;L...............................EEKf;:iKKE::::::E::::::::::::::ifDDDDDDDDD * *fjt: :::,,,,,,,,;;;DEEWWWWWWWWWEKKKKKKKKKKKKKKKKKKKKKKKKKKWWKKWWWW####KKKKKEiii;;;;f,.........iDEEEEKKKKKKKKKKKKKKKf;iG......i..........................fK::::KKE::::::E::::::::::::::::,tGGDDDDD * *fjt: t:::,,,,,,;;;;iDEKWWWWWWKEKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWKKWWWW####WKKKKLiii;;;;;L,....,Li;EDEEEEKKKKKKKKKKKKKKKKiG......;:...........................:i:::KKE:::::,E,::::::::::::::::::iGDDDD * *jjt. f::,,,,,,,;;;;GEEWWWWKEEKEKKKKKKKKKKKKKKKKWKKKKKKKKKKKWWKWWWWW###WWKKKKiii;;;;;;;G,;L;;iiEEEEEEEKKKKKKKKKKKKKWWKE......;t.........:....................j::KEE:,::,,D,,::::,,,,,,:::::::::tDDD * *fjt:. ,::,,,,,,,;;;;EEWWKEEEEEEKKKKKKKKKKKKKKKKWKKKKKKKKKKKWWKKWWWWW#W#KKKKKKiiiiii;;;;;i;;iiiEEKEEKKWKKKKKKKWKKKKKWWWGi;...;t......,;;;;,....................:,EEE,,,,,,D,,,,,,,,,,,,,,,,::,::::tG * *fjt:. ,::,,,,,,,;;;;DEKEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWW#W#KKKKKKiiiii;;i;;;;;iiiKKKEKKKKWWKWWWWWWKKKKWWWWW;;;:;L.....;;;;;;;;;....................,KEE,,,,,,E,,,,,,,,,,,,,,,,,,,,,,,,; * *fjt:. f:,,,,,,,;;;;jEDEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWW#W##KKKKKKiiiiiiii;i;;iiiEKKKKKKKKWKWWWWWWWWKKKWWWWWKi;;i.....,jEEfGi;;;;;...................EED,,,,,,E,,,,,,,,,,,,,,,,,,,,,,,,, * *fjt:. .f::,,,,,,;;jEEDEEEEEEEEEEKKKKKKKKKKKKKKKWKKKKKKKKKKKKKWWWKWWWWW###KKKKKLiiiiiiiiiiiiiiEEKKKKKKKKWWWWWWWWWWWWKWWWWWWGi;i;,..;jDDDKEGi;;;;;;:................EED,,,,,,D,,,,,,,,,,,,,,,,,,,,,,,,, * *fjt:. .. ;::,,,,,;;EDDEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWKKW#WW####KWKKKiiiiiiiiiiiiijKKKKKKKKKKWWWWWWWWWWWWWWWWWWWWWt;i;;;;i;DDDDDDGi;;;;;;;;:.............EDf;,,,;,G;;;;;;;;;;;;;;;,,,,,,,,,, * *fjt:......:,,,,,,;LDDDEEEEEEEEEEEKKKKKKKKKKKKKKKKWKKKKKKKKKKKKKWWWWKWWWW####KKKKKiiiiiiiiiiijKEKKWKKKKKKKWWWWWWWWWWWWWWWWWWWWWWiLiii;i;DEEEEDDE;i;;;;;;;;;:..........EDi,;;;;;L;;;;;;;;;;;;;;;;;;,,,,,,, * *fjt:......:,,,,,;EDDDEEKEEEEEEEEEKKKKKKKKKKKKKKKWKKKKKKKKKKKKKKWWWWKKWWW##W#KWKKWEiiiiiijGKKKKKWWKKKKKKKKWWWWWWWWWWWWWWWWWWWWWWKi;iiiiDDEEEEEEDEi;;;;;;;;;;;;;,:.....ED;;;;;;;j;;;;;;;;;;;;;;;;;;;;;;;,, * *fjt:.....t,,,,,;DDDDEEEKEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWKKKWWWW##WKWKKWKiiiKKKKKKKKKWWKKKKKKKKWWWWWWWWWWWWWWW#WWWWWWWWWiiiiifLEEEEEEEEDi;i;;;;;;;;;;;;.....DD;;;;;;;i;;;;;;;;;;;;;;;;;;;;;;;;; * *fjt:.....G,,,,,GDDDEEEEEEEEEEEEKKKKKKKKKKKKKKKKWKKKKKKKKKKKKKKKWWWKKKWWW###WKWKKWKitKKKKKKKKKWKKKKKKKKKKWWWWWWWWWWWWWW###WWWWWWWWEiiiiiiiEEEEEEEEDGiiii;;;;;;;;;.....GD;;;;;;;i;;;;;;;;;;;;;;;;;;;;;;;;; * *fjt:.....L,,,,;GDDDEEEEEEEEEEKEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWDGWWW###KKWWKWKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWW####WWWWWWWWWiiiiiiiiEEEEEEEEEEDi;i;;;;;;;;.....Lj;;;;;;i;iiiiii;;;;;;ii;;;;;;;;;;; * ***********************************************************************************************************************************************************************************************************/ void sort(int a[]) { ArrayList<Integer> list=new ArrayList<>(); for(int x: a) list.add(x); Collections.sort(list); for(int i=0;i<a.length;i++) a[i]=list.get(i); } void sort(long a[]) { ArrayList<Long> list=new ArrayList<>(); for(long x: a) list.add(x); Collections.sort(list); for(int i=0;i<a.length;i++) a[i]=list.get(i); } void ruffleSort(int a[]) { Random rand=new Random(); int n=a.length; for(int i=0;i<n;i++) { int j=rand.nextInt(n); int temp=a[i]; a[i]=a[j]; a[j]=temp; } Arrays.sort(a); } void ruffleSort(long a[]) { Random rand=new Random(); int n=a.length; for(int i=0;i<n;i++) { int j=rand.nextInt(n); long temp=a[i]; a[i]=a[j]; a[j]=temp; } Arrays.sort(a); } 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 s=""; try { s=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } } long fact[]; long invFact[]; void init(int n) { fact=new long[n+1]; invFact=new long[n+1]; fact[0]=1; for(int i=1;i<=n;i++) { fact[i]=mul(i,fact[i-1]); } invFact[n]=power(fact[n],mod-2); for(int i=n-1;i>=0;i--) { invFact[i]=mul(invFact[i+1],i+1); } } long nCr(int n, int r) { if(n<r || r<0) return 0; return mul(fact[n],mul(invFact[r],invFact[n-r])); } long mul(long a, long b) { return a*b%mod; } long add(long a, long b) { return (a+b)%mod; } long power(long x, long y) { long gg=1; while(y>0) { if(y%2==1) gg=mul(gg,x); x=mul(x,x); y/=2; } return gg; } // Functions static long gcd(long a, long b) { return b==0?a:gcd(b,a%b); } static int gcd(int a, int b) { return b==0?a:gcd(b,a%b); } void print(int a[]) { int n=a.length; for(int i=0;i<n;i++) out.print(a[i]+" "); } void print(long a[]) { int n=a.length; for(int i=0;i<n;i++) out.print(a[i]+" "); } //Input Arrays static void input(long a[], int n) { for(int i=0;i<n;i++) { a[i]=inp.nextLong(); } } static void input(int a[], int n) { for(int i=0;i<n;i++) { a[i]=inp.nextInt(); } } static void input(String s[],int n) { for(int i=0;i<n;i++) { s[i]=inp.next(); } } static void input(int a[][], int n, int m) { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { a[i][j]=inp.nextInt(); } } } static void input(long a[][], int n, int m) { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { a[i][j]=inp.nextLong(); } } } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
5655c1dc6d337e725accd0037c910a21
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
/* * Date Created : 28/10/2020 * Have A Good Day ! */ import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.util.Random; import java.io.IOException; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Arpit */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); OutputWriter out = new OutputWriter(outputStream); CChefMonocarp solver = new CChefMonocarp(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class CChefMonocarp { int[] arr; int[][] dp; int REC(int idx, int time) { if (idx >= dp.length) return 0; if (time >= dp[0].length) return (Integer.MAX_VALUE >> 1); if (dp[idx][time] != -1) return dp[idx][time]; return dp[idx][time] = Math.min(REC(idx, time + 1), (Math.abs(arr[idx] - time) + REC(idx + 1, time + 1))); } void shuffleSort(int[] arr) { int n = arr.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { int tmp = arr[i]; int randomPos = i + rnd.nextInt(n - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } Arrays.sort(arr); } public void solve(int testNumber, FastReader r, OutputWriter out) { int n = r.nextInt(); arr = r.nextIntArray(n); dp = new int[n][401]; shuffleSort(arr); for (int i = 0; i < n; i++) Arrays.fill(dp[i], -1); out.println(REC(0, 1)); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return nextString(); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { writer.print(objects[i]); if (i != objects.length - 1) writer.print(" "); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
7dcc4379d1a9d5ee9d1d1fb80a873697
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
import java.util.*; import java.io.*; public class C_1437 { static int n, m; static int[][] dist; static Pair[] array; static int[][] memo; public static int dp(int i, int prev) { if(i == m) return 0; if(memo[i][prev] != -1) return memo[i][prev]; if(prev >= array[i].t) return memo[i][prev] = (prev - array[i].t) * array[i].c + (array[i].c * (array[i].c + 1) / 2) + dp(i + 1, prev + array[i].c); else { int min = Integer.MAX_VALUE; for(int j = prev + 1; j <= array[i].t; j++) min = Math.min(min, dist[i][j] + dp(i + 1, j + array[i].c - 1)); return memo[i][prev] = min; } } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0) { n = sc.nextInt(); int[] freq = new int[n + 1]; m = 0; for(int i = 0; i < n; i++) { int x = sc.nextInt(); freq[x]++; if(freq[x] == 1) m++; } int uu = 0; array = new Pair[m]; for(int i = 1; i <= n; i++) if(freq[i] > 0) array[uu++] = new Pair(i, freq[i]); Arrays.sort(array); dist = new int[m][n + 1]; for(int i = 0; i < m; i++) { for(int j = 0; j <= n; j++) { int c = 0; for(int k = j; k < j + array[i].c; k++) c += Math.abs(k - array[i].t); dist[i][j] = c; } } memo = new int[m][2 * n + 1]; for(int[] i : memo) Arrays.fill(i, -1); pw.println(dp(0, 0)); } pw.flush(); } public static class Pair implements Comparable<Pair> { int t, c; public Pair(int t, int c) { this.t = t; this.c = c; } public int compareTo(Pair p) { return this.t - p.t; } } public 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 int[] nextIntArray(int n) throws IOException { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] array = new Integer[n]; for (int i = 0; i < n; i++) array[i] = new Integer(nextInt()); return array; } public long[] nextLongArray(int n) throws IOException { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } public double[] nextDoubleArray(int n) throws IOException { double[] array = new double[n]; for (int i = 0; i < n; i++) array[i] = nextDouble(); return array; } public static int[] shuffle(int[] a) { int n = a.length; Random rand = new Random(); for (int i = 0; i < n; i++) { int tmpIdx = rand.nextInt(n); int tmp = a[i]; a[i] = a[tmpIdx]; a[tmpIdx] = tmp; } return a; } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
9de4ab21d12160d19b3ba9e587765e49
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
import java.util.*; import java.io.*; public class Main { static Scanner sc = new Scanner(System.in); static PrintWriter out = new PrintWriter(System.out); static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); long mod = 998244353; int[] id, size; public static void main(String[] args){ Main main = new Main(); int t = sc.nextInt(); while(t-->0){ main.solve(); } out.flush(); } void solve(){ int n = sc.nextInt(); int maxt = 2*n; int[] t = new int[n+1]; for(int i=1; i<=n; i++) t[i] = sc.nextInt(); Arrays.sort(t); long[][] dp = new long[n+1][maxt+1]; for(long[] arr:dp) Arrays.fill(arr, n*n); dp[0][0] = 0; for(int i=1; i<=n; i++){ for(int j=i; j<=n+i; j++){ for(int k=j-1; k>=i-1; k--){ dp[i][j] = Math.min(dp[i][j], dp[i-1][k]+Math.abs(j-t[i])); } } } long ans = Integer.MAX_VALUE; for(int j=n; j<=maxt; j++) ans = Math.min(ans, dp[n][j]); out.println(ans); } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
eb15ed743a939ff72ee5dbb076049126
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
// package NickMikeMurderers; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class ChefMonocarp { public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int test=Integer.parseInt(br.readLine()); StringBuilder print=new StringBuilder(); while(test--!=0){ int n=Integer.parseInt(br.readLine()); StringTokenizer st=new StringTokenizer(br.readLine()); int a[]=new int[n+1]; for(int i=1;i<=n;i++){ a[i]=Integer.parseInt(st.nextToken()); } Arrays.sort(a); int dp[][]=new int[n+1][2*n+1]; for(int i=1;i<=n;i++){ for(int j=1;j<=2*n;j++){ dp[i][j]=Integer.MAX_VALUE; } } for(int i=1;i<=n;i++){ for(int j=i;j<=2*n;j++){ dp[i][j]=Math.min(dp[i][j],dp[i-1][j-1]+Math.abs(j-a[i])); if(j!=1) dp[i][j]=Math.min(dp[i][j],dp[i][j-1]); } } int ans=Integer.MAX_VALUE; for(int j=n;j<=2*n;j++){ ans=Math.min(ans,dp[n][j]); } print.append(ans).append("\n"); } System.out.print(print.toString()); } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
bcd054f68bc1a3268c95d0ed430955d9
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
import java.io.*; import java.util.*; public class A { static int n; static int[] arr; static char[] s; public static void main(String[] args) throws IOException { Flash f = new Flash(); int T = f.ni(); for(int tc = 1; tc <= T; tc++){ n = f.ni(); arr = f.arr(n); sop(fn()); } } static long fn() { sort(arr); long[][] dp = new long[n+1][2*n+1]; for(int i = 1; i <= n; i++){ for(int t = 1; t <= 2*n; t++){ dp[i][t] = Integer.MAX_VALUE; if(i == 1){ dp[i][t] = Math.abs(arr[i-1] - t); continue; } for(int k = 1; k < t; k++){ dp[i][t] = Math.min(dp[i][t], dp[i-1][k] + Math.abs(arr[i-1]-t)); } } } long ans = Integer.MAX_VALUE; for(int i = 1; i <= 2*n; i++) ans = Math.min(ans, dp[n][i]); return ans; } static void sort(int[] a){ List<Integer> A = new ArrayList<>(); for(int i : a) A.add(i); Collections.sort(A); for(int i = 0; i < A.size(); i++) a[i] = A.get(i); } static int swap(int itself, int dummy){ return itself; } static void sop(Object o){ System.out.println(o); } static void print(int[] a){ StringBuilder sb = new StringBuilder(); for(int i = 0; i < a.length; i++) sb.append(a[i] + " "); System.out.println(sb); } static class Flash { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next(){ while(!st.hasMoreTokens()){ try{ st = new StringTokenizer(br.readLine()); }catch(IOException e){ e.printStackTrace(); } } return st.nextToken(); } String ns(){ String s = new String(); try{ s = br.readLine().trim(); }catch(IOException e){ e.printStackTrace(); } return s; } int ni(){ return Integer.parseInt(next()); } int[] arr(int n){ int[] a = new int[n]; for(int i = 0; i < n; i++) a[i] = ni(); return a; } long nl(){ return Long.parseLong(next()); } double nd(){ return Double.parseDouble(next()); } } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
ff1a6106076e2865c7cfd2156dc11bf0
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
import java.io.*; import java.util.*; public class A { static int n; static int[] arr; static char[] s; public static void main(String[] args) throws IOException { Flash f = new Flash(); int T = f.ni(); for(int tc = 1; tc <= T; tc++){ n = f.ni(); arr = f.arr(n); sop(fn()); } } static long fn() { sort(arr); long[][] dp = new long[n+1][2*n+1]; for(int i = 1; i <= n; i++){ for(int j = 0; j <= 2*n; j++){ dp[i][j] = Integer.MAX_VALUE; } } for(int i = 1; i <= n; i++){ for(int t = 1; t <= 2*n; t++){ dp[i][t] = Math.min(dp[i][t], dp[i-1][t-1]+Math.abs(arr[i-1]-t)); dp[i][t] = Math.min(dp[i][t], dp[i][t-1]); } } //for(int i = 0; i <= n; i++) sop(Arrays.toString(dp[i])); return dp[n][2*n]; } static void sort(int[] a){ List<Integer> A = new ArrayList<>(); for(int i : a) A.add(i); Collections.sort(A); for(int i = 0; i < A.size(); i++) a[i] = A.get(i); } static int swap(int itself, int dummy){ return itself; } static void sop(Object o){ System.out.println(o); } static void print(int[] a){ StringBuilder sb = new StringBuilder(); for(int i = 0; i < a.length; i++) sb.append(a[i] + " "); System.out.println(sb); } static class Flash { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next(){ while(!st.hasMoreTokens()){ try{ st = new StringTokenizer(br.readLine()); }catch(IOException e){ e.printStackTrace(); } } return st.nextToken(); } String ns(){ String s = new String(); try{ s = br.readLine().trim(); }catch(IOException e){ e.printStackTrace(); } return s; } int ni(){ return Integer.parseInt(next()); } int[] arr(int n){ int[] a = new int[n]; for(int i = 0; i < n; i++) a[i] = ni(); return a; } long nl(){ return Long.parseLong(next()); } double nd(){ return Double.parseDouble(next()); } } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
d0afa731ba5e184ac5ba1d48dd8176a4
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
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(){ for(int q=ni();q>0;q--){ work(); } out.flush(); } long mod=1000000007; long gcd(long a,long b) { return a==0?b:gcd(b%a,a); } void work() { int n=ni(); int[] A=nia(n); int[][] dp=new int[n+1][n*2+2]; for(int[] d:dp)Arrays.fill(d,9999999); for(int j=0;j<2*n+2;j++){ dp[0][j]=0; } Arrays.sort(A); for(int i=1;i<=n;i++){ for(int j=i;j<2*n+2;j++){ dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(A[i-1]-j)); } } int ret=9999999; for(int j=n;j<2*n+2;j++){ ret=Math.min(ret,dp[n][j]); } out.println(ret); } private ArrayList<long[]>[] ngw(int n, int m) { ArrayList<long[]>[] graph=(ArrayList<long[]>[])new ArrayList[n]; for(int i=0;i<n;i++) { graph[i]=new ArrayList<>(); } for(int i=1;i<=m;i++) { long s=in.nextLong()-1,e=in.nextLong()-1,w=in.nextLong(); graph[(int)s].add(new long[] {e,w,i}); graph[(int)e].add(new long[] {s,w}); } return graph; } private int ni() { return in.nextInt(); } private long nl() { return in.nextLong(); } private String ns() { return in.next(); } private long[] na(int n) { long[] A=new long[n]; for(int i=0;i<n;i++) { A[i]=in.nextLong(); } return A; } private int[] nia(int n) { int[] A=new int[n]; for(int i=0;i<n;i++) { A[i]=in.nextInt(); } return A; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } public String next() { while(st==null || !st.hasMoreElements())//回车,空行情况 { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
db4ced93aceb95c09a313aa1f44bd9d9
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class C { private static class Solver { private void solve() throws Exception { int t = in.nextInt(); while (t-- > 0) { solveOne(); } } private void solveOne() throws Exception { int n = in.nextInt(); int[] t = new int[n + 1]; for (int i = 1; i <= n; i++) { t[i] = in.nextInt(); } Arrays.sort(t, 1, n + 1); int MS = 401; int[][] d = new int[n + 1][MS]; for (int i = 1; i <= n; i++) { Arrays.fill(d[i], -1); } int ans = Integer.MAX_VALUE; for (int i = 0; i <= n; i++) { int reach = -1; for (int j = 0; j < MS; j++) { if (reach >= 0 && reach < d[i][j]) { d[i][j] = reach; } else if (reach == -1 || reach > d[i][j]) { reach = d[i][j]; } } for (int j = 0; j < MS; j++) { if (d[i][j] >= 0) { int c = j + 1; int acc = 0; for (int k = i + 1; k <= n && c < MS; k++, c++) { acc += Math.abs(c - t[k]); d[k][c] = Math.min( d[k][c] == -1 ? Integer.MAX_VALUE : d[k][c], d[i][j] + acc); if (k == n) { ans = Math.min(ans, d[k][c]); } } } } } out.println(ans); } } //-------------------------------------------------------- private static final MyScanner in = new MyScanner(System.in); private static final PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) throws Exception { Solver solver = new Solver(); solver.solve(); out.close(); } public static class MyScanner { private final BufferedReader br; private StringTokenizer st; public MyScanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } String next() throws IOException { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
26c8e05d1e919bdd80cfcf6a9e388778
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class C_v2 { //-------------------------------------------------------- private static final MyScanner in = new MyScanner(System.in); private static final PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) throws Exception { Solver solver = new Solver(); solver.solve(); out.close(); } private static class Solver { private void solve() throws Exception { int t = in.nextInt(); while (t-- > 0) { solveOne(); } } private void solveOne() throws Exception { int n = in.nextInt(); int[] t = new int[n + 1]; for (int i = 1; i <= n; i++) { t[i] = in.nextInt(); } Arrays.sort(t, 1, n + 1); int MS = 401; int[][] d = new int[n + 1][MS]; for (int i = 0; i <= n; i++) { Arrays.fill(d[i], Integer.MAX_VALUE / 2); } d[0][0] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j + 1 < MS; j++) { d[i][j + 1] = Math.min(d[i][j], d[i][j + 1]); d[i + 1][j + 1] = Math.min(d[i + 1][j + 1], d[i][j] + Math.abs(j + 1 - t[i + 1])); } } int ans = Integer.MAX_VALUE; for (int j = 0; j < MS; j++) { ans = Math.min(ans, d[n][j]); } out.println(ans); } } public static class MyScanner { private final BufferedReader br; private StringTokenizer st; public MyScanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } String next() throws IOException { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
609b2c61cd7cbc440cf4d69b94d26019
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
import java.io.*; import java.util.*; public class Solution{ static int[][] dp; public static void main(String[] args){ try { PrintWriter out=new PrintWriter(System.out,true); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); for(int i=0;i<t;i++){ int n = Integer.parseInt(br.readLine()); String[] ss = br.readLine().split(" "); int[] arr = new int[n]; for(int j=0;j<n;j++){ arr[j] = Integer.parseInt(ss[j]); } shuffle(arr); Arrays.sort(arr); dp = new int[n][2*n+1]; for(int j=0;j<n;j++){ for(int k=0;k<=2*n;k++){ dp[j][k]=-1; } } int ans = solve(n-1,2*n,arr); out.println(ans); } out.close(); } catch (Exception e) { System.out.println("kkkk "+ e.getMessage()); } } static int solve(int i,int m,int[] arr){ if(dp[i][m]!=-1){ return dp[i][m]; } if(m==0){ return 100000; } if(i==0){ dp[i][m] = Math.min(Math.abs(arr[i]-m),solve(0,m-1,arr)); return dp[i][m]; } dp[i][m] = Math.min(Math.abs(arr[i]-m)+solve(i-1,m-1,arr),solve(i,m-1,arr)); return dp[i][m]; } static void sieveOfEratosthenes(int n) { boolean[] prime = new boolean[n+1]; for(int i=0;i<n;i++) prime[i] = true; for(int p = 2; p*p <=n; p++) { if(prime[p]) { for(int i = p*p; i <= n; i += p) prime[i] = false; } } } static void shuffle(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } } static int lcm(int a,int b){ long c=a*b; return (int)(c/hcf(a,b)); } static int hcf(int a,int b){ if(a==0){ return b; } if(b==0){ return a; } if(a>b) return hcf(a%b,b); return hcf(a,b%a); } static int modInverse(int x,int m){ return power(x,m-2,m); } static int power(int x, int y, int m) { if (y == 0) return 1; long p = power(x, y / 2, m) % m; p = (p * p) % m; return (int)((y%2==0)? p : (x*p)%m); } static class pair{ int a,b; public pair(int a,int b){ this.a=a; this.b=b; } } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
01631f6c3d686267f4c84bccb65428b5
train_002.jsonl
1603809300
Chef Monocarp has just put $$$n$$$ dishes into an oven. He knows that the $$$i$$$-th dish has its optimal cooking time equal to $$$t_i$$$ minutes.At any positive integer minute $$$T$$$ Monocarp can put no more than one dish out of the oven. If the $$$i$$$-th dish is put out at some minute $$$T$$$, then its unpleasant value is $$$|T - t_i|$$$ — the absolute difference between $$$T$$$ and $$$t_i$$$. Once the dish is out of the oven, it can't go back in.Monocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?
256 megabytes
import java.util.*; public class MyClass { static int dp[][]; static int find(int a[],int i,int t) { if(i==a.length) return 0; if(t>=2*a.length) return 500000; if(dp[i][t]!=-1) return dp[i][t]; return dp[i][t] = Math.min(find(a,i,t+1),find(a,i+1,t+1)+Math.abs(a[i]-t)); } public static void main(String args[]) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t-->0) { int n =s.nextInt(); int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = s.nextInt(); Arrays.sort(a); dp = new int[n+1][500]; for(int i=0;i<=n;i++) Arrays.fill(dp[i],-1); System.out.println(find(a,0,1)); } } }
Java
["6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13"]
2 seconds
["4\n12\n0\n0\n2\n21"]
NoteIn the first example Monocarp can put out the dishes at minutes $$$3, 1, 5, 4, 6, 2$$$. That way the total unpleasant value will be $$$|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$$$.In the second example Monocarp can put out the dishes at minutes $$$4, 5, 6, 7, 8, 9, 10$$$.In the third example Monocarp can put out the dish at minute $$$1$$$.In the fourth example Monocarp can put out the dishes at minutes $$$5, 1, 2, 4, 3$$$.In the fifth example Monocarp can put out the dishes at minutes $$$1, 3, 4, 5$$$.
Java 8
standard input
[ "dp", "greedy", "flows", "math", "graph matchings", "sortings" ]
27998621de63e50a7d89cb1c1e30f67c
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of testcases. Then $$$q$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of dishes in the oven. The second line of the testcase contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le n$$$) — the optimal cooking time for each dish. The sum of $$$n$$$ over all $$$q$$$ testcases doesn't exceed $$$200$$$.
1,800
Print a single integer for each testcase — the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.
standard output
PASSED
44b4aca0476570cbc4f5b704b2f85a1a
train_002.jsonl
1520696100
Alice has a string consisting of characters 'A', 'B' and 'C'. Bob can use the following transitions on any substring of our string in any order any number of times: A BC B AC C AB AAA empty string Note that a substring is one or more consecutive characters. For given queries, determine whether it is possible to obtain the target string from source.
256 megabytes
import java.io.*; import java.util.*; public class Main extends Thread { private static FastScanner scanner = new FastScanner(System.in); private static PrintWriter writer = new PrintWriter(System.out); // private static Scanner scanner = new Scanner(System.in); private static char[] S; private static char[] T; private static int[] bSCount; private static int[] aSLastPos; private static int[] bTCount; private static int[] aTLastPos; //AAAAAAABBBAABBBB AB BBAAAAAABBBBBA AAAABB public static void main(String[] args) throws IOException { S = scanner.next().toCharArray(); T = scanner.next().toCharArray(); bSCount = preComputeB(S); aSLastPos = preComputeA(S); bTCount = preComputeB(T); aTLastPos = preComputeA(T); int n = scanner.nextInt(); StringBuilder ans = new StringBuilder(); for (int i = 0; i < n; i++) { ans.append( solve(scanner.nextInt() - 1, scanner.nextInt() - 1, scanner.nextInt() - 1, scanner.nextInt() - 1) ? 1 : 0); } System.out.println(ans); } private static int[] preComputeA(char[] s) { int aPos = -1; int[] ans = new int[s.length]; for (int i = 0; i < s.length; i++) { if (s[i] == 'A') { ans[i] = aPos == -1 ? aPos = i : aPos; } else { ans[i] = aPos = -1; } } return ans; } private static int[] preComputeB(char[] s) { int[] ans = new int[s.length]; ans[0] = s[0] == 'B' || s[0] == 'C' ? 1 : 0; for (int i = 1; i < s.length; i++) { ans[i] = ans[i - 1] + (s[i] == 'B' || s[i] == 'C' ? 1 : 0); } return ans; } private static boolean solve(int s1, int s2, int t1, int t2) { int bS = countB(bSCount, s1, s2); int aS = countA(aSLastPos, s1, s2); int bT = countB(bTCount, t1, t2); int aT = countA(aTLastPos, t1, t2); if (aS < aT || bS > bT || (bT - bS) % 2 != 0) return false; if (bS == bT) { return (aS - aT) % 3 == 0; } else if (bS == 0) { return aS > aT; } else if (bS < bT) { return true; } return false; } private static int countA(int[] a, int s1, int s2) { if (a[s2] == -1) return 0; return s2 - Integer.max(a[s2], s1) + 1; } private static int countB(int[] s, int s1, int s2) { return s[s2] - (s1 == 0 ? 0 : s[s1 - 1]); } static class FastScanner { StringTokenizer st; BufferedReader br; FastScanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } boolean hasNext() throws IOException { return br.ready() || (st != null && st.hasMoreTokens()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next().replace(',', '.')); } String nextLine() throws IOException { return br.readLine(); } boolean hasNextLine() throws IOException { return br.ready(); } } static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> { public U first; public V second; public Pair(U first, V second) { this.first = first; this.second = second; } public int hashCode() { return (first == null ? 0 : first.hashCode() * 31) + (second == null ? 0 : second.hashCode()); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<U, V> p = (Pair<U, V>) o; return (first == null ? p.first == null : first.equals(p.first)) && (second == null ? p.second == null : second.equals(p.second)); } public int compareTo(Pair<U, V> b) { int cmpU = first.compareTo(b.first); return cmpU != 0 ? cmpU : second.compareTo(b.second); } } }
Java
["AABCCBAAB\nABCB\n5\n1 3 1 2\n2 2 2 4\n7 9 1 1\n3 4 2 3\n4 5 1 3"]
2 seconds
["10011"]
NoteIn the first query we can achieve the result, for instance, by using transitions .The third query asks for changing AAB to A — but in this case we are not able to get rid of the character 'B'.
Java 8
standard input
[ "constructive algorithms", "implementation", "strings" ]
98e3182f047a7e7b10be7f207b219267
The first line contains a string S (1 ≤ |S| ≤ 105). The second line contains a string T (1 ≤ |T| ≤ 105), each of these strings consists only of uppercase English letters 'A', 'B' and 'C'. The third line contains the number of queries Q (1 ≤ Q ≤ 105). The following Q lines describe queries. The i-th of these lines contains four space separated integers ai, bi, ci, di. These represent the i-th query: is it possible to create T[ci..di] from S[ai..bi] by applying the above transitions finite amount of times? Here, U[x..y] is a substring of U that begins at index x (indexed from 1) and ends at index y. In particular, U[1..|U|] is the whole string U. It is guaranteed that 1 ≤ a ≤ b ≤ |S| and 1 ≤ c ≤ d ≤ |T|.
2,500
Print a string of Q characters, where the i-th character is '1' if the answer to the i-th query is positive, and '0' otherwise.
standard output
PASSED
3e148fc54f1cb7e441990c151b103257
train_002.jsonl
1520696100
Alice has a string consisting of characters 'A', 'B' and 'C'. Bob can use the following transitions on any substring of our string in any order any number of times: A BC B AC C AB AAA empty string Note that a substring is one or more consecutive characters. For given queries, determine whether it is possible to obtain the target string from source.
256 megabytes
import java.io.*; import java.util.*; public class MainD { static final StdIn in = new StdIn(); static final PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { char[] s = in.next().toCharArray(), t = in.next().toCharArray(); int n=s.length, m=t.length; int[] a = new int[n+1], b = new int[m+1]; List<Integer> c1 = new ArrayList<Integer>(), c2 = new ArrayList<Integer>(); c1.add(-1); c2.add(-1); for(int i=0; i<n; ++i) if(s[i]!='A') { ++a[i+1]; c1.add(i); } for(int i=0; i<m; ++i) if(t[i]!='A') { ++b[i+1]; c2.add(i); } for(int i=0; i<n; ++i) a[i+1]+=a[i]; for(int i=0; i<m; ++i) b[i+1]+=b[i]; int q=in.nextInt(); while(q-->0) { int l1=in.nextInt()-1, r1=in.nextInt()-1, l2=in.nextInt()-1, r2=in.nextInt()-1; int s1=a[r1+1]-a[l1], s2=b[r2+1]-b[l2]; if(!(s1%2==s2%2&&s2>=s1)) { out.print('0'); continue; } int p1=Collections.binarySearch(c1, r1), p2=Collections.binarySearch(c2, r2); int ls1=c1.get(p1>=0?p1:-p1-2), ls2=c2.get(p2>=0?p2:-p2-2); ls1=Math.max(l1-1, ls1); ls2=Math.max(l2-1, ls2); //out.println(r1+" "+r2+" "+ls1+" "+ls2); if(r1-ls1!=r2-ls2&&!(((r1-ls1)%3==(r2-ls2)%3||s2>s1)&&r1-ls1>=r2-ls2)) { out.print('0'); //out.println(); continue; } if(s1==0&&s2!=0&&r1-ls1==r2-ls2) { out.print('0'); continue; } out.print('1'); } out.close(); } static class StdIn { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public StdIn() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public StdIn(InputStream in) { try{ din = new DataInputStream(in); } catch(Exception e) { throw new RuntimeException(); } buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String next() { int c; while((c=read())!=-1&&(c==' '||c=='\n'||c=='\r')); StringBuilder s = new StringBuilder(); while (c != -1) { if (c == ' ' || c == '\n'||c=='\r') break; s.append((char)c); c=read(); } return s.toString(); } public String nextLine() { int c; while((c=read())!=-1&&(c==' '||c=='\n'||c=='\r')); StringBuilder s = new StringBuilder(); while (c != -1) { if (c == '\n'||c=='\r') break; s.append((char)c); c = read(); } return s.toString(); } public int nextInt() { 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 int[] readIntArray(int n, int os) { int[] ar = new int[n]; for(int i=0; i<n; ++i) ar[i]=nextInt()+os; return ar; } public long nextLong() { 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 long[] readLongArray(int n, long os) { long[] ar = new long[n]; for(int i=0; i<n; ++i) ar[i]=nextLong()+os; return ar; } public double nextDouble() { 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() { try{ if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } catch(IOException e) { throw new RuntimeException(); } } public void close() throws IOException { if (din == null) return; din.close(); } } }
Java
["AABCCBAAB\nABCB\n5\n1 3 1 2\n2 2 2 4\n7 9 1 1\n3 4 2 3\n4 5 1 3"]
2 seconds
["10011"]
NoteIn the first query we can achieve the result, for instance, by using transitions .The third query asks for changing AAB to A — but in this case we are not able to get rid of the character 'B'.
Java 8
standard input
[ "constructive algorithms", "implementation", "strings" ]
98e3182f047a7e7b10be7f207b219267
The first line contains a string S (1 ≤ |S| ≤ 105). The second line contains a string T (1 ≤ |T| ≤ 105), each of these strings consists only of uppercase English letters 'A', 'B' and 'C'. The third line contains the number of queries Q (1 ≤ Q ≤ 105). The following Q lines describe queries. The i-th of these lines contains four space separated integers ai, bi, ci, di. These represent the i-th query: is it possible to create T[ci..di] from S[ai..bi] by applying the above transitions finite amount of times? Here, U[x..y] is a substring of U that begins at index x (indexed from 1) and ends at index y. In particular, U[1..|U|] is the whole string U. It is guaranteed that 1 ≤ a ≤ b ≤ |S| and 1 ≤ c ≤ d ≤ |T|.
2,500
Print a string of Q characters, where the i-th character is '1' if the answer to the i-th query is positive, and '0' otherwise.
standard output
PASSED
7ff79f452c522ec3bffb60119a2b86d7
train_002.jsonl
1520696100
Alice has a string consisting of characters 'A', 'B' and 'C'. Bob can use the following transitions on any substring of our string in any order any number of times: A BC B AC C AB AAA empty string Note that a substring is one or more consecutive characters. For given queries, determine whether it is possible to obtain the target string from source.
256 megabytes
import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.Random; import java.util.TreeSet; public final class CF_VK_2018_R1{ static boolean verb=true; static void log(Object X){if (verb) System.err.println(X);} static void log(Object[] X){if (verb) {for (Object U:X) System.err.print(U+" ");System.err.println("");}} static void log(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");System.err.println("");}} static void log(int[] X,int L){if (verb) {for (int i=0;i<L;i++) System.err.print(X[i]+" ");System.err.println("");}} static void log(long[] X){if (verb) {for (long U:X) System.err.print(U+" ");System.err.println("");}} static void logWln(Object X){if (verb) System.err.print(X);} static void info(Object o){ System.out.println(o);} static void output(Object o){outputWln(""+o+"\n"); } static void outputWln(Object o){try {out.write(""+ o);} catch (Exception e) {}} static long mod=1000000007; static long MX=1000000000000000001L; // Global vars static BufferedWriter out; static InputReader reader; static long powerMod(long b,long e,long m){ long x=1; while (e>0) { if (e%2==1) x=(b*x)%m; b=(b*b)%m; e=e/2; } return x; } /* rules 0-> 11 1-> 01 111 -> . So we cannot add 0 at the end We can add any number of zeros before a 1 number of 1 is stable %2 */ static void process() throws Exception { out = new BufferedWriter(new OutputStreamWriter(System.out)); reader=new InputReader(System.in); char[][] s=new char[2][]; int[][] last=new int[2][]; for (int e=0;e<2;e++) { String ss=reader.readString(); int L=ss.length(); char[] c=new char[L]; last[e]=new int[L]; int cur=-1; for (int i=0;i<L;i++){ if (ss.charAt(i)=='A') c[i]='0'; else { c[i]='1'; cur=i; } last[e][i]=cur; } s[e]=c; } int[][] num=new int[2][]; for (int e=0;e<2;e++){ int L=s[e].length; num[e]=new int[L]; int cur=0; for (int i=0;i<L;i++){ if (s[e][i]=='1') cur++; num[e][i]=cur; } } int Q=reader.readInt(); int[] l=new int[2]; int[] r=new int[2]; for (int q=0;q<Q;q++){ //log("------"); for (int e=0;e<2;e++){ l[e]=reader.readInt()-1; r[e]=reader.readInt()-1; //log(s[e].substring(l[e],r[e]+1)); } int ans=1; int[] cnt=new int[2]; // number of ending '0' for (int e=0;e<2;e++){ int x=r[e]; if (s[e][x]=='0'){ int i=last[e][x]; if (i<l[e]) i=l[e]-1; cnt[e]=x-i; } } int[] b=new int[2]; for (int e=0;e<2;e++){ b[e]=num[e][r[e]]; if (l[e]>0) b[e]-=num[e][l[e]-1]; } // numbero 1 cannot be lower nor different %2 if (b[1]<b[0] || b[1]%2!=b[0]%2) ans=0; // number of ending zeros cannot be bigger if (cnt[1]>cnt[0]) ans=0; else { if (cnt[0]==cnt[1]){ // we have an issue only if we have to generate a 1 from 0... // because we need to sacrifice a zero if (b[0]==0 && b[1]>0) ans=0; } else { // we have more // it's ok if either we have right modulo or we have b to generate if (cnt[0]%3!=cnt[1]%3 && b[1]==b[0]) ans=0; } } outputWln(ans); ////log(ans==1); } //log(""); output(""); try { out.close(); } catch (Exception EX){} } public static void main(String[] args) throws Exception { process(); } static final class InputReader { private final InputStream stream; private final byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } private int read() throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read(buf); if (numChars <= 0) { return -1; } } return buf[curChar++]; } public final String readString() throws IOException { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res=new StringBuilder(); do { res.append((char)c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public final int readInt() throws IOException { int c = read(); boolean neg=false; while (isSpaceChar(c)) { c = read(); } char d=(char)c; //log("d:"+d); if (d=='-') { neg=true; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); //log("res:"+res); if (neg) return -res; return res; } public final long readLong() throws IOException { int c = read(); boolean neg=false; while (isSpaceChar(c)) { c = read(); } char d=(char)c; //log("d:"+d); if (d=='-') { neg=true; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); //log("res:"+res); if (neg) return -res; return res; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
Java
["AABCCBAAB\nABCB\n5\n1 3 1 2\n2 2 2 4\n7 9 1 1\n3 4 2 3\n4 5 1 3"]
2 seconds
["10011"]
NoteIn the first query we can achieve the result, for instance, by using transitions .The third query asks for changing AAB to A — but in this case we are not able to get rid of the character 'B'.
Java 8
standard input
[ "constructive algorithms", "implementation", "strings" ]
98e3182f047a7e7b10be7f207b219267
The first line contains a string S (1 ≤ |S| ≤ 105). The second line contains a string T (1 ≤ |T| ≤ 105), each of these strings consists only of uppercase English letters 'A', 'B' and 'C'. The third line contains the number of queries Q (1 ≤ Q ≤ 105). The following Q lines describe queries. The i-th of these lines contains four space separated integers ai, bi, ci, di. These represent the i-th query: is it possible to create T[ci..di] from S[ai..bi] by applying the above transitions finite amount of times? Here, U[x..y] is a substring of U that begins at index x (indexed from 1) and ends at index y. In particular, U[1..|U|] is the whole string U. It is guaranteed that 1 ≤ a ≤ b ≤ |S| and 1 ≤ c ≤ d ≤ |T|.
2,500
Print a string of Q characters, where the i-th character is '1' if the answer to the i-th query is positive, and '0' otherwise.
standard output
PASSED
25680ad997aa4e7a9cefe23f4ecc13a9
train_002.jsonl
1520696100
Alice has a string consisting of characters 'A', 'B' and 'C'. Bob can use the following transitions on any substring of our string in any order any number of times: A BC B AC C AB AAA empty string Note that a substring is one or more consecutive characters. For given queries, determine whether it is possible to obtain the target string from source.
256 megabytes
import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.Random; import java.util.TreeSet; public final class CF_VK_2018_R1{ static boolean verb=true; static void log(Object X){if (verb) System.err.println(X);} static void log(Object[] X){if (verb) {for (Object U:X) System.err.print(U+" ");System.err.println("");}} static void log(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");System.err.println("");}} static void log(int[] X,int L){if (verb) {for (int i=0;i<L;i++) System.err.print(X[i]+" ");System.err.println("");}} static void log(long[] X){if (verb) {for (long U:X) System.err.print(U+" ");System.err.println("");}} static void logWln(Object X){if (verb) System.err.print(X);} static void info(Object o){ System.out.println(o);} static void output(Object o){outputWln(""+o+"\n"); } static void outputWln(Object o){try {out.write(""+ o);} catch (Exception e) {}} static long mod=1000000007; static long MX=1000000000000000001L; // Global vars static BufferedWriter out; static InputReader reader; static long powerMod(long b,long e,long m){ long x=1; while (e>0) { if (e%2==1) x=(b*x)%m; b=(b*b)%m; e=e/2; } return x; } /* rules 0-> 11 1-> 01 111 -> . So we cannot add 0 at the end We can add any number of zeros before a 1 number of 1 is stable %2 */ static void process() throws Exception { out = new BufferedWriter(new OutputStreamWriter(System.out)); reader=new InputReader(System.in); char[][] s=new char[2][]; int[][] last=new int[2][]; for (int e=0;e<2;e++) { String ss=reader.readString(); int L=ss.length(); char[] c=new char[L]; last[e]=new int[L]; int cur=-1; for (int i=0;i<L;i++){ if (ss.charAt(i)=='A') c[i]='0'; else { c[i]='1'; cur=i; } last[e][i]=cur; } s[e]=c; } int[][] num=new int[2][]; for (int e=0;e<2;e++){ int L=s[e].length; num[e]=new int[L]; int cur=0; for (int i=0;i<L;i++){ if (s[e][i]=='1') cur++; num[e][i]=cur; } } int Q=reader.readInt(); int[] l=new int[2]; int[] r=new int[2]; char[] res=new char[Q]; for (int q=0;q<Q;q++){ //log("------"); for (int e=0;e<2;e++){ l[e]=reader.readInt()-1; r[e]=reader.readInt()-1; //log(s[e].substring(l[e],r[e]+1)); } int ans=1; int[] cnt=new int[2]; // number of ending '0' for (int e=0;e<2;e++){ int x=r[e]; if (s[e][x]=='0'){ int i=last[e][x]; if (i<l[e]) i=l[e]-1; cnt[e]=x-i; } } int[] b=new int[2]; for (int e=0;e<2;e++){ b[e]=num[e][r[e]]; if (l[e]>0) b[e]-=num[e][l[e]-1]; } // numbero 1 cannot be lower nor different %2 if (b[1]<b[0] || b[1]%2!=b[0]%2) ans=0; // number of ending zeros cannot be bigger if (cnt[1]>cnt[0]) ans=0; else { if (cnt[0]==cnt[1]){ // we have an issue only if we have to generate a 1 from 0... // because we need to sacrifice a zero if (b[0]==0 && b[1]>0) ans=0; } else { // we have more // it's ok if either we have right modulo or we have b to generate if (cnt[0]%3!=cnt[1]%3 && b[1]==b[0]) ans=0; } } res[q]=(char)('0'+ans); } //log(""); output(new String(res)); try { out.close(); } catch (Exception EX){} } public static void main(String[] args) throws Exception { process(); } static final class InputReader { private final InputStream stream; private final byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } private int read() throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read(buf); if (numChars <= 0) { return -1; } } return buf[curChar++]; } public final String readString() throws IOException { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res=new StringBuilder(); do { res.append((char)c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public final int readInt() throws IOException { int c = read(); boolean neg=false; while (isSpaceChar(c)) { c = read(); } char d=(char)c; //log("d:"+d); if (d=='-') { neg=true; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); //log("res:"+res); if (neg) return -res; return res; } public final long readLong() throws IOException { int c = read(); boolean neg=false; while (isSpaceChar(c)) { c = read(); } char d=(char)c; //log("d:"+d); if (d=='-') { neg=true; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); //log("res:"+res); if (neg) return -res; return res; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
Java
["AABCCBAAB\nABCB\n5\n1 3 1 2\n2 2 2 4\n7 9 1 1\n3 4 2 3\n4 5 1 3"]
2 seconds
["10011"]
NoteIn the first query we can achieve the result, for instance, by using transitions .The third query asks for changing AAB to A — but in this case we are not able to get rid of the character 'B'.
Java 8
standard input
[ "constructive algorithms", "implementation", "strings" ]
98e3182f047a7e7b10be7f207b219267
The first line contains a string S (1 ≤ |S| ≤ 105). The second line contains a string T (1 ≤ |T| ≤ 105), each of these strings consists only of uppercase English letters 'A', 'B' and 'C'. The third line contains the number of queries Q (1 ≤ Q ≤ 105). The following Q lines describe queries. The i-th of these lines contains four space separated integers ai, bi, ci, di. These represent the i-th query: is it possible to create T[ci..di] from S[ai..bi] by applying the above transitions finite amount of times? Here, U[x..y] is a substring of U that begins at index x (indexed from 1) and ends at index y. In particular, U[1..|U|] is the whole string U. It is guaranteed that 1 ≤ a ≤ b ≤ |S| and 1 ≤ c ≤ d ≤ |T|.
2,500
Print a string of Q characters, where the i-th character is '1' if the answer to the i-th query is positive, and '0' otherwise.
standard output
PASSED
0eb5c430c825943dbfcfd64d24a8124e
train_002.jsonl
1520696100
Alice has a string consisting of characters 'A', 'B' and 'C'. Bob can use the following transitions on any substring of our string in any order any number of times: A BC B AC C AB AAA empty string Note that a substring is one or more consecutive characters. For given queries, determine whether it is possible to obtain the target string from source.
256 megabytes
import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.Random; import java.util.TreeSet; public final class CF_VK_2018_R1{ static boolean verb=true; static void log(Object X){if (verb) System.err.println(X);} static void log(Object[] X){if (verb) {for (Object U:X) System.err.print(U+" ");System.err.println("");}} static void log(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");System.err.println("");}} static void log(int[] X,int L){if (verb) {for (int i=0;i<L;i++) System.err.print(X[i]+" ");System.err.println("");}} static void log(long[] X){if (verb) {for (long U:X) System.err.print(U+" ");System.err.println("");}} static void logWln(Object X){if (verb) System.err.print(X);} static void info(Object o){ System.out.println(o);} static void output(Object o){outputWln(""+o+"\n"); } static void outputWln(Object o){try {out.write(""+ o);} catch (Exception e) {}} static long mod=1000000007; static long MX=1000000000000000001L; // Global vars static BufferedWriter out; static InputReader reader; static long powerMod(long b,long e,long m){ long x=1; while (e>0) { if (e%2==1) x=(b*x)%m; b=(b*b)%m; e=e/2; } return x; } static void process() throws Exception { out = new BufferedWriter(new OutputStreamWriter(System.out)); reader=new InputReader(System.in); String[] s=new String[2]; ArrayList<Integer>[] pos=new ArrayList[2]; int[][] last=new int[2][]; for (int e=0;e<2;e++) { //pos[e]=new ArrayList<Integer>(); String ss=reader.readString(); int L=ss.length(); char[] c=new char[L]; last[e]=new int[L]; int cur=-1; for (int i=0;i<L;i++){ if (ss.charAt(i)=='A') c[i]='0'; else { c[i]='1'; cur=i; //pos[e].add(i); } last[e][i]=cur; } s[e]=new String(c); } int[][] num=new int[2][]; for (int e=0;e<2;e++){ int L=s[e].length(); num[e]=new int[L]; int cur=0; for (int i=0;i<L;i++){ if (s[e].charAt(i)=='1') cur++; num[e][i]=cur; } } /* int[][][] cnt=new int[2][2][]; for (int e=0;e<2;e++){ int[] cur=new int[2]; for (int i=0;i<s[e].length();i++){ int x=0; char c=s[e].charAt(i); if (x=='B' ||c=='C') x=1; cur[x]++; cnt[e][0][i]=cur[0]; cnt[e][1][i]=cur[1]; } } */ int Q=reader.readInt(); int[] l=new int[2]; int[] r=new int[2]; for (int q=0;q<Q;q++){ //log("------"); for (int e=0;e<2;e++){ l[e]=reader.readInt()-1; r[e]=reader.readInt()-1; //log(s[e].substring(l[e],r[e]+1)); } int ans=1; int[] cnt=new int[2]; for (int e=0;e<2;e++){ int x=r[e]; if (s[e].charAt(x)=='0'){ int i=last[e][x]; if (i<l[e]) i=l[e]-1; cnt[e]=x-i; /* int i=Collections.binarySearch(pos[e],x); if (i<0) i=-i-2; if (i<l[e]) i=l[e]-1; cnt[e]=x-i; */ } } int[] b=new int[2]; for (int e=0;e<2;e++){ b[e]=num[e][r[e]]; if (l[e]>0) b[e]-=num[e][l[e]-1]; } if (b[1]<b[0] || b[1]%2!=b[0]%2) ans=0; if (cnt[0]<cnt[1]) ans=0; else { if (cnt[0]==cnt[1]){ // we have an issue only if we have to generate a 1 from 0... // because we need to sacrifice a zero if (b[0]==0 && b[1]>0) ans=0; } else { // we have more // it's ok if either we have right modulo or we have b to generate if (cnt[0]%3!=cnt[1]%3 && b[1]==b[0]) ans=0; } } /* if (cnt[1]>cnt[0]) { log("case 1"); ans=0; } if (b[0]>b[1] || b[1]%2!=b[0]%2){ log("case 3"); ans=0; } if (b[0]==0 && b[1]==0 && cnt[0]%3!=cnt[1]%3){ log("case 4"); ans=0; } if (cnt[0]>cnt[1] && cnt[0]%3!=cnt[1]%3 && b[0]>=b[1]){ log("case 5"); ans=0; } */ outputWln(ans); ////log(ans==1); } //log(""); output(""); /* captured ? 000000 --> (0)*11(0)* AAAAAAAA AAAABBAAAAA 5 1 8 1 10 1 8 5 6 1 8 5 8 1 8 4 8 1 8 3 8 */ try { out.close(); } catch (Exception EX){} } public static void main(String[] args) throws Exception { process(); } static final class InputReader { private final InputStream stream; private final byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } private int read() throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read(buf); if (numChars <= 0) { return -1; } } return buf[curChar++]; } public final String readString() throws IOException { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res=new StringBuilder(); do { res.append((char)c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public final int readInt() throws IOException { int c = read(); boolean neg=false; while (isSpaceChar(c)) { c = read(); } char d=(char)c; //log("d:"+d); if (d=='-') { neg=true; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); //log("res:"+res); if (neg) return -res; return res; } public final long readLong() throws IOException { int c = read(); boolean neg=false; while (isSpaceChar(c)) { c = read(); } char d=(char)c; //log("d:"+d); if (d=='-') { neg=true; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); //log("res:"+res); if (neg) return -res; return res; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
Java
["AABCCBAAB\nABCB\n5\n1 3 1 2\n2 2 2 4\n7 9 1 1\n3 4 2 3\n4 5 1 3"]
2 seconds
["10011"]
NoteIn the first query we can achieve the result, for instance, by using transitions .The third query asks for changing AAB to A — but in this case we are not able to get rid of the character 'B'.
Java 8
standard input
[ "constructive algorithms", "implementation", "strings" ]
98e3182f047a7e7b10be7f207b219267
The first line contains a string S (1 ≤ |S| ≤ 105). The second line contains a string T (1 ≤ |T| ≤ 105), each of these strings consists only of uppercase English letters 'A', 'B' and 'C'. The third line contains the number of queries Q (1 ≤ Q ≤ 105). The following Q lines describe queries. The i-th of these lines contains four space separated integers ai, bi, ci, di. These represent the i-th query: is it possible to create T[ci..di] from S[ai..bi] by applying the above transitions finite amount of times? Here, U[x..y] is a substring of U that begins at index x (indexed from 1) and ends at index y. In particular, U[1..|U|] is the whole string U. It is guaranteed that 1 ≤ a ≤ b ≤ |S| and 1 ≤ c ≤ d ≤ |T|.
2,500
Print a string of Q characters, where the i-th character is '1' if the answer to the i-th query is positive, and '0' otherwise.
standard output
PASSED
cfebf463e483992aa7a510198206e443
train_002.jsonl
1520696100
Alice has a string consisting of characters 'A', 'B' and 'C'. Bob can use the following transitions on any substring of our string in any order any number of times: A BC B AC C AB AAA empty string Note that a substring is one or more consecutive characters. For given queries, determine whether it is possible to obtain the target string from source.
256 megabytes
import java.io.*; /** * Created by dalt on 2018/3/14. */ public class CF923D { public static BlockReader input; public static PrintStream output; public static void main(String[] args) throws FileNotFoundException { if (System.getProperty("ONLINE_JUDGE") == null) { input = new BlockReader(new FileInputStream("D:\\DataBase\\TESTCASE\\codeforces\\CF923D.in")); output = System.out; } else { input = new BlockReader(System.in); output = new PrintStream(new BufferedOutputStream(System.out), false); } solve(); output.flush(); } public static void solve() { char[] s = new char[100001]; char[] t = new char[100001]; int slen = input.nextBlock(s, 1); int tlen = input.nextBlock(t, 1); int[] sumOfBOrCInS = new int[slen + 1]; int[] tailALengthInS = new int[slen + 1]; for (int i = 1; i <= slen; i++) { if (s[i] == 'A') { tailALengthInS[i] = tailALengthInS[i - 1] + 1; } sumOfBOrCInS[i] = sumOfBOrCInS[i - 1]; if (s[i] != 'A') { sumOfBOrCInS[i]++; } } int[] sumOfBOrCInT = new int[tlen + 1]; int[] tailALengthInT = new int[tlen + 1]; for (int i = 1; i <= tlen; i++) { if (t[i] == 'A') { tailALengthInT[i] = tailALengthInT[i - 1] + 1; } sumOfBOrCInT[i] = sumOfBOrCInT[i - 1]; if (t[i] != 'A') { sumOfBOrCInT[i]++; } } int q = input.nextInteger(); for (int i = 0; i < q; i++) { int sl = input.nextInteger(); int sr = input.nextInteger(); int tl = input.nextInteger(); int tr = input.nextInteger(); int sbcCnt = sumOfBOrCInS[sr] - sumOfBOrCInS[sl - 1]; int tbcCnt = sumOfBOrCInT[tr] - sumOfBOrCInT[tl - 1]; if (sbcCnt > tbcCnt || ((tbcCnt - sbcCnt) & 1) != 0) { output.print('0'); continue; } int stail = Math.min(tailALengthInS[sr], sr - sl + 1); int ttail = Math.min(tailALengthInT[tr], tr - tl + 1); if (ttail > stail) { output.print('0'); continue; } if (ttail == stail && sbcCnt == 0 && tbcCnt > 0) { output.print('0'); continue; } stail -= ttail; stail %= 3; if (stail != 0 && sbcCnt == tbcCnt) { output.print('0'); continue; } output.print('1'); } } public static class BlockReader { static final int EOF = -1; InputStream is; byte[] dBuf; int dPos, dSize, next; StringBuilder builder = new StringBuilder(); public BlockReader(InputStream is) { this(is, 4096); } public BlockReader(InputStream is, int bufSize) { this.is = is; dBuf = new byte[bufSize]; next = nextByte(); } public int nextByte() { while (dPos >= dSize) { if (dSize == -1) { return EOF; } dPos = 0; try { dSize = is.read(dBuf); } catch (Exception e) { } } return dBuf[dPos++]; } public String nextBlock() { builder.setLength(0); skipBlank(); while (next != EOF && !Character.isWhitespace(next)) { builder.append((char) next); next = nextByte(); } return builder.toString(); } public void skipBlank() { while (Character.isWhitespace(next)) { next = nextByte(); } } public int nextInteger() { skipBlank(); int ret = 0; boolean rev = false; if (next == '+' || next == '-') { rev = next == '-'; next = nextByte(); } while (next >= '0' && next <= '9') { ret = (ret << 3) + (ret << 1) + next - '0'; next = nextByte(); } return rev ? -ret : ret; } public int nextBlock(char[] data, int offset) { skipBlank(); int index = offset; int bound = data.length; while (next != EOF && index < bound && !Character.isWhitespace(next)) { data[index++] = (char) next; next = nextByte(); } return index - offset; } public boolean hasMore() { skipBlank(); return next != EOF; } } }
Java
["AABCCBAAB\nABCB\n5\n1 3 1 2\n2 2 2 4\n7 9 1 1\n3 4 2 3\n4 5 1 3"]
2 seconds
["10011"]
NoteIn the first query we can achieve the result, for instance, by using transitions .The third query asks for changing AAB to A — but in this case we are not able to get rid of the character 'B'.
Java 8
standard input
[ "constructive algorithms", "implementation", "strings" ]
98e3182f047a7e7b10be7f207b219267
The first line contains a string S (1 ≤ |S| ≤ 105). The second line contains a string T (1 ≤ |T| ≤ 105), each of these strings consists only of uppercase English letters 'A', 'B' and 'C'. The third line contains the number of queries Q (1 ≤ Q ≤ 105). The following Q lines describe queries. The i-th of these lines contains four space separated integers ai, bi, ci, di. These represent the i-th query: is it possible to create T[ci..di] from S[ai..bi] by applying the above transitions finite amount of times? Here, U[x..y] is a substring of U that begins at index x (indexed from 1) and ends at index y. In particular, U[1..|U|] is the whole string U. It is guaranteed that 1 ≤ a ≤ b ≤ |S| and 1 ≤ c ≤ d ≤ |T|.
2,500
Print a string of Q characters, where the i-th character is '1' if the answer to the i-th query is positive, and '0' otherwise.
standard output
PASSED
29db81de8629cf06c7757976d1d013a2
train_002.jsonl
1520696100
Alice has a string consisting of characters 'A', 'B' and 'C'. Bob can use the following transitions on any substring of our string in any order any number of times: A BC B AC C AB AAA empty string Note that a substring is one or more consecutive characters. For given queries, determine whether it is possible to obtain the target string from source.
256 megabytes
import java.util.Scanner; public class laupc { public static void main(String[] args) { Scanner in=new Scanner(System.in); char[] S=in.next().toCharArray(); char[] T=in.next().toCharArray(); int[] AinS=new int[S.length+1]; int count=0; for(int i=0;i<S.length;i++){ if(S[i]=='A'){ count++; } AinS[i+1]=count; } count=0; int[] AinT=new int[T.length+1]; for(int i=0;i<T.length;i++){ if(T[i]=='A'){ count++; } AinT[i+1]=count; } int n=in.nextInt(); StringBuilder sb=new StringBuilder(); for(int i=0;i<n;i++){ int a=in.nextInt(),b=in.nextInt(),c=in.nextInt(),d=in.nextInt(); int AT=AinT[d]-AinT[c-1]; int AS=AinS[b]-AinS[a-1]; int XT=d-c+1-AT; int XS=b-a+1-AS; if(XT-XS<0 || (XT-XS)%2!=0){ sb.append(0); continue; } if(XT==XS && XT==0 && (AS<AT ||(AS-AT)%3!=0)){ sb.append(0); continue; } int tailT=0; while(d>=c && T[d-1]=='A'){ tailT++; d--; } int tailS=0; while(b>=a && S[b-1]=='A'){ tailS++; b--; } if(XT>XS && XS!=0 && tailS<tailT){ sb.append(0); continue; } if(XT==XS && (tailS<tailT || (tailS-tailT)%3!=0)){ sb.append(0); continue; } if(XS==0 && XT!=0 && tailS<=tailT){ sb.append(0); continue; } sb.append(1); } System.out.println(sb); } }
Java
["AABCCBAAB\nABCB\n5\n1 3 1 2\n2 2 2 4\n7 9 1 1\n3 4 2 3\n4 5 1 3"]
2 seconds
["10011"]
NoteIn the first query we can achieve the result, for instance, by using transitions .The third query asks for changing AAB to A — but in this case we are not able to get rid of the character 'B'.
Java 8
standard input
[ "constructive algorithms", "implementation", "strings" ]
98e3182f047a7e7b10be7f207b219267
The first line contains a string S (1 ≤ |S| ≤ 105). The second line contains a string T (1 ≤ |T| ≤ 105), each of these strings consists only of uppercase English letters 'A', 'B' and 'C'. The third line contains the number of queries Q (1 ≤ Q ≤ 105). The following Q lines describe queries. The i-th of these lines contains four space separated integers ai, bi, ci, di. These represent the i-th query: is it possible to create T[ci..di] from S[ai..bi] by applying the above transitions finite amount of times? Here, U[x..y] is a substring of U that begins at index x (indexed from 1) and ends at index y. In particular, U[1..|U|] is the whole string U. It is guaranteed that 1 ≤ a ≤ b ≤ |S| and 1 ≤ c ≤ d ≤ |T|.
2,500
Print a string of Q characters, where the i-th character is '1' if the answer to the i-th query is positive, and '0' otherwise.
standard output
PASSED
d12021d29f2d7ebe0aa88f7c28ee8318
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.util.*; public class Solution { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { long n = sc.nextInt(); long negative = 0; int zero = 0; long res = 0; for (int i = 0; i < n; i++) { long a = sc.nextInt(); if (a < 0) { negative++; res += Math.abs(a) - 1; } else if (a == 0) { zero++; } else { res += a - 1; } } if (negative % 2 == 0 || zero >= 1) { System.out.println(res + zero); } else { System.out.println(res + 2); } } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
c3e233c19444d6e5d466b1cefb92cb3c
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.util.Scanner; public class codeforcesDP { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int n = sc.nextInt(); int[] arr = takeInputInt(n); long positive = 0; long zeros = 0; long negatives = 0; long negativeCount = 0; for(int i = 0;i<n;i++){ if(arr[i] > 0){ positive += arr[i] - 1; } if(arr[i] == 0) zeros++; if(arr[i] < 0){ negatives += Math.abs(arr[i]) - 1; negativeCount++; } } if(negativeCount % 2 != 0){ if (zeros > 0){ negatives++; zeros--; } else{ negatives += 2; } } System.out.println(negatives + zeros + positive); } static void printDP(int[][] dp){ for(int i=0;i<dp.length;i++){ for(int j = 0;j<dp[0].length;j++){ System.out.print(dp[i][j] + " "); } System.out.println(); } } static int helpMe(int index, char[] ch, int length, int[][] dp){ char c = 'Q'; if(length == 1) c = 'A'; if(length == 3) return 1; if(index == ch.length) return 0; if(dp[index][length] != -1) return dp[index][length]; int considering = 0; if(c == ch[index]) considering = helpMe(index+1,ch,length+1,dp); int notConsidering = helpMe(index+1,ch,length,dp); dp[index][length] = considering+notConsidering; return considering+notConsidering; } static int[] takeInputInt(int n){ int[] input = new int[n]; for(int i=0;i<n;i++){ input[i] = sc.nextInt(); } return input; } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
af2a97532b173485eaf5272aa2fa7be3
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.io.BufferedReader; import java.io.*; import java.util.StringTokenizer; import java.util.Vector; import java.lang.Exception.*; import java.util.InputMismatchException; public class p1206B{ public static Print print=new Print(); public static Scan scan=new Scan(); public static void solve(int n,int[] arr) throws Exception{ long moves=0; boolean hasZero=false; for(int i=0;i<n;i++){ if(arr[i]==0)hasZero=true; if(Math.abs(arr[i]-1)<Math.abs(arr[i]+1)){ moves+=Math.abs(arr[i]-1); arr[i]=1; } else{ moves+=Math.abs(arr[i]+1); arr[i]=-1; } //arr[i]=Math.min(Math.abs(arr[i]-1),Math.abs(arr[i]+1)); } int prod=1; for(int i=0;i<n;i++){ prod*=arr[i]; } if(prod==1)print.println(moves); else { if(hasZero)print.println(moves); else print.println(moves+2); } } public static void main(String[] args) throws Exception{ int n; n=scan.scanInt(); int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=scan.scanInt(); } solve(n,arr); print.close(); } } class Scan { private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; public Scan() { in=System.in; } public int scan()throws IOException { if(total<0) throw new InputMismatchException(); if(index>=total) { index=0; total=in.read(buf); if(total<=0) return -1; } return buf[index++]; } public int scanInt()throws IOException { int integer=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { integer*=10; integer+=n-'0'; n=scan(); } else throw new InputMismatchException(); } return neg*integer; } public double scanDouble()throws IOException { double doub=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)&&n!='.') { if(n>='0'&&n<='9') { doub*=10; doub+=n-'0'; n=scan(); } else throw new InputMismatchException(); } if(n=='.') { n=scan(); double temp=1; while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { temp/=10; doub+=(n-'0')*temp; n=scan(); } else throw new InputMismatchException(); } } return doub*neg; } public String scanString()throws IOException { StringBuilder sb=new StringBuilder(); int n=scan(); while(isWhiteSpace(n)) n=scan(); while(!isWhiteSpace(n)) { sb.append((char)n); n=scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } } class Print { private final BufferedWriter bw; public Print() { this.bw=new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object)throws IOException { bw.append(""+object); } public void println(Object object)throws IOException { print(object); bw.append("\n"); } public void close()throws IOException { bw.close(); } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
79c66963ffc2b12595547c8b6d2eefda
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.util.*; public class Main{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for(int i=0; i<n; i++) a[i] = sc.nextInt(); long sum = 0, neg = 0, pos = 0, zero = 0; for(int i=0; i<n; i++){ if(a[i] < 0){ neg++; sum += -1-a[i]; }else if(a[i] > 0){ pos++; sum += a[i]-1; }else{ zero++; } // System.out.println(sum + " sum "); } // System.out.println(sum); if(neg%2==0){ sum += zero; }else{ if(zero > 0){ neg--; sum += 1; zero--; }else{ sum += 2; neg--; } sum += zero; } System.out.println(sum); } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
35ce4b21c552bc8724f94528d00aaa86
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.Collections; import java.util.StringTokenizer; public class Codeforces1206B { public static void main(String[] args) { FastScanner sc=new FastScanner(); int n = sc.nextInt(); long zero = 0; long ans = 0; long negative = 0; for(int i = 0 ; i < n ; i++) { int a = sc.nextInt(); if(a == 0) { zero++; ans++; }else if(a < 0) { ans += Math.abs(a) -1; negative++; }else ans += a - 1; } if(negative % 2 == 0) { System.out.println(ans); return; } else { if(zero > 0) { System.out.println(ans); return; }else { System.out.println(ans + 2); } } } public void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
e2b48f545c1e2c0917e6e4686f95fbea
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; import static java.util.stream.IntStream.range; public class AL { public static void main(String[] args){ //----------------------- Input ---------------------------------------// Scanner in=new Scanner(System.in); int t=in.nextInt(),n; long y=0,m=0; Vector <Integer>v=new Vector(0,1); Vector <Integer>v1=new Vector(0,1); HashMap dp = new HashMap(); int minp=999999999,maxni=-99999999,po=0,ni=0; minp*=100;maxni*=100; for(long i=0;i<t;i++) { n=in.nextInt(); if(n>0){po++;if(n<minp)minp=n;} else if(n<0){ni++;if(n>maxni)maxni=n;} else if(n==0) y++; if(n==0) m=m+1; else m=m+(Math.abs(n)-1); } ////////////////////////////////// if(y==t) { System.out.print(y); } else if(ni%2==0||y>=1) { System.out.print(m); } else { if(Math.abs(maxni)>=minp) { //m=m+minp+1; System.out.print(m+2); } else { // m+=Math.abs(maxni)+m+1; System.out.print(m+2); } } }}
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
5eaa9143396450541435c0e4c2c99642
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.util.*; public class productone { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int z=0,p=0,ne=0; int a[] = new int[n]; for(int i=0;i<n;i++) { a[i] = sc.nextInt(); if(a[i] == 0) z++; else if(a[i] > 0) p++; else ne++; } long count=0; for(int i=0;i<n;i++) if(a[i] < 0) count += -(a[i] + 1); for(int i=0;i<n;i++) if(a[i] > 0) count += a[i] - 1; count += z; if(ne%2 !=0) if(z == 0) count += 2; System.out.print(count); } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
fdb64eb8667509ba37d3493d27560a0f
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.util.*; import java.lang.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int z = 0; List<Integer> l = new ArrayList<>(); long ans = 0; int min =1000000007; for(int i=0;i<n;i++){ int a = sc.nextInt(); if(a>0) min = Math.min(min,a); if(a>0) ans+=Math.abs(a-1); else if(a==0) z++; else if(a<0) l.add(a); } if(l.size()%2==0){ for(int i : l) ans+=Math.abs(-1-i); ans+=z; } else if(l.size()%2==1&&z>0){ for(int i : l) ans+=Math.abs(-1-i); ans+=z; } else{ for(int i=0;i<l.size();i++){ ans+=Math.abs(-1-l.get(i)); } ans+=2; } System.out.println(ans); } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
d207d00d3073fecc51d84499f0cb3e35
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.StringTokenizer; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.File; public class cf1200A { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args)throws Exception{ FastReader f=new FastReader(); int n =f.nextInt();long sum=0;int count=0;int count2=0; for(int i=0;i<n;i++){ int t=f.nextInt(); if(t<0){ sum+=-1-t; count++; } else if(t>0){ sum+=t-1; } else{ sum++; count2++; } } if(count%2==0||(count%2==1&&count2>0)){ System.out.println(sum); } else{ System.out.println(sum+2); } } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
47e17b21b2b8dad9901f1603a4330a61
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.util.Scanner; public class Coin { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), neg = 0; long sum = 0l; boolean hasZero = false; while (n-- > 0) { int a = sc.nextInt(); if (a == 0) hasZero = true; else if (a < 0) neg++; sum += Math.abs(Math.abs(a) - 1); } if (neg % 2 != 0 && !hasZero) sum +=2; sc.close(); System.out.println(sum); } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
9f1ab02eda763b301ca96179167625bd
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
// import java.util.Scanner; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; // import java.util.Arrays; // import java.lang.Math; // import java.util.Arrays; // import java.util.HashSet; // import java.util.HashMap; // import java.util.Collections; // import java.math.BigInteger; public class Try { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] sr = br.readLine().split(" "); int ec = 0, zc = 0; long res = 0; for(int i=0;i<n;i++){ long num = Long.parseLong(sr[i]); if(num<0){ res+=num*-1 - 1; ec+=1; } else if(num>0) res+=num-1; else{ res+=1; zc+=1; } } if(ec%2==0) System.out.println(res); else if(zc==0) System.out.println(res+2); else System.out.println(res); } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
697fd2277e5f95c7037c28c6ddde0a71
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; public class Main { private static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); private static Lib l = new Lib(); /** * Make Product Equal One * https://codeforces.com/problemset/problem/1206/B */ public static void main(String[] args) { final int size = l.nextInt(); final long[] numbers = l.nextLongArr(size); long coins = 0; int negatives = 0; int zeros = 0; for (int i = 0; i < size; i++) { if (numbers[i] == 0) { zeros++; } else { if (numbers[i] < 0) { negatives++; } if (numbers[i] != -1 && numbers[i] != 1) { coins += Math.abs(numbers[i]) - 1; } } } coins += zeros; if (negatives % 2 != 0 && zeros == 0) { coins += 2; } out.println(coins); out.close(); } public static class Lib { BufferedReader br; StringTokenizer st; public Lib() { br = new BufferedReader(new InputStreamReader(System.in)); } private 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[] nextLongArr(int size) { long[] a = new long[size]; for (int i = 0; i < size; i++) { a[i] = nextInt(); } return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } void shuffle(long[] arr) { int n = arr.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { long tmp = arr[i]; int randomPos = i + rnd.nextInt(n - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } void sort(long[] arr) { // mitigate quicksort's worst case shuffle(arr); Arrays.sort(arr); } } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
ebd06093008a9fec5339314c62169d61
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; public class Main { private static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); private static Lib l = new Lib(); /** * Make Product Equal One * https://codeforces.com/problemset/problem/1206/B */ public static void main(String[] args) { final int size = l.nextInt(); final long[] numbers = l.nextLongArr(size); long coins = 0; float sign = 1; for (int i = 0; i < size; i++) { coins += Math.abs(Math.abs(numbers[i]) - 1); sign *= Math.signum(numbers[i]); } out.println(sign == -1 ? coins + 2 : coins); out.close(); } public static class Lib { BufferedReader br; StringTokenizer st; public Lib() { br = new BufferedReader(new InputStreamReader(System.in)); } private 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[] nextLongArr(int size) { long[] a = new long[size]; for (int i = 0; i < size; i++) { a[i] = nextInt(); } return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } void shuffle(long[] arr) { int n = arr.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { long tmp = arr[i]; int randomPos = i + rnd.nextInt(n - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } void sort(long[] arr) { // mitigate quicksort's worst case shuffle(arr); Arrays.sort(arr); } } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
1c72c67429f4190ad20f2bf699f1aa64
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
//BUNCH OF IMPORTS import java.util.Scanner; import java.util.Random; import java.util.Collections; import java.util.Arrays; import java.util.ArrayList; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; //MAIN CLASS public class practice { static FastReader input = new FastReader(); static void test_case() { int n = input.nextInt(); Long ans = (long)0 ; Long a[] = new Long[n]; Long minuses = (long)0; int zeroes=0; for(int i=0;i<n;i++) { a[i] = input.nextLong(); ans = ans + Math.abs(Math.abs(a[i])-1); if(a[i] < 0){minuses++;} if(a[i] == 0){zeroes++;} } if(minuses%2!=0 && zeroes==0) { ans+=2 ; } System.out.println(ans); } public static void main(String[]args) { practice call = new practice(); int number_of_test_cases = 1; for(int i=0;i<number_of_test_cases;i++) { call.test_case(); } } } //FAST READER CLASS FOR FASTER INPUT AND OUTPUT class FastReader { private BufferedReader br; private StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); //BufferReader creates an input stream which enables reading data //Then, InputStreamReader reads the stream through System.in } String inputter() { while(st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(inputter()); } Long nextLong() { return Long.parseLong(inputter()); } Double nextDouble() { return Double.parseDouble(inputter()); } String next() { return inputter(); } String nextLine() { String theString = " "; try { theString = br.readLine(); } catch(IOException e) { e.printStackTrace(); } return theString; } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
478efb3935e252da3a6e9bdc36f6f31d
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.util.*; import java.io.*; import java.math.*; import java.awt.geom.*; import static java.lang.Math.*; public class Solution implements Runnable { int min=(int)1e9; int dp[]; public void solve() throws Exception { // int t=sc.nextInt(); // for(int ii=1;ii<=t;ii++) // { //System.out.print("Case #"+ii+": "); int n=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=sc.nextInt(); int minus=0; int plus=0; long result=0l; int zero=0; for(int i=0;i<n;i++) { if(arr[i]<0) { minus++; result=result-1l-arr[i]; } else if(arr[i]==0) { zero++; result++; } else { plus++; result+=arr[i]-1; } } if(minus%2==1) { if(zero==0)result+=2l; } out.println(result); // } } static Throwable uncaught; BufferedReader in; FastScanner sc; PrintWriter out; @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new FastScanner(in); solve(); } catch (Throwable uncaught) { Solution.uncaught = uncaught; } finally { out.close(); } } public static void main(String[] args) throws Throwable { Thread thread = new Thread(null, new Solution(), "", (1 << 26)); thread.start(); thread.join(); if (Solution.uncaught != null) { throw Solution.uncaught; } } } class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
6c412c34c05b3920fcb06bc61d8cdd62
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.util.*; public class MakeProductEqualOne { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long c = 0; int p = 0; int o = 0; for (int i = 0; i < n; i++) { int k = (sc.nextInt()); if (k == 0) { c++; p++; } if (k > 0) { c += k - 1; } if (k < 0) { c += -1 - k; o++; } } if (o % 2 != 0 && p == 0) { c = c + 2; } System.out.println(c); } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
66ec17a6858e697a174a2011225fad2f
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { int getint(BufferedReader sc) throws java.lang.Exception { // String ss=sc.readLine(); int a= Integer.parseInt(sc.readLine()); return a; } ArrayList<Integer> getlistint(BufferedReader sc) throws java.lang.Exception { ArrayList<Integer> res=new ArrayList<>(); String s=sc.readLine(); String[] ss=s.split(" "); for(int i=0;i<ss.length;i++) { // String ss=sc.readLine(); int a=Integer.valueOf(ss[i]); res.add(a); } return res; } ArrayList<Float> getlistfl(BufferedReader sc) throws java.lang.Exception { ArrayList<Float> res=new ArrayList<>(); String s=sc.readLine(); String[] ss=s.split(" "); for(int i=0;i<ss.length;i++) { // String ss=sc.readLine(); float a=Float.valueOf(ss[i]); res.add(a); } return res; } ArrayList<Long> getlist(BufferedReader sc) throws java.lang.Exception { ArrayList<Long> res=new ArrayList<>(); String s=sc.readLine(); String[] ss=s.split(" "); for(int i=0;i<ss.length;i++) { // String ss=sc.readLine(); long a=Long.valueOf(ss[i]); res.add(a); } return res; } ArrayList<Character> getlistchar(BufferedReader sc) throws java.lang.Exception { ArrayList<Character> res=new ArrayList<>(); String s=sc.readLine(); // String[] ss=s.split(" "); for(int i=0;i<s.length();i++) { // String ss=sc.readLine(); //char[] a=ss[i].toCharArray(); res.add(s.charAt(i)); } return res; } static int n; static int b[]; static StringBuffer ans = new StringBuffer(""); static BufferedReader bf; static class pair{ int a,b; pair(int a,int b){ this.a =a; this.b =b; } }private static StringBuilder result = new StringBuilder(); public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int a[]=new int[n]; long count=0; long pos=0; long neg=0; long zero=0; for(int i=0;i<n;i++){ a[i]=sc.nextInt(); if(a[i]<0){ count=count+Math.abs(a[i]-(-1)); neg++; } else if(a[i]==0){ zero++; count++; } else if(a[i]>0){ a[i]=a[i]-1; count=count+a[i]; pos++; } } if(neg%2!=0 && zero!=0){ System.out.println(count); } else if(neg%2!=0 && zero==0){ System.out.println(count+2); } else { System.out.println(count); } } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
7e6445aec2f817650c67ef1e78586254
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.util.Scanner; public class productOne_1206B { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); long ans = 0; long product = 1; long[] arr = new long[n]; int numNegative = 0; int numZero = 0; for (int i = 0; i < n; i++) { arr[i] = s.nextLong(); if(arr[i] < 0){ numNegative++; } else if(arr[i] == 0){ numZero++; } } for (int i = 0; i < n; i++) { long num = arr[i]; if(num > 1){ ans = ans + num - 1; } else if(num < -1){ long temp = -1*num; ans = ans + temp - 1; } } if(numZero!=0 && numNegative%2!=0){ ans = ans + numZero; } else if(numZero == 0 && numNegative%2!=0){ ans = ans + 2; } else{ ans = ans + numZero; } System.out.println(ans); } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
286122abbf1775d7fa15b7864b9f0e4b
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.util.*; import java.lang.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int r[]=new int[n]; long coin=0; for(int i=0;i<n;i++) { r[i]=sc.nextInt(); } Arrays.sort(r); int p=1; for(int i=0;i<n;i++) { if(r[i]==0) { if(p==1) { r[i]++; coin++; p*=r[i]; } else if(p==-1) { r[i]--; coin++; p*=r[i]; } } else { if(r[i]>0) { if(r[i]!=1) { coin+=r[i]-1; r[i]=1; } p*=r[i]; } else if(r[i]<0) { if(r[i]!=-1) { coin+=Math.abs(r[i]+1); r[i]=-1; } p*=r[i]; } } } if(p==-1) { coin+=2; } System.out.println(coin); } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
9dc1cb3fff1446e536889330256afa81
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] x=new int[n]; long sum=0,a=0,b=0; for(int i=0;i<n;i++) { x[i]=sc.nextInt(); if(x[i]<-1) { sum+=-1*x[i]-1; x[i]=-1; }else if(x[i]>1) { sum+=x[i]-1; x[i]=1; } if(x[i]==-1) a++; else if(x[i]==0) { b++; } } if(a%2!=0) { if(b!=0) { sum+=b; }else { sum+=2; } }else { sum+=b; } System.out.println(sum); } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
dbfee55d66b4a56bdac04bdaedfe016c
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.io.*; public class B1206 { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub new B1206(); } B1206() throws IOException { in = new StreamTokenizer(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int n = nextInt(), nulls = 0, neg = 0, pos = 0; long ans = 0; for(int i = 0; i<n;i++) { int a = nextInt(); if(a==0) nulls++; else if(a<0) { neg++; ans += Math.abs(a)-1; } else { pos++; ans += a-1; } } if(neg==0 || neg>0 && neg%2!=0 && nulls>0 || neg%2==0 && neg>0) { ans += nulls; } else if(neg%2!=0 && neg>0 && nulls==0) ans+=2; out.println(ans); out.close(); } StreamTokenizer in; PrintWriter out; int nextInt() throws IOException { in.nextToken(); return (int) in.nval; } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
af335108db1195a85dac2a5db4eefcac
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
//Codeforces 1206B import java.util.Scanner; public class CF1206B { static final Scanner SC = new Scanner(System.in); public static void main(String[] args) { int numElements = SC.nextInt(); int[] numbers = new int[numElements]; for (int n = 0; n < numElements; ++n) numbers[n] = SC.nextInt(); long minCoinsTo1 = computeMinCoinsTo1(numElements, numbers); System.out.println(minCoinsTo1); } // Computes and returns number of coins to be paid (minimum) in order to get 1 as the product static long computeMinCoinsTo1(int numElements, int[] numbers) { long totalCost = 0; int zeroes = 0; int negatives = 0; for (int i = 0; i < numElements; ++i) { if (numbers[i] < 0) { negatives += 1; totalCost += -1 - numbers[i]; } else if (numbers[i] == 0) { totalCost += 1; zeroes += 1; } else { totalCost += numbers[i] - 1; } } if (negatives % 2 != 0 && zeroes == 0) totalCost += 2; return totalCost; } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
d4436ed5d84171002b816b0ac863736b
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.util.*;import java.io.*;import java.math.*; public class Main { public static void process()throws IOException { int n=ni(),arr[]=new int[n+1]; for(int i=1;i<=n;i++){ arr[i]=ni(); } int cnt=0,cntz=0; long res=0l; for(int i=1;i<=n;i++){ if(arr[i]==-1) cnt++; if(arr[i]==-1 || arr[i]==1) continue; if(arr[i]==0){ cntz++; continue; } if(arr[i]>0) res+=Math.abs(arr[i]-1)*1l; else{ res+=Math.abs(-1-arr[i])*1l; cnt++; } } if((cnt & 1) == 1){ if(cntz==0) res+=2l; else res+=cntz*1l; } else res+=cntz*1l; pn(res); } static AnotherReader sc; static PrintWriter out; public static void main(String[]args)throws IOException { out = new PrintWriter(System.out); sc=new AnotherReader(); boolean oj = true; oj = System.getProperty("ONLINE_JUDGE") != null; if(!oj) sc=new AnotherReader(100); long s = System.currentTimeMillis(); int t=1; while(t-->0) process(); out.flush(); if(!oj) System.out.println(System.currentTimeMillis()-s+"ms"); System.out.close(); } static void pn(Object o){out.println(o);} static void p(Object o){out.print(o);} static void pni(Object o){out.println(o);System.out.flush();} static int ni()throws IOException{return sc.nextInt();} static long nl()throws IOException{return sc.nextLong();} static double nd()throws IOException{return sc.nextDouble();} static String nln()throws IOException{return sc.nextLine();} static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} static boolean multipleTC=false; ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class AnotherReader{BufferedReader br; StringTokenizer st; AnotherReader()throws FileNotFoundException{ br=new BufferedReader(new InputStreamReader(System.in));} AnotherReader(int a)throws FileNotFoundException{ br = new BufferedReader(new FileReader("input.txt"));} String next()throws IOException{ while (st == null || !st.hasMoreElements()) {try{ st = new StringTokenizer(br.readLine());} catch (IOException e){ e.printStackTrace(); }} return st.nextToken(); } int nextInt() throws IOException{ return Integer.parseInt(next());} long nextLong() throws IOException {return Long.parseLong(next());} double nextDouble()throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException{ String str = ""; try{ str = br.readLine();} catch (IOException e){ e.printStackTrace();} return str;}} ///////////////////////////////////////////////////////////////////////////////////////////////////////////// }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
e06ffec77e1380a2776029ccb8ca3f65
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Scanner; public class MakeProductEqualOne { public static void main(String[]args) { Scanner s = new Scanner(new BufferedReader(new InputStreamReader(System.in))); int n=s.nextInt(); int counterZero=0; int counterNegative=0; long tot=0; for(int i=0;i<n;i++){ long el=s.nextLong(); if(el>0) tot+=el-1; else if(el==0){ counterZero++; tot++; }else{ tot+=(Math.abs(el)-1); counterNegative++; } } if(counterNegative%2==1&&counterZero==0){ tot+=2; } System.out.println(tot); } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
3d15e5b3176f839a941300732234a114
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.util.*; import java.math.*; import java.io.*; public class B{ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); }catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str = ""; try{ str = br.readLine(); }catch (IOException e){ e.printStackTrace(); } return str; } } public static void main(String[] args){ FastReader in = new FastReader(); // int t = in.nextInt(); // while(t-- != 0){ int n = in.nextInt(), neg = 0, pos = 0, zero = 0; long res = 0; for(int i = 0; i < n; i++){ int a = in.nextInt(); if(a == 0) zero++; if(a >= 0){ res += Math.abs(a - 1); }else{ res += Math.abs((-1) - a); neg++; } } if(neg % 2 == 1){ if(zero != 0){ System.out.println(res); }else{ System.out.println(res + 2); } }else{ System.out.println(res); } // } } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output
PASSED
d2c0f02c136189e24af2557f6fea78e2
train_002.jsonl
1566135900
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { static PrintWriter out; static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); out=new PrintWriter(System.out); } 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; } } // SHIVAM GUPTA : // ASCII = 48 + i ; // SHIVAM GUPTA : public static int min(int a ,int b , int c, int d) { int[] arr = new int[4] ; arr[0] = a; arr[1] = b ; arr[2] = c; arr[3] = d; Arrays.sort(arr) ; return arr[0]; } public static int max(int a ,int b , int c, int d) { int[] arr = new int[4] ; arr[0] = a; arr[1] = b ; arr[2] = c; arr[3] = d; Arrays.sort(arr) ; return arr[3]; } static int sieve = 1000000 ; static boolean[] prime = new boolean[sieve + 1] ; public static void sieveOfEratosthenes() { // TRUE == prime // FALSE == COMPOSITE // FALSE== 1 for(int i=0;i< sieve + 1;i++) prime[i] = true; for(int p = 2; p*p <= sieve; p++) { if(prime[p] == true) { for(int i = p*p; i <= sieve; i += p) prime[i] = false; } } } public static String reverse(String input) { String op = "" ; for(int i = 0; i < input.length() ; i++ ) { op = input.charAt(i)+ op ; } return op ; } public static int[] sortI(int[] arr) { Arrays.sort(arr) ; return arr ; } public static int[] sortD(int[] arr) { Arrays.sort(arr) ; int i =0 ; int j = arr.length -1 ; while( i < j) { int temp = arr[i] ; arr[i] =arr[j] ; arr[j] = temp ; i++ ; j-- ; } return arr ; } public static boolean isPossibleTriangle(int a ,int b , int c) { if( a + b > c && c+b > a && a +c > b) { return true ; } return false ; } public static int gcd(int a, int b ) { if(b==0)return a ; else return gcd(b,a%b) ; } public static int lcm(int a, int b ,int c , int d ) { int temp = lcm(a,b , c) ; int ans = lcm(temp ,d ) ; return ans ; } public static int lcm(int a, int b ,int c ) { int temp = lcm(a,b) ; int ans = lcm(temp ,c) ; return ans ; } public static int lcm(int a , int b ) { int gc = gcd(a,b); return (a*b)/gc ; } static boolean isPrime(int n) { if(n==1) { return false ; } boolean ans = true ; for(int i = 2; i*i <= n ;i++) { if(n% i ==0) { ans = false ;break ; } } return ans ; } static boolean isPowerOfTwo(int n) { if(n==0) return false; if(((n ) & (n-1)) == 0 ) return true ; else return false ; } public static int countDigit(long n) { return (int)Math.floor(Math.log10(n) + 1); } static final int MAXN = 100001; static int spf[] = new int[MAXN]; static void sieve() { spf[1] = 1; for (int i=2; i<MAXN; i++) spf[i] = i; for (int i=4; i<MAXN; i+=2) spf[i] = 2; for (int i=3; i*i<MAXN; i++) { if (spf[i] == i) { for (int j=i*i; j<MAXN; j+=i) if (spf[j]==j) spf[j] = i; } } } // The above code works well for n upto the order of 10^7. // Beyond this we will face memory issues. // Time Complexity: The precomputation for smallest prime factor is done in O(n log log n) // using sieve. // Where as in the calculation step we are dividing the number every time by // the smallest prime number till it becomes 1. // So, let’s consider a worst case in which every time the SPF is 2 . // Therefore will have log n division steps. // Hence, We can say that our Time Complexity will be O(log n) in worst case. static Vector<Integer> getFactorization(int x) { Vector<Integer> ret = new Vector<>(); while (x != 1) { ret.add(spf[x]); x = x / spf[x]; } return ret; } public static void main (String[] args) throws java.lang.Exception { FastReader scn = new FastReader() ; int n = scn.nextInt() ; int[] arr = new int[n] ; int countp = 0; int countz = 0; int countn = 0 ; for(int i = 0; i < n ;i++) { arr[i] = scn.nextInt() ; if(arr[i] > 0)countp++ ; else if(arr[i] == 0)countz++ ; else countn++ ; } long ans = 0; if(countz + countp == n) //ALL ARR[I] >= 0 { for(int i = 0; i < n ;i++) { if(arr[i] == 0) { ans = ans + 1 ; } else{ ans = ans + (arr[i] - 1) ; } } } else{ if(countn % 2 ==0) //EVEN NO OF NEGATIVES ARE PRESENT { for(int i = 0; i < n ;i++) { if(arr[i] >= 0) { ans += (long)Math.abs(arr[i] - 1) ; } else{ ans += (long)Math.abs(arr[i] + 1) ; } } } else{ //ODD NO OF NEGATIVES ARE PRESENT if(countz > 0) // NO OF ZEROS >= 1 ARE PRESENT { for(int i = 0; i < n ;i++) ans = ans + Math.abs(arr[i] -1) ; ans = ans - 2*countn ; } else{ // 0 ZEROS ARE PRESENT for(int i = 0; i< n ;i++) ans += (long)Math.abs(arr[i] -1 ) ; ans = ans - 2*countn + 2 ; } } } out.println(ans) ; out.flush() ; } }
Java
["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"]
1 second
["2", "4", "13"]
NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Java 11
standard input
[ "dp", "implementation" ]
3b3b2408609082fa5c3a0d55bb65d29a
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
900
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
standard output