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
ab20fa42c48f581a3406d6a4829d6978
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 > 1$$$ and $$$d_2 > 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
import java.util.*; import java.io.*; import java.lang.Math; import java.util.Random; public class Solution{ static int max = (int)(1e7); static int[] minDiv; public static void main(String[] args) throws Exception{ FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int n = fs.nextInt(); int[] arr = fs.readArray(n); //STORING MINIMUM PRIME DIVISOR O(nloglog(n)) minDiv = new int[max+1]; for(int i=1;i<=max;i++) minDiv[i] = i; for(int i=2;i*i<=max;i++){ if(minDiv[i]!=i) continue; for(int j=i*i;j<=max;j+=i){ minDiv[j] = Math.min(minDiv[j],i); } } int[] d1 = new int[n]; int[] d2 = new int[n]; for(int i=0;i<n;i++){ ArrayList<Integer> list = primeFactors(arr[i]); if(list.size()<2){ d1[i] = d2[i] = -1; } else{ d1[i] = list.get(0); d2[i] = 1; for(int j=1;j<list.size();j++) d2[i] *= list.get(j); } } for(int i=0;i<n;i++) out.print(d1[i]+" "); out.println(""); for(int i=0;i<n;i++) out.print(d2[i]+" "); out.println(""); out.close(); } static ArrayList<Integer> primeFactors(int n){ ArrayList<Integer> list = new ArrayList<Integer>(); while(n!=1){ if(list.isEmpty() || list.get(list.size()-1)!=minDiv[n]){ list.add(minDiv[n]); } n /= minDiv[n]; } return list; } 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(); } public int nextInt(){ return Integer.parseInt(next()); } public int[] readArray(int n){ int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } public long nextLong(){ return Long.parseLong(next()); } } }
Java
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
5bf67e49c0b8ffa959daaaeb73848675
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
import java.io.*; public class cf1366d { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); boolean[] seive = new boolean[10000001]; int[] div = new int[10000001]; for (int i=2;i*i<=seive.length;i++) { if (!seive[i]) { for (int j = i*i;j<seive.length;j+=i) { seive[j] = true; if (div[j]==0) div[j] = i; } } } int n = Integer.parseInt(br.readLine()); String[] s = br.readLine().trim().split(" "); StringBuilder r1 = new StringBuilder(); StringBuilder r2 = new StringBuilder(); for (int i=0;i<n;i++) { int a = Integer.parseInt(s[i]); if (!seive[a]) { r1.append(-1+" "); r2.append(-1+" "); } else { int b = a; while (a%div[b]==0) a = a/div[b]; if(a!=1) { r1.append(a+" "); r2.append(div[b]+" "); } else { r1.append(-1+" "); r2.append(-1+" "); } } } System.out.println(r1); System.out.println(r2); } }
Java
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
0903f2c5863607a311849186a0db47f6
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
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 mod1=(int)1e9+7; int mod2=998244353; public void solve() throws Exception { // int t=sc.nextInt(); // while(t-->0) // { int n=sc.nextInt(); int arr[]=sc.readArray(n); Arrays.fill(smallest,-1); sieve(); int result[]=new int[n]; for(int i=0;i<n;i++) { if(smallest[arr[i]]==-1) { out.print(-1+" "); result[i]=-1; } else { int temp=smallest[arr[i]]; int num=arr[i]; int val=1; while(num%temp==0) { num=num/temp; val=val*temp; } if(num!=1) { out.print(val+" "); result[i]=arr[i]/val; } else { out.print(-1+" "); result[i]=-1; } } } out.println(); for(int i:result) { out.print(i+" "); } } int max=10000001; int smallest[]=new int[max]; public void sieve() { for(int i=2;i<max;i++) { if(smallest[i]==-1) { for(int j=i;j<max && j>0;j+=i) { smallest[j]=i; //out.println(j+" "+i); } } } } static long power(long x, long y, long p) { long res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if((y & 1)==1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } 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 int[] readArray(int n) throws Exception { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } }
Java
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
e2a59c2122d01f04bd782ad1737a9858
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner s=new Scanner(System.in); int N=Integer.parseInt(s.nextLine()); int[] ans1=new int[N]; int[] ans2=new int[N]; for(int i=0;i<N;i++){ int[] l=help(s); ans1[i]=l[0]; ans2[i]=l[1]; } System.out.println(help(ans1)); System.out.println(help(ans2)); } public static int[] help(Scanner s){ int num=s.nextInt(); int[] p=new int[]{2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597,1601,1607,1609,1613,1619,1621,1627,1637,1657,1663,1667,1669,1693,1697,1699,1709,1721,1723,1733,1741,1747,1753,1759,1777,1783,1787,1789,1801,1811,1823,1831,1847,1861,1867,1871,1873,1877,1879,1889,1901,1907,1913,1931,1933,1949,1951,1973,1979,1987,1993,1997,1999,2003,2011,2017,2027,2029,2039,2053,2063,2069,2081,2083,2087,2089,2099,2111,2113,2129,2131,2137,2141,2143,2153,2161,2179,2203,2207,2213,2221,2237,2239,2243,2251,2267,2269,2273,2281,2287,2293,2297,2309,2311,2333,2339,2341,2347,2351,2357,2371,2377,2381,2383,2389,2393,2399,2411,2417,2423,2437,2441,2447,2459,2467,2473,2477,2503,2521,2531,2539,2543,2549,2551,2557,2579,2591,2593,2609,2617,2621,2633,2647,2657,2659,2663,2671,2677,2683,2687,2689,2693,2699,2707,2711,2713,2719,2729,2731,2741,2749,2753,2767,2777,2789,2791,2797,2801,2803,2819,2833,2837,2843,2851,2857,2861,2879,2887,2897,2903,2909,2917,2927,2939,2953,2957,2963,2969,2971,2999,3001,3011,3019,3023,3037,3041,3049,3061,3067,3079,3083,3089,3109,3119,3121,3137}; for (int i:p) { if(i*i>num)break; if (num % i == 0) { num/=i; while (num%i==0){ num/=i; } if(num==1)return new int[]{-1,-1}; else return new int[]{i,num}; } } return new int[]{-1,-1}; } public static String help(int[] ans){ StringBuilder s=new StringBuilder(); s.append(ans[0]); for(int i=1;i<ans.length;i++){ s.append(" "); s.append(ans[i]); } return s.toString(); } }
Java
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
45d3ec1d44225753e085790bd5e8bb68
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Main { public static void main(String[] args) { FastScanner s=new FastScanner(); PrintWriter out = new PrintWriter(System.out); int N=Integer.parseInt(s.nextLine()); int[] ans1=new int[N]; int[] ans2=new int[N]; for(int i=0;i<N;i++){ int[] l=help(s); ans1[i]=l[0]; ans2[i]=l[1]; } out.println(help(ans1)); out.println(help(ans2)); out.flush(); } public static int[] help(FastScanner s){ int num=s.nextInt(); int[] p=new int[]{2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597,1601,1607,1609,1613,1619,1621,1627,1637,1657,1663,1667,1669,1693,1697,1699,1709,1721,1723,1733,1741,1747,1753,1759,1777,1783,1787,1789,1801,1811,1823,1831,1847,1861,1867,1871,1873,1877,1879,1889,1901,1907,1913,1931,1933,1949,1951,1973,1979,1987,1993,1997,1999,2003,2011,2017,2027,2029,2039,2053,2063,2069,2081,2083,2087,2089,2099,2111,2113,2129,2131,2137,2141,2143,2153,2161,2179,2203,2207,2213,2221,2237,2239,2243,2251,2267,2269,2273,2281,2287,2293,2297,2309,2311,2333,2339,2341,2347,2351,2357,2371,2377,2381,2383,2389,2393,2399,2411,2417,2423,2437,2441,2447,2459,2467,2473,2477,2503,2521,2531,2539,2543,2549,2551,2557,2579,2591,2593,2609,2617,2621,2633,2647,2657,2659,2663,2671,2677,2683,2687,2689,2693,2699,2707,2711,2713,2719,2729,2731,2741,2749,2753,2767,2777,2789,2791,2797,2801,2803,2819,2833,2837,2843,2851,2857,2861,2879,2887,2897,2903,2909,2917,2927,2939,2953,2957,2963,2969,2971,2999,3001,3011,3019,3023,3037,3041,3049,3061,3067,3079,3083,3089,3109,3119,3121,3137}; for (int i:p) { if(i*i>num)break; if (num % i == 0) { num/=i; while (num%i==0){ num/=i; } if(num==1)return new int[]{-1,-1}; else return new int[]{i,num}; } } return new int[]{-1,-1}; } public static String help(int[] ans){ StringBuilder s=new StringBuilder(); s.append(ans[0]); for(int i=1;i<ans.length;i++){ s.append(" "); s.append(ans[i]); } return s.toString(); } static class FastScanner { public BufferedReader reader; public StringTokenizer tokenizer; public FastScanner() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return reader.readLine(); } catch(IOException e) { throw new RuntimeException(e); } } } }
Java
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
259cec23beff1e2c1493f906411e8891
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.StringTokenizer; public class Solve8 { public static void main(String[] args) throws IOException { PrintWriter pw = new PrintWriter(System.out); new Solve8().solve(pw); pw.flush(); pw.close(); } public void solve(PrintWriter pw) throws IOException { FastReader sc = new FastReader(); int[] lpd = getLargestPrimeDivisor(); int n = sc.nextInt(); long[] a = new long[n], b = new long[n]; Arrays.fill(a, -1); Arrays.fill(b, -1); for (int i = 0; i < n; i++) { ArrayList<Integer> pf = getPrimeFactorsUsingLPD(sc.nextInt(), lpd); long[] temp = new long[pf.size()]; for (int j = 0; j < pf.size(); j++) { temp[j] = pf.get(j); } ArrayList<RLE_Pair> al = runLengthEncoding(temp); if (al.size() == 1) { continue; } a[i] = al.get(0).num; for (int j = 1; j < al.size(); j++) { b[i] *= al.get(j).num; } b[i] = -b[i]; } for (int i = 0; i < n; i++) { pw.print(a[i] + (i + 1 == n ? "\n" : " ")); } for (int i = 0; i < n; i++) { pw.print(b[i] + (i + 1 == n ? "\n" : " ")); } } public long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public ArrayList<Integer> getPrimeFactorsUsingLPD(int x, int[] lpd) { ArrayList<Integer> primeFactors = new ArrayList(); while (x > 1) { primeFactors.add(lpd[x]); x /= lpd[x]; } Collections.reverse(primeFactors); return primeFactors; } public ArrayList<RLE_Pair> runLengthEncoding(long[] a) { int n = a.length; ArrayList<RLE_Pair> rle = new ArrayList(); for (int i = 0, cnt = 1; i < n; i++, cnt = 1) { while (i + 1 < n && a[i + 1] == a[i]) { ++cnt; ++i; } rle.add(new RLE_Pair(a[i], cnt)); } return rle; } class RLE_Pair { char c; long num; int frequent; public RLE_Pair(char c, int frequent) { this.c = c; this.frequent = frequent; } public RLE_Pair(long num, int frequent) { this.num = num; this.frequent = frequent; } } public int[] getLargestPrimeDivisor() { final int MAX = (int) 1E+7; int[] lpd = new int[MAX + 1]; for (int i = 2; i <= MAX; i++) { if (lpd[i] == 0) { for (int j = i; j <= MAX; j += i) { lpd[j] = i; } } } return lpd; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { } } public String next() { if (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { } return null; } public boolean hasNext() throws IOException { if (st != null && st.hasMoreTokens()) { return true; } String s = br.readLine(); if (s == null || s.isEmpty()) { return false; } st = new StringTokenizer(s); return true; } } }
Java
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
698ede9944d1f9ceb475493fb584155d
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
import java.math.*; import java.io.*; import java.util.*; import java.awt.*; public class Main implements Runnable { @Override public void run() { try { new Solver().solve(); System.exit(0); } catch (Exception | Error e) { e.printStackTrace(); System.exit(1); } } public static void main(String[] args) throws Exception { //new Thread(null, new Main(), "Solver", 1l << 25).start(); new Main().run(); } } class Solver { final Helper hp; final int MAXN = 10_000_007; final long MOD = (long) 998244353; final Timer timer; final TimerTask task; Solver() { hp = new Helper(MOD, MAXN); hp.initIO(System.in, System.out); timer = new Timer(); task = new TimerTask() { @Override public void run() { try { hp.flush(); System.exit(0); } catch (Exception e) { } } }; //timer.schedule(task, 4700); } void solve() throws Exception { int tc = TESTCASES ? hp.nextInt() : 1; for (int tce = 1; tce <= tc; ++tce) solve(tce); timer.cancel(); hp.flush(); } boolean TESTCASES = false; void solve(int tc) throws Exception { int i, j, k; int[][] facts = new int[2][MAXN]; for (i = 2; i < MAXN; ++i) if (facts[0][i] == 0) { for (j = i + i; j < MAXN; j += i) { if (facts[0][j] == 0) facts[0][j] = i; else if (facts[1][j] == 0) facts[1][j] = i; else if (hp.gcd(j, facts[0][j] + facts[1][j]) != 1) facts[1][j] *= i; } } int N = hp.nextInt(); int[][] ans = new int[2][N]; for (i = 0; i < N; ++i) { //int ele = i; //System.err.println(facts[0][ele] + " " + facts[1][ele]); int ele = hp.nextInt(); if (facts[1][ele] == 0 || hp.gcd(ele, facts[0][ele] + facts[1][ele]) != 1) { //if (check(ele)) System.err.println(ele); ans[0][i] = ans[1][i] = -1; } else { ans[0][i] = facts[0][ele]; ans[1][i] = facts[1][ele]; } } hp.println(hp.joinElements(ans[0])); hp.println(hp.joinElements(ans[1])); } boolean check(int ele) { HashSet<Integer> facts = new HashSet<>(); for (int i = 1; i * i <= ele; ++i) if (ele % i == 0) { facts.add(i); facts.add(ele / i); } facts.remove(1); for (int f1 : facts) for (int f2 : facts) if (f1 != f2 && hp.gcd(ele, f1 + f2) == 1) { //System.err.println(f1 + " " + f2); return true; } return false; } } class Helper { final long MOD; final int MAXN; final Random rnd; public Helper(long mod, int maxn) { MOD = mod; MAXN = maxn; rnd = new Random(); } public static int[] sieve; public static ArrayList<Integer> primes; public void setSieve() { primes = new ArrayList<>(); sieve = new int[MAXN]; int i, j; for (i = 2; i < MAXN; ++i) if (sieve[i] == 0) { primes.add(i); for (j = i; j < MAXN; j += i) { sieve[j] = i; } } } public static long[] factorial; public void setFactorial() { factorial = new long[MAXN]; factorial[0] = 1; for (int i = 1; i < MAXN; ++i) factorial[i] = factorial[i - 1] * i % MOD; } public long getFactorial(int n) { if (factorial == null) setFactorial(); return factorial[n]; } public long ncr(int n, int r) { if (r > n) return 0; if (factorial == null) setFactorial(); long numerator = factorial[n]; long denominator = factorial[r] * factorial[n - r] % MOD; return numerator * pow(denominator, MOD - 2, MOD) % MOD; } public long[] getLongArray(int size) throws Exception { long[] ar = new long[size]; for (int i = 0; i < size; ++i) ar[i] = nextLong(); return ar; } public int[] getIntArray(int size) throws Exception { int[] ar = new int[size]; for (int i = 0; i < size; ++i) ar[i] = nextInt(); return ar; } public String[] getStringArray(int size) throws Exception { String[] ar = new String[size]; for (int i = 0; i < size; ++i) ar[i] = next(); return ar; } public String joinElements(long... ar) { StringBuilder sb = new StringBuilder(); for (long itr : ar) sb.append(itr).append(" "); return sb.toString().trim(); } public String joinElements(int... ar) { StringBuilder sb = new StringBuilder(); for (int itr : ar) sb.append(itr).append(" "); return sb.toString().trim(); } public String joinElements(String... ar) { StringBuilder sb = new StringBuilder(); for (String itr : ar) sb.append(itr).append(" "); return sb.toString().trim(); } public String joinElements(Object... ar) { StringBuilder sb = new StringBuilder(); for (Object itr : ar) sb.append(itr).append(" "); return sb.toString().trim(); } public long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } public int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public long max(long... ar) { long ret = ar[0]; for (long itr : ar) ret = Math.max(ret, itr); return ret; } public int max(int... ar) { int ret = ar[0]; for (int itr : ar) ret = Math.max(ret, itr); return ret; } public long min(long... ar) { long ret = ar[0]; for (long itr : ar) ret = Math.min(ret, itr); return ret; } public int min(int... ar) { int ret = ar[0]; for (int itr : ar) ret = Math.min(ret, itr); return ret; } public long sum(long... ar) { long sum = 0; for (long itr : ar) sum += itr; return sum; } public long sum(int... ar) { long sum = 0; for (int itr : ar) sum += itr; return sum; } public void shuffle(int[] ar) { int r; for (int i = 0; i < ar.length; ++i) { r = rnd.nextInt(ar.length); if (r != i) { ar[i] ^= ar[r]; ar[r] ^= ar[i]; ar[i] ^= ar[r]; } } } public void shuffle(long[] ar) { int r; for (int i = 0; i < ar.length; ++i) { r = rnd.nextInt(ar.length); if (r != i) { ar[i] ^= ar[r]; ar[r] ^= ar[i]; ar[i] ^= ar[r]; } } } public void reverse(int[] ar) { int r; for (int i = 0; i < ar.length; ++i) { r = ar.length - 1 - i; if (r > i) { ar[i] ^= ar[r]; ar[r] ^= ar[i]; ar[i] ^= ar[r]; } } } public void reverse(long[] ar) { int r; for (int i = 0; i < ar.length; ++i) { r = ar.length - 1 - i; if (r > i) { ar[i] ^= ar[r]; ar[r] ^= ar[i]; ar[i] ^= ar[r]; } } } public long pow(long base, long exp, long MOD) { base %= MOD; long ret = 1; while (exp > 0) { if ((exp & 1) == 1) ret = ret * base % MOD; base = base * base % MOD; exp >>= 1; } return ret; } static final int BUFSIZE = 1 << 20; static byte[] buf; static int index, total; static InputStream in; static BufferedWriter bw; public void initIO(InputStream is, OutputStream os) { try { in = is; bw = new BufferedWriter(new OutputStreamWriter(os)); buf = new byte[BUFSIZE]; } catch (Exception e) { } } public void initIO(String inputFile, String outputFile) { try { in = new FileInputStream(inputFile); bw = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(outputFile))); buf = new byte[BUFSIZE]; } catch (Exception e) { } } private int scan() throws Exception { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) return -1; } return buf[index++]; } public String next() throws Exception { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) sb.append((char) c); return sb.toString(); } public int nextInt() throws Exception { int c, val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') c = scan(); for (; c >= '0' && c <= '9'; c = scan()) val = (val << 3) + (val << 1) + (c & 15); return neg ? -val : val; } public long nextLong() throws Exception { int c; long val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') c = scan(); for (; c >= '0' && c <= '9'; c = scan()) val = (val << 3) + (val << 1) + (c & 15); return neg ? -val : val; } public void print(Object a) throws Exception { bw.write(a.toString()); } public void printsp(Object a) throws Exception { print(a); print(" "); } public void println() throws Exception { bw.write("\n"); } public void println(Object a) throws Exception { print(a); println(); } public void flush() throws Exception { bw.flush(); } }
Java
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
9d188e0c0d8f2d9fe65f71b44548fb18
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; import java.awt.Point; // SHIVAM GUPTA : //NSIT //decoder_1671 //BEING PERFECTIONIST IS NOT AN OPTION. // STOP NOT TILL IT IS DONE OR U DIE . //EITHER MAKE IT OR BREAK IT. //NEVER UNDERESTIMATE URSELF. // U KNOW THAT IF THIS DAY WILL BE URS THEN NO ONE CAN DEFEAT U // REST ..HAVE FAITH ON DESTINY //MIRACLE HAPPENS RARELY .....BUT IT HAPPENS // ASCII = 48 + i ; // 2^28 = 268,435,456 > 10^8 // log 10 base 2 = 3.3219 // odd:: (x^2+1)/2 , (x^2-1)/2 ; x>=3 // even:: (x^2/4)+1 ,(x^2/4)-1 x >=4 // FOR ANY ODD NO N : N,N-1,N-2 //ALL ARE PAIRWISE COPRIME //THEIR COMBINED LCM IS PRODUCT OF ALL THESE NOS // two consecutive odds are always coprime to each other // two consecutive even have always gcd = 2 ; /* Name of the class has to be "Main" only if the class is public. */ public class Main { // static int[] arr = new int[100002] ; // static int[] dp = new int[100002] ; 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; } } //////////////////////////////////////////////////////////////////////////////////// public static int countDigit(long n) { return (int)Math.floor(Math.log10(n) + 1); } ///////////////////////////////////////////////////////////////////////////////////////// public static int sumOfDigits(long n) { if( n< 0)return -1 ; int sum = 0; while( n > 0) { sum = sum + (int)( n %10) ; n /= 10 ; } return sum ; } ////////////////////////////////////////////////////////////////////////////////////////////////// public static long arraySum(int[] arr , int start , int end) { long ans = 0 ; for(int i = start ; i <= end ; i++)ans += arr[i] ; return ans ; } ///////////////////////////////////////////////////////////////////////////////// public static int mod(int x) { if(x <0)return -1*x ; else return x ; } public static long mod(long x) { if(x <0)return -1*x ; else return x ; } //////////////////////////////////////////////////////////////////////////////// public static void swapArray(int[] arr , int start , int end) { while(start < end) { int temp = arr[start] ; arr[start] = arr[end]; arr[end] = temp; start++ ;end-- ; } } ////////////////////////////////////////////////////////////////////////////////// public static int[][] rotate(int[][] input){ int n =input.length; int m = input[0].length ; int[][] output = new int [m][n]; for (int i=0; i<n; i++) for (int j=0;j<m; j++) output [j][n-1-i] = input[i][j]; return output; } /////////////////////////////////////////////////////////////////////////////////////////////////////// public static int countBits(long n) { int count = 0; while (n != 0) { count++; n = (n) >> (1L) ; } return count; } /////////////////////////////////////////// //////////////////////////////////////////////// public static boolean isPowerOfTwo(long n) { if(n==0) return false; if(((n ) & (n-1)) == 0 ) return true ; else return false ; } ///////////////////////////////////////////////////////////////////////////////////// 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]; } /////////////////////////////////////////////////////////////////////////////////// public static String reverse(String input) { StringBuilder str = new StringBuilder("") ; for(int i =input.length()-1 ; i >= 0 ; i-- ) { str.append(input.charAt(i)); } return str.toString() ; } /////////////////////////////////////////////////////////////////////////////////////////// public static boolean sameParity(long a ,long b ) { if(a %2 ==0 && b % 2 == 0)return true ; else return false ; } //////////////////////////////////////////////////////////////////////////////////////////////////// public static boolean isPossibleTriangle(int a ,int b , int c) { if( a + b > c && c+b > a && a +c > b)return true ; else return false ; } //////////////////////////////////////////////////////////////////////////////////////////// public static int gcd(int a, int b ) { if(b==0)return a ; else return gcd(b,a%b) ; } public static long gcd(long a, long b ) { if(b==0)return a ; else return gcd(b,a%b) ; } //////////////////////////////////////////////////////////////////////////////////// public static 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(long n) { if(n==1) { return false ; } boolean ans = true ; for(long i = 2L; i*i <= n ;i++) { if(n% i ==0) { ans = false ;break ; } } return ans ; } /////////////////////////////////////////////////////////////////////////// static int sieve = 1000000 ; static boolean[] prime = new boolean[sieve + 1] ; public static void sieveOfEratosthenes() { // FALSE == prime // TRUE == COMPOSITE // FALSE== 1 // time complexity = 0(NlogLogN)== o(N) // gives prime nos bw 1 to N for(int i = 4; i<= sieve ; i++) { prime[i] = true ; i++ ; } for(int p = 3; p*p <= sieve; p++) { if(prime[p] == false) { for(int i = p*p; i <= sieve; i += p) prime[i] = true; } p++ ; } } /////////////////////////////////////////////////////////////////////////////////// public static void sortD(int[] arr , int s , int e) { sort(arr ,s , e) ; int i =s ; int j = e ; while( i < j) { int temp = arr[i] ; arr[i] =arr[j] ; arr[j] = temp ; i++ ; j-- ; } return ; } ///////////////////////////////////////////////////////////////////////////////////////// public static long countSubarraysSumToK(long[] arr ,long sum ) { HashMap<Long,Long> map = new HashMap<>() ; int n = arr.length ; long prefixsum = 0 ; long count = 0L ; for(int i = 0; i < n ; i++) { prefixsum = prefixsum + arr[i] ; if(sum == prefixsum)count = count+1 ; if(map.containsKey(prefixsum -sum)) { count = count + map.get(prefixsum -sum) ; } if(map.containsKey(prefixsum )) { map.put(prefixsum , map.get(prefixsum) +1 ); } else{ map.put(prefixsum , 1L ); } } return count ; } /////////////////////////////////////////////////////////////////////////////////////////////// // CALCULATE TOTIENT Fn FOR ALL VALUES FROM 1 TO n // TOTIENT(N) = count of nos less than n and grater than 1 whose gcd with n is 1 // or n and the no will be coprime in nature //time : O(n*(log(logn))) public static void eulerTotientFunction(int[] arr ,int n ) { for(int i = 1; i <= n ;i++)arr[i] =i ; for(int i= 2 ; i<= n ;i++) { if(arr[i] == i) { arr[i] =i-1 ; for(int j =2*i ; j<= n ; j+= i ) { arr[j] = (arr[j]*(i-1))/i ; } } } return ; } ///////////////////////////////////////////////////////////////////////////////////////////// public static long nCr(int n,int k) { long ans=1; k=k>n-k?n-k:k; int j=1; for(;j<=k;j++,n--) { if(n%j==0) { ans*=n/j; }else if(ans%j==0) { ans=ans/j*n; }else { ans=(ans*n)/j; } } return ans; } /////////////////////////////////////////////////////////////////////////////////////////// public static ArrayList<Integer> allFactors(int n) { ArrayList<Integer> list = new ArrayList<>() ; for(int i = 1; i*i <= n ;i++) { if( n % i == 0) { if(i*i == n) { list.add(i) ; } else{ list.add(i) ; list.add(n/i) ; } } } return list ; } public static ArrayList<Long> allFactors(long n) { ArrayList<Long> list = new ArrayList<>() ; for(long i = 1L; i*i <= n ;i++) { if( n % i == 0) { if(i*i == n) { list.add(i) ; } else{ list.add(i) ; list.add(n/i) ; } } } return list ; } //////////////////////////////////////////////////////////////////////////////////////////////////// static final int MAXN = 10000001; 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 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() public static 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); } } public static void sort(long 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); } } public static void merge(long 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 */ long L[] = new long[n1]; long R[] = new long[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++; } } ///////////////////////////////////////////////////////////////////////////////////////// public static long knapsack(int[] weight,long value[],int maxWeight){ int n= value.length ; //dp[i] stores the profit with KnapSack capacity "i" long []dp = new long[maxWeight+1]; //initially profit with 0 to W KnapSack capacity is 0 Arrays.fill(dp, 0); // iterate through all items for(int i=0; i < n; i++) //traverse dp array from right to left for(int j = maxWeight; j >= weight[i]; j--) dp[j] = Math.max(dp[j] , value[i] + dp[j - weight[i]]); /*above line finds out maximum of dp[j](excluding ith element value) and val[i] + dp[j-wt[i]] (including ith element value and the profit with "KnapSack capacity - ith element weight") */ return dp[maxWeight]; } /////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// // to return max sum of any subarray in given array public static long kadanesAlgorithm(long[] arr) { long[] dp = new long[arr.length] ; dp[0] = arr[0] ; long max = dp[0] ; for(int i = 1; i < arr.length ; i++) { if(dp[i-1] > 0) { dp[i] = dp[i-1] + arr[i] ; } else{ dp[i] = arr[i] ; } if(dp[i] > max)max = dp[i] ; } return max ; } ///////////////////////////////////////////////////////////////////////////////////////////// public static long kadanesAlgorithm(int[] arr) { long[] dp = new long[arr.length] ; dp[0] = arr[0] ; long max = dp[0] ; for(int i = 1; i < arr.length ; i++) { if(dp[i-1] > 0) { dp[i] = dp[i-1] + arr[i] ; } else{ dp[i] = arr[i] ; } if(dp[i] > max)max = dp[i] ; } return max ; } /////////////////////////////////////////////////////////////////////////////////////// // Arrays.sort(arr, new Comparator<Pair>() { // @Override // public int compare(Pair first, Pa second) { // if (first.getAge() != second.getAge()) { // return first.getAge() - second.getAge(); // } // return first.getName().compareTo(second.getName()); // } // }); ///////////////////////////////////////////////////////////////////////////////////////// public static long binarySerachGreater(int[] arr , int start , int end , int val) { // fing total no of elements strictly grater than val in sorted array arr if(start > end)return 0 ; //Base case int mid = (start + end)/2 ; if(arr[mid] <=val) { return binarySerachGreater(arr,mid+1, end ,val) ; } else{ return binarySerachGreater(arr,start , mid -1,val) + end-mid+1 ; } } ////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// //TO GENERATE ALL(DUPLICATE ALSO EXIST) PERMUTATIONS OF A STRING // JUST CALL generatePermutation( str, start, end) start :inclusive ,end : exclusive //Function for swapping the characters at position I with character at position j public static String swapString(String a, int i, int j) { char[] b =a.toCharArray(); char ch; ch = b[i]; b[i] = b[j]; b[j] = ch; return String.valueOf(b); } //Function for generating different permutations of the string public static void generatePermutation(String str, int start, int end) { //Prints the permutations if (start == end-1) System.out.println(str); else { for (int i = start; i < end; i++) { //Swapping the string by fixing a character str = swapString(str,start,i); //Recursively calling function generatePermutation() for rest of the characters generatePermutation(str,start+1,end); //Backtracking and swapping the characters again. str = swapString(str,start,i); } } } //////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// public static long factMod(long n, long mod) { if (n <= 1) return 1; long ans = 1; for (int i = 1; i <= n; i++) { ans = (ans * i) % mod; } return ans; } ///////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////// public static long power(int x ,int n) { //time comp : o(logn) if(n==0)return 1 ; if(n==1)return x; long ans =1L ; while(n>0) { if(n % 2 ==1) { ans = ans *x ; } n /= 2 ; x = x*x ; } return ans ; } //////////////////////////////////////////////////////////////////////////////////////////////////// public static long powerMod(long x, long n, long mod) { //time comp : o(logn) if(n==0)return 1L ; if(n==1)return x; long ans = 1; while (n > 0) { if (n % 2 == 1) ans = (ans * x) % mod; x = (x * x) % mod; n /= 2; } return ans; } ////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////// /* lowerBound - finds largest element equal or less than value paased upperBound - finds smallest element equal or more than value passed if not present return -1; */ public static long lowerBound(long[] arr,long k) { long ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]<=k) { ans=arr[mid]; start=mid+1; } else { end=mid-1; } } return ans; } public static int lowerBound(int[] arr,int k) { int ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]<=k) { ans=arr[mid]; start=mid+1; } else { end=mid-1; } } return ans; } public static long upperBound(long[] arr,long k) { long ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]>=k) { ans=arr[mid]; end=mid-1; } else { start=mid+1; } } return ans; } public static int upperBound(int[] arr,int k) { int ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]>=k) { ans=arr[mid]; end=mid-1; } else { start=mid+1; } } return ans; } ////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////// public static void solve() { FastReader scn = new FastReader() ; //int[] store = {2 ,3, 5 , 7 ,11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 } ; // product of first 11 prime nos is gre ater than 10 ^ 12; //ArrayList<Integer> list = new ArrayList<>() ; ArrayList<Long> list = new ArrayList<>() ; ArrayList<Integer> lista = new ArrayList<>() ; ArrayList<Integer> listb = new ArrayList<>() ; //HashMap<Integer,Integer> map = new HashMap<>() ; HashMap<Long,Long> map = new HashMap<>() ; HashMap<Integer,Integer> map1 = new HashMap<>() ; HashMap<Integer,Integer> map2 = new HashMap<>() ; //HashMap<String,Integer> maps = new HashMap<>() ; //HashMap<Integer,Boolean> mapb = new HashMap<>() ; //HashMap<Point,Integer> point = new HashMap<>() ; Set<Integer> set = new HashSet<>() ; Set<Integer> setx = new HashSet<>() ; Set<Integer> sety = new HashSet<>() ; //Collections.sort(list); //if(map.containsKey(arr[i]))map.put(arr[i] , map.get(arr[i]) +1 ) ; //else map.put(arr[i],1) ; // if(map.containsKey(temp))map.put(temp , map.get(temp) +1 ) ; // else map.put(temp,1) ; //int bit =Integer.bitCount(n); // gives total no of set bits in n; int t = 1; // t = scn.nextInt() ; while(t-- > 0) { //String str = scn.next() ; // long n = scn.nextLong() ; //long k = scn.nextLong() ; //long a = scn.nextLong() ; //long b = scn.nextLong() ; int n = scn.nextInt() ; //long x = scn.nextLong(); // int m = scn.nextInt() ; // int x = scn.nextInt() ; // int y = scn.nextInt() ; //int k = scn.nextInt() ; //int a = scn.nextInt() ; //int c = scn.nextInt() ; // int d = scn.nextInt() ; //int b = scn.nextInt() ; int[] d1 =new int[n] ;int[] d2 =new int[n] ; sieve() ; for(int i = 0; i< n ;i++) { int temp = scn.nextInt() ; int sf = spf[temp] ; while(temp % sf == 0) { temp = temp/sf ; } if(temp ==1) { d1[i] =-1; d2[i] =-1 ; } else{ d1[i] =sf ; d2[i] =temp ; } } for(int i = 0; i < n; i++)out.print(d1[i]+ " ") ; out.println() ; for(int i = 0; i < n; i++)out.print(d2[i]+ " ") ; //for(int i = 0; i < n; i++)out.print(arr[i]+ " ") ; // for(int i = 0; i < arr.length ; i++) // { // for(int j = 0; j < arr[0].length ; j ++) // { // out.print(arr[i][j] +" "); // } // out.println() ; // } //out.println() ; list.clear() ; map.clear() ; map1.clear() ; map2.clear() ; set.clear() ; } // test case end loop out.flush() ; } public static void main (String[] args) throws java.lang.Exception { solve() ; } } class Pair { int first ; int second ; @Override public String toString() { String ans = "" ; ans += this.first ; ans += " "; ans += this.second ; return ans ; } }
Java
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
4fa76e23757ed23c9dc5b0c1924e1b3a
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
// upsolve with kaiboy, coached by rainboy import java.io.*; import java.util.*; public class CF1366D extends PrintWriter { CF1366D() { super(System.out); } static class Scanner { Scanner(InputStream in) { this.in = in; } InputStream in; int k, l; byte[] bb = new byte[1 << 15]; byte getc() { if (k >= l) { k = 0; try { l = in.read(bb); } catch (IOException e) { l = 0; } if (l <= 0) return -1; } return bb[k++]; } int nextInt() { byte c = 0; while (c <= 32) c = getc(); int a = 0; while (c > 32) { a = a * 10 + c - '0'; c = getc(); } return a; } } Scanner sc = new Scanner(System.in); public static void main(String[] $) { CF1366D o = new CF1366D(); o.main(); o.flush(); } static final int A = 10000000; void main() { int[] pp = new int[A + 1]; for (int a = 2; a <= A; a++) { if (pp[a] != 0) continue; for (int b = a + a; b <= A; b += a) if (pp[b] == 0) pp[b] = a; } int n = sc.nextInt(); int[] aa = new int[n]; int[] bb = new int[n]; Arrays.fill(aa, -1); Arrays.fill(bb, -1); for (int i = 0; i < n; i++) { int a = sc.nextInt(); int p = pp[a]; if (p != 0) { while (a % p == 0) a /= p; if (a > 1) { aa[i] = p; bb[i] = a; } } } for (int i = 0; i < n; i++) print(aa[i] + " "); println(); for (int i = 0; i < n; i++) print(bb[i] + " "); println(); } }
Java
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
3c326428503b1d0875f85e0726436aea
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Unstoppable solver = new Unstoppable(); // int t=in.nextInt(); // while(t-->0) solver.solve(in, out); out.close(); } static class Unstoppable { static int hm[]; static boolean prime[]; static int b[]; static void sieveOfEratosthenes(int n) { Arrays.fill(prime,true); for(int p = 2; p <=n; p++) { if(prime[p] == true) { for(int i = p; i <= n; i += p) { prime[i]=false; b[i]=p; hm[i]*=p; } } } } static int gcd(int a,int b){ if(b==0) return a; else return gcd(b,a%b); } public void solve(InputReader in, PrintWriter out) { int n=in.nextInt() ; int a[]=new int[n]; b=new int[10000001]; for(int i=0;i<n;i++) { a[i]=in.nextInt(); } prime = new boolean[10000001]; hm=new int[10000001]; Arrays.fill(hm,1); sieveOfEratosthenes(10000000); b[1]=1; int d1[]=new int[n]; int d2[]=new int[n]; for(int i=0;i<n;i++){ int x=b[a[i]]; int y=hm[a[i]]/x; if(y==1||gcd(x+y,a[i])!=1) { d1[i]=-1; d2[i]=-1; } else{ d1[i]=x; d2[i]=y; } } for(int i=0;i<n;i++){ out.print(d1[i]+" "); }out.println(""); for(int i=0;i<n;i++){ out.print(d2[i]+" "); }out.println(""); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public long nextLong(){ return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
df68e30cae4f39bcec5a01ebe6d3291d
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
import java.io.*; import java.util.*; public class Main{ static int[]lp; static void sieveLinear(int N) { ArrayList<Integer> primes = new ArrayList<Integer>(); lp = new int[N + 1]; //lp[i] = least prime divisor of i for(int i = 2; i <= N; ++i) { if(lp[i] == 0) { primes.add(i); lp[i] = i; } int curLP = lp[i]; for(int p: primes)//all primes smaller than or equal my lowest prime divisor if(p > curLP || p * 1l * i > N) break; else lp[p * i] = p; } } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static void main() throws Exception{ int maxn=(int)1e7; sieveLinear(maxn); int n=sc.nextInt(); int[]in=sc.intArr(n); int[][]ans=new int[2][n]; for(int i=0;i<n;i++) { int x=in[i]; LinkedList<Integer>primeD=new LinkedList<>(); while(x>1) { int lowestDivisor=lp[x]; primeD.add(lowestDivisor); while(x%lowestDivisor==0) { x/=lowestDivisor; } } if(primeD.size()==1) { ans[0][i]=-1; ans[1][i]=-1; } else { ans[0][i]=primeD.poll(); ans[1][i]=primeD.poll(); while(!primeD.isEmpty()) { ans[1][i]*=primeD.poll(); } } } for(int i:ans[0])pw.print(i+" "); pw.println(); for(int i:ans[1])pw.print(i+" "); pw.println(); } public static void main(String[] args) throws Exception{ pw=new PrintWriter(System.out); sc = new MScanner(System.in); // int tc=sc.nextInt(); // while(tc-->0) main(); pw.flush(); } static PrintWriter pw; static MScanner sc; static class MScanner { StringTokenizer st; BufferedReader br; public MScanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public MScanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int[] intArr(int n) throws IOException { int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public long[] longArr(int n) throws IOException { long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public int[] intSortedArr(int n) throws IOException { int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt(); shuffle(in); Arrays.sort(in); return in; } public long[] longSortedArr(int n) throws IOException { long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong(); shuffle(in); Arrays.sort(in); return in; } public Integer[] IntegerArr(int n) throws IOException { Integer[]in=new Integer[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public Long[] LongArr(int n) throws IOException { Long[]in=new Long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } static void shuffle(int[]in) { for(int i=0;i<in.length;i++) { int idx=(int)(Math.random()*in.length); int tmp=in[i]; in[i]=in[idx]; in[idx]=tmp; } } static void shuffle(long[]in) { for(int i=0;i<in.length;i++) { int idx=(int)(Math.random()*in.length); long tmp=in[i]; in[i]=in[idx]; in[idx]=tmp; } } }
Java
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
551afe9cbac495409ccd0060938086d1
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
import java.io.*; import java.security.SecureRandom; import java.util.*; import java.util.stream.*; public class D1366 { // 420 remainder: use long everywhere private final static boolean MULTIPLE_TESTS = false; private final static int MAX_N = 10_000_000; private void solve() { int[] smallest_div = new int[MAX_N + 1]; List<Integer> primes = new ArrayList<>(); for (int i = 2; i <= MAX_N; i++) { if (smallest_div[i] == 0) { smallest_div[i] = i; primes.add(i); } for (int j = 0; j < primes.size() && primes.get(j) <= smallest_div[i] && (long) i * primes.get(j) <= MAX_N; j++) { int cur_prime = primes.get(j); if (cur_prime > smallest_div[i] || (long) i * cur_prime > MAX_N) { break; } smallest_div[i * cur_prime] = cur_prime; } } int n = reader.readInt(); int[] a = reader.readIntArray(n); List<Integer> first = new ArrayList<>(); List<Integer> second = new ArrayList<>(); for (int x : a) { int d = smallest_div[x]; int d1 = 1; while (x % d == 0) { x /= d; d1 *= d; } if (x == 1) { first.add(-1); second.add(-1); } else { first.add(d1); second.add(x); } } writer.println(first); writer.println(second); } private FastReader reader; private FastWriter writer; private void run() throws Exception { reader = new FastReader(); writer = new FastWriter(); int t = MULTIPLE_TESTS ? reader.readInt() : 1; for (int testNum = 0; testNum < t; testNum++) { solve(); } writer.close(); reader.close(); } public static void main(String[] args) throws Exception { new D1366().run(); } private final static boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; private static class FastWriter implements AutoCloseable { @SuppressWarnings(value="all") private final static Optional<String> OUTPUT_FILENAME = Optional.empty(); private final PrintWriter out; public FastWriter() throws FileNotFoundException { this.out = OUTPUT_FILENAME.isEmpty() ? new PrintWriter(System.out) : new PrintWriter(new File(OUTPUT_FILENAME.get())); } public <T> void print(T value) { out.print(value); } public <T> void println(T value) { out.println(value); } public <T> void print(List<? extends T> list) { for (int i = 0; i < list.size(); i++) { out.print(list.get(i)); if (i + 1 < list.size()) { out.print(' '); } } } public <T> void println(List<? extends T> list) { print(list); out.println(); } public <T> void println(T[] array) { for (int i = 0; i < array.length; i++) { out.print(array[i]); if (i + 1 < array.length) { out.print(' '); } } out.println(); } public void println(int[] array) { for (int i = 0; i < array.length; i++) { out.print(array[i]); if (i + 1 < array.length) { out.print(' '); } } out.println(); } public void println(long[] array) { for (int i = 0; i < array.length; i++) { out.print(array[i]); if (i + 1 < array.length) { out.print(' '); } } out.println(); } public void println(double[] array) { for (int i = 0; i < array.length; i++) { out.print(array[i]); if (i + 1 < array.length) { out.print(' '); } } out.println(); } @Override public void close() { out.close(); } } private static class FastReader implements AutoCloseable { private final static String INPUT_FILENAME = "input.txt"; private final InputStream inputStream; private final byte[] buffer = new byte[1024]; private int bufferLen = 0; private int bufferPtr = 0; public FastReader() throws FileNotFoundException { this.inputStream = ONLINE_JUDGE ? System.in : new FileInputStream(INPUT_FILENAME); } public int[] readIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = readInt(); } return array; } public long[] readLongArray(int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) { array[i] = readLong(); } return array; } public double[] readDoubleArray(int size) { double[] array = new double[size]; for (int i = 0; i < size; i++) { array[i] = readLong(); } return array; } public double readDouble() { return Double.parseDouble(readToken()); } public String readToken() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public int readInt() { 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 long readLong() { 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(); } } public int readByte() { if (bufferLen == -1) { throw new InputMismatchException(); } if (bufferPtr >= bufferLen) { bufferPtr = 0; try { bufferLen = inputStream.read(buffer); } catch (IOException e) { throw new InputMismatchException(); } if (bufferLen <= 0) { return -1; } } return buffer[bufferPtr++]; } public char readChar() { return (char) skip(); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)); return b; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } @Override public void close() throws Exception { inputStream.close(); bufferPtr = bufferLen; } } private static class Tuple { public static <A, B> Tuple2<A, B> of(A first, B second) { return new Tuple2<>(first, second); } public static <A, B> List<Tuple2<A, B>> array(A[] first, B[] second) { if (first.length != second.length) { throw new RuntimeException( String.format("Can't create tuples array from arrays with different length (%d != %d)", first.length, second.length)); } return IntStream.range(0, first.length) .mapToObj(i -> Tuple.of(first[i], second[i])) .collect(Collectors.toList()); } public static <A, B, C> Tuple3<A, B, C> of(A first, B second, C third) { return new Tuple3<>(first, second, third); } public static <A, B, C> List<Tuple3<A, B, C>> array(A[] first, B[] second, C[] third) { if (first.length != second.length || second.length != third.length) { throw new RuntimeException( String.format("Can't create tuples array from arrays with different length (%d, %d, %d)", first.length, second.length, third.length)); } return IntStream.range(0, first.length) .mapToObj(i -> Tuple.of(first[i], second[i], third[i])) .collect(Collectors.toList()); } public static <A extends Comparable<A>, B extends Comparable<B>> Comparator<Tuple2<A, B>> tuple2Comparator() { return Comparator.comparing((Tuple2<A, B> t) -> t.first).thenComparing(t -> t.second); } public static <A extends Comparable<A>, B extends Comparable<B>, C extends Comparable<C>> Comparator<Tuple3<A, B, C>> tuple3Comparator() { return Comparator.comparing((Tuple3<A, B, C> t) -> t.first).thenComparing(t -> t.second).thenComparing(t -> t.third); } private static class Tuple2<A, B> extends Tuple { public final A first; public final B second; private Tuple2(A first, B second) { this.first = first; this.second = second; } @Override public String toString() { return "(" + first + ", " + second + ")"; } } private static class Tuple3<A, B, C> extends Tuple2<A, B> { public final C third; private Tuple3(A first, B second, C third) { super(first, second); this.third = third; } @Override public String toString() { return "(" + first + ", " + second + ", " + third + ")"; } } } private static class Utils { private static final Random RANDOM = new SecureRandom(); public static <T extends Comparable<T>> void sort(T[] array) { shuffle(array); Arrays.sort(array); } public static void sort(int[] array) { shuffle(array); Arrays.sort(array); } public static void sort(long[] array) { shuffle(array); Arrays.sort(array); } public static void sort(double[] array) { shuffle(array); Arrays.sort(array); } public static <T> void sort(T[] array, Comparator<T> comparator) { shuffle(array); Arrays.sort(array, comparator); } public static <T> void shuffle(T[] array) { for (int i = array.length - 1; i > 0; i--) { int index = RANDOM.nextInt(i + 1); T a = array[index]; array[index] = array[i]; array[i] = a; } } public static void shuffle(int[] array) { for (int i = array.length - 1; i > 0; i--) { int index = RANDOM.nextInt(i + 1); int a = array[index]; array[index] = array[i]; array[i] = a; } } public static void shuffle(long[] array) { for (int i = array.length - 1; i > 0; i--) { int index = RANDOM.nextInt(i + 1); long a = array[index]; array[index] = array[i]; array[i] = a; } } public static void shuffle(double[] array) { for (int i = array.length - 1; i > 0; i--) { int index = RANDOM.nextInt(i + 1); double a = array[index]; array[index] = array[i]; array[i] = a; } } public static <T extends Comparable<T>> void sort(List<T> list) { Collections.shuffle(list); Collections.sort(list); } public static <T> void sort(List<T> list, Comparator<T> comparator) { Collections.shuffle(list); list.sort(comparator); } public static int randomInt(int fromIncluded, int toExcluded) { return fromIncluded + RANDOM.nextInt(toExcluded - fromIncluded); } public static Integer[] generify(int[] a) { return IntStream.of(a).boxed().toArray(Integer[]::new); } public static Long[] generify(long[] a) { return LongStream.of(a).boxed().toArray(Long[]::new); } public static Double[] generify(double[] a) { return DoubleStream.of(a).boxed().toArray(Double[]::new); } } }
Java
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
76fbf4b595dc4aa275d2fbbb35bc0d22
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.FileReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); DTwoDivisors solver = new DTwoDivisors(); solver.solve(1, in, out); out.close(); } static class DTwoDivisors { int[] smallestPrime; public void sieve(int N) { smallestPrime = new int[N + 1]; for (int i = 1; i < smallestPrime.length; i++) smallestPrime[i] = i; for (int i = 2; i * i <= smallestPrime.length; i++) if (smallestPrime[i] == i) for (int j = i * i; j < smallestPrime.length; j += i) { if (smallestPrime[j] == j) smallestPrime[j] = i; } } public void solve(int testNumber, Scanner sc, PrintWriter pw) { int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt(); StringBuilder sb = new StringBuilder(), sb2 = new StringBuilder(); sieve((int) 1e7); for (int i = 0; i < n; i++) { if (smallestPrime[arr[i]] == arr[i]) { sb.append(-1 + " "); sb2.append(-1 + " "); continue; } int p = arr[i]; while (p % smallestPrime[arr[i]] == 0) p /= smallestPrime[arr[i]]; if (p == 1) { sb.append(-1 + " "); sb2.append(-1 + " "); } else { sb.append(smallestPrime[arr[i]] + " "); sb2.append(p + " "); } } pw.println(sb); pw.println(sb2); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
a498032f94d35a02c297daf8f9a71927
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; public class test { static int sf[]; public static void main(String[] args) { // int test = fs.nextInt(); int test = 1; // int t = fs.nextInt(); sieve(); for (int cases = 0; cases < test; cases++) { int n = fs.nextInt(); int ar[] = getintarray(n); int d1[] = new int[n]; int d2[] = new int[n]; for (int i = 0; i < n; i++) { ArrayList<Integer> al = gf(ar[i]); if (al.size() == 1) { d1[i] = -1; d2[i] = -1; } else { int x = al.get(0); int k1 = al.get(0); int ii = 1; for (; ii < al.size(); ii++) { if (al.get(ii) == x) { k1 *= al.get(ii); } else { break; } } int k2 = ar[i] / k1; if (k1>1 && k2>1 && gcd(k1, k2) == 1) { d1[i] = k1; d2[i] = k2; } else { d1[i] = -1; d2[i] = -1; } } } for (int i : d1) { op.print(i + " "); } op.print("\n"); for (int i : d2) { op.print(i + " "); } op.print("\n"); op.flush(); } } static void sieve() { sf = new int[10000001]; int l = sf.length; sf[1] = 1; for (int i = 2; i < l; i++) { sf[i] = i; } for (int i = 4; i < l; i += 2) { sf[i] = 2; } for (int i = 3; i * i < l; i++) { if (sf[i] == i) { for (int j = i * i; j < l; j += i) if (sf[j] == j) sf[j] = i; } } } static ArrayList<Integer> gf(int x) { ArrayList<Integer> al = new ArrayList<Integer>(); while (x != 1) { al.add(sf[x]); x = x / sf[x]; } return al; } static long expmodulo(long a, long b, long c) { long x = 1; long y = a; while (b > 0) { if (b % 2 == 1) { x = (x * y) % c; } y = (y * y) % c; // squaring the base b /= 2; } return (long) x % c; } static int modInverse(int a, int m) { int m0 = m; int y = 0, x = 1; if (m == 1) return 0; while (a > 1) { // q is quotient int q = a / m; int t = m; // m is remainder now, process // same as Euclid's algo m = a % m; a = t; t = y; // Update x and y y = x - q * y; x = t; } // Make x positive if (x < 0) x += m0; return x; } static int countBits(int number) { return (int) (Math.log(number) / Math.log(2) + 1); } static int countDifferentBits(int a, int b) { int count = 0; for (int i = 0; i < 32; i++) { if (((a >> i) & 1) != ((b >> i) & 1)) { ++count; } } return count; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static void sortMyMapusingValues(HashMap<String, Integer> hm) { List<Map.Entry<String, Integer>> capitalList = new LinkedList<>(hm.entrySet()); Collections.sort(capitalList, new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); HashMap<String, Integer> result = new HashMap<>(); for (Map.Entry<String, Integer> entry : capitalList) { result.put(entry.getKey(), entry.getValue()); } } static boolean ispowerof2(long num) { if ((num & (num - 1)) == 0) return true; return false; } static void primeFactors(int n) { while (n % 2 == 0) { System.out.print(2 + " "); n /= 2; } for (int i = 3; i <= Math.sqrt(n); i += 2) { while (n % i == 0) { System.out.print(i + " "); n /= i; } } if (n > 2) System.out.print(n); } static boolean isPrime(long n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static class Graph { HashMap<Integer, LinkedList<Integer>> hm = new HashMap<Integer, LinkedList<Integer>>(); private void addVertex(int vertex) { hm.put(vertex, new LinkedList<>()); } private void addEdge(int source, int dest, boolean bi) { if (!hm.containsKey(source)) addVertex(source); if (!hm.containsKey(dest)) addVertex(dest); hm.get(source).add(dest); if (bi) { hm.get(dest).add(source); } } private boolean uniCycle(int i, HashSet<Integer> visited, int parent) { visited.add(i); LinkedList<Integer> list = hm.get(i); Iterator<Integer> it = list.iterator(); while (it.hasNext()) { Integer integer = (Integer) it.next(); if (!visited.contains(integer)) { if (uniCycle(integer, visited, i)) return true; } else if (integer != parent) { return true; } } return false; } private boolean uniCyclic() { HashSet<Integer> visited = new HashSet<Integer>(); Set<Integer> set = hm.keySet(); for (Integer integer : set) { if (!visited.contains(integer)) { if (uniCycle(integer, visited, -1)) { return true; } } } return false; } private boolean isbiCycle(int i, HashSet<Integer> visited, HashSet<Integer> countered) { if (countered.contains(i)) return true; if (visited.contains(i)) return false; visited.add(i); countered.add(i); LinkedList<Integer> list = hm.get(i); Iterator<Integer> it = list.iterator(); while (it.hasNext()) { Integer integer = (Integer) it.next(); if (isbiCycle(integer, visited, countered)) { return true; } } countered.remove(i); return false; } private boolean isbiCyclic() { HashSet<Integer> visited = new HashSet<Integer>(); HashSet<Integer> countered = new HashSet<Integer>(); Set<Integer> set = hm.keySet(); for (Integer integer : set) { if (isbiCycle(integer, visited, countered)) { return true; } } return false; } } static class Node { Node left, right; int data; public Node(int data) { this.data = data; } public void insert(int val) { if (val <= data) { if (left == null) { left = new Node(val); } else { left.insert(val); } } else { if (right == null) { right = new Node(val); } else { right.insert(val); } } } public boolean contains(int val) { if (data == val) { return true; } else if (val < data) { if (left == null) { return false; } else { return left.contains(val); } } else { if (right == null) { return false; } else { return right.contains(val); } } } public void inorder() { if (left != null) { left.inorder(); } System.out.print(data + " "); if (right != null) { right.inorder(); } } public int maxDepth() { if (left == null) return 0; if (right == null) return 0; else { int ll = left.maxDepth(); int rr = right.maxDepth(); if (ll > rr) return (ll + 1); else return (rr + 1); } } public int countNodes() { if (left == null) return 1; if (right == null) return 1; else { return left.countNodes() + right.countNodes() + 1; } } public void preorder() { System.out.print(data + " "); if (left != null) { left.inorder(); } if (right != null) { right.inorder(); } } public void postorder() { if (left != null) { left.inorder(); } if (right != null) { right.inorder(); } System.out.print(data + " "); } public void levelorder(Node node) { LinkedList<Node> ll = new LinkedList<Node>(); ll.add(node); getorder(ll); } public void getorder(LinkedList<Node> ll) { while (!ll.isEmpty()) { Node node = ll.poll(); System.out.print(node.data + " "); if (node.left != null) ll.add(node.left); if (node.right != null) ll.add(node.right); } } } 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 class Pair { int first, second; public Pair(int first, int second) { this.first = first; this.second = second; } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } private static final Scanner sc = new Scanner(System.in); private static final FastReader fs = new FastReader(); private static final OutputWriter op = new OutputWriter(System.out); static int[] getintarray(int n) { int ar[] = new int[n]; for (int i = 0; i < n; i++) { ar[i] = fs.nextInt(); } return ar; } static long[] getlongarray(int n) { long ar[] = new long[n]; for (int i = 0; i < n; i++) { ar[i] = fs.nextLong(); } return ar; } static int[][] get2darray(int n, int m) { int ar[][] = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ar[i][j] = fs.nextInt(); } } return ar; } static Pair[] getpairarray(int n) { Pair ar[] = new Pair[n]; for (int i = 0; i < n; i++) { ar[i] = new Pair(fs.nextInt(), fs.nextInt()); } return ar; } static void printarray(int ar[]) { for (int i : ar) { op.print(i + " "); } op.flush(); } static int fact(int n) { int fact = 1; for (int i = 2; i <= n; i++) { fact *= i; } return fact; } // // function to find largest prime factor }
Java
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
cfd33d099d5aaae24a6ea19e66c946db
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.util.*; public class Main { static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static void main(String args[]) throws IOException { FastReader in = new FastReader(System.in); int i, j, k = 0, MN = 1000_000_0; StringBuilder sb = new StringBuilder(); int arr[] = new int[MN + 1]; int size[] = new int[MN + 1]; for (i = 2; i <= MN; i++) { if (size[i] == 0) { for (j = i; j <= MN; j += i) { if (arr[j] == 0) arr[j] = i; size[j]++; } } } StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); int n = in.nextInt(); for (i = 0; i < n; i++) { int x=in.nextInt(); if (size[x] < 2) { sb1.append("-1 "); sb2.append("-1 "); } else { int temp = x; HashMap<Integer,Integer> map = new HashMap<>(); int f=0; while (temp != 1) { map.put(arr[temp],map.getOrDefault(arr[temp],0)+1); temp = temp / arr[temp]; } for(int z:map.keySet()){ int y=map.get(z); int pow=1; for(j=0;j<y;j++) pow=pow*z; //System.out.println(pow); if(gcd(pow+x/pow,x)==1){ f=pow; break; } } if(f==0){ sb1.append("-1 "); sb2.append("-1 "); } else { sb1.append(f).append(" "); sb2.append(x/f).append(" "); } } } System.out.println(sb1); System.out.println(sb2); } } class FastReader { byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } }
Java
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
120bc9edc10f0677b84e925bb0ec70f2
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; /** * @author Tran Anh Tai * @template for CP codes */ public class ProbD { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(in, out); out.close(); } // main solver static class Task { int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; int N = 10000008; boolean[] isPrime = new boolean[N]; int[] div = new int[N]; public void solve(InputReader in, PrintWriter out) { int n = in.nextInt(); int a[][] = new int[n][2]; Arrays.fill(isPrime, true); for (int i = 2; i < N; i++){ if (isPrime[i]){ for (int j = i * 2; j <N; j+=i){ isPrime[j] = false; if (div[j] == 0) div[j] = i; } } } for (int i = 0; i < n; i++){ int num = in.nextInt(); if (isPrime[num]){ a[i]= new int[]{-1, -1}; } else{ assert (div[num]!=0); int t = num; while ((num % div[t]) == 0){ num = num / div[t]; } if (num == 1){ a[i] = new int[]{-1, -1}; } else{ a[i] = new int[]{num, t / num}; } } } for (int i = 0; i < n; i++){ out.print(a[i][0] + " "); } out.println(); for (int i = 0; i < n; i++){ out.print(a[i][1] + " "); } } } private class Pair{ public int y, x; public Pair(int y, int x){ this.x = x; this.y = y; } } // fast input reader class; static class InputReader { BufferedReader br; StringTokenizer st; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } public String nextToken() { while (st == null || !st.hasMoreTokens()) { String line = null; try { line = br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } if (line == null) { return null; } st = new StringTokenizer(line); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong(){ return Long.parseLong(nextToken()); } } }
Java
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
5489cf573c1ec4f0e7dd7272e1b8bee9
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
/* package codechef; // don't place package name! */ import java.lang.*; import java.io.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) throws java.lang.Exception { // your code goes here FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); int max=(int)Math.pow(10,7); boolean isPrime[] = new boolean[max+1]; Arrays.fill(isPrime,true); isPrime[0]=false;isPrime[1]=false; for(int i=2;i<=(int)Math.sqrt(max);i++) { if(!isPrime[i]) continue; for(int j=i*i;j<=max;j+=i) { isPrime[j]=false; } } int n = sc.nextInt(); int ans1[]=new int[n]; int ans2[]=new int[n]; Arrays.fill(ans1,-1);Arrays.fill(ans2,-1); HashMap<Integer,Pair> map = new HashMap<>(); for(int i=0;i<n;i++) { int value=sc.nextInt(); if(map.containsKey(value)) {Pair p=map.get(value);ans1[i]=p.a;ans2[i]=p.b;continue;} if(isPrime[value]) {ans1[i]=-1;ans2[i]=-1;map.put(value,new Pair(-1,-1));continue;} for(int j=2;j<=(int)Math.sqrt(value);j++) { if(value%j==0) { if(gcd(value,j+(value/j))==1) {ans1[i]=j;ans2[i]=value/j;break;} } } map.put(value,new Pair(ans1[i],ans2[i])); } for(int i=0;i<n;i++) out.print(ans1[i]+" "); out.println(); for(int i=0;i<n;i++) out.print(ans2[i]+" "); out.close(); } static int gcd(int a,int b) { if(b>a) gcd(b,a); if(b==0) return a; return gcd(b,a%b); } } class Pair { int a,b; Pair(int a,int b) { this.a=a; this.b=b; } } 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
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
7a4ad5ea7415a3793267f4038f0305ba
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; public class D_Two_Divisors { static int[] minDiv; static ArrayList<Integer> primes; public static void main(String[] args) { MyScanner sc = new MyScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int n = sc.nextInt(); int[] a = new int[n]; for(int i = 0; i < n; i++) { a[i] = sc.nextInt(); } sieve(10000000+1); int[] row1 = new int[n]; int[] row2 = new int[n]; for(int i = 0; i < n; i++) { int prime_div = minDiv[a[i]]; int prime_val = minDiv[a[i]]; int rem = a[i]/minDiv[a[i]]; while(rem > 1) { if(minDiv[rem] == prime_val) { prime_div *= minDiv[rem]; } rem /= minDiv[rem]; } if(a[i]/prime_div != 1) { row1[i] = prime_div; row2[i] = a[i]/prime_div; }else { row1[i] = -1; row2[i] = -1; } } // output Arrays.stream(row1).forEach(b -> out.print(b + " ")); out.println(); Arrays.stream(row2).forEach(b -> out.print(b + " ")); out.println(); out.close(); } public static void sieve(int n) { minDiv = new int[n + 1]; primes = new ArrayList<>(); for(int i = 2; i < n; i++) { if(minDiv[i] == 0) { primes.add(i); minDiv[i] = i; } for(int j = 0; j < primes.size() && primes.get(j) <= minDiv[i] && primes.get(j)*i <= n; j++) { minDiv[primes.get(j)*i] = primes.get(j); } } } private 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
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
5a5e5bc0d06ed91e49e7f827b846d239
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
import java.util.Scanner; public class twoDivisors { static int div[] = new int[10000001]; static void findDiv() { for (int i = 2; i <= Math.sqrt(div.length); i++) if (div[i] == 0) for (int j = i * i; j < div.length; j = j + i) if (div[j] == 0) div[j] = i; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), a[] = new int[n]; findDiv(); StringBuilder d1s = new StringBuilder(), d2s = new StringBuilder(); for (int i = 0; i < n; i++) a[i] = sc.nextInt(); for (int i = 0; i < n; i++) { int temp = div[a[i]]; if (temp == 0 || Math.sqrt(temp) == a[i]) { d1s.append("-1 "); d2s.append("-1 "); } else { temp = a[i]; while (temp > 0 && temp % div[a[i]] == 0) { temp /= div[a[i]]; } int d1 = temp, d2 = a[i] / temp; if (d1 <= 1 || d2 <= 1) { d1s.append("-1 "); d2s.append("-1 "); } else { d1s.append(d1 + " "); d2s.append(d2 + " "); } } } System.out.print(d1s + "\n" + d2s); sc.close(); } }
Java
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
d5ce4e99d7e8bbedf74abdce3aa915c1
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
import java.io.*; import java.util.*; public class A { static int minPrime(int x) { for (int p : primes) { if (p * p > x) break; int d = 1; while (x % p == 0) { x /= p; d *= p; } if (d > 1) return d; } return x; } static ArrayList<Integer> primes; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(); PrintWriter out = new PrintWriter(System.out); primes = new ArrayList(); int N = 4000; boolean[] isPrime = new boolean[N]; Arrays.fill(isPrime, true); for (int i = 2; i < N; i++) if (isPrime[i]) { primes.add(i); for (int j = i * i; j < N; j += i) isPrime[j] = false; } int n = sc.nextInt(); int[][] ans = new int[2][n]; for (int i = 0; i < n; i++) { int x = sc.nextInt(); int p=minPrime(x); if(p==x) { ans[0][i]=ans[1][i]=-1; } else { ans[0][i]=p; ans[1][i]=x/p; } } for (int i = 0; i < 2; i++) { for (int x:ans[i]) out.print(x + " "); out.println(); } out.close(); } private static int gcd(int a, int b) { // TODO Auto-generated method stub return b == 0 ? a : gcd(b, a % b); } static class Scanner { BufferedReader br; StringTokenizer st; Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } Scanner(String fileName) throws FileNotFoundException { br = new BufferedReader(new FileReader(fileName)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine() throws IOException { return br.readLine(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(next()); } boolean ready() throws IOException { return br.ready(); } int[] nxtArr(int n) throws IOException { int[] ans = new int[n]; for (int i = 0; i < n; i++) ans[i] = nextInt(); return ans; } } static void sort(int[] a) { shuffle(a); Arrays.sort(a); } static void 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; } } }
Java
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
573d4d9a835c1edff9cf48715538b5af
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
import java.util.*; import java.io.*; public class I { private int[] minDiv; public I(FastScanner in, PrintWriter out) { minDiv = new int[(int)1e7 + 1]; for (int i = 2; i < minDiv.length; i++) { if (minDiv[i] == 0) { minDiv[i] = i; for (int j = 2 * i; j < minDiv.length; j += i) { if (minDiv[j] == 0) minDiv[j] = i; } } } int n = in.nextInt(); List<String> d1 = new ArrayList<>(n); List<String> d2 = new ArrayList<>(n); for (int i = 0; i < n; i++) { int a = in.nextInt(); List<Integer> f = getPrimeFactors(a); if (f.size() < 2) { d1.add("-1"); d2.add("-1"); } else { int p1 = f.get(0); int p2 = f.stream().reduce(1, (x, y) -> x * y) / p1; d1.add(String.valueOf(p1)); d2.add(String.valueOf(p2)); // if (gcd(p1 + p2, a) != 1) { // out.printf("%d -- %d, %d%n", a, p1, p2); // } } } out.println(String.join(" ", d1)); out.println(String.join(" ", d2)); } private List<Integer> getPrimeFactors(int a) { List<Integer> res = new ArrayList<>(); while (a > 1) { int f = minDiv[a]; if (res.size() == 0 || res.get(res.size() - 1) != f) res.add(f); a /= f; } return res; } private int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // given integer s, find the smallest integer so that its digits // sums up to s. This integer cannot ends with 9. // greedy -- add as many 9 as possible. set last digit to 8 private long build(int s) { if (s < 9) return s; long pow = 10; s -= 8; int m = s / 9; // number of 9s while (m-- > 0) pow *= 10; return s % 9 * pow + (pow - 10) + 8; } public static void main(String[] args) { PrintWriter out = new PrintWriter(System.out); // Scanner in = new Scanner( // new BufferedReader(new InputStreamReader(System.in))); FastScanner in = new FastScanner(System.in); I sol = new I(in, out); out.close(); } } class FastScanner{ private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastScanner(InputStream stream) { this.stream = stream; } int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars){ curChar = 0; try{ numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } boolean isSpaceChar(int c) { return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1; } boolean isEndline(int c) { return c=='\n'||c=='\r'||c==-1; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String next(){ int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do{ res.appendCodePoint(c); c = read(); }while(!isSpaceChar(c)); return res.toString(); } String nextLine(){ int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do{ res.appendCodePoint(c); c = read(); }while(!isEndline(c)); return res.toString(); } }
Java
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
c80645973a4d8394d7592c9f05659261
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
import java.util.*; import java.io.*; public class I { private int[] minDiv; public I(FastScanner in, PrintWriter out) { minDiv = new int[(int)1e7 + 1]; for (int i = 2; i < minDiv.length; i++) minDiv[i] = i; int bound = (int)Math.sqrt(minDiv.length - 1); for (int i = 2; i <= bound; i++) { if (minDiv[i] == i) { for (int j = i * i; j < minDiv.length; j += i) { if (minDiv[j] == j) minDiv[j] = i; } } } int n = in.nextInt(); List<String> d1 = new ArrayList<>(n); List<String> d2 = new ArrayList<>(n); for (int i = 0; i < n; i++) { int a = in.nextInt(); List<Integer> f = getPrimeFactors(a); if (f.size() < 2) { d1.add("-1"); d2.add("-1"); } else { int p1 = f.get(0); int p2 = f.stream().reduce(1, (x, y) -> x * y) / p1; d1.add(String.valueOf(p1)); d2.add(String.valueOf(p2)); // if (gcd(p1 + p2, a) != 1) { // out.printf("%d -- %d, %d%n", a, p1, p2); // } } } out.println(String.join(" ", d1)); out.println(String.join(" ", d2)); } private List<Integer> getPrimeFactors(int a) { List<Integer> res = new ArrayList<>(); while (a > 1) { int f = minDiv[a]; if (res.size() == 0 || res.get(res.size() - 1) != f) res.add(f); a /= f; } return res; } private int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // given integer s, find the smallest integer so that its digits // sums up to s. This integer cannot ends with 9. // greedy -- add as many 9 as possible. set last digit to 8 private long build(int s) { if (s < 9) return s; long pow = 10; s -= 8; int m = s / 9; // number of 9s while (m-- > 0) pow *= 10; return s % 9 * pow + (pow - 10) + 8; } public static void main(String[] args) { PrintWriter out = new PrintWriter(System.out); // Scanner in = new Scanner( // new BufferedReader(new InputStreamReader(System.in))); FastScanner in = new FastScanner(System.in); I sol = new I(in, out); out.close(); } } class FastScanner{ private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastScanner(InputStream stream) { this.stream = stream; } int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars){ curChar = 0; try{ numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } boolean isSpaceChar(int c) { return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1; } boolean isEndline(int c) { return c=='\n'||c=='\r'||c==-1; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String next(){ int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do{ res.appendCodePoint(c); c = read(); }while(!isSpaceChar(c)); return res.toString(); } String nextLine(){ int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do{ res.appendCodePoint(c); c = read(); }while(!isEndline(c)); return res.toString(); } }
Java
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
9fdd42ebf91b3ffa6d0a6042053aab80
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public static int[] generateMinDivisors(int n) { int[] lp = new int[n + 1]; lp[1] = 1; int[] primes = new int[n + 1]; int cnt = 0; for (int i = 2; i <= n; ++i) { if (lp[i] == 0) { lp[i] = i; primes[cnt++] = i; } for (int j = 0; j < cnt && primes[j] <= lp[i] && i * primes[j] <= n; ++j) lp[i * primes[j]] = primes[j]; } return lp; } public void solve(int testNumber, InputReader in, OutputWriter out) { int[] lp = generateMinDivisors(10000001); int n = in.nextInt(); int[] A = new int[n], B = new int[n]; for (int i = 0; i < n; i++) { int x = in.nextInt(); A[i] = B[i] = 1; while (x != 1) { int p = lp[x]; if (A[i] == 1) A[i] *= p; else B[i] *= p; while (x % p == 0) x /= p; } if (A[i] == 1 || B[i] == 1) A[i] = B[i] = -1; } for (int i = 0; i < n; i++) out.print(A[i] + " "); out.println(); for (int i = 0; i < n; i++) out.print(B[i] + " "); out.println(); } } 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 println(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } writer.print('\n'); } public void close() { writer.close(); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
8799f1dcd3c89aa45b65516186bca292
train_002.jsonl
1591886100
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class D { static final boolean RUN_TIMING = false; static char[] inputBuffer = new char[1024]; static PushbackReader in = new PushbackReader(new BufferedReader(new InputStreamReader(System.in)), 1024); static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public void go() throws IOException { // in = new PushbackReader(new BufferedReader(new FileReader(new File("test.txt"))), 1024); // out = new PrintWriter(new FileWriter(new File("output.txt"))); boolean[] isPrime = new boolean[10000001]; Arrays.fill(isPrime, true); for (int i = 2; i < isPrime.length; i++) { if (!isPrime[i]) { continue; } for (int e = 2; i*e < isPrime.length; e++) { isPrime[i*e] = false; } } int pn = 0; int[] primes = new int[1000000]; for (int i = 2; i < isPrime.length; i++) { if (isPrime[i]) { primes[pn] = i; pn++; // out.println(primes[pn-1]); } } int n = ipar(); int[][] ans = new int[n][2]; for (int i = 0; i < n; i++) { // int[] ans2 = {-1, -1}; ans[i][0] = -1; ans[i][1] = -1; int x = ipar(); int xx = x; HashMap<Integer, Integer> divPrime = new HashMap<>(); for (int e : primes) { if (e == 0 || e*e > x) { break; } // out.print(x + " "); int c = 0; while (x % e == 0) { x /= e; c++; } if (c > 0) { divPrime.put(e, c); } } if (x != 1) { divPrime.put(x, 1); } OUTER: // for (int a : divPrime.keySet()) { // for (int b : divPrime.keySet()) { // if (a == b) { // continue; // } // for (int c1 = 1; c1 <= divPrime.get(a); c1++) { // for (int c2 = 1; c2 <= divPrime.get(b); c2++) { // if (gcd(a*c1+b*c2, xx) == 1) { // ans[i][0] = a*c1; // ans[i][1] = b*c2; // // ans2[0] = a*c1; // // ans2[1] = b*c2; // break OUTER; // } // } // } // } // } for (int a : divPrime.keySet()) { int d = a; for (int c1 = 1; c1 <= divPrime.get(a); c1++) { if (xx != d && gcd(d+xx/d, xx) == 1) { ans[i][0] = d; ans[i][1] = xx/d; // ans2[0] = d; // ans2[1] = xx/d; break; } d *= a; } } // x = xx; // ArrayList<Integer> divs = new ArrayList<>(); // for (int e = 2; e*e <= x; e++) { // if (x % e == 0) { // divs.add(e); // divs.add(x/e); // } // } // divs.add(x); // // out.println(divs); // for (int a : divs) { // for (int b : divs) { // if (a == b) { // continue; // } // if (gcd(a+b, xx) == 1) { // // ans[i][0] = a; // // ans[i][1] = b; // if (ans2[0] == -1) { // out.println("WRONG"); // out.println(x); // out.println(a + " " + b); // return; // } // } // } // } } for (int i = 0; i < n; i++) { out.print(ans[i][0]); out.print(" "); } out.println(); for (int i = 0; i < n; i++) { out.print(ans[i][1]); out.print(" "); } out.println(); } public int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a%b); } public int ipar() throws IOException { return Integer.parseInt(spar()); } public int[] iapar(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ipar(); } return arr; } public long lpar() throws IOException { return Long.parseLong(spar()); } public long[] lapar(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = lpar(); } return arr; } public double dpar() throws IOException { return Double.parseDouble(spar()); } public String spar() throws IOException { int len = 0; int c; do { c = in.read(); } while (Character.isWhitespace(c) && c != -1); if (c == -1) { throw new NoSuchElementException("Reached EOF"); } do { inputBuffer[len] = (char)c; len++; c = in.read(); } while (!Character.isWhitespace(c) && c != -1); while (c != '\n' && Character.isWhitespace(c) && c != -1) { c = in.read(); } if (c != -1 && c != '\n') { in.unread(c); } return new String(inputBuffer, 0, len); } public String linepar() throws IOException { int len = 0; int c; while ((c = in.read()) != '\n' && c != -1) { if (c == '\r') { continue; } inputBuffer[len] = (char)c; len++; } return new String(inputBuffer, 0, len); } public boolean haspar() throws IOException { String line = linepar(); if (line.isEmpty()) { return false; } in.unread('\n'); in.unread(line.toCharArray()); return true; } public static void main(String[] args) throws IOException { long time = 0; time -= System.nanoTime(); new D().go(); time += System.nanoTime(); if (RUN_TIMING) { System.out.printf("%.3f ms%n", time / 1000000.0); } out.flush(); in.close(); } }
Java
["10\n2 3 4 5 6 7 8 9 10 24"]
2 seconds
["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"]
NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them.
Java 11
standard input
[ "constructive algorithms", "number theory", "math" ]
8639d3ec61f25457c24c5e030b15516e
The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$.
2,000
To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 &gt; 1$$$ and $$$d_2 &gt; 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them.
standard output
PASSED
2eb2f9163d8fa560b2c6d00cc1d4b073
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class test{ public static void main(String... args) throws NumberFormatException, IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(br.readLine()); int n=Integer.parseInt(st.nextToken()); int m=Integer.parseInt(st.nextToken()); int[] a=new int[n]; int[] b=new int[m]; st=new StringTokenizer(br.readLine()); for(int i=0;i<n;i++) { a[i]=Integer.parseInt(st.nextToken()); } st=new StringTokenizer(br.readLine()); for(int i=0;i<m;i++) { b[i]=Integer.parseInt(st.nextToken()); } Arrays.sort(a); Arrays.sort(b); int i=n-1; int j=m-1; int count=0; while(i>=0 && j>=0) { if(a[i]>b[j]) { count++; i--; } else { i--; j--; } } count=count+(i>=0?i+1:0); System.out.println(count); } } class pair{ int val; int index; }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
2e250afa09b923e456ae56be176f8a88
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
import java.util.ArrayList; import java.io.InputStream; import java.io.IOException; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { //input data Scanner s = new Scanner(System.in); int n = s.nextInt(); int m = s.nextInt(); ArrayList<Integer> round = new ArrayList(); ArrayList<Integer> prob = new ArrayList(); int maxCom = Integer.MIN_VALUE; for (int i=0; i< n; i++){ round.add(s.nextInt()); } int[] probCom = new int[round.get(n-1)]; //the maximum complexity is the last complexity required Arrays.fill(probCom, 0); for (int i=0; i<m; i++){ int a = s.nextInt(); if (a > maxCom) maxCom =a; prob.add(a); } int create =0; //record number of problems need to create int i,j=0; for (i=0; i<n; i++){ if (j==m) create++; else { if (round.get(i) <= prob.get(j)) { j++; } else { while (j < m && round.get(i) > prob.get(j)) { j++; } if (j<m) j++; else create++; } } } System.out.println(create); } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
36862f068a505de7fa2d535bce15c6dd
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
import java.util.ArrayList; import java.io.InputStream; import java.io.IOException; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { //Main idea: run 2 pointers on the round requirement list and prepared problem list //for each complexity in requirement, there has to be one problem satisfying following condition: round complexity lower or equal to the problem's complexity //if pointer on prepared problem list is already at the end, add the number of extra problem to create //input data Scanner s = new Scanner(System.in); int n = s.nextInt(); int m = s.nextInt(); ArrayList<Integer> round = new ArrayList(); ArrayList<Integer> prob = new ArrayList(); for (int i=0; i< n; i++){ round.add(s.nextInt()); } for (int i=0; i<m; i++){ prob.add(s.nextInt()); } //processing data int create =0; //record number of problems need to create int i,j=0; //pointers on the round requirement and list of problems prepared for (i=0; i<n; i++){ if (j==m) //check pointer 2 at the end create++; else { if (round.get(i) <= prob.get(j)) { //check whether the problem satisfies the requirement j++; } else { while (j < m && round.get(i) > prob.get(j)) { //move up the pointer 2 if the problem does not satisfy j++; } if (j<m) j++; else create++; } } } System.out.println(create); } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
2c2eaea4ebe334ad4a55c927bcba9950
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
import java.util.Scanner; public class GeorgeAndRound { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int m=sc.nextInt(); int a[]=new int[n]; int b[]=new int[m]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); for(int i=0;i<m;i++) b[i]=sc.nextInt(); int j=0,count=0; for(int i=0;i<m&&j<n;i++) { if(a[j]<=b[i]) { j++; } } System.out.println(n-j); } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
2121d56b27d50c49478cca21c27fa61c
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
import java.util.Scanner; public class George { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int m=sc.nextInt(); int a[]=new int[n]; int b[]=new int[m]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); for(int i=0;i<m;i++) b[i]=sc.nextInt(); int j=0,count=0; for(int i=0;i<m&&j<n;i++) { if(a[j]<=b[i]) { j++; } } System.out.println(n-j); } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
7a5f45e3937c67df719fc5b9753a3aa9
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Solution { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out,true); int n = sc.nextInt(); int m = sc.nextInt(); int[] a = new int[n]; int[] b = new int[m]; for(int i = 0;i<n;i++)a[i] = sc.nextInt(); for(int i = 0;i<m;i++)b[i] = sc.nextInt(); Arrays.sort(a); Arrays.sort(b); int ans = 0; n--;m--; while(n>-1){ while(n>-1 && a[n]>b[m]){ ans++; n--; } while(n>-1 && m>-1 && a[n]<=b[m]){ n--; m--; } if(m<0){ ans+=n+1; break; } } out.println(ans); } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
950cd2e5336fee8f9e3daf4d9c009384
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; public class codeForce_387B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); List<Integer> A = new ArrayList<Integer>(); Map<Integer, Integer> B = new TreeMap<Integer, Integer>(); int t = 0; for (int i = 0; i < n; ++i) { t = sc.nextInt(); A.add(t); } for (int i = 0; i < m; ++i) { t = sc.nextInt(); if (B.get(t) == null) { B.put(t, 1); } else { B.put(t, B.get(t) + 1); } } int result = 0; for (int i = 0; i < n; ++i) { boolean found = false; for (Map.Entry<Integer, Integer> entry : B.entrySet()) { // System.out.println(entry.getKey() + " " + entry.getValue()); if (entry.getKey().intValue() >= A.get(i) && entry.getValue().intValue() >= 1) { B.put(entry.getKey(), entry.getValue() - 1); found = true; break; } } if (!found) { ++result; } } System.out.println(result); } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
eb8d8d4d6b39b9ba65a19027c08ea1e4
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
import java.io.IOException; import java.util.InputMismatchException; /** * Created by jizhe on 2015/12/25. */ public class GeorgeAndRound { public static class FasterScanner { private byte[] buf = new byte[1024]; private int curChar; private int numChars; public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = System.in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public 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 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 int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } public static void main(String[] args) { //Scanner in = new Scanner(new BufferedInputStream(System.in)); FasterScanner in = new FasterScanner(); int N = in.nextInt(); int M = in.nextInt(); int[] a = new int[N]; int[] b = new int[M]; for( int i = 0; i < N; i++ ) { a[i] = in.nextInt(); } for( int i = 0; i < M; i++ ) { b[i] = in.nextInt(); } int countA = 0, countB = 0; for( ; countA < N; ) { //System.out.printf("countA: %d, countB: %d\n", countA, countB); int ca = a[countA]; for( ; countB < M; ) { int cb = b[countB++]; if( ca <= cb ) { countA++; break; } } if( countB == M ) { break; } } //System.out.printf("countA: %d, countB: %d\n", countA, countB); System.out.printf("%d\n", N-countA); } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
7b0c2b29a32934396d73ec88b6ebb7f6
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Map; import java.util.PriorityQueue; import java.util.Scanner; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); StringBuilder r = new StringBuilder(); int n = in.nextInt(); int m = in.nextInt(); int[] arr1 = new int[n]; int[] arr2 = new int[m]; for (int i = 0; i < n; i++) { arr1[i] = in.nextInt(); } for (int i = 0; i < m; i++) { arr2[i] = in.nextInt(); } int x = 0; int y = 0; int count = 0; while(count < n && x < n && y < m) { if(arr1[x] <= arr2[y]) { x++; y++; count++; } else { y++; } } int z = n - count; out.println(z); out.flush(); out.close(); } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
882eac5b212f0152e704840d4a7b43ad
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
import java.util.Scanner; /** * * @author thachlp */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] a = new int[n]; // so de cho cuoc thi int[] b = new int[m]; // so de Geogre chuan bi for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } for (int i = 0; i < m; i++) { b[i] = sc.nextInt(); } int j = 0; int count = 0; for (int i = 0; i < n; i++) { for (; j < m; j++) { if (b[j] >= a[i]) { count++; j++; break; } } } System.out.println(n - count); } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
f85cd4e39132595dd51851e38e453f29
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
import java.util.Scanner; /** * * @author thachlp */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] a = new int[n]; // so de cho cuoc thi int[] b = new int[m]; // so de Geogre chuan bi for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } for (int i = 0; i < m; i++) { b[i] = sc.nextInt(); } int j = 0; int count = 0; for (int i = 0; i < n; i++) { for (; j < m; ) { if (b[j] >= a[i]) { count++; j++; break; } j++; } } System.out.println(n - count); } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
4d1da41cbfe5de90ad221a5131ff9c67
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
import java.util.Arrays; import java.util.HashSet; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int m = s.nextInt(); int arr1[] = new int[n]; int arr2[] = new int[m]; for(int i=0;i<n;i++){ arr1[i] = s.nextInt(); } int count = 0; for(int i=0;i<m;i++){ arr2[i] = s.nextInt(); } int i = 0; int j = 0; while(i<n&& j<m){ if(arr1[i]<=arr2[j]){ i++; j++; count++; } else { j++; } } System.out.println(n-count); } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
b1e23ec203f83947b7ecfdc4431783f8
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
import java.util.Arrays; import java.util.HashSet; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int m = s.nextInt(); int arr1[] = new int[n]; int arr2[] = new int[m]; for(int i=0;i<n;i++){ arr1[i] = s.nextInt(); } int count = 0; for(int i=0;i<m;i++){ arr2[i] = s.nextInt(); } int i = n-1; int j = m-1; while(i>=0 && j>=0){ if(arr1[i]<=arr2[j]){ i--; j--; count++; } else { i--; } } System.out.println(n-count); } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
c33ac3f872442b125be9c76edd7bc34e
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class Ex1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); ArrayList<Integer> arr1 = new ArrayList<>(); ArrayList<Integer> arr2 = new ArrayList<>(); for (int i = 0; i < n; i++) { arr1.add(sc.nextInt()); } for(int i = 0; i < m; i++) { arr2.add(sc.nextInt()); } sc.close(); int count = 0, j = 0; for(int i = 0; i < n; i++) { for(; j < m; j++) { if(arr1.get(i) <= arr2.get(j)) { count++; j++; break; } } } System.out.println(n - count); } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
1b603dcf62ac9742bf6bfa9abe574a38
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
//package com.prituladima.codeforce.a2oj.div2B; import javax.print.attribute.IntegerSyntax; import java.io.*; import java.lang.reflect.Array; import java.util.*; import static java.lang.Double.parseDouble; import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.util.Arrays.stream; import static java.util.stream.IntStream.range; /** * Created by prituladima on 5/23/18. */ public final class Pro26 { private void solve() { int n = nextInt(), m = nextInt(); int[] a = nextArr(n); int[] b = nextArr(m); Set<Integer> req = new HashSet<>(); for (int i = 0; i < n; i++) { req.add(a[i]); } boolean[] used = new boolean[m]; int count = 0; for (int cur : req) { for (int i = 0; i < m; i++) { if (b[i] == cur && !used[i]) { used[i] = true; count++; break; } } } for (int i = 0; i < used.length; i++) { if (used[i]) { req.remove(b[i]); } } for (int cur : req) { for (int i = 0; i < m; i++) { if (b[i] > cur && !used[i]) { used[i] = true; count++; break; } } } // int need_to_do = 0; // // for (int i = 0; i < used.length; i++) { // need_to_do += used[i] ? 1 : 0; // } sout(n - count); } public static void main(String[] args) { new Pro26().run(); } private BufferedReader reader; private StringTokenizer tokenizer; private PrintWriter writer; private void run() { try { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } private int nextInt() { return parseInt(nextToken()); } private long nextLong() { return parseLong(nextToken()); } private double nextDouble() { return parseDouble(nextToken()); } private int[] nextArr(int size) { return stream(new int[size]).map(c -> nextInt()).toArray(); } private long[] nextArrL(int size) { return stream(new long[size]).map(c -> nextLong()).toArray(); } private double[] nextArrD(int size) { return stream(new double[size]).map(c -> nextDouble()).toArray(); } private char[][] nextCharMatrix(int n) { return range(0, n).mapToObj(i -> nextToken().toCharArray()).toArray(char[][]::new); } private int[][] nextIntMatrix(final int n, final int m) { return range(0, n).mapToObj(i -> nextArr(m)).toArray(int[][]::new); } private double[][] nextDoubleMatrix(int n, int m) { return range(0, n).mapToObj(i -> nextArr(m)).toArray(double[][]::new); } private String nextToken() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } private void souf(String format, Object... args) { writer.printf(format, args); } private void sout(Object o) { writer.print(o); } private void newLine() { writer.println(); } private void soutnl(Object o) { sout(o); newLine(); } private void soutCharArray(char[] arr) { for (int i = 0; i < arr.length; i++) { sout(arr[i]); } } private <T extends Number> void soutNumberArray(T[] arr) { for (int i = 0; i < arr.length; i++) { soutnl(arr[i]); } } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
e9911bcdf3c1832dd49d578e5d8c5b98
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.util.concurrent.ThreadLocalRandom; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.Random; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author PersonOfInterest */ 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); BGeorgeAndRound solver = new BGeorgeAndRound(); solver.solve(1, in, out); out.close(); } static class BGeorgeAndRound { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(), m = in.nextInt(); int[] a = in.nextIntArray(n); int[] b = in.nextIntArray(m); Utilities.sortArray(a); Utilities.sortArray(b); int i = 0, j = 0; while (i < n && j < m) { while (j < m && a[i] > b[j]) { j++; } if (j < m && a[i] <= b[j]) { i++; j++; } } out.println(n - i); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } 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 close() { writer.close(); } public void println(int i) { writer.println(i); } } static class Utilities { public static void shuffleArray(int[] ar) { Random rnd = ThreadLocalRandom.current(); for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); int a = ar[index]; ar[index] = ar[i]; ar[i] = a; } } public static void sortArray(int[] ar) { shuffleArray(ar); Arrays.sort(ar); } } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
8b36f6b8be3856bf2b95672061ed4edc
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
import java.util.*; // */ //package pkgimport; /** * * @author mac */ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String line1 = in.nextLine(); String line2 = in.nextLine(); String line3 = in.nextLine(); String[] value1 = line2.split(" "); String[] value2 = line3.split(" "); int pos = 0; int totalSimilar = 0; boolean nextValue = false; for (int i = 0; i < value1.length; i++) { nextValue = false; for (int j = pos; j < value2.length; j++) { if (Integer.parseInt(value1[i]) <= Integer.parseInt(value2[j]) && !nextValue) { totalSimilar++; nextValue = true; pos = j + 1; break; } } } System.out.println(value1.length - totalSimilar); } // 100 50 //101 102 104 105 106 //6228 8162 18735 60716 }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
3aabec6833c035501602a4256f38c975
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
import java.util.Scanner; /** * Created by anhtran on 5/30/17. */ public class George387B { public static int getMinProbNum(int n, int m, int[] require, int[] prep) { // int[] freq = new int[3000+1]; // for(int i = 0; i < prep.length; i++){ // freq[prep[i]]++; // } // int i = 0; // while (i < n){ // for(int j = 0; j < m; j++){ // if (require[i] == prep[j]){ // j+=freq[prep[j]]; // i++; // if(j >= m) { // return 0; // } // } // } // if(i <= n) { // return n-i; // } // else { // i++; // } // // } // return n-i; int count = 0; int i = 0; //require int j = 0; //prep while (j < m && i < n) { if (prep[j] >= require[i]) { i++; j++; count++; } else { j++; } } return n-count; } public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int m = s.nextInt(); int[] require = new int[n]; int[] prep = new int[m]; for(int i = 0; i < n; i++){ require[i] = s.nextInt(); } for(int i = 0; i < m; i++){ prep[i] = s.nextInt(); } System.out.println(getMinProbNum(n,m,require,prep)); } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
b7196e068304de19b284ce6d4b8d94fb
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.lang.reflect.Array; import java.util.*; public class Main{ private void solve() throws Exception{ int[] in = ria(2); int n = in[0], m = in[1]; int[] a = ria(n); int[] b = ria(m); PriorityQueue<Integer> ap = new PriorityQueue<>(), bp = new PriorityQueue<>(); for (int x: a) ap.add(x); for (int x: b) bp.add(x); while(!ap.isEmpty() && !bp.isEmpty()){ int k = ap.peek(); if (k <=bp.poll()) ap.poll(); } println(""+ap.size()); } public static void main(String[] args) throws Exception{ Main m = new Main(); m.solve(); } /* Template Methods */ private BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); private PrintWriter out = new PrintWriter(System.out, true); private StringTokenizer st; /* console write methods */ private void print(String s){ out.print(s); } private void println(String s){ out.println(s); } /* console read methods */ private String rs() throws IOException{ return in.readLine(); } private int ri() throws IOException{ return Integer.parseInt(rs()); } private float rf() throws IOException{ return Float.parseFloat(rs()); } private double rd() throws IOException{ return Double.parseDouble(rs()); } private int[] ria(int size) throws IOException{ int[] a = new int[size]; st = new StringTokenizer(rs(), " "); int i = 0; while(st.hasMoreTokens()){ a[i] = Integer.parseInt(st.nextToken()); i++; } return a; } private long[] rla(int size) throws IOException{ long[] a = new long[size]; st = new StringTokenizer(rs(), " "); int i = 0; while(st.hasMoreTokens()){ a[i] = Long.parseLong(st.nextToken()); i++; } return a; } private ArrayList ral() throws IOException{ ArrayList al = new ArrayList(); st = new StringTokenizer(rs(), " "); while(st.hasMoreTokens()) al.add(st.nextToken()); return al; } /* MAX & MIN */ private int max(int[] a){ int max = Integer.MIN_VALUE; for (int x: a) if (x > max) max = x; return max; } private int min(int[] a){ int min = Integer.MAX_VALUE; for (int x: a) if (x < min) min = x; return min; } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
9c4330f6f5391e6561483fd0efc27c6d
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
import java.io.PrintStream; import java.util.Scanner; public class GeorgeAndRound { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintStream out = new PrintStream(System.out); String[] inputs = in.nextLine().split(" "); int goodRoundProblemSize = Integer.valueOf(inputs[0]); int preparedProblemSize = Integer.valueOf(inputs[1]); String[] goodRoundProblems = in.nextLine().split(" "); String[] preparedProblems = in.nextLine().split(" "); int left, right; left = right = 0; int intLeft, intRight; while (right < preparedProblemSize && left < goodRoundProblemSize) { intRight = Integer.valueOf(preparedProblems[right]); intLeft = Integer.valueOf(goodRoundProblems[left]); if (intLeft <= intRight) { left ++; } right ++; } out.print(goodRoundProblemSize - left); } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
540f31617c7d3fa430be5f9ca80fe199
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
/* * Idea: * - Create 2 arrays: one for the complexities of the problems George has already prepared (prepared[]), * one for the complexities of the problems required (required[]) to make a good round. * - Originally, number of new problems needed n = number of problems required. * - Since both required[] and prepared[] are already sorted, if required[i] <= prepared[j], * decrement n, and increment i and j to continue comparing. * - Otherwise, prepared[j] < required[i], so increment j to compare required[i] with prepared[j + 1] * and continue until prepared[j] >= required[i]. * - Print n. */ import java.util.Scanner; public class George_and_Round { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); //number of problems required int m = sc.nextInt(); //number of problems prepared int[] required = new int[n+1]; //complexities of problems required int[] prepared = new int[m+1]; //complexities of problems prepared for (int i = 1; i <=n; i++) required[i] = sc.nextInt(); for (int i = 1; i <=m; i++) prepared[i] = sc.nextInt(); sc.close(); int j = 1; //pointer of complexities of problems prepared int count = n; //number of new problems needed for (int i = 1; i <= n; i++) //pointer of complexities of problems required { while (j <= m) { if (required[i] <= prepared[j]) { count--; j++; break; } j++; } if (j>m) break; } System.out.println(count); } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
4d6fb26953928906859f0d61da4694cc
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
import java.util.Scanner; public class HW1 { public static void main (String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); int a[] = new int[1000001]; int b[] = new int[1000001]; for (int i = 0; i < n; i++) { a[i] = scanner.nextInt(); } for (int i = 0; i < m; i++) { b[i] = scanner.nextInt(); } int j = 0; for (int i = 0; i < m; i++) { if (b[i] >= a[j] && n > 0) { n--; j++; } } System.out.println(n); } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
cecbac7a4ddb1e0b26584edfb7977f3e
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author o_panda_o(emailofpanda@yahoo.com) */ public class Code_387B_GeorgeAndRound{ public static void main(String[] args){ InputStream inputStream=System.in; OutputStream outputStream=System.out; InputReader in=new InputReader(inputStream); OutputWriter out=new OutputWriter(outputStream); _387B_ solver=new _387B_(); solver.solve(1,in,out); out.close(); } static class _387B_{ public void solve(int testNumber,InputReader in,OutputWriter out){ int n=in.nextInt(), m=in.nextInt(); int[] difficulty=new int[1000001]; int[] problem=new int[1000001]; for(int i=0;i<n;++i) ++difficulty[in.nextInt()]; for(int i=0;i<m;++i) ++problem[in.nextInt()]; int buffer=0; for(int i=1000000;i>=1;--i){ if(problem[i]>0) buffer+=problem[i]; if(difficulty[i]==1&&buffer>0){ --buffer; --n; } } out.print(n); } } 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 close(){ writer.close(); } public void print(int i){ writer.print(i); } } static class InputReader{ private InputStream stream; private byte[] buf=new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream){ this.stream=stream; } public int read(){ if(numChars==-1){ throw new InputMismatchException(); } if(curChar>=numChars){ curChar=0; try{ numChars=stream.read(buf); }catch(IOException e){ throw new InputMismatchException(); } if(numChars<=0){ return -1; } } return buf[curChar++]; } public int nextInt(){ int c=read(); while(isSpaceChar(c)){ c=read(); } int sgn=1; if(c=='-'){ sgn=-1; c=read(); } int res=0; do{ if(c<'0'||c>'9'){ throw new InputMismatchException(); } res*=10; res+=c-'0'; c=read(); }while(!isSpaceChar(c)); return res*sgn; } public boolean isSpaceChar(int c){ if(filter!=null){ return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c){ return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1; } public interface SpaceCharFilter{ public boolean isSpaceChar(int ch); } } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
3af8a00113f69cdd0a4a451a8afb8332
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
/* Author LAVLESH */ import java.util.*; import java.io.*; public class solution { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st=new StringTokenizer(""); static public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } /*static boolean isPalindrome(char[]a){ for(int i=0,j=a.length-1;i<a.length;i++,j--) if(a[i]!=a[j])return false; return true; }*/ //static int gcd(int a,int b){return b==0?a:gcd(b,a%b);} /* public static int lcm(int a,int b, int c){ return lcm(lcm(a,b),c); } public static int lcm(int a, int b){ return a*b/gcd(a,b); }*/ //static long max(long a,long b){return a>b?a:b;} //static int min(int a,int b){return a>=b?b:a;} //static int mod=(int)1e9+7; /*static boolean isprime[]=new boolean[100015]; static void sieve(){ Arrays.fill(isprime,true); boolean visited[]=new boolean[100015]; isprime[0]=false; isprime[1]=false; for(int i=2;i*i<=100015;i++){ visited[i]=true; for(int j=i*i;j<100015;j+=i){ if(!visited[j]){ visited[j]=true; isprime[j]=false; } } } }*/ static int[] nextArray(int n,int x,int y){ int []a=new int[n]; for(int i=x;i<y;i++) a[i]=Integer.parseInt(next()); return a; } /*static void ArrayPrint(int[]a){ for(int i=0;i<a.length;i++) System.out.print(a[i]+" "); System.out.println(); }*/ public static void main(String[]args)throws IOException{ PrintWriter op =new PrintWriter(new BufferedOutputStream(System.out)); int n=Integer.parseInt(next()); int m=Integer.parseInt(next()); int[]a=nextArray(n,0,n); int[]b=nextArray(m,0,m); int last=0; int count=n; for(int i=0;i<n;i++){ int j=last; while(j<m&&(b[j]<=a[i]||b[j]<0))j++; j--; if(j>=0&&b[j]==a[i]){b[j]=-1;count--;last=j;} else if(j<m-1&&b[j+1]>a[i]){b[j+1]=-1;count--;last=j+1;} } op.println(count); op.close(); } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
970f4fbf90c64ae1b94818863f11975d
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String args[]) throws Exception { Scanner sc = new Scanner(System.in); int need = sc.nextInt(); int prep = sc.nextInt(); sc.nextLine(); ArrayList<Integer> needList = new ArrayList<Integer>(); ArrayList<Integer> prepList = new ArrayList<Integer>(); for (int i = 0; i < need; i++) { needList.add(sc.nextInt()); } sc.nextLine(); for (int i = 0; i < prep; i++) { prepList.add(sc.nextInt()); } int count = 0; int p = 0; for (int n = 0; n < need; n++) { while (p < prep && prepList.get(p) < needList.get(n)) { p++; } if (p >= prep) { count += need - n; break; } if (prepList.get(p) >= needList.get(n)) { p++; continue; } else { // while (p < prep && prepList.get(p) < needList.get(n)) { // p++; // } if (prepList.get(p) < needList.get(n)) count++; } } System.out.println(count); } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
db17e445f10ad80c34d4a56ef34d5cb3
train_002.jsonl
1391095800
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
256 megabytes
import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Bai1_387B_GeorgeandRound { /*https://codeforces.com/problemset/problem/387/B*/ public static void main(String[] args) { /* * m problems for the round (mark from 1 to m) * round good => put at least n problems. least 1 pro w exactly a1 * ... to an * simply => c to d (c >= d) by changin limit on iput data * find out minimum pro to come up w m => make good round*/ int n, m; // 1<= n,m <= 3000 - minimal number pro Scanner sc = new Scanner(System.in); n = sc.nextInt(); m = sc.nextInt(); int[] arrComplexReq = new int[n]; int[] arrComplexPre = new int[m]; int[] tmp = new int[1000001]; // Set<Integer> arrComplexPre = new HashSet<>(); for (int i = 0; i < n; i++) { arrComplexReq[i] = sc.nextInt(); } for (int i = 0; i < m; i++) { arrComplexPre[i] = sc.nextInt(); // arrComplexPre.add(sc.nextInt()); } int count = 0; /*for (int i = 0; i < n; i++) { if(!arrComplexPre.contains(arrComplexReq[i])) count++; }*/ /*for (int i = 0; i < n; i++) { // for (int j = 0; j < m; j++) { if (arrComplexPre[m-1] >= arrComplexReq[i] *//*&& tmp[arrComplexPre[j]] == 0*//*) { *//*count++; tmp[arrComplexPre[j]]++;*//* continue; } if(j == m - 1) count++; // } }*/ int j = 0, i = 0; while (j < m && i < n) { if (arrComplexPre[j] >= arrComplexReq[i]) { ++i; ++j; }else { ++j; } } System.out.println(n - i); } }
Java
["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"]
1 second
["0", "2", "3"]
NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
Java 8
standard input
[ "two pointers", "greedy", "brute force" ]
bf0422de4347a308d68a52421fbad0f3
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 &lt; a2 &lt; ... &lt; an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
1,200
Print a single integer — the answer to the problem.
standard output
PASSED
103a306d1148fb96762f4d51d893abbf
train_002.jsonl
1460127600
You are given an array a with n elements. Each element of a is either 0 or 1.Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
256 megabytes
//package src; import java.util.Scanner; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.PriorityQueue; import java.util.Queue; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.Vector; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.StringBuilder; import java.math.BigInteger; public class idk { static Scanner scn = new Scanner(System.in); static int mod = 1000000007; 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(); public static void main(String args[]) { int n=s.nextInt(),k=s.nextInt(); int arr[]=new int[n],zero=0; for(int i=0;i<n;i++) { arr[i]=s.nextInt(); if(arr[i]==0) zero++; } if(k>=zero) { StringBuilder sb=new StringBuilder(); System.out.println(n); for(int i=0;i<n;i++) sb.append("1 "); System.out.println(sb.toString()); } else { int i=0,j=0,max=0,l=0,r=0; StringBuilder sb=new StringBuilder(); while(j<n) { if(arr[j]==0) k--; if(k<0) { if(arr[i]==0) k++; i++; } j++; if(j-i>max) { max=Math.max(max, j-i); l=i;r=j; } } System.out.println(max); i=0; for(i=0;i<l;i++) sb.append(arr[i]+" "); for(;l<r;l++) sb.append("1 "); for(i=r;i<n;i++) sb.append(arr[i]+" "); System.out.print(sb.toString()); } } public static int binarysearch(Long arr[], int l, int r, int key) { int mid, ans = r; while (l <= r) { mid = l + (r - l) / 2; if (arr[mid] == key) { l = mid + 1; ans = mid; } else if (arr[mid] > key) { r = mid - 1; ans = r; } else { l = mid + 1; } } return ans; } public static boolean word(String arr[], String str, String ori, HashMap<String, Integer> map, int i, int j) { if (j == str.length()) { // System.out.println(str); if (str.length() == 0) return true; else return false; } String re = ori.substring(i, j); System.out.println(re); if (map.containsKey(re)) { String res = ori.substring(j); return word(arr, str, ori, map, j, j); } return word(arr, str, ori, map, i, j + 1); } public static int decode(String str, int i, int[] memo) { if (i == 0) { return 1; } int ind = str.length() - i; if (str.charAt(ind) == '0') return 0; if (memo[i] != -1) return memo[i]; int res = decode(str, i - 1, memo); if (i >= 2 && Integer.parseInt(str.substring(ind, ind + 2)) <= 26) { res += decode(str, i - 2, memo); } memo[i] = res; return res; } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static String factorial(int n) { BigInteger fac = new BigInteger("1"); for (int i = 1; i <= n; i++) { fac = fac.multiply(BigInteger.valueOf(i)); } return fac.toString(); } } class pair { int a; int b; pair(int x, int y) { this.a = x; this.b = y; } } class sorting implements Comparator<pair> { @Override public int compare(pair o1, pair o2) { // TODO Auto-generated method stub int x = o1.a - o2.a, y = o1.b - o2.b; if (y == 0) { if (x >= 0) return -1; else return 1; } else if (y > 0) return -1; else return 1; } }
Java
["7 1\n1 0 0 1 1 0 1", "10 2\n1 0 0 1 0 1 0 1 0 1"]
1 second
["4\n1 0 0 1 1 1 1", "5\n1 0 0 1 1 1 1 1 0 1"]
null
Java 8
standard input
[ "dp", "two pointers", "binary search" ]
ec9b03577868d8999bcc54dfc1aae056
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
1,600
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones. On the second line print n integers aj — the elements of the array a after the changes. If there are multiple answers, you can print any one of them.
standard output
PASSED
ec9981593033a8934ef173e05550618c
train_002.jsonl
1460127600
You are given an array a with n elements. Each element of a is either 0 or 1.Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
256 megabytes
// Nice question revise this import java.util.*; import java.io.*; public class lp{ static PrintWriter out = new PrintWriter(System.out); static boolean ok(int len,int k,int p[]){ int i1=0,i2=len-1; while(i2<p.length){ int sum = p[i2]; if(i1>0) sum = sum-p[i1-1]; i1++;i2++; if((sum+k)>=len) return true; } return false; } static void fill(int len,int k,int a[],int p[]){ if(len==0) return; int n = a.length,i1=0,i2=len-1; while(i2<p.length){ int sum = p[i2]; if(i1>0) sum = sum-p[i1-1]; if((sum+k)>=len) break; else {i1++; i2++;} } for(int i=i1;i<=i2;i++) a[i]=1; } public static void main(String[] args){ int n = ni(); int k = ni(); int a[] = new int[n]; int p[] = new int[n]; for(int i=0;i<n;i++){ a[i] = ni(); p[i] = a[i]; if(i>0) p[i] = p[i]+p[i-1]; } int l=0,r=n+1; while((r-l)>1){ int mid = (l+r)/2; if(ok(mid,k,p)==true) l=mid; else r=mid; } fill(l,k,a,p); out.println(l); for(int i=0;i<n;i++) out.print(a[i]+" "); out.flush(); } static FastReader sc=new FastReader(); static int ni(){ int x = sc.nextInt(); return(x); } static long nl(){ long x = sc.nextLong(); return(x); } static String n(){ String str = sc.next(); return(str); } static String ns(){ String str = sc.nextLine(); return(str); } static double nd(){ double d = sc.nextDouble(); return(d); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["7 1\n1 0 0 1 1 0 1", "10 2\n1 0 0 1 0 1 0 1 0 1"]
1 second
["4\n1 0 0 1 1 1 1", "5\n1 0 0 1 1 1 1 1 0 1"]
null
Java 8
standard input
[ "dp", "two pointers", "binary search" ]
ec9b03577868d8999bcc54dfc1aae056
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
1,600
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones. On the second line print n integers aj — the elements of the array a after the changes. If there are multiple answers, you can print any one of them.
standard output
PASSED
b35895f7de1ff481ab889ef8ebaa0be8
train_002.jsonl
1460127600
You are given an array a with n elements. Each element of a is either 0 or 1.Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
256 megabytes
/* Keep solving problems. */ import java.util.*; import java.io.*; public class CF660C { BufferedReader br; PrintWriter out; StringTokenizer st; boolean eof; final long MOD = 1000L * 1000L * 1000L + 7; void solve() throws IOException { int n = nextInt(); int k = nextInt(); int[] arr = nextIntArr(n); int start = 0; int end = 0; int j = 0; int cnt = 0; for(int i = 0; i < n; i++) { if(j < i) { j = i; cnt = 0; } while(j < n) { if(cnt + (1 - arr[j]) > k) { break; } cnt += 1 - arr[j]; j++; } if(j - i > end - start) { start = i; end = j; } if(cnt > 0) { cnt -= 1 - arr[i]; } } outln(end - start); for(int i = 0; i < n; i++) { if(i >= start && i < end) { out(1 + " "); } else { out(arr[i] + " "); } } } 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; } } private void outln(Object o) { out.println(o); } private void out(Object o) { out.print(o); } public CF660C() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } public static void main(String[] args) throws IOException { new CF660C(); } public long[] nextLongArr(int n) throws IOException{ long[] res = new long[n]; for(int i = 0; i < n; i++) res[i] = nextLong(); return res; } public int[] nextIntArr(int n) throws IOException { 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() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }
Java
["7 1\n1 0 0 1 1 0 1", "10 2\n1 0 0 1 0 1 0 1 0 1"]
1 second
["4\n1 0 0 1 1 1 1", "5\n1 0 0 1 1 1 1 1 0 1"]
null
Java 8
standard input
[ "dp", "two pointers", "binary search" ]
ec9b03577868d8999bcc54dfc1aae056
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
1,600
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones. On the second line print n integers aj — the elements of the array a after the changes. If there are multiple answers, you can print any one of them.
standard output
PASSED
0d990e6d92a386f25b813d04a2175768
train_002.jsonl
1460127600
You are given an array a with n elements. Each element of a is either 0 or 1.Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
256 megabytes
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = in.nextInt(); int[] values = new int[n]; for (int i = 0; i < n; i++) { values[i] = in.nextInt(); } int left = 0; int right = 0; int zeroCt = 0; int best = 0; int bestLeft = 0; int bestRight = -1; for (; right < n; right++) { if (values[right] != 1) { if (zeroCt < k) { zeroCt++; } else { while (left <= right && values[left++] == 1) {} } } if((right - left + 1) > best) { best = (right - left + 1); bestLeft = left; bestRight = right; } } StringBuilder s = new StringBuilder(); System.out.println(best); for (int i = 0; i < n; i++) { if (i >= bestLeft && i <= bestRight) { s.append("1 "); } else { s.append(values[i]); s.append(" "); } } System.out.println(s.toString()); } }
Java
["7 1\n1 0 0 1 1 0 1", "10 2\n1 0 0 1 0 1 0 1 0 1"]
1 second
["4\n1 0 0 1 1 1 1", "5\n1 0 0 1 1 1 1 1 0 1"]
null
Java 8
standard input
[ "dp", "two pointers", "binary search" ]
ec9b03577868d8999bcc54dfc1aae056
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
1,600
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones. On the second line print n integers aj — the elements of the array a after the changes. If there are multiple answers, you can print any one of them.
standard output
PASSED
e44d60c6de2da8ba6ce049f53f2431c0
train_002.jsonl
1460127600
You are given an array a with n elements. Each element of a is either 0 or 1.Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class WorkFile { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(), k = s.nextInt(); StringBuilder array = new StringBuilder(); ArrayList<Integer> list = new ArrayList<>(); int length = 0, res = 0, ind = -1; for (int i=0; i<n; i++) { int x = s.nextInt(); array.append(x).append(" "); if (x==0) { list.add(i); length++; } if (length>k) { if (i-list.get(length-1-k)>res) { res = i-list.get(length-1-k); ind = i; } } else if (length==k) { res = i+1; ind = i; } } if (k>=length) { System.out.println(n); array = new StringBuilder(); for (int i=0; i<n; i++) { array.append("1 "); } System.out.println(array); System.exit(0); } if (k==0) { System.out.println(res); System.out.println(array); System.exit(0); } StringBuilder change = new StringBuilder(); for (int i=0; i<res; i++) { change.append("1 "); } array.replace((ind+1)*2-res-res,(ind+1)*2, String.valueOf(change)); System.out.println(res); System.out.println(array); } }
Java
["7 1\n1 0 0 1 1 0 1", "10 2\n1 0 0 1 0 1 0 1 0 1"]
1 second
["4\n1 0 0 1 1 1 1", "5\n1 0 0 1 1 1 1 1 0 1"]
null
Java 8
standard input
[ "dp", "two pointers", "binary search" ]
ec9b03577868d8999bcc54dfc1aae056
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
1,600
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones. On the second line print n integers aj — the elements of the array a after the changes. If there are multiple answers, you can print any one of them.
standard output
PASSED
4bb94e151193f808f7f4d22e05fecd9e
train_002.jsonl
1460127600
You are given an array a with n elements. Each element of a is either 0 or 1.Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
256 megabytes
// No sorceries shall prevail. // import java.util.*; import java.io.*; public class InVoker { static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long mod = 1000000007; public static void main(String args[]) { Scanner inp=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); int n=inp.nextInt(); int k=inp.nextInt(); int leftI=-1,rightI=-1; int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=inp.nextInt(); } int sum[]=new int[n+1]; for(int i=1;i<=n;i++) { sum[i]=sum[i-1]; sum[i]+=a[i-1]; } long gg=0; int left=0,right=k; while(right<=n) { if((right-left)-(sum[right]-sum[left]) > k) { left++; if(left==right) right++; } else if((right-left)-(sum[right]-sum[left]) <= k) { if(right-left>gg) { gg=right-left; leftI=left; rightI=right-1; } right++; } } out.println(gg); //out.println(leftI+" "+rightI); for(int i=0;i<n;i++) { if(i==leftI) { while(i<=rightI) { out.print("1 "); i++; } } if(i<n) { out.print(a[i]+" "); } } out.close(); inp.close(); } }
Java
["7 1\n1 0 0 1 1 0 1", "10 2\n1 0 0 1 0 1 0 1 0 1"]
1 second
["4\n1 0 0 1 1 1 1", "5\n1 0 0 1 1 1 1 1 0 1"]
null
Java 8
standard input
[ "dp", "two pointers", "binary search" ]
ec9b03577868d8999bcc54dfc1aae056
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
1,600
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones. On the second line print n integers aj — the elements of the array a after the changes. If there are multiple answers, you can print any one of them.
standard output
PASSED
9537d2f96a335e5b300cddcaa3667ac5
train_002.jsonl
1460127600
You are given an array a with n elements. Each element of a is either 0 or 1.Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class cf { static int n; static int k; static int arr[]; public static void main(String args[]) { PrintWriter out=new PrintWriter(System.out); Scanner sc = new Scanner(System.in); n= sc.nextInt(); k=sc.nextInt(); arr=new int[n]; for(int i = 0 ;i<n;i++) arr[i]=sc.nextInt(); if(k==0) { int j=0; int count=0; while(j<n) { int i=j; while(j<n&&arr[j]==1) j++; count=Math.max(count,j-i); j++; } out.println(count); for(int i = 0 ;i<n;i++) out.println(arr[i]); out.flush(); System.exit(0); } int i=0; int j=0; int count=0; int ans=0; while(i!=n) { while(j!=n&&!(count==k&&arr[j]==0)) { if(arr[j]==0) ++count; j++; } ans=Math.max(ans,j-i); if(arr[i]==0) count--; i++; } out.println(ans); i=0; j=0; while(i!=n) { while(j!=n&&!(count==k&&arr[j]==0)) { if(arr[j]==0) ++count; j++; } if(ans==j-i) { for(int k = 0;k<i;k++) out.println(arr[k]); for(int k=i;k<j;k++) out.println(1); for(int k=j;k<n;k++) out.println(arr[k]); out.flush(); System.exit(0); } if(arr[i]==0) count--; i++; } out.flush(); } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE];bufferPointer = bytesRead = 0; } public int nextInt() throws IOException { int ret = 0;byte c = read(); while (c <= ' ')c = read(); boolean neg = (c == '-'); if (neg) c = read(); do{ret = ret * 10 + c - '0';} while ((c = read()) >= '0' && c <= '9'); if (neg)return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } } }
Java
["7 1\n1 0 0 1 1 0 1", "10 2\n1 0 0 1 0 1 0 1 0 1"]
1 second
["4\n1 0 0 1 1 1 1", "5\n1 0 0 1 1 1 1 1 0 1"]
null
Java 8
standard input
[ "dp", "two pointers", "binary search" ]
ec9b03577868d8999bcc54dfc1aae056
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
1,600
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones. On the second line print n integers aj — the elements of the array a after the changes. If there are multiple answers, you can print any one of them.
standard output
PASSED
c0c22b3fc25471261d59147d0f5eef39
train_002.jsonl
1460127600
You are given an array a with n elements. Each element of a is either 0 or 1.Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
256 megabytes
import java.util.*; import java.io.*; public class Main{ public static void main(String[] args) { InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); int n = in.nextInt(); int[] a= new int[n]; int k = in.nextInt(); for (int i =0;i<n;i++) a[i] = in.nextInt(); int max=0; int ans0=-1; int ans1=-1; int cntr=0; int p0=0; int p1=0; while (p0<n) { while (p1<n&&cntr<=k) { if (a[p1]==0) cntr++; if (cntr>k) { cntr--; break; } if (max<p1-p0+1) { max=p1-p0+1; ans0=p0; ans1=p1; } p1++; } if (a[p0]==0) cntr--; p0++; } if (ans0>-1) for (int i =ans0;i<=ans1;i++) { if (a[i]==0) a[i]=1; } out.printLine(max); for (int item:a ) { out.print(item+" "); } out.flush(); } static void MergeSort(int[] a, int p, int r) { if (p < r) { int q = (r + p) / 2; MergeSort(a, p, q); MergeSort(a, q + 1, r); Merge(a, p, q, r); } } static void Merge(int[] a, int p, int q, int r) { int n1 = q - p + 1; int n2 = r - q; int[] R = new int[n1 + 1]; int[] L = new int[n2 + 1]; for (int i = 0; i < n1; i++) { R[i] = a[p + i]; } R[n1] = Integer.MAX_VALUE; for (int i = 0; i < n2; i++) { L[i] = a[q + i + 1]; } L[n2] = Integer.MAX_VALUE; int n = a.length; int j = 0; int k = 0; for (int i = p; i <= r; i++) { if (L[j] < R[k]) { a[i] = L[j]; j++; } else { a[i] = R[k]; k++; } } } static void MergeSort(int[] a, int[] b, int p, int r) { if (p < r) { int q = (r + p) / 2; MergeSort(a, b, p, q); MergeSort(a, b, q + 1, r); Merge(a, b, p, q, r); } } static void Merge(int[] a, int[] b, int p, int q, int r) { int n1 = q - p + 1; int n2 = r - q; int[] R = new int[n1 + 1]; int[] L = new int[n2 + 1]; int[] R1 = new int[n1]; int[] L1 = new int[n2]; for (int i = 0; i < n1; i++) { R[i] = a[p + i]; R1[i] = b[p + i]; } R[n1] = Integer.MAX_VALUE; for (int i = 0; i < n2; i++) { L[i] = a[q + i + 1]; L1[i] = b[q + i + 1]; } L[n2] =Integer.MAX_VALUE; int n = a.length; int j = 0; int k = 0; for (int i = p; i <= r; i++) { if (L[j] < R[k]) { a[i] = L[j]; b[i] = L1[j]; j++; } else { a[i] = R[k]; b[i] = R1[k]; k++; } } } } class Graph { int n; LinkedList<Integer>[] adjList; public Graph(int n) { this.n = n; adjList = new LinkedList[n]; for (int i = 0; i < n; i++) adjList[i] = new LinkedList<>(); } } class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream stream) { this.stream = stream; } 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 nextInt() { 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 nextLong() { 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[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = 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; } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } }
Java
["7 1\n1 0 0 1 1 0 1", "10 2\n1 0 0 1 0 1 0 1 0 1"]
1 second
["4\n1 0 0 1 1 1 1", "5\n1 0 0 1 1 1 1 1 0 1"]
null
Java 8
standard input
[ "dp", "two pointers", "binary search" ]
ec9b03577868d8999bcc54dfc1aae056
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
1,600
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones. On the second line print n integers aj — the elements of the array a after the changes. If there are multiple answers, you can print any one of them.
standard output
PASSED
628de6d0a7d02a3f0039ecde79d3031f
train_002.jsonl
1460127600
You are given an array a with n elements. Each element of a is either 0 or 1.Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Solution { static final int MAX_INT = 1000000; public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); int[] arr = new int[n]; st = new StringTokenizer(br.readLine()); for(int i=0 ; i<n ; i++) arr[i] = Integer.parseInt(st.nextToken()); int j=0, count = 0, al=0 , ar = 0; for(int i=0 ; i<n ; i++){ if(j<i){ j = i; count = 0; } //System.out.println(i+" "+j); while(j<n && (count+(1-arr[j]))<=k){ count = count + (1-arr[j]); j++; } // System.out.println(i+" "+j); if(j-i>ar-al){ al = i; ar = j; } if(j==n) break; //System.out.println(al+" "+ar); count = count - (1-arr[i]); } for(int i=al ; i<ar ; i++) arr[i] = 1; System.out.println(ar-al); for(int i:arr) System.out.print(i+" "); System.out.println(); } }
Java
["7 1\n1 0 0 1 1 0 1", "10 2\n1 0 0 1 0 1 0 1 0 1"]
1 second
["4\n1 0 0 1 1 1 1", "5\n1 0 0 1 1 1 1 1 0 1"]
null
Java 8
standard input
[ "dp", "two pointers", "binary search" ]
ec9b03577868d8999bcc54dfc1aae056
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
1,600
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones. On the second line print n integers aj — the elements of the array a after the changes. If there are multiple answers, you can print any one of them.
standard output
PASSED
92f48da1eae1a846e5b91b21481d3b10
train_002.jsonl
1460127600
You are given an array a with n elements. Each element of a is either 0 or 1.Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Solution { static final int MAX_INT = 1000000; public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); int[] arr = new int[n]; st = new StringTokenizer(br.readLine()); for(int i=0 ; i<n ; i++) arr[i] = Integer.parseInt(st.nextToken()); int j=0, count = 0, al=0 , ar = 0; for(int i=0 ; i<n ; i++){ if(j<i){ j = i; count = 0; } //System.out.println(i+" "+j); while(j<n){ int ncount = count + (1-arr[j]); if(ncount>k) break; count = count + (1-arr[j]); j++; } // System.out.println(i+" "+j); if(j-i>ar-al){ al = i; ar = j; } //System.out.println(al+" "+ar); if(j==n) break; count = count - (1-arr[i]); } for(int i=al ; i<ar ; i++) arr[i] = 1; System.out.println(ar-al); for(int i:arr) System.out.print(i+" "); System.out.println(); } }
Java
["7 1\n1 0 0 1 1 0 1", "10 2\n1 0 0 1 0 1 0 1 0 1"]
1 second
["4\n1 0 0 1 1 1 1", "5\n1 0 0 1 1 1 1 1 0 1"]
null
Java 8
standard input
[ "dp", "two pointers", "binary search" ]
ec9b03577868d8999bcc54dfc1aae056
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
1,600
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones. On the second line print n integers aj — the elements of the array a after the changes. If there are multiple answers, you can print any one of them.
standard output
PASSED
5858a21cb751b1093e916a55d67af842
train_002.jsonl
1460127600
You are given an array a with n elements. Each element of a is either 0 or 1.Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
256 megabytes
// package stupiddog; import java.io.*; import java.lang.*; import java.util.*; /** * * @author Huynh Sam Ha - BKU - VN - 1610852@hcmut.edu.vn * @author Stupid - Dog * */ public class StupidDog { public static void main(String[] args) throws IOException { inputSystem = new InputStreamReader(System.in); outputSystem = new OutputStreamWriter(System.out); //inputFile = new FileReader("D:/Java/File/inp.txt"); //outputFile = new FileWriter("D:/Java/File/out.txt"); inp = new BufferedReader(inputSystem); out = new PrintWriter(outputSystem); solve(); inp.close(); out.close(); } static int n, k, a[], f[]; static void solve() throws IOException { n = nextInt(); k =nextInt(); a = new int[n+1]; f = new int[n+1]; f[0] = 0; for (int i = 1; i <= n; i++) { a[i] = nextInt(); if (a[i] == 0) f[i] = f[i-1] + 1; else f[i] = f[i-1]; } int ans = 0, L = 0, R = 0; for (int i = 1; i <= n; i++) { int l = i, r = n, x = -1; while (l <= r) { int m = (l+r)>>1; if (f[m] - f[i-1] <= k) { x = m; l = m+1; } else r = m-1; } if (x-i+1 > ans) { ans = x-i+1; L = i; R = x; } } for (int i = L; i <= R; i++) { a[i] = 1; } out.println(ans); for (int i = 1; i <= n; i++) { out.print(a[i] + " "); } out.println(); } /* ---------------------------------------------------- */ static int gcd(int a, int b) { while (b > 0) { int m = a % b; a = b; b = m; } return a; } static long gcd(long a, long b) { while (b > 0) { long m = a % b; a = b; b = m; } return a; } static BufferedReader inp; static PrintWriter out; static InputStreamReader inputSystem; static OutputStreamWriter outputSystem; static Reader inputFile; static Writer outputFile; static StringTokenizer token; /*NSt + TAB*/ static String nextString() throws IOException { while (token == null || !token.hasMoreElements()) { token = new StringTokenizer(inp.readLine()); } return token.nextToken(); } /*NI + TAB*/ static int nextInt() throws IOException { return Integer.parseInt(nextString()); } /*NL + TAB*/ static long nextLong() throws IOException { return Long.parseLong(nextString()); } /*NDB + TAB*/ static double nextDouble() throws IOException { return Double.parseDouble(nextString()); } } /** * * TAB * out.println(); outln * out.printf(""); outf * * for1n */
Java
["7 1\n1 0 0 1 1 0 1", "10 2\n1 0 0 1 0 1 0 1 0 1"]
1 second
["4\n1 0 0 1 1 1 1", "5\n1 0 0 1 1 1 1 1 0 1"]
null
Java 8
standard input
[ "dp", "two pointers", "binary search" ]
ec9b03577868d8999bcc54dfc1aae056
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
1,600
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones. On the second line print n integers aj — the elements of the array a after the changes. If there are multiple answers, you can print any one of them.
standard output
PASSED
d2a21b6d1cdce75ea780d2f91ea62a34
train_002.jsonl
1460127600
You are given an array a with n elements. Each element of a is either 0 or 1.Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
256 megabytes
import java.io.*; import java.util.*; public class qwe { static FastScanner in = new FastScanner(); //static PrintWriter ww = new PrintWriter(new OutputStreamWriter(System.out)); //static PrintWriter pw = new PrintWriter(System.out); //pw.print();pw.println();pw.printf(); public static void main(String[] args) throws IOException { PrintStream ps = new PrintStream(System.out); //Scanner in=new Scanner(System.in); int n=in.nextInt(); int k=in.nextInt(); int []a=new int[n+1]; int []z=new int[n+1]; int zero=0,p=0; for(int i=1; i<=n; i++){ a[i]=in.nextInt(); if(a[i]==0){ zero++; z[p]=i; p++; } } int ans=0; int[]pre=new int[n+1]; int[]suf=new int[n+2]; int cnt=0; for(int i=1;i<=n; i++){ if(a[i]==1){ cnt++; } else{ cnt=0; } pre[i]=cnt; if(cnt>ans) ans=cnt; } cnt=0; for(int i=n;i>=1;i--){ if(a[i]==1) cnt++; else cnt=0; suf[i]=cnt; } int x=0,y=0; if(zero<k) k=zero; if(k>0) for(int i=0; i<=p-k; i++){ if(pre[z[i]-1]+(z[i+k-1]-z[i]+1)+suf[z[i+k-1]+1]>ans){ ans=pre[z[i]-1]+(z[i+k-1]-z[i]+1)+suf[z[i+k-1]+1]; x=z[i]; y=z[i+k-1]; } } System.out.println(ans); //System.out.println(); for(int i=1;i<=n;i++){ if(i>=x&&i<=y) ps.print("1 "); else ps.print(a[i]+" "); } ps.close(); } } class FastScanner{ BufferedReader s; StringTokenizer st; public FastScanner(){ st = new StringTokenizer(""); s = new BufferedReader(new InputStreamReader(System.in)); } public FastScanner(File f) throws FileNotFoundException{ st = new StringTokenizer(""); s = new BufferedReader (new FileReader(f)); } public int nextInt() throws IOException{ if(st.hasMoreTokens()) return Integer.parseInt(st.nextToken()); else{ st = new StringTokenizer(s.readLine()); return nextInt(); } } public double nextDouble() throws IOException{ if(st.hasMoreTokens()) return Double.parseDouble(st.nextToken()); else{ st = new StringTokenizer(s.readLine()); return nextDouble(); } } public long nextLong() throws IOException{ if(st.hasMoreTokens()) return Long.parseLong(st.nextToken()); else{ st = new StringTokenizer(s.readLine()); return nextLong(); } } public String nextString() throws IOException{ if(st.hasMoreTokens()) return st.nextToken(); else{ st = new StringTokenizer(s.readLine()); return nextString(); } } public String readLine() throws IOException{ return s.readLine(); } public void close() throws IOException{ s.close(); } }
Java
["7 1\n1 0 0 1 1 0 1", "10 2\n1 0 0 1 0 1 0 1 0 1"]
1 second
["4\n1 0 0 1 1 1 1", "5\n1 0 0 1 1 1 1 1 0 1"]
null
Java 8
standard input
[ "dp", "two pointers", "binary search" ]
ec9b03577868d8999bcc54dfc1aae056
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
1,600
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones. On the second line print n integers aj — the elements of the array a after the changes. If there are multiple answers, you can print any one of them.
standard output
PASSED
3124d0b7c8f4c27a700a234c90897e89
train_002.jsonl
1460127600
You are given an array a with n elements. Each element of a is either 0 or 1.Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.Set; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.util.Vector; import java.util.logging.Level; import java.util.logging.Logger; public class Main { public static int pref[]; public static int getNull(int l, int r) { if(l == 0) return pref[r]; else return pref[r] - pref[l - 1]; } public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); InputReader in = new InputReader(inputStream); int n = in.nextInt(), k = in.nextInt(); int[] a = in.nextArray(n); pref = new int[n]; if(a[0] == 0) pref[0] = 1; for(int i = 1; i < n; i++) { if(a[i] == 0) pref[i] = pref[i - 1] + 1; else pref[i] = pref[i - 1]; } int beg = -1, end = -2; for(int i = 0; i < n; i++) { int l = i, r = n - 1, m, ans = -1; while(l <= r) { m = l + r >> 1; if(getNull(i, m) <= k) { ans = m; l = m + 1; }else if(getNull(i, m) > k) { r = m - 1; } } if(ans - i + 1 > end - beg + 1) { beg = i; end = ans; } } for(int i = beg; i <= end; i++) { if(a[i] == 0) a[i] = 1; } out.println(end - beg + 1); for(int i = 0; i < n; i++) { out.print(a[i] + " "); } out.close(); } static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream inputStream) { reader = new BufferedReader(new InputStreamReader(inputStream), 32624); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } private long nextLong() { return Long.parseLong(next()); } private double nextDouble() { return Double.parseDouble(next()); } public int[] nextArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public double[] nextArrayd(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) { a[i] = nextDouble(); } return a; } private BigInteger nextBigInteger() { return new BigInteger(next()); } public void printAr(int[] a) { System.out.print("["); for (long x : a) { System.out.print(x + ","); } System.out.print("]"); } } }
Java
["7 1\n1 0 0 1 1 0 1", "10 2\n1 0 0 1 0 1 0 1 0 1"]
1 second
["4\n1 0 0 1 1 1 1", "5\n1 0 0 1 1 1 1 1 0 1"]
null
Java 8
standard input
[ "dp", "two pointers", "binary search" ]
ec9b03577868d8999bcc54dfc1aae056
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
1,600
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones. On the second line print n integers aj — the elements of the array a after the changes. If there are multiple answers, you can print any one of them.
standard output
PASSED
41dacd6317faf132ec3d2117166053e4
train_002.jsonl
1460127600
You are given an array a with n elements. Each element of a is either 0 or 1.Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; import java.util.stream.Collectors; /** * C. Hard Process * */ public class D2C { public static void main(String args[] ) throws IOException { try (MyScanner in = new MyScanner(); PrintWriter out = new PrintWriter(System.out);) { int n = in.nextInt(); int k = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); // copy a and replace k left zeros. int[] cpy = new int[n]; int c = 0; for (int i = 0; i < n; i++) { if (a[i] == 0 && c < k) { c += 1; cpy[i] = 1; } else { cpy[i] = a[i]; } } int z = 0; int ansr = 0; int cnt = 0; int l = 0; for (int r = 0; r < n; r++) { if (cpy[r] == 1) { cnt += 1; if (z < cnt) { z = cnt; ansr = r; } continue; } if (k == 0) { cnt = 0; continue; } // else if ans[r] == 0 cpy[r] = 1; while (cpy[l] == a[l]) { // until ans[l]==1 && a[l]==0 cnt -= 1; l += 1; } cpy[l] = a[l];// = 0 l += 1; } cnt = z; while (cnt > 0) { cnt -= 1; a[ansr - cnt] = 1; } out.println(z); out.println(Arrays.stream(a).mapToObj(Integer::toString).collect(Collectors.joining(" "))); } } /** see http://codeforces.com/blog/entry/7018 */ static class MyScanner implements AutoCloseable { BufferedReader br; StringTokenizer st; MyScanner() {br = new BufferedReader(new InputStreamReader(System.in));} String next() { while (st == null || !st.hasMoreElements()) try {st = new StringTokenizer(br.readLine());} catch (IOException e) {throw new IllegalStateException(e);} return st.nextToken(); } int nextInt() {return Integer.parseInt(next());} long nextLong() {return Long.parseLong(next());} double nextDouble() {return Double.parseDouble(next());} String nextLine() {try {return br.readLine();} catch (IOException e) {throw new IllegalStateException(e);}} @Override public void close() {try {br.close();} catch (IOException e) {throw new IllegalStateException(e);}} } }
Java
["7 1\n1 0 0 1 1 0 1", "10 2\n1 0 0 1 0 1 0 1 0 1"]
1 second
["4\n1 0 0 1 1 1 1", "5\n1 0 0 1 1 1 1 1 0 1"]
null
Java 8
standard input
[ "dp", "two pointers", "binary search" ]
ec9b03577868d8999bcc54dfc1aae056
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
1,600
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones. On the second line print n integers aj — the elements of the array a after the changes. If there are multiple answers, you can print any one of them.
standard output
PASSED
68a3fa9cb5d6ae585812ce8d7cffba6e
train_002.jsonl
1460127600
You are given an array a with n elements. Each element of a is either 0 or 1.Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class Solution{ public static void main(String[] args){ try { PrintWriter out=new PrintWriter(System.out,true); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String[] ss = br.readLine().split(" "); int n = Integer.parseInt(ss[0]); int k = Integer.parseInt(ss[1]); ss=br.readLine().split(" "); int[] arr = new int[n]; for(int j=0;j<n;j++){ arr[j]=Integer.parseInt(ss[j]); } int[][] dp = new int[n][2]; int z=0,o=0; for(int j=0;j<n;j++){ if(arr[j]==1){ o++; }else{ z++; } dp[j][0]=z; dp[j][1]=o; } int i=0,j=0; int max=0,l=-1; while(j<n){ if(i==j){ if(dp[j][0]<=k){ l=j; } j++; continue; } if (dp[j][0] - dp[i][0] <= k) { if(max<j-i){ max=j-i; l=j; } j++; }else{ i++; } } while(l>=0 && k>0){ if(arr[l]==0){ arr[l]=1; k--; } l--; } int[] brr=new int[n]; brr[0]=arr[0]; int ans=brr[0]; StringBuilder str=new StringBuilder(arr[0]+" "); for(int p=1;p<n;p++){ str.append(arr[p]).append(" "); if(arr[p]==1){ brr[p]=brr[p-1]+1; } ans=Math.max(ans,brr[p]); } out.println(ans); out.println(str); out.close(); } catch (Exception e) { System.out.println("kkkk "+ e.getMessage()); } } static boolean isPal(String s){ StringBuilder str=new StringBuilder(s); str=str.reverse(); String ss=String.valueOf(str); if (ss.equals(s)) { return true; } return false; } static int mod(int a,int b){ if (a>b){ return a-b; } return b-a; } 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 void dfs(ArrayList<HashSet<Integer>> list,boolean[] vis,int i){ vis[i]=true; Iterator<Integer> it=list.get(i).iterator(); while(it.hasNext()){ int u=it.next(); if(!vis[u]){ dfs(list,vis,u); } } } 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
["7 1\n1 0 0 1 1 0 1", "10 2\n1 0 0 1 0 1 0 1 0 1"]
1 second
["4\n1 0 0 1 1 1 1", "5\n1 0 0 1 1 1 1 1 0 1"]
null
Java 8
standard input
[ "dp", "two pointers", "binary search" ]
ec9b03577868d8999bcc54dfc1aae056
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
1,600
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones. On the second line print n integers aj — the elements of the array a after the changes. If there are multiple answers, you can print any one of them.
standard output
PASSED
f293576aaa8c302cab4df79a6cb20c58
train_002.jsonl
1460127600
You are given an array a with n elements. Each element of a is either 0 or 1.Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
256 megabytes
import java.util.Scanner; public class HardProcess4 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); boolean[] a = new boolean[n]; int[] s = new int[n]; // boolean debug = false; int[] zeroindexes = new int[n]; int whichzero = 0; int zeronum = 0; int firstchangedzero = -1; int used = 0; int sum = 0; int ans = -1; int start = 0; int beststart = -1; int bestend = -1; int shifted = 0; for(int i = 0; i < n; i++) { int in = sc.nextInt(); a[i] = in == 1; if(a[i]) //1 { } else //0 { zeroindexes[zeronum] = i; zeronum += 1; } sum += in; s[i] = sum; if(a[i]) { } else { if(k == 0) { start = zeroindexes[whichzero]; whichzero += 1; } else { if(used >= k) { start = firstchangedzero + 1; // System.out.println("Shift at " + i + " " + zeroindexes); if(whichzero < zeronum) { firstchangedzero = zeroindexes[whichzero]; whichzero += 1; } if(shifted == 0 && whichzero < zeronum) { start = firstchangedzero + 1; firstchangedzero = zeroindexes[whichzero]; whichzero += 1; } shifted += 1; // if(debug)System.out.println(i + " Shifting fcz to " + firstchangedzero + ", start " + start); } else { used += 1; } } } int cur; if(start == 0) { cur = s[i] + used; } else { cur = s[i] - s[start-1] + used; } if(cur > ans) { ans = cur; beststart = start; bestend = i; } } // if(debug)System.out.println("S " + beststart + " E " + bestend); System.out.println(ans); int rem = used; StringBuilder sb = new StringBuilder(); for(int i = 0; i < n; i++) { if(i >= beststart && i <= bestend) { if(!a[i]) { if(rem > 0) { rem -= 1; sb.append(1); } else { sb.append(0); } } else { sb.append(1); } } else { if(a[i]) sb.append(1); else sb.append(0); } if(i < n-1) sb.append(" "); } System.out.println(sb); } }
Java
["7 1\n1 0 0 1 1 0 1", "10 2\n1 0 0 1 0 1 0 1 0 1"]
1 second
["4\n1 0 0 1 1 1 1", "5\n1 0 0 1 1 1 1 1 0 1"]
null
Java 8
standard input
[ "dp", "two pointers", "binary search" ]
ec9b03577868d8999bcc54dfc1aae056
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
1,600
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones. On the second line print n integers aj — the elements of the array a after the changes. If there are multiple answers, you can print any one of them.
standard output
PASSED
5cfb3633c7c127c62b00c4fd36626a67
train_002.jsonl
1460127600
You are given an array a with n elements. Each element of a is either 0 or 1.Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
256 megabytes
import java.io.*; import java.util.*; public class hard_process { public int advanceL(int []a, int l) { for (int i = l; i< a.length; i++) { if (a[i]==0) return(i+1); } return(0); } public void printSequence(int[] a, int l_max, int r_max, int k) { int n = a.length; StringBuilder ans = new StringBuilder(); //System.out.println(l_max +" --> "+r_max); for (int k1 = 0; k1< n;k1++) { if ((k1>= l_max)&&(k1<=r_max)&&(k>0)) a[k1] = 1; ans.append(a[k1]).append(" "); } System.out.println(ans.toString().trim()); } public static void main(String args[])throws IOException { Scanner sc = new Scanner(System.in); hard_process hp = new hard_process(); int n = sc.nextInt(); int k = sc.nextInt(); int [] a = new int [n]; for (int i = 0; i< n; i++) a[i] = sc.nextInt(); int [] sum = new int [n]; int cum = 0; for (int i = 0; i< n; i++) { cum +=a[i]; sum[i] = cum; } int l = 0; int r = 0; int c = 0; int i = 0; for (i = 0; i< n; i++) { if (a[i]==0) c++; if (c==k+1) break; } r = i-1; int max_seq = r-l+1; int r_max = r; int l_max = l; //for (int k1 = 0; k1< n;k1++) //System.out.print(sum[k1]+" "); //System.out.println(max_seq); //hp.printSequence(a, l, r, k); //System.out.println(); r = r+1; while ((l < n) && (r < n)) { if (a[r] == 0) { l = hp.advanceL(a, l); //System.out.println(l+" here "); if (r-l+1>max_seq) { max_seq = r-l+1; r_max = r; l_max = l; } //System.out.println(l+" here " +r); } else { if (r-l+1>max_seq) { max_seq = r-l+1; r_max = r; l_max = l; } } r = r+1; } System.out.println(max_seq); hp.printSequence(a, l_max, r_max, k); } }
Java
["7 1\n1 0 0 1 1 0 1", "10 2\n1 0 0 1 0 1 0 1 0 1"]
1 second
["4\n1 0 0 1 1 1 1", "5\n1 0 0 1 1 1 1 1 0 1"]
null
Java 8
standard input
[ "dp", "two pointers", "binary search" ]
ec9b03577868d8999bcc54dfc1aae056
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
1,600
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones. On the second line print n integers aj — the elements of the array a after the changes. If there are multiple answers, you can print any one of them.
standard output
PASSED
d6914d2e9f0dcd1c1f15bf24138fcefa
train_002.jsonl
1460127600
You are given an array a with n elements. Each element of a is either 0 or 1.Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class CFC { public static void main(String[] args) throws IOException { FastScanner9 in = new FastScanner9(); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int k = in.nextInt(); int[] a = new int[n+1]; int[] sum = new int[n+1]; a[0] = 0; for (int i = 1; i<=n; i++){ a[i] = in.nextInt(); if (a[i]==0) sum[i] = sum[i-1]+1; else sum[i] = sum[i-1]; } int l = 0; int r = n+1; int cz = 0; int ans = Integer.MIN_VALUE; while (l<r){ int m = (l+r)/2; if (!f(a, sum, m, k)){ r = m; } else { l = m+1; } } l--; out.println(l); for (int i = 1; i<=n-l+1; i++){ if (sum[i+l-1]-sum[i-1]<=k){ for (int j = i; j<i+l; j++){ a[j] = 1; } break; } } for (int i = 1; i<=n; i++) out.print(a[i]+" "); out.close(); } static boolean f(int[] a, int[] sum, int d, int k){ int n = a.length; for (int i = 1; i<n-d+1; i++){ if (sum[i+d-1]-sum[i-1]<=k){ return true; } } return false; } } class FastScanner9{ BufferedReader reader; StringTokenizer tokenizer; FastScanner9() throws IOException{ reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; } String next() throws IOException{ while (tokenizer == null||!tokenizer.hasMoreTokens()){ tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } int nextInt() throws IOException{ return Integer.parseInt(next()); } long nextLong() throws IOException{ return Long.parseLong(next()); } double nextDouble() throws IOException{ return Double.parseDouble(next()); } }
Java
["7 1\n1 0 0 1 1 0 1", "10 2\n1 0 0 1 0 1 0 1 0 1"]
1 second
["4\n1 0 0 1 1 1 1", "5\n1 0 0 1 1 1 1 1 0 1"]
null
Java 8
standard input
[ "dp", "two pointers", "binary search" ]
ec9b03577868d8999bcc54dfc1aae056
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
1,600
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones. On the second line print n integers aj — the elements of the array a after the changes. If there are multiple answers, you can print any one of them.
standard output
PASSED
cd9ebc7564128ecee3cdc1acbee33295
train_002.jsonl
1566484500
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $$$[0, n]$$$ on $$$OX$$$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $$$(x, x + 1)$$$ with integer $$$x$$$. So we can represent the road as a binary string consisting of $$$n$$$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.Usually, we can install the pipeline along the road on height of $$$1$$$ unit with supporting pillars in each integer point (so, if we are responsible for $$$[0, n]$$$ road, we must install $$$n + 1$$$ pillars). But on crossroads we should lift the pipeline up to the height $$$2$$$, so the pipeline won't obstruct the way for cars.We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $$$[x, x + 1]$$$ with integer $$$x$$$ consisting of three parts: $$$0.5$$$ units of horizontal pipe + $$$1$$$ unit of vertical pipe + $$$0.5$$$ of horizontal. Note that if pipeline is currently on height $$$2$$$, the pillars that support it should also have length equal to $$$2$$$ units. Each unit of gas pipeline costs us $$$a$$$ bourles, and each unit of pillar — $$$b$$$ bourles. So, it's not always optimal to make the whole pipeline on the height $$$2$$$. Find the shape of the pipeline with minimum possible cost and calculate that cost.Note that you must start and finish the pipeline on height $$$1$$$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { static long t, n, a, b; static String s; static long[][][] memo; public static long dp(int indx, int height, int next) { if(indx == n - 1) return (height + 1) * b + (height == 2 ? 2 * a : a); if(memo[indx][height][next] != -1) return memo[indx][height][next]; int newNext = (indx + 2 < n ? s.charAt(indx + 2) - '0' : 0); if(next == 1 || s.charAt(indx) == '1') { memo[indx][height][next] = dp(indx + 1, 2, newNext); memo[indx][height][next] += height * b + (height == 1 ? 2 * a : a); return memo[indx][height][next]; } else { long val1 = dp(indx + 1, 2, newNext) + height * b + (height != 2 ? 2 * a : a); long val2 = dp(indx + 1, 1, newNext) + height * b + (height != 1 ? 2 * a : a); return memo[indx][height][next] = Math.min(val1, val2); } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); t = scanner.nextInt(); for(int test = 0 ; test < t ; test++) { n = scanner.nextInt(); a = scanner.nextInt(); b = scanner.nextInt(); s = scanner.next(); memo = new long[(int)n + 1][3][3]; for(int i = 0 ; i <= n ; i++) for(int j = 0 ; j < 3 ; j++) for(int k = 0 ; k < 3 ; k++) memo[i][j][k] = -1; System.out.println(dp(0, 1, s.charAt(1) - '0')); } scanner.close(); } }
Java
["4\n8 2 5\n00110010\n8 1 1\n00110010\n9 100000000 100000000\n010101010\n2 5 1\n00"]
2 seconds
["94\n25\n2900000000\n13"]
NoteThe optimal pipeline for the first query is shown at the picture above.The optimal pipeline for the second query is pictured below: The optimal (and the only possible) pipeline for the third query is shown below: The optimal pipeline for the fourth query is shown below:
Java 11
standard input
[ "dp", "greedy" ]
4fa609ef581f705df901f7532b52cf7c
The fist line contains one integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of queries. Next $$$2 \cdot T$$$ lines contain independent queries — one query per two lines. The first line contains three integers $$$n$$$, $$$a$$$, $$$b$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le a \le 10^8$$$, $$$1 \le b \le 10^8$$$) — the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $$$s$$$ ($$$|s| = n$$$, $$$s_i \in \{0, 1\}$$$, $$$s_1 = s_n = 0$$$) — the description of the road. It's guaranteed that the total length of all strings $$$s$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,500
Print $$$T$$$ integers — one per query. For each query print the minimum possible cost of the constructed pipeline.
standard output
PASSED
5953b8e24b17ae40e33e3be82e5fd0ee
train_002.jsonl
1566484500
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $$$[0, n]$$$ on $$$OX$$$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $$$(x, x + 1)$$$ with integer $$$x$$$. So we can represent the road as a binary string consisting of $$$n$$$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.Usually, we can install the pipeline along the road on height of $$$1$$$ unit with supporting pillars in each integer point (so, if we are responsible for $$$[0, n]$$$ road, we must install $$$n + 1$$$ pillars). But on crossroads we should lift the pipeline up to the height $$$2$$$, so the pipeline won't obstruct the way for cars.We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $$$[x, x + 1]$$$ with integer $$$x$$$ consisting of three parts: $$$0.5$$$ units of horizontal pipe + $$$1$$$ unit of vertical pipe + $$$0.5$$$ of horizontal. Note that if pipeline is currently on height $$$2$$$, the pillars that support it should also have length equal to $$$2$$$ units. Each unit of gas pipeline costs us $$$a$$$ bourles, and each unit of pillar — $$$b$$$ bourles. So, it's not always optimal to make the whole pipeline on the height $$$2$$$. Find the shape of the pipeline with minimum possible cost and calculate that cost.Note that you must start and finish the pipeline on height $$$1$$$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.*; public class cf1207c { public static void main(String[] args) throws IOException { int t = ri(); while (t --> 0) { int n = rni(), a = ni(), b = ni(), c[] = new int[n]; char[] s = rcha(); for (int i = 0; i < n; ++i) { c[i] = s[i] - '0'; } long ans = b, cur_dip = 0; for (int i = 0; i < n; ++i) { if (c[i] == 0) { ans += a + b; if (i > 0 && c[i - 1] == 1) { ans += a; ++cur_dip; } else if (cur_dip > 0) { ++cur_dip; } if (i < n - 1 && c[i + 1] == 1) { ans += a + b; if (cur_dip > 0) { if (cur_dip == 1) { ans -= a + a; } else { --cur_dip; ans = min(ans, ans - 2 * a + cur_dip * b); } cur_dip = 0; } } } else { ans += a + b + b; } } prln(ans); } close(); } static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out)); static StringTokenizer input; static Random __rand = new Random(); // references // IBIG = 1e9 + 7 // IMAX ~= 2e10 // LMAX ~= 9e18 // constants static final int IBIG = 1000000007; static final int IMAX = 2147483647; static final int IMIN = -2147483648; static final long LMAX = 9223372036854775807L; static final long LMIN = -9223372036854775808L; // math util static int minof(int a, int b, int c) {return min(a, min(b, c));} static int minof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;} static long minof(long a, long b, long c) {return min(a, min(b, c));} static long minof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;} static int maxof(int a, int b, int c) {return max(a, max(b, c));} static int maxof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;} static long maxof(long a, long b, long c) {return max(a, max(b, c));} static long maxof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;} static int powi(int a, int b) {if(a == 0) return 0; int ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static long powl(long a, int b) {if(a == 0) return 0; long ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static int fli(double d) {return (int)d;} static int cei(double d) {return (int)ceil(d);} static long fll(double d) {return (long)d;} static long cel(double d) {return (long)ceil(d);} static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);} static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);} static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;} static long hash(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);} // array util static void reverse(int[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void reverse(long[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void reverse(char[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void shuffle(int[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void shuffle(long[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void rsort(int[] a) {shuffle(a); sort(a);} static void rsort(long[] a) {shuffle(a); sort(a);} static int[] copy(int[] a) {int[] ans = new int[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static long[] copy(long[] a) {long[] ans = new long[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static char[] copy(char[] a) {char[] ans = new char[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static int[] sorted(int[] a) {int[] ans = copy(a); sort(ans); return ans;} static long[] sorted(long[] a) {long[] ans = copy(a); sort(ans); return ans;} static int[] rsorted(int[] a) {int[] ans = copy(a); rsort(ans); return ans;} static long[] rsorted(long[] a) {long[] ans = copy(a); rsort(ans); return ans;} // graph util static List<List<Integer>> graph(int n) {List<List<Integer>> g = new ArrayList<>(); for(int i = 0; i < n; ++i) g.add(new ArrayList<>()); return g;} static List<List<Integer>> graph(List<List<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) connect(g, rni() - 1, ni() - 1); return g;} static List<List<Integer>> graph(int n, int m) throws IOException {return graph(graph(n), m);} static List<List<Integer>> dgraph(List<List<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) connecto(g, rni() - 1, ni() - 1); return g;} static List<List<Integer>> dgraph(List<List<Integer>> g, int n, int m) throws IOException {return dgraph(graph(n), m);} static List<Set<Integer>> sgraph(int n) {List<Set<Integer>> g = new ArrayList<>(); for(int i = 0; i < n; ++i) g.add(new HashSet<>()); return g;} static List<Set<Integer>> sgraph(List<Set<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) connect(g, rni() - 1, ni() - 1); return g;} static List<Set<Integer>> sgraph(int n, int m) throws IOException {return sgraph(sgraph(n), m);} static List<Set<Integer>> dsgraph(List<Set<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) connecto(g, rni() - 1, ni() - 1); return g;} static List<Set<Integer>> dsgraph(List<Set<Integer>> g, int n, int m) throws IOException {return dsgraph(sgraph(n), m);} static void connect(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v); g.get(v).add(u);} static void connecto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v);} static void dconnect(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v); g.get(v).remove(u);} static void dconnecto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v);} // input static void r() throws IOException {input = new StringTokenizer(__in.readLine());} static int ri() throws IOException {return Integer.parseInt(__in.readLine());} static long rl() throws IOException {return Long.parseLong(__in.readLine());} static int[] ria(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()); return a;} static int[] riam1(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()) - 1; return a;} static long[] rla(int n) throws IOException {long[] a = new long[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Long.parseLong(input.nextToken()); return a;} static char[] rcha() throws IOException {return __in.readLine().toCharArray();} static String rline() throws IOException {return __in.readLine();} static int rni() throws IOException {input = new StringTokenizer(__in.readLine()); return Integer.parseInt(input.nextToken());} static int ni() {return Integer.parseInt(input.nextToken());} static long rnl() throws IOException {input = new StringTokenizer(__in.readLine()); return Long.parseLong(input.nextToken());} static long nl() {return Long.parseLong(input.nextToken());} // output static void pr(int i) {__out.print(i);} static void prln(int i) {__out.println(i);} static void pr(long l) {__out.print(l);} static void prln(long l) {__out.println(l);} static void pr(double d) {__out.print(d);} static void prln(double d) {__out.println(d);} static void pr(char c) {__out.print(c);} static void prln(char c) {__out.println(c);} static void pr(char[] s) {__out.print(new String(s));} static void prln(char[] s) {__out.println(new String(s));} static void pr(String s) {__out.print(s);} static void prln(String s) {__out.println(s);} static void pr(Object o) {__out.print(o);} static void prln(Object o) {__out.println(o);} static void prln() {__out.println();} static void pryes() {__out.println("yes");} static void pry() {__out.println("Yes");} static void prY() {__out.println("YES");} static void prno() {__out.println("no");} static void prn() {__out.println("No");} static void prN() {__out.println("NO");} static void pryesno(boolean b) {__out.println(b ? "yes" : "no");}; static void pryn(boolean b) {__out.println(b ? "Yes" : "No");} static void prYN(boolean b) {__out.println(b ? "YES" : "NO");} static void prln(int... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);} static void prln(long... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);} static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for(int i = 0; i < n; __out.print(iter.next()), __out.print(' '), ++i); if(n >= 0) __out.println(iter.next()); else __out.println();} static void h() {__out.println("hlfd");} static void flush() {__out.flush();} static void close() {__out.close();} }
Java
["4\n8 2 5\n00110010\n8 1 1\n00110010\n9 100000000 100000000\n010101010\n2 5 1\n00"]
2 seconds
["94\n25\n2900000000\n13"]
NoteThe optimal pipeline for the first query is shown at the picture above.The optimal pipeline for the second query is pictured below: The optimal (and the only possible) pipeline for the third query is shown below: The optimal pipeline for the fourth query is shown below:
Java 11
standard input
[ "dp", "greedy" ]
4fa609ef581f705df901f7532b52cf7c
The fist line contains one integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of queries. Next $$$2 \cdot T$$$ lines contain independent queries — one query per two lines. The first line contains three integers $$$n$$$, $$$a$$$, $$$b$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le a \le 10^8$$$, $$$1 \le b \le 10^8$$$) — the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $$$s$$$ ($$$|s| = n$$$, $$$s_i \in \{0, 1\}$$$, $$$s_1 = s_n = 0$$$) — the description of the road. It's guaranteed that the total length of all strings $$$s$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,500
Print $$$T$$$ integers — one per query. For each query print the minimum possible cost of the constructed pipeline.
standard output
PASSED
719a538347a3c155cbef15a3e9695a87
train_002.jsonl
1566484500
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $$$[0, n]$$$ on $$$OX$$$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $$$(x, x + 1)$$$ with integer $$$x$$$. So we can represent the road as a binary string consisting of $$$n$$$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.Usually, we can install the pipeline along the road on height of $$$1$$$ unit with supporting pillars in each integer point (so, if we are responsible for $$$[0, n]$$$ road, we must install $$$n + 1$$$ pillars). But on crossroads we should lift the pipeline up to the height $$$2$$$, so the pipeline won't obstruct the way for cars.We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $$$[x, x + 1]$$$ with integer $$$x$$$ consisting of three parts: $$$0.5$$$ units of horizontal pipe + $$$1$$$ unit of vertical pipe + $$$0.5$$$ of horizontal. Note that if pipeline is currently on height $$$2$$$, the pillars that support it should also have length equal to $$$2$$$ units. Each unit of gas pipeline costs us $$$a$$$ bourles, and each unit of pillar — $$$b$$$ bourles. So, it's not always optimal to make the whole pipeline on the height $$$2$$$. Find the shape of the pipeline with minimum possible cost and calculate that cost.Note that you must start and finish the pipeline on height $$$1$$$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; public class Rextester{ public static void main(String[] args)throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); StringBuffer sb = new StringBuffer(); while(t-->0){ StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); long a = Long.parseLong(st.nextToken()); long b = Long.parseLong(st.nextToken()); String s = br.readLine(); long ch = b,cl=b; for(int i=0;i<n;i++){ if(i==0){ cl = cl+a+b; ch = ch+2*a+2*b; continue; } if(s.charAt(i)=='0'){ if(cl!=-1){ long temp = Math.min(ch+a+2*b,cl+2*a+2*b); cl = Math.min(ch+2*a+b,cl+a+b); ch = temp; } else{ long temp = ch+a+2*b; cl = ch+2*a+b; ch = temp; } } else{ if(cl!=-1){ ch = ch+a+2*b; cl = -1; } else{ ch = ch+a+2*b; cl = -1; } } } sb.append(cl).append("\n"); } br.close(); System.out.println(sb); } }
Java
["4\n8 2 5\n00110010\n8 1 1\n00110010\n9 100000000 100000000\n010101010\n2 5 1\n00"]
2 seconds
["94\n25\n2900000000\n13"]
NoteThe optimal pipeline for the first query is shown at the picture above.The optimal pipeline for the second query is pictured below: The optimal (and the only possible) pipeline for the third query is shown below: The optimal pipeline for the fourth query is shown below:
Java 11
standard input
[ "dp", "greedy" ]
4fa609ef581f705df901f7532b52cf7c
The fist line contains one integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of queries. Next $$$2 \cdot T$$$ lines contain independent queries — one query per two lines. The first line contains three integers $$$n$$$, $$$a$$$, $$$b$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le a \le 10^8$$$, $$$1 \le b \le 10^8$$$) — the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $$$s$$$ ($$$|s| = n$$$, $$$s_i \in \{0, 1\}$$$, $$$s_1 = s_n = 0$$$) — the description of the road. It's guaranteed that the total length of all strings $$$s$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,500
Print $$$T$$$ integers — one per query. For each query print the minimum possible cost of the constructed pipeline.
standard output
PASSED
bb01c25468fe80fd32edc03686dc2ab4
train_002.jsonl
1566484500
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $$$[0, n]$$$ on $$$OX$$$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $$$(x, x + 1)$$$ with integer $$$x$$$. So we can represent the road as a binary string consisting of $$$n$$$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.Usually, we can install the pipeline along the road on height of $$$1$$$ unit with supporting pillars in each integer point (so, if we are responsible for $$$[0, n]$$$ road, we must install $$$n + 1$$$ pillars). But on crossroads we should lift the pipeline up to the height $$$2$$$, so the pipeline won't obstruct the way for cars.We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $$$[x, x + 1]$$$ with integer $$$x$$$ consisting of three parts: $$$0.5$$$ units of horizontal pipe + $$$1$$$ unit of vertical pipe + $$$0.5$$$ of horizontal. Note that if pipeline is currently on height $$$2$$$, the pillars that support it should also have length equal to $$$2$$$ units. Each unit of gas pipeline costs us $$$a$$$ bourles, and each unit of pillar — $$$b$$$ bourles. So, it's not always optimal to make the whole pipeline on the height $$$2$$$. Find the shape of the pipeline with minimum possible cost and calculate that cost.Note that you must start and finish the pipeline on height $$$1$$$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
256 megabytes
import java.io.*; import java.util.*; public class Main { private void solve()throws IOException { int n=nextInt(); int a=nextInt(); int b=nextInt(); long ans=0; String s=nextLine(); int i=0; boolean first=true; while(i<n) if(s.charAt(i)=='1') { int trail=0; while(s.charAt(i)=='1') { trail++; i++; } ans+=1l*(trail+1)*2*b+1l*trail*a+4l*a; } else { int trail=0; while(i<n && s.charAt(i)=='0') { trail++; i++; } if(i==n && first) ans+=1l*trail*a+1l*(trail+1)*b; else if(i==n || first) ans+=1l*(trail-1)*a+1l*trail*b; else ans+=1l*(trail-2)*a+1l*(trail-1)*b+Math.min(0,-2l*a+1l*(trail-1)*b); first=false; } out.println(ans); } /////////////////////////////////////////////////////////// public void run()throws IOException { br=new BufferedReader(new InputStreamReader(System.in)); st=null; out=new PrintWriter(System.out); int t=nextInt(); while(t-->0) solve(); br.close(); out.close(); } public static void main(String args[])throws IOException{ new Main().run(); } BufferedReader br; StringTokenizer st; PrintWriter out; String nextToken()throws IOException{ while(st==null || !st.hasMoreTokens()) st=new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine()throws IOException{ return br.readLine(); } int nextInt()throws IOException{ return Integer.parseInt(nextToken()); } long nextLong()throws IOException{ return Long.parseLong(nextToken()); } double nextDouble()throws IOException{ return Double.parseDouble(nextToken()); } }
Java
["4\n8 2 5\n00110010\n8 1 1\n00110010\n9 100000000 100000000\n010101010\n2 5 1\n00"]
2 seconds
["94\n25\n2900000000\n13"]
NoteThe optimal pipeline for the first query is shown at the picture above.The optimal pipeline for the second query is pictured below: The optimal (and the only possible) pipeline for the third query is shown below: The optimal pipeline for the fourth query is shown below:
Java 11
standard input
[ "dp", "greedy" ]
4fa609ef581f705df901f7532b52cf7c
The fist line contains one integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of queries. Next $$$2 \cdot T$$$ lines contain independent queries — one query per two lines. The first line contains three integers $$$n$$$, $$$a$$$, $$$b$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le a \le 10^8$$$, $$$1 \le b \le 10^8$$$) — the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $$$s$$$ ($$$|s| = n$$$, $$$s_i \in \{0, 1\}$$$, $$$s_1 = s_n = 0$$$) — the description of the road. It's guaranteed that the total length of all strings $$$s$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,500
Print $$$T$$$ integers — one per query. For each query print the minimum possible cost of the constructed pipeline.
standard output
PASSED
1c5228743e7a24ef983cee9df91729f9
train_002.jsonl
1566484500
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $$$[0, n]$$$ on $$$OX$$$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $$$(x, x + 1)$$$ with integer $$$x$$$. So we can represent the road as a binary string consisting of $$$n$$$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.Usually, we can install the pipeline along the road on height of $$$1$$$ unit with supporting pillars in each integer point (so, if we are responsible for $$$[0, n]$$$ road, we must install $$$n + 1$$$ pillars). But on crossroads we should lift the pipeline up to the height $$$2$$$, so the pipeline won't obstruct the way for cars.We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $$$[x, x + 1]$$$ with integer $$$x$$$ consisting of three parts: $$$0.5$$$ units of horizontal pipe + $$$1$$$ unit of vertical pipe + $$$0.5$$$ of horizontal. Note that if pipeline is currently on height $$$2$$$, the pillars that support it should also have length equal to $$$2$$$ units. Each unit of gas pipeline costs us $$$a$$$ bourles, and each unit of pillar — $$$b$$$ bourles. So, it's not always optimal to make the whole pipeline on the height $$$2$$$. Find the shape of the pipeline with minimum possible cost and calculate that cost.Note that you must start and finish the pipeline on height $$$1$$$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
256 megabytes
import java.util.*; public class GasPipeline { public static void main(String[] args) { Scanner s = new Scanner(System.in); int tc = s.nextInt(); for (int t = 0; t < tc; t++) { int n = s.nextInt(); long a = s.nextLong(), b = s.nextLong(); char[] cs = s.next().toCharArray(); int i = 0; long curH = 1, total = b; while (i < cs.length - 1) { //System.out.println(i + " " + curH + " " + total); if (cs[i] == cs[i+1]) { total += curH * b + a; i++; } else if (cs[i] == '1' && cs[i+1] == '0') { total += curH * b + a; int j = i + 1; long zCnt = 0; while (j < cs.length && cs[j] == '0') { zCnt++; j++; } if (j == cs.length) { total += (zCnt - 1) * b + 2 * a + a * (zCnt - 2); curH = 1; break; } else if (zCnt < 2) { i++; continue; } long down = a * zCnt + 2 * a + zCnt * b + b; long up = a * zCnt + 2 * zCnt * b; if (down <= up) { total += a; curH--; } i++; } else { if (curH == 1) { curH++; total += curH * b + 2 * a; } else total += curH * b + a; i++; } } total += b + a; System.out.println(total); } s.close(); } }
Java
["4\n8 2 5\n00110010\n8 1 1\n00110010\n9 100000000 100000000\n010101010\n2 5 1\n00"]
2 seconds
["94\n25\n2900000000\n13"]
NoteThe optimal pipeline for the first query is shown at the picture above.The optimal pipeline for the second query is pictured below: The optimal (and the only possible) pipeline for the third query is shown below: The optimal pipeline for the fourth query is shown below:
Java 11
standard input
[ "dp", "greedy" ]
4fa609ef581f705df901f7532b52cf7c
The fist line contains one integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of queries. Next $$$2 \cdot T$$$ lines contain independent queries — one query per two lines. The first line contains three integers $$$n$$$, $$$a$$$, $$$b$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le a \le 10^8$$$, $$$1 \le b \le 10^8$$$) — the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $$$s$$$ ($$$|s| = n$$$, $$$s_i \in \{0, 1\}$$$, $$$s_1 = s_n = 0$$$) — the description of the road. It's guaranteed that the total length of all strings $$$s$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,500
Print $$$T$$$ integers — one per query. For each query print the minimum possible cost of the constructed pipeline.
standard output
PASSED
48039cf509b6069410376d404409a10b
train_002.jsonl
1566484500
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $$$[0, n]$$$ on $$$OX$$$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $$$(x, x + 1)$$$ with integer $$$x$$$. So we can represent the road as a binary string consisting of $$$n$$$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.Usually, we can install the pipeline along the road on height of $$$1$$$ unit with supporting pillars in each integer point (so, if we are responsible for $$$[0, n]$$$ road, we must install $$$n + 1$$$ pillars). But on crossroads we should lift the pipeline up to the height $$$2$$$, so the pipeline won't obstruct the way for cars.We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $$$[x, x + 1]$$$ with integer $$$x$$$ consisting of three parts: $$$0.5$$$ units of horizontal pipe + $$$1$$$ unit of vertical pipe + $$$0.5$$$ of horizontal. Note that if pipeline is currently on height $$$2$$$, the pillars that support it should also have length equal to $$$2$$$ units. Each unit of gas pipeline costs us $$$a$$$ bourles, and each unit of pillar — $$$b$$$ bourles. So, it's not always optimal to make the whole pipeline on the height $$$2$$$. Find the shape of the pipeline with minimum possible cost and calculate that cost.Note that you must start and finish the pipeline on height $$$1$$$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
256 megabytes
import java.io.*; import java.util.*; public class GasPipe { static Reader sc=new Reader(); static PrintWriter out=new PrintWriter((System.out)); public static void main(String args[])throws IOException { int t=sc.nextInt(); while(t-->0) { solve(); } out.close(); } public static void solve() { long n = sc.nextLong(); long a = sc.nextLong(), b= sc.nextLong(); String s = sc.next(); long dp[][] = new long[(int)n+1][2]; dp[0][0] = b; dp[0][1] = b; dp[1][0] = dp[0][0] + a+b; dp[1][1] = (long) 1e17; for(int i = 2; i <= n-1; i++) { if(s.charAt(i-1) == '1') { dp[i][1] = Math.min(dp[i-1][1] + a + 2 * b, dp[i-1][0] + 2*a +3*b); dp[i][0] = (long) 1e17; }else { dp[i][1] = Math.min(dp[i-1][1] + a + 2 * b, dp[i-1][0] + 2*a + 3*b); dp[i][0] = Math.min(dp[i-1][0] + a+b, dp[i-1][1] + 2*a+b); } //System.out.println(dp[i][0] + " " + dp[i][1]); } System.out.println(Math.min(dp[(int)n-1][0] + a+b, dp[(int)n-1][1] + 2*a+b)); } static class Reader { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); public String next() { while(!st.hasMoreTokens()) { try { st=new StringTokenizer(br.readLine()); } catch(Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return br.readLine(); } catch(Exception e) { e.printStackTrace(); } return null; } public boolean hasNext() { String next=null; try { next=br.readLine(); } catch(Exception e) { } if(next==null) { return false; } st=new StringTokenizer(next); return true; } } }
Java
["4\n8 2 5\n00110010\n8 1 1\n00110010\n9 100000000 100000000\n010101010\n2 5 1\n00"]
2 seconds
["94\n25\n2900000000\n13"]
NoteThe optimal pipeline for the first query is shown at the picture above.The optimal pipeline for the second query is pictured below: The optimal (and the only possible) pipeline for the third query is shown below: The optimal pipeline for the fourth query is shown below:
Java 11
standard input
[ "dp", "greedy" ]
4fa609ef581f705df901f7532b52cf7c
The fist line contains one integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of queries. Next $$$2 \cdot T$$$ lines contain independent queries — one query per two lines. The first line contains three integers $$$n$$$, $$$a$$$, $$$b$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le a \le 10^8$$$, $$$1 \le b \le 10^8$$$) — the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $$$s$$$ ($$$|s| = n$$$, $$$s_i \in \{0, 1\}$$$, $$$s_1 = s_n = 0$$$) — the description of the road. It's guaranteed that the total length of all strings $$$s$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,500
Print $$$T$$$ integers — one per query. For each query print the minimum possible cost of the constructed pipeline.
standard output
PASSED
a90e8f398ae2af13ac5dd47b0bac4c04
train_002.jsonl
1566484500
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $$$[0, n]$$$ on $$$OX$$$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $$$(x, x + 1)$$$ with integer $$$x$$$. So we can represent the road as a binary string consisting of $$$n$$$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.Usually, we can install the pipeline along the road on height of $$$1$$$ unit with supporting pillars in each integer point (so, if we are responsible for $$$[0, n]$$$ road, we must install $$$n + 1$$$ pillars). But on crossroads we should lift the pipeline up to the height $$$2$$$, so the pipeline won't obstruct the way for cars.We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $$$[x, x + 1]$$$ with integer $$$x$$$ consisting of three parts: $$$0.5$$$ units of horizontal pipe + $$$1$$$ unit of vertical pipe + $$$0.5$$$ of horizontal. Note that if pipeline is currently on height $$$2$$$, the pillars that support it should also have length equal to $$$2$$$ units. Each unit of gas pipeline costs us $$$a$$$ bourles, and each unit of pillar — $$$b$$$ bourles. So, it's not always optimal to make the whole pipeline on the height $$$2$$$. Find the shape of the pipeline with minimum possible cost and calculate that cost.Note that you must start and finish the pipeline on height $$$1$$$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
256 megabytes
import java.io.*; import java.util.*; /** * Built using my Brain * Actual solution is at the bottom * * @author Lenard Hoffstader */ public class cfjava { public static void main(String[] args) { OutputStream outputStream = System.out; FastReader in = new FastReader(); PrintWriter out = new PrintWriter(outputStream); PROBLEM solver = new PROBLEM(); int t=1; t=in.nextInt(); for(int i=0;i<t;i++){ solver.solve(in, out); } out.close(); } static class PROBLEM { public void solve(FastReader in,PrintWriter out) { int n=in.nextInt(); int a=in.nextInt(); int b=in.nextInt(); char str[]=in.nextLine().toCharArray(); long dp[][]=new long[2][n+1]; for(int i=0;i<=n;i++){dp[0][i]=dp[1][i]=(long)1e18;} dp[0][0]=b; for(int i=0;i<n;i++){ if(str[i]=='0'){ dp[0][i+1]=Math.min(dp[0][i]+a+b,dp[1][i]+2*a+b); dp[1][i+1]=Math.min(dp[1][i]+2*b+a,dp[0][i]+2*a+2*b); } else { dp[1][i+1]=dp[1][i]+a+2*b; } } out.println(dp[0][n]); } } 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());} boolean nextBoolean(){return Boolean.parseBoolean(next());} String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e){e.printStackTrace();} return str; } } } /* int a=in.nextInt(); int b=in.nextInt(); int c=in.nextInt(); int d=in.nextInt(); char ch[][]=new int[50][50]; int p1=0,p2=0,q1=0,q2=0; while(true){ ch[p1][p2]='a'; p2+=2; a--; if(a==0){break;} if(p2>=50){p2=0;p1+=2;} } q1=p1;q2=p2; for(int i=0;i<=p1+1;i++){ for(int j=0;j<50;j++){ if(ch[i][j]!='a'){ch[i][j]='b';} } } p1+=2; p2=0; while(true){ ch[p1][p2]='c'; p2+=2; c--; if(c==0){break;} if(p2>=50){p2=0;p1+=2;} } q1=p1;q2=p2; for(int i=0;i<=p1+1;i++){ for(int j=0;j<50;j++){ if(ch[i][j]!='c'){ch[i][j]='b';} } } p1+=2; p2=0; if(b<d){ while(true){ } } */
Java
["4\n8 2 5\n00110010\n8 1 1\n00110010\n9 100000000 100000000\n010101010\n2 5 1\n00"]
2 seconds
["94\n25\n2900000000\n13"]
NoteThe optimal pipeline for the first query is shown at the picture above.The optimal pipeline for the second query is pictured below: The optimal (and the only possible) pipeline for the third query is shown below: The optimal pipeline for the fourth query is shown below:
Java 11
standard input
[ "dp", "greedy" ]
4fa609ef581f705df901f7532b52cf7c
The fist line contains one integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of queries. Next $$$2 \cdot T$$$ lines contain independent queries — one query per two lines. The first line contains three integers $$$n$$$, $$$a$$$, $$$b$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le a \le 10^8$$$, $$$1 \le b \le 10^8$$$) — the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $$$s$$$ ($$$|s| = n$$$, $$$s_i \in \{0, 1\}$$$, $$$s_1 = s_n = 0$$$) — the description of the road. It's guaranteed that the total length of all strings $$$s$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,500
Print $$$T$$$ integers — one per query. For each query print the minimum possible cost of the constructed pipeline.
standard output
PASSED
ca64ab9508cfdcef8594ea17e32d3dd9
train_002.jsonl
1566484500
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $$$[0, n]$$$ on $$$OX$$$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $$$(x, x + 1)$$$ with integer $$$x$$$. So we can represent the road as a binary string consisting of $$$n$$$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.Usually, we can install the pipeline along the road on height of $$$1$$$ unit with supporting pillars in each integer point (so, if we are responsible for $$$[0, n]$$$ road, we must install $$$n + 1$$$ pillars). But on crossroads we should lift the pipeline up to the height $$$2$$$, so the pipeline won't obstruct the way for cars.We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $$$[x, x + 1]$$$ with integer $$$x$$$ consisting of three parts: $$$0.5$$$ units of horizontal pipe + $$$1$$$ unit of vertical pipe + $$$0.5$$$ of horizontal. Note that if pipeline is currently on height $$$2$$$, the pillars that support it should also have length equal to $$$2$$$ units. Each unit of gas pipeline costs us $$$a$$$ bourles, and each unit of pillar — $$$b$$$ bourles. So, it's not always optimal to make the whole pipeline on the height $$$2$$$. Find the shape of the pipeline with minimum possible cost and calculate that cost.Note that you must start and finish the pipeline on height $$$1$$$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
256 megabytes
import java.io.*; import java.util.*; import java.lang.Math; public class GasPipeline { public static void main (String [] args) throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); int ttttt = Integer.parseInt(f.readLine()); for(int aaa = 0; aaa < ttttt; aaa++){ StringTokenizer s = new StringTokenizer(f.readLine()); int N = Integer.parseInt(s.nextToken()); int a = Integer.parseInt(s.nextToken()); int b = Integer.parseInt(s.nextToken()); String str = "0" + f.readLine(); long[][] dp = new long[N + 10][2]; for(int i = 0; i <= N; i++){ Arrays.fill(dp[i], Long.MAX_VALUE/2); } dp[0][1] = Long.MAX_VALUE/2; dp[0][0] = b; for(int i = 1; i <= N; i++){ if(str.charAt(i) == '1'){ dp[i][1] = Math.min(dp[i][1], dp[i - 1][1] + a + b * 2); }else{ dp[i][0] = Math.min(dp[i][0], Math.min(dp[i - 1][0] + a + b, dp[i - 1][1] + 2 * a + b)); dp[i][1] = Math.min(dp[i][1], Math.min(dp[i - 1][0] + 2 * a + 2 * b, dp[i - 1][1] + a + b * 2)); } } System.out.println(dp[N][0]); } } }
Java
["4\n8 2 5\n00110010\n8 1 1\n00110010\n9 100000000 100000000\n010101010\n2 5 1\n00"]
2 seconds
["94\n25\n2900000000\n13"]
NoteThe optimal pipeline for the first query is shown at the picture above.The optimal pipeline for the second query is pictured below: The optimal (and the only possible) pipeline for the third query is shown below: The optimal pipeline for the fourth query is shown below:
Java 11
standard input
[ "dp", "greedy" ]
4fa609ef581f705df901f7532b52cf7c
The fist line contains one integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of queries. Next $$$2 \cdot T$$$ lines contain independent queries — one query per two lines. The first line contains three integers $$$n$$$, $$$a$$$, $$$b$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le a \le 10^8$$$, $$$1 \le b \le 10^8$$$) — the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $$$s$$$ ($$$|s| = n$$$, $$$s_i \in \{0, 1\}$$$, $$$s_1 = s_n = 0$$$) — the description of the road. It's guaranteed that the total length of all strings $$$s$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,500
Print $$$T$$$ integers — one per query. For each query print the minimum possible cost of the constructed pipeline.
standard output
PASSED
9f2f9f54c5748ede26dc84ae456f324b
train_002.jsonl
1566484500
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $$$[0, n]$$$ on $$$OX$$$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $$$(x, x + 1)$$$ with integer $$$x$$$. So we can represent the road as a binary string consisting of $$$n$$$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.Usually, we can install the pipeline along the road on height of $$$1$$$ unit with supporting pillars in each integer point (so, if we are responsible for $$$[0, n]$$$ road, we must install $$$n + 1$$$ pillars). But on crossroads we should lift the pipeline up to the height $$$2$$$, so the pipeline won't obstruct the way for cars.We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $$$[x, x + 1]$$$ with integer $$$x$$$ consisting of three parts: $$$0.5$$$ units of horizontal pipe + $$$1$$$ unit of vertical pipe + $$$0.5$$$ of horizontal. Note that if pipeline is currently on height $$$2$$$, the pillars that support it should also have length equal to $$$2$$$ units. Each unit of gas pipeline costs us $$$a$$$ bourles, and each unit of pillar — $$$b$$$ bourles. So, it's not always optimal to make the whole pipeline on the height $$$2$$$. Find the shape of the pipeline with minimum possible cost and calculate that cost.Note that you must start and finish the pipeline on height $$$1$$$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
256 megabytes
import java.io.*; import java.util.*; public class cf{ public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(bf.readLine()); for(int testcase = 0; testcase < t; testcase++){ StringTokenizer stk = new StringTokenizer(bf.readLine()); int N = Integer.parseInt(stk.nextToken()); long pipecost = Integer.parseInt(stk.nextToken()); long pillarcost = Integer.parseInt(stk.nextToken()); String[] strarr = bf.readLine().split(""); long[] s = new long[N]; for(int i = 0; i < N; i++) s[i] = Long.parseLong(strarr[i]); long[][] dp = new long[N+1][2]; for(int i = 0; i <= N; i++)Arrays.fill(dp[i], Long.MAX_VALUE-Integer.MAX_VALUE); dp[0][0] = pillarcost; for(int i = 1; i <= N; i++){ if(s[i-1]==0 && (i == N || s[i] == 0))dp[i][0] = Math.min(dp[i-1][0] + pillarcost + pipecost, dp[i-1][1] + 2*pipecost + pillarcost); dp[i][1] = Math.min(dp[i-1][0] + 2*pipecost + 2*pillarcost, dp[i-1][1] + 2*pillarcost + pipecost); } pw.println(dp[N][0]); } pw.close(); } }
Java
["4\n8 2 5\n00110010\n8 1 1\n00110010\n9 100000000 100000000\n010101010\n2 5 1\n00"]
2 seconds
["94\n25\n2900000000\n13"]
NoteThe optimal pipeline for the first query is shown at the picture above.The optimal pipeline for the second query is pictured below: The optimal (and the only possible) pipeline for the third query is shown below: The optimal pipeline for the fourth query is shown below:
Java 11
standard input
[ "dp", "greedy" ]
4fa609ef581f705df901f7532b52cf7c
The fist line contains one integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of queries. Next $$$2 \cdot T$$$ lines contain independent queries — one query per two lines. The first line contains three integers $$$n$$$, $$$a$$$, $$$b$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le a \le 10^8$$$, $$$1 \le b \le 10^8$$$) — the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $$$s$$$ ($$$|s| = n$$$, $$$s_i \in \{0, 1\}$$$, $$$s_1 = s_n = 0$$$) — the description of the road. It's guaranteed that the total length of all strings $$$s$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,500
Print $$$T$$$ integers — one per query. For each query print the minimum possible cost of the constructed pipeline.
standard output
PASSED
5e605c862ac06e64137c533da575644a
train_002.jsonl
1566484500
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $$$[0, n]$$$ on $$$OX$$$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $$$(x, x + 1)$$$ with integer $$$x$$$. So we can represent the road as a binary string consisting of $$$n$$$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.Usually, we can install the pipeline along the road on height of $$$1$$$ unit with supporting pillars in each integer point (so, if we are responsible for $$$[0, n]$$$ road, we must install $$$n + 1$$$ pillars). But on crossroads we should lift the pipeline up to the height $$$2$$$, so the pipeline won't obstruct the way for cars.We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $$$[x, x + 1]$$$ with integer $$$x$$$ consisting of three parts: $$$0.5$$$ units of horizontal pipe + $$$1$$$ unit of vertical pipe + $$$0.5$$$ of horizontal. Note that if pipeline is currently on height $$$2$$$, the pillars that support it should also have length equal to $$$2$$$ units. Each unit of gas pipeline costs us $$$a$$$ bourles, and each unit of pillar — $$$b$$$ bourles. So, it's not always optimal to make the whole pipeline on the height $$$2$$$. Find the shape of the pipeline with minimum possible cost and calculate that cost.Note that you must start and finish the pipeline on height $$$1$$$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
256 megabytes
import java.io.*; import static java.lang.Double.*; import java.util.*; public class Mao { static Reader in=new Reader (); static long ans,mz=0,mx=0; static int a[],b[]; static StringBuilder sd=new StringBuilder(); public static void main(String [] args) { int t=in.nextInt(); while(t-->0){mz=0;mx=0;ans=0; long n=in.l(),a=in.l(),b=in.l(),po=0; char ch[]=in.s().toCharArray(); for(int i=0;i<n;i++) if(ch[i]=='0'){po++;} if(po==n){ ans=b*(n+1); ans+=a*(n); System.out.println(ans); } else{ ans=b*(long)(n+1)*(long)2; ans+=a*((long)n); for(int i=0;i<n;i++){long lol=0L; if(ch[i]=='0') { boolean B=(i==0); while(i<n&&ch[i]=='0'){lol++;i++;} B|=(i>=(n)); if(B){ mx=(((long)lol+1)*a)+(((long)lol)*b); mz=((long)lol*(a))+((long)lol*(b*2)); ans-=(mz-mx); } else{ mx=((long)lol+2)*a; mx+=((long)lol-1)*b; mz=((long)lol)*(a); mz+=((long)lol-1)*(b*2); if(mx<=mz) ans-=(mz-mx); }} else if(i==0||i==n-1){ ans-=(b-a); } } System.out.println(ans);} } } static class Reader { private InputStream mIs;private byte[] buf = new byte[1024];private int curChar,numChars;public Reader() { this(System.in); }public Reader(InputStream is) { mIs = is;} public int read() {if (numChars == -1) throw new InputMismatchException();if (curChar >= numChars) {curChar = 0;try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();}if (numChars <= 0) return -1; }return buf[curChar++];} public String nextLine(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isEndOfLine(c));return res.toString() ;} public String s(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isSpaceChar(c));return res.toString();} public long l(){int c = read();while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }long res = 0; do{ if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read();}while(!isSpaceChar(c));return res * sgn;} public int 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 double d() throws IOException {return Double.parseDouble(s()) ;} public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public int[] arr(int n){int[] ret = new int[n];for (int i = 0; i < n; i++) {ret[i] = nextInt();}return ret;} } } class node implements Comparable<node> { int x,y; node(int x,int y){ this.x=x; this.y=y; } @Override public int compareTo(node o) { return x-o.x;} } class Sorting{ public static int[] bucketSort(int[] array, int bucketCount) { if (bucketCount <= 0) throw new IllegalArgumentException("Invalid bucket count"); if (array.length <= 1) return array; //trivially sorted int high = array[0]; int low = array[0]; for (int i = 1; i < array.length; i++) { //find the range of input elements if (array[i] > high) high = array[i]; if (array[i] < low) low = array[i]; } double interval = ((double)(high - low + 1))/bucketCount; //range of one bucket ArrayList<Integer> buckets[] = new ArrayList[bucketCount]; for (int i = 0; i < bucketCount; i++) { //initialize buckets buckets[i] = new ArrayList(); } for (int i = 0; i < array.length; i++) { //partition the input array buckets[(int)((array[i] - low)/interval)].add(array[i]); } int pointer = 0; for (int i = 0; i < buckets.length; i++) { Collections.sort(buckets[i]); //mergeSort for (int j = 0; j < buckets[i].size(); j++) { //merge the buckets array[pointer] = buckets[i].get(j); pointer++; } } return array; } }
Java
["4\n8 2 5\n00110010\n8 1 1\n00110010\n9 100000000 100000000\n010101010\n2 5 1\n00"]
2 seconds
["94\n25\n2900000000\n13"]
NoteThe optimal pipeline for the first query is shown at the picture above.The optimal pipeline for the second query is pictured below: The optimal (and the only possible) pipeline for the third query is shown below: The optimal pipeline for the fourth query is shown below:
Java 11
standard input
[ "dp", "greedy" ]
4fa609ef581f705df901f7532b52cf7c
The fist line contains one integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of queries. Next $$$2 \cdot T$$$ lines contain independent queries — one query per two lines. The first line contains three integers $$$n$$$, $$$a$$$, $$$b$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le a \le 10^8$$$, $$$1 \le b \le 10^8$$$) — the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $$$s$$$ ($$$|s| = n$$$, $$$s_i \in \{0, 1\}$$$, $$$s_1 = s_n = 0$$$) — the description of the road. It's guaranteed that the total length of all strings $$$s$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,500
Print $$$T$$$ integers — one per query. For each query print the minimum possible cost of the constructed pipeline.
standard output
PASSED
c27da84757fcf56642069bff330eb50b
train_002.jsonl
1566484500
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $$$[0, n]$$$ on $$$OX$$$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $$$(x, x + 1)$$$ with integer $$$x$$$. So we can represent the road as a binary string consisting of $$$n$$$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.Usually, we can install the pipeline along the road on height of $$$1$$$ unit with supporting pillars in each integer point (so, if we are responsible for $$$[0, n]$$$ road, we must install $$$n + 1$$$ pillars). But on crossroads we should lift the pipeline up to the height $$$2$$$, so the pipeline won't obstruct the way for cars.We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $$$[x, x + 1]$$$ with integer $$$x$$$ consisting of three parts: $$$0.5$$$ units of horizontal pipe + $$$1$$$ unit of vertical pipe + $$$0.5$$$ of horizontal. Note that if pipeline is currently on height $$$2$$$, the pillars that support it should also have length equal to $$$2$$$ units. Each unit of gas pipeline costs us $$$a$$$ bourles, and each unit of pillar — $$$b$$$ bourles. So, it's not always optimal to make the whole pipeline on the height $$$2$$$. Find the shape of the pipeline with minimum possible cost and calculate that cost.Note that you must start and finish the pipeline on height $$$1$$$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
256 megabytes
import java.util.*; import java.io.*; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static final long mod=(long)1e9+7; public static void main(String args[])throws Exception { FastReader fs=new FastReader(); PrintWriter pw=new PrintWriter(System.out); int t=fs.nextInt(); while(t-->0) { int n=fs.nextInt(); long a=fs.nextLong(); long b=fs.nextLong(); char c[]=fs.nextLine().toCharArray(); long cost[][]=new long[n][2]; cost[0][0]=a+2*b; cost[0][1]=a*2+3*b; for(int i=1;i<n-1;i++) { if(c[i]=='1') cost[i][1]=cost[i][0]=cost[i-1][1]+2*b+a; else { if(c[i-1]=='0') { cost[i][0]=cost[i-1][0]+a+b; cost[i][1]=Math.min(cost[i-1][1]+2*b+a,cost[i-1][0]+2*(a+b)); } else { cost[i][0]=cost[i-1][1]+a*2+b; cost[i][1]=cost[i-1][1]+2*b+a; } } } if(c[n-2]=='1') pw.println(2*a+b+cost[n-2][1]); else pw.println(Math.min(2*a+b+cost[n-2][1],cost[n-2][0]+a+b)); } pw.flush(); pw.close(); } }
Java
["4\n8 2 5\n00110010\n8 1 1\n00110010\n9 100000000 100000000\n010101010\n2 5 1\n00"]
2 seconds
["94\n25\n2900000000\n13"]
NoteThe optimal pipeline for the first query is shown at the picture above.The optimal pipeline for the second query is pictured below: The optimal (and the only possible) pipeline for the third query is shown below: The optimal pipeline for the fourth query is shown below:
Java 11
standard input
[ "dp", "greedy" ]
4fa609ef581f705df901f7532b52cf7c
The fist line contains one integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of queries. Next $$$2 \cdot T$$$ lines contain independent queries — one query per two lines. The first line contains three integers $$$n$$$, $$$a$$$, $$$b$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le a \le 10^8$$$, $$$1 \le b \le 10^8$$$) — the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $$$s$$$ ($$$|s| = n$$$, $$$s_i \in \{0, 1\}$$$, $$$s_1 = s_n = 0$$$) — the description of the road. It's guaranteed that the total length of all strings $$$s$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,500
Print $$$T$$$ integers — one per query. For each query print the minimum possible cost of the constructed pipeline.
standard output
PASSED
7b4291da18f99a4fa22b6fa95ea48042
train_002.jsonl
1305299400
There are n animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number i in the queue will have to visit his office exactly ai times. We will assume that an examination takes much more time than making tests and other extra procedures, and therefore we will assume that once an animal leaves the room, it immediately gets to the end of the queue to the doctor. Of course, if the animal has visited the doctor as many times as necessary, then it doesn't have to stand at the end of the queue and it immediately goes home. Doctor plans to go home after receiving k animals, and therefore what the queue will look like at that moment is important for him. Since the doctor works long hours and she can't get distracted like that after all, she asked you to figure it out.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class B83 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); long k = in.nextLong(); int[] a = new int[n]; long allSum = 0; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); allSum += a[i]; } if (allSum == k) { System.out.println(" "); return; } else if (allSum < k) { System.out.println(-1); return; } int[] sorted = a.clone(); Arrays.sort(sorted); int sum = 0; long i = 0; for (; i < n; i++) { // System.out.println(k); if ((sorted[(int)i] - sum) * (n - i) <= k) { k -= (n - i) * (sorted[(int)i] - sum); sum = sorted[(int)i]; } else { break; } } int[] answer = new int[n - (int)i]; int[] last = new int[n - (int)i]; int curret = 0; for (int j = 0; j < n; j++) { if (a[j] >= sorted[(int)i]) { answer[curret] = j; last[curret] = (int) (a[j] - sum - k / (n - i)); curret++; } } k %= (n - i); for (int j = (int) k; j < answer.length; j++) { System.out.print(answer[j] + 1 + " "); } for (int j = 0; j < k; j++) { if (last[j] > 1) { System.out.print(answer[j] + 1 + " "); } } } }
Java
["3 3\n1 2 1", "4 10\n3 3 2 1", "7 10\n1 3 3 1 2 3 1"]
2 seconds
["2", "-1", "6 2 3"]
NoteIn the first sample test: Before examination: {1, 2, 3} After the first examination: {2, 3} After the second examination: {3, 2} After the third examination: {2} In the second sample test: Before examination: {1, 2, 3, 4, 5, 6, 7} After the first examination: {2, 3, 4, 5, 6, 7} After the second examination: {3, 4, 5, 6, 7, 2} After the third examination: {4, 5, 6, 7, 2, 3} After the fourth examination: {5, 6, 7, 2, 3} After the fifth examination: {6, 7, 2, 3, 5} After the sixth examination: {7, 2, 3, 5, 6} After the seventh examination: {2, 3, 5, 6} After the eighth examination: {3, 5, 6, 2} After the ninth examination: {5, 6, 2, 3} After the tenth examination: {6, 2, 3}
Java 7
standard input
[ "binary search", "sortings", "math" ]
8126f40439f2ea808683d22115421c18
The first line of input data contains two space-separated integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 1014). In the second line are given space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109). Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
1,800
If the doctor will overall carry out less than k examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers — number of animals in the order in which they stand in the queue. Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted.
standard output
PASSED
039cbcadc4ad0f71cfecc3831cfe55f7
train_002.jsonl
1305299400
There are n animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number i in the queue will have to visit his office exactly ai times. We will assume that an examination takes much more time than making tests and other extra procedures, and therefore we will assume that once an animal leaves the room, it immediately gets to the end of the queue to the doctor. Of course, if the animal has visited the doctor as many times as necessary, then it doesn't have to stand at the end of the queue and it immediately goes home. Doctor plans to go home after receiving k animals, and therefore what the queue will look like at that moment is important for him. Since the doctor works long hours and she can't get distracted like that after all, she asked you to figure it out.
256 megabytes
import java.io.IOException; import java.io.OutputStreamWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.util.Comparator; import java.io.OutputStream; import java.io.PrintWriter; import java.util.NoSuchElementException; import java.io.Writer; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Nguyen Trung Hieu - vuondenthanhcong11@yahoo.com */ 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); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { public void solve(int testNumber, InputReader in, OutputWriter out) { int count = in.readInt(); long length = in.readLong(); int[] repetitions = IOUtils.readIntArray(in, count); int[] repetitionsCopy = repetitions.clone(); Arrays.sort(repetitionsCopy); int last = 0; int remaining = count; for (int i : repetitionsCopy) { long timeRequired = (long) (i - last) * remaining; if (timeRequired <= length) { length -= timeRequired; remaining--; last = i; } else { long delta = last + length / remaining + 1; length %= remaining; if (length == 0) { int index = 0; int[] answer = new int[remaining]; for (int k = 0; k < count; k++) { if (repetitions[k] >= delta) answer[index++] = k + 1; } IOUtils.printArray(answer, out); return; } int[] answer = new int[remaining]; for (int j = 0; j < count; j++) { if (repetitions[j] > last) { length--; if (length == 0) { int index = 0; for (int k = j + 1; k < count; k++) { if (repetitions[k] >= delta) answer[index++] = k + 1; } for (int k = 0; k <= j; k++) { if (repetitions[k] > delta) answer[index++] = k + 1; } answer = Arrays.copyOf(answer, index); IOUtils.printArray(answer, out); return; } } } } } if (length == 0) out.printLine(); else out.printLine(-1); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int 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 c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public long readLong() { 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 interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object...objects) { print(objects); writer.println(); } public void close() { writer.close(); } } class IOUtils { public static void printArray(int[] array, OutputWriter out) { if (array.length == 0) { out.printLine(); return; } out.print(array[0]); for (int i = 1; i < array.length; i++) out.print(" " + array[i]); out.printLine(); } public static int[] readIntArray(InputReader in, int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = in.readInt(); return array; } }
Java
["3 3\n1 2 1", "4 10\n3 3 2 1", "7 10\n1 3 3 1 2 3 1"]
2 seconds
["2", "-1", "6 2 3"]
NoteIn the first sample test: Before examination: {1, 2, 3} After the first examination: {2, 3} After the second examination: {3, 2} After the third examination: {2} In the second sample test: Before examination: {1, 2, 3, 4, 5, 6, 7} After the first examination: {2, 3, 4, 5, 6, 7} After the second examination: {3, 4, 5, 6, 7, 2} After the third examination: {4, 5, 6, 7, 2, 3} After the fourth examination: {5, 6, 7, 2, 3} After the fifth examination: {6, 7, 2, 3, 5} After the sixth examination: {7, 2, 3, 5, 6} After the seventh examination: {2, 3, 5, 6} After the eighth examination: {3, 5, 6, 2} After the ninth examination: {5, 6, 2, 3} After the tenth examination: {6, 2, 3}
Java 7
standard input
[ "binary search", "sortings", "math" ]
8126f40439f2ea808683d22115421c18
The first line of input data contains two space-separated integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 1014). In the second line are given space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109). Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
1,800
If the doctor will overall carry out less than k examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers — number of animals in the order in which they stand in the queue. Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted.
standard output
PASSED
f09fb00505a4fba7607310d5a6a3e04c
train_002.jsonl
1305299400
There are n animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number i in the queue will have to visit his office exactly ai times. We will assume that an examination takes much more time than making tests and other extra procedures, and therefore we will assume that once an animal leaves the room, it immediately gets to the end of the queue to the doctor. Of course, if the animal has visited the doctor as many times as necessary, then it doesn't have to stand at the end of the queue and it immediately goes home. Doctor plans to go home after receiving k animals, and therefore what the queue will look like at that moment is important for him. Since the doctor works long hours and she can't get distracted like that after all, she asked you to figure it out.
256 megabytes
import java.io.*; import java.util.*; public class B38 { public static void main(String[] args) throws FileNotFoundException { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); long k = in.nextLong(); int a[] = new int[n]; int max = -1000000000; long sum = 0; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); sum = (long) sum + a[i]; if (a[i] > max) { max = a[i]; } } if (sum < k) { out.print(-1); } else { if (sum == k) { out.println(); return; } else { long l = 0; long r = max; while (l + 1 < r) { long mid = ((long) l + r) / 2; sum = 0; for (int i = 0; i < n; i++) { sum = (long) sum + Math.min(mid, a[i]); } if (sum > k) { r = mid; } else { l = mid; } } sum = 0; for (int i = 0; i < n; i++) { sum = (long) sum + Math.min(l, a[i]); a[i] -= l; } k = (long) k - sum; int count = 0; while (k > 0) { if (a[count] > 0) { a[count]--; count++; k--; } else { count++; } } if (k == 0) { for (int i = count; i < n; i++) { if (a[i] > 0) { out.print((i + 1) + " "); } } for (int i = 0; i < count; i++) { if (a[i] > 0) { out.print((i + 1) + " "); } } } } } out.close(); } }
Java
["3 3\n1 2 1", "4 10\n3 3 2 1", "7 10\n1 3 3 1 2 3 1"]
2 seconds
["2", "-1", "6 2 3"]
NoteIn the first sample test: Before examination: {1, 2, 3} After the first examination: {2, 3} After the second examination: {3, 2} After the third examination: {2} In the second sample test: Before examination: {1, 2, 3, 4, 5, 6, 7} After the first examination: {2, 3, 4, 5, 6, 7} After the second examination: {3, 4, 5, 6, 7, 2} After the third examination: {4, 5, 6, 7, 2, 3} After the fourth examination: {5, 6, 7, 2, 3} After the fifth examination: {6, 7, 2, 3, 5} After the sixth examination: {7, 2, 3, 5, 6} After the seventh examination: {2, 3, 5, 6} After the eighth examination: {3, 5, 6, 2} After the ninth examination: {5, 6, 2, 3} After the tenth examination: {6, 2, 3}
Java 7
standard input
[ "binary search", "sortings", "math" ]
8126f40439f2ea808683d22115421c18
The first line of input data contains two space-separated integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 1014). In the second line are given space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109). Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
1,800
If the doctor will overall carry out less than k examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers — number of animals in the order in which they stand in the queue. Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted.
standard output
PASSED
f1e48e5f7049bfb88803defa3b52f65e
train_002.jsonl
1305299400
There are n animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number i in the queue will have to visit his office exactly ai times. We will assume that an examination takes much more time than making tests and other extra procedures, and therefore we will assume that once an animal leaves the room, it immediately gets to the end of the queue to the doctor. Of course, if the animal has visited the doctor as many times as necessary, then it doesn't have to stand at the end of the queue and it immediately goes home. Doctor plans to go home after receiving k animals, and therefore what the queue will look like at that moment is important for him. Since the doctor works long hours and she can't get distracted like that after all, she asked you to figure it out.
256 megabytes
import java.io.InputStreamReader; import java.io.IOException; import java.util.Arrays; import java.util.InputMismatchException; import java.util.Comparator; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Reader; import java.io.Writer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Niyaz Nigmatullin */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); FastPrinter out = new FastPrinter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { public void solve(int testNumber, FastScanner in, FastPrinter out) { int n = in.nextInt(); long k = in.nextLong(); final int[] a = in.readIntArray(n); long sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; } if (sum < k) { out.println(-1); return; } if (sum == k) { return; } Integer[] id = new Integer[n]; for (int i = 0; i < n; i++) { id[i] = i; } Arrays.sort(id, new Comparator<Integer>() { public int compare(Integer o1, Integer o2) { return a[o1] - a[o2]; } }); int last = 0; for (int man = 0; man < n; ) { int next = man; while (next < n && a[id[man]] == a[id[next]]) { ++next; } int cur = a[id[man]]; if (k >= (long) (n - man) * (cur - last)) { k -= (long) (n - man) * (cur - last); } else { long howMany = k / (n - man); int day = (int) (last + howMany + 1); k %= n - man; int i = 0; while (k > 0) { if (a[i] >= day) { --k; } ++i; } for (int j = i; j < n; j++) { if (a[j] >= day) { out.print(j + 1 + " "); } } for (int j = 0; j < i; j++) { if (a[j] > day) { out.print(j + 1 + " "); } } return; } man = next; last = cur; } } } class FastScanner extends BufferedReader { boolean isEOF; public FastScanner(InputStream is) { super(new InputStreamReader(is)); } public int read() { try { int ret = super.read(); if (isEOF && ret < 0) { throw new InputMismatchException(); } isEOF = ret == -1; return ret; } catch (IOException e) { throw new InputMismatchException(); } } public String next() { StringBuilder sb = new StringBuilder(); int c = read(); while (isWhiteSpace(c)) { c = read(); } while (!isWhiteSpace(c)) { sb.appendCodePoint(c); c = read(); } return sb.toString(); } static boolean isWhiteSpace(int c) { return c >= -1 && c <= 32; } public int nextInt() { int c = read(); while (isWhiteSpace(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int ret = 0; while (!isWhiteSpace(c)) { if (c < '0' || c > '9') { throw new NumberFormatException("digit expected " + (char) c + " found"); } ret = ret * 10 + c - '0'; c = read(); } return ret * sgn; } public long nextLong() { return Long.parseLong(next()); } public int[] readIntArray(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = nextInt(); } return ret; } } class FastPrinter extends PrintWriter { public FastPrinter(OutputStream out) { super(out); } public FastPrinter(Writer out) { super(out); } }
Java
["3 3\n1 2 1", "4 10\n3 3 2 1", "7 10\n1 3 3 1 2 3 1"]
2 seconds
["2", "-1", "6 2 3"]
NoteIn the first sample test: Before examination: {1, 2, 3} After the first examination: {2, 3} After the second examination: {3, 2} After the third examination: {2} In the second sample test: Before examination: {1, 2, 3, 4, 5, 6, 7} After the first examination: {2, 3, 4, 5, 6, 7} After the second examination: {3, 4, 5, 6, 7, 2} After the third examination: {4, 5, 6, 7, 2, 3} After the fourth examination: {5, 6, 7, 2, 3} After the fifth examination: {6, 7, 2, 3, 5} After the sixth examination: {7, 2, 3, 5, 6} After the seventh examination: {2, 3, 5, 6} After the eighth examination: {3, 5, 6, 2} After the ninth examination: {5, 6, 2, 3} After the tenth examination: {6, 2, 3}
Java 7
standard input
[ "binary search", "sortings", "math" ]
8126f40439f2ea808683d22115421c18
The first line of input data contains two space-separated integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 1014). In the second line are given space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109). Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
1,800
If the doctor will overall carry out less than k examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers — number of animals in the order in which they stand in the queue. Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted.
standard output
PASSED
ac252f3bc0d3135cce2c0abaaaad8563
train_002.jsonl
1305299400
There are n animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number i in the queue will have to visit his office exactly ai times. We will assume that an examination takes much more time than making tests and other extra procedures, and therefore we will assume that once an animal leaves the room, it immediately gets to the end of the queue to the doctor. Of course, if the animal has visited the doctor as many times as necessary, then it doesn't have to stand at the end of the queue and it immediately goes home. Doctor plans to go home after receiving k animals, and therefore what the queue will look like at that moment is important for him. Since the doctor works long hours and she can't get distracted like that after all, she asked you to figure it out.
256 megabytes
import java.io.*; import java.util.*; public class Pr83B { public static void main(String[] args) throws IOException { new Pr83B().run(); } BufferedReader in; PrintWriter out; StringTokenizer st; String nextToken() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out, true); solve(); out.flush(); } class pair implements Comparable<pair> { long val; int ind; public pair() { } public pair(int val, int ind) { this.val = val; this.ind = ind; } @Override public int compareTo(pair o) { if (this.val < o.val) { return -1; } if (this.val > o.val) { return 1; } if (this.ind < o.ind) { return -1; } if (this.ind > o.ind) { return 1; } return 0; } } int[] t; void change(int i, int l, int r, int ind, int val) { if (r == l + 1) { t[i] = val; return; } int m = (l + r) / 2; if (ind < m) { change(2 * i + 1, l, m, ind, val); } else { change(2 * i + 2, m, r, ind, val); } t[i] = t[2 * i + 1] + t[2 * i + 2]; } int sum(int i, int l, int r, int a, int b) { if (l >= a && r <= b) { return t[i]; } if (l >= b) { return 0; } if (r <= a) { return 0; } return sum(2 * i + 1, l, (l + r) / 2, a, b) + sum(2 * i + 2, (l + r) / 2, r, a, b); } void solve() throws IOException { int n = nextInt(); long k = nextLong(); pair[] a = new pair[n]; long[] prsum = new long[n + 1]; for (int i = 0; i < n; i++) { a[i] = new pair(nextInt(), i + 1); } Arrays.sort(a); for (int i = 0; i < n; i++) { prsum[i + 1] = prsum[i] + a[i].val; } t = new int[4 * n + 4]; for (int i = 0; i < n; i++) { change(0, 0, n, i, 1); } int ansi = -1; for (int i = 0; i < n; i++) { if (k >= prsum[i + 1] + (a[i].val - 1) * (n - i - 1) + sum(0, 0, n, 0, a[i].ind - 1)) { ansi = i; change(0, 0, n, a[i].ind - 1, 0); } } if (ansi == n - 1) { if (k == prsum[ansi + 1]) { return; } else { out.println(-1); return; } } k -= prsum[ansi + 1]; int[] inds = new int[n - ansi - 1]; for (int i = ansi + 1; i < n; i++) { inds[i - ansi - 1] = a[i].ind; } k %= (n - ansi - 1); Arrays.sort(inds); for (int i = (int) k; i < n - ansi - 1 + k; i++) { out.print(inds[i % (n - ansi - 1)] + " "); } } }
Java
["3 3\n1 2 1", "4 10\n3 3 2 1", "7 10\n1 3 3 1 2 3 1"]
2 seconds
["2", "-1", "6 2 3"]
NoteIn the first sample test: Before examination: {1, 2, 3} After the first examination: {2, 3} After the second examination: {3, 2} After the third examination: {2} In the second sample test: Before examination: {1, 2, 3, 4, 5, 6, 7} After the first examination: {2, 3, 4, 5, 6, 7} After the second examination: {3, 4, 5, 6, 7, 2} After the third examination: {4, 5, 6, 7, 2, 3} After the fourth examination: {5, 6, 7, 2, 3} After the fifth examination: {6, 7, 2, 3, 5} After the sixth examination: {7, 2, 3, 5, 6} After the seventh examination: {2, 3, 5, 6} After the eighth examination: {3, 5, 6, 2} After the ninth examination: {5, 6, 2, 3} After the tenth examination: {6, 2, 3}
Java 7
standard input
[ "binary search", "sortings", "math" ]
8126f40439f2ea808683d22115421c18
The first line of input data contains two space-separated integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 1014). In the second line are given space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109). Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
1,800
If the doctor will overall carry out less than k examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers — number of animals in the order in which they stand in the queue. Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted.
standard output
PASSED
9296c835705a29f60a4a3b2d350956c6
train_002.jsonl
1305299400
There are n animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number i in the queue will have to visit his office exactly ai times. We will assume that an examination takes much more time than making tests and other extra procedures, and therefore we will assume that once an animal leaves the room, it immediately gets to the end of the queue to the doctor. Of course, if the animal has visited the doctor as many times as necessary, then it doesn't have to stand at the end of the queue and it immediately goes home. Doctor plans to go home after receiving k animals, and therefore what the queue will look like at that moment is important for him. Since the doctor works long hours and she can't get distracted like that after all, she asked you to figure it out.
256 megabytes
import java.io.*; import java.util.*; public class B83 { BufferedReader br; StringTokenizer in; PrintWriter out; int n, max; long k; int[] a; public String nextToken() throws IOException { while (in == null || !in.hasMoreTokens()) { in = new StringTokenizer(br.readLine()); } return in.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public static void main(String[] args) throws IOException { new B83().run(); } long count(long x) { long answer = 0; for (int i = 0; i < n; i++) { answer += Math.min(a[i], x); } return answer; } int binarySearch() { int L = 0; int R = max + 1; while (R - L > 1) { int M = (R + L) / 2; if (count(M) < k + 1) { L = M; } else { R = M; } } return R; } public void solve() throws IOException { n = nextInt(); k = nextLong(); a = new int[n]; long cnt = 0; max = 0; for (int i = 0; i < n; i++) { a[i] = nextInt(); cnt += a[i]; max = Math.max(max, a[i]); } if (cnt < k) { out.println(-1); out.flush(); System.exit(0); } int last = binarySearch(); ArrayDeque<Integer> lastQueue = new ArrayDeque<Integer>(); ArrayDeque<Integer> moves = new ArrayDeque<Integer>(); for (int i = 0; i < n; i++) { if (a[i] >= last) { lastQueue.addLast(i); moves.addLast(a[i] - last + 1); } } long movesLeft = k - count(last - 1); for (int i = 0; i < movesLeft; i++) { int x = moves.pollFirst(); int y = lastQueue.pollFirst(); x--; if (x > 0) { moves.addLast(x); lastQueue.addLast(y); } } for (int i : lastQueue) { out.print((lastQueue.pollFirst() + 1) + " "); } } public void run() { try { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } }
Java
["3 3\n1 2 1", "4 10\n3 3 2 1", "7 10\n1 3 3 1 2 3 1"]
2 seconds
["2", "-1", "6 2 3"]
NoteIn the first sample test: Before examination: {1, 2, 3} After the first examination: {2, 3} After the second examination: {3, 2} After the third examination: {2} In the second sample test: Before examination: {1, 2, 3, 4, 5, 6, 7} After the first examination: {2, 3, 4, 5, 6, 7} After the second examination: {3, 4, 5, 6, 7, 2} After the third examination: {4, 5, 6, 7, 2, 3} After the fourth examination: {5, 6, 7, 2, 3} After the fifth examination: {6, 7, 2, 3, 5} After the sixth examination: {7, 2, 3, 5, 6} After the seventh examination: {2, 3, 5, 6} After the eighth examination: {3, 5, 6, 2} After the ninth examination: {5, 6, 2, 3} After the tenth examination: {6, 2, 3}
Java 7
standard input
[ "binary search", "sortings", "math" ]
8126f40439f2ea808683d22115421c18
The first line of input data contains two space-separated integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 1014). In the second line are given space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109). Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
1,800
If the doctor will overall carry out less than k examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers — number of animals in the order in which they stand in the queue. Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted.
standard output
PASSED
53d5197e08e96af8259376d0d0cab360
train_002.jsonl
1411218000
Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that?
256 megabytes
import java.util.Scanner; public class Solution { public static void main(String args[]) { Scanner s=new Scanner(System.in); int p=s.nextInt(); int q=s.nextInt(); int l=s.nextInt(); int r=s.nextInt(); int[][] zTimes=new int[p][2]; for(int i=0;i<p;i++) { zTimes[i][0]=s.nextInt(); zTimes[i][1]=s.nextInt(); } int[][] xTimes=new int[q][2]; for(int i=0;i<q;i++) { xTimes[i][0]=s.nextInt(); xTimes[i][1]=s.nextInt(); } System.out.println(numWakeUpTimes(zTimes,xTimes,l,r)); } public static int numWakeUpTimes(int[][] zTimes,int[][] xTimes,int l,int r) { int numWakeUpTimes=0; for(int wakeUpTime=l;wakeUpTime<=r;wakeUpTime++) { for(int i=0;i<xTimes.length;i++) { int j=0; for(j=0;j<zTimes.length;j++) { if((xTimes[i][0]+wakeUpTime>=zTimes[j][0] && xTimes[i][0]+wakeUpTime<=zTimes[j][1]) || (xTimes[i][1]+wakeUpTime<=zTimes[j][1] && xTimes[i][1]+wakeUpTime>=zTimes[j][0]) || (xTimes[i][0]+wakeUpTime<=zTimes[j][0] && xTimes[i][1]+wakeUpTime>=zTimes[j][1])) { numWakeUpTimes++; break; } } if(j!=zTimes.length) { break; } } } return numWakeUpTimes; } }
Java
["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"]
1 second
["3", "20"]
null
Java 8
standard input
[ "implementation" ]
aa77158bf4c0854624ddd89aa8b424b3
The first line contains four space-separated integers p, q, l, r (1 ≤  p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai &lt; bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj &lt; dj ≤ 1000). It's guaranteed that bi &lt; ai + 1 and dj &lt; cj + 1 for all valid i and j.
1,300
Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation.
standard output
PASSED
d62b3dd6e4d47541b87d8971749d83f0
train_002.jsonl
1411218000
Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.StringTokenizer; public class ChatOnline { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(bf.readLine()); int p = Integer.parseInt(st.nextToken()); int q = Integer.parseInt(st.nextToken()); int l = Integer.parseInt(st.nextToken()); int r = Integer.parseInt(st.nextToken()); int[]a = new int [p*2]; int[]b = new int [q*2]; for (int i = 0; i < p*2; i++) { st=new StringTokenizer(bf.readLine()); a[i++] = Integer.parseInt(st.nextToken()); a[i] = Integer.parseInt(st.nextToken()); } for (int i = 0; i < q*2; i++) { st=new StringTokenizer(bf.readLine()); b[i++] = Integer.parseInt(st.nextToken()); b[i] = Integer.parseInt(st.nextToken()); } int c = 0 ; while(l<=r){ boolean found = false ; for (int i = 0; i < a.length; i+=2) { for (int j = 0; j < b.length; j+=2) { if(!(b[j+1]+l<a[i] || b[j]+l >a[i+1])){ //System.out.println(l); found = true ; break ; } }if(found) break; } if(found) c++; l++; } System.out.println(c); } }
Java
["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"]
1 second
["3", "20"]
null
Java 8
standard input
[ "implementation" ]
aa77158bf4c0854624ddd89aa8b424b3
The first line contains four space-separated integers p, q, l, r (1 ≤  p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai &lt; bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj &lt; dj ≤ 1000). It's guaranteed that bi &lt; ai + 1 and dj &lt; cj + 1 for all valid i and j.
1,300
Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation.
standard output
PASSED
210a1dd2d3eff785542cf273ba277d38
train_002.jsonl
1411218000
Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that?
256 megabytes
/** * Created by Omar on 1/30/2016. */ import java.util.*; import java.io.*; public class Chat { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] parts=br.readLine().split(" "); int p=Integer.parseInt(parts[0]); int q=Integer.parseInt(parts[1]); int l=Integer.parseInt(parts[2]); int r=Integer.parseInt(parts[3]); int lt,rt; int[] a= new int[1003]; int[] b= new int[1003]; for(int i=0;i<p;i++){ parts=br.readLine().split(" "); lt=Integer.parseInt(parts[0]); rt=Integer.parseInt(parts[1]); for(int j=lt;j<=rt;j++){ a[j]=1; } } for(int i=0;i<q;i++){ parts=br.readLine().split(" "); lt=Integer.parseInt(parts[0]); rt=Integer.parseInt(parts[1]); for(int j=lt;j<=rt;j++){ b[j]=1; } } int s=0; for(int i=l;i<=r;i++) for(int j=0;i+j<1001;j++) if(a[i+j]==1 && b[j]==1){ s++; break; } System.out.println(s); br.close(); } }
Java
["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"]
1 second
["3", "20"]
null
Java 8
standard input
[ "implementation" ]
aa77158bf4c0854624ddd89aa8b424b3
The first line contains four space-separated integers p, q, l, r (1 ≤  p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai &lt; bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj &lt; dj ≤ 1000). It's guaranteed that bi &lt; ai + 1 and dj &lt; cj + 1 for all valid i and j.
1,300
Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation.
standard output
PASSED
5550363f9929fe80d7ef64489584d649
train_002.jsonl
1411218000
Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that?
256 megabytes
import java.util.Scanner; import java.util.List; import java.util.ArrayList; import java.util.Set; import java.util.HashSet; public class B469 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int p = sc.nextInt(); int q = sc.nextInt(); int l = sc.nextInt(); int r = sc.nextInt(); int[][] z = new int[p][2]; int[][] x = new int[q][2]; Set<Integer> res; for (int i = 0; i < p; i++) { z[i][0] = sc.nextInt(); z[i][1] = sc.nextInt(); } res = new HashSet<>(); for (int i = 0; i < q; i++) { x[i][0] = sc.nextInt(); x[i][1] = sc.nextInt(); } sc.close(); for (int t = l; t <= r; t++) { for (int i = 0; i < x.length; i++) { int a = t + x[i][0]; int b = t + x[i][1]; for (int j = 0; j < z.length; j++) { if ((a >= z[j][0] && a <= z[j][1] || (b >= z[j][0] && b <= z[j][1] || a <= z[j][0] && b >= z[j][1]))) { if (! res.contains(t)) res.add(t); } } } } System.out.println(res.size()); } }
Java
["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"]
1 second
["3", "20"]
null
Java 8
standard input
[ "implementation" ]
aa77158bf4c0854624ddd89aa8b424b3
The first line contains four space-separated integers p, q, l, r (1 ≤  p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai &lt; bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj &lt; dj ≤ 1000). It's guaranteed that bi &lt; ai + 1 and dj &lt; cj + 1 for all valid i and j.
1,300
Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation.
standard output
PASSED
b5018120ecb7ad8aa72b3117d573056c
train_002.jsonl
1411218000
Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that?
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; import java.math.*; public class j8 implements Runnable { public void run(){ InputReader in = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int p=in.nextInt(); int q=in.nextInt(); int l=in.nextInt(); int r=in.nextInt(); int[] vis=new int[1001]; int[] a=new int[p]; int[] b=new int[p]; int[] c=new int[q]; int[] d=new int[q]; for(int i=0;i<p;i++) { a[i]=in.nextInt(); b[i]=in.nextInt(); } for(int i=0;i<q;i++) { c[i]=in.nextInt(); d[i]=in.nextInt(); } for(int i=0;i<p;i++) { for(int j=0;j<q;j++) { int d1=-1,d2=-1,d3=-1,d4=-1; if(c[j]>b[i]) continue; for(int k=l;k<=r;k++) { if(c[j]+k>=a[i] && c[j]+k<=b[i]) vis[k]++; if(d[j]+k>=a[i] && d[j]+k<=b[i]) vis[k]++; if(c[j]+k<=a[i] && d[j]+k>=a[i]) vis[k]++; if(c[j]+k<=b[i] && d[j]+k>=b[i]) vis[k]++; } } } int t=0; /*for(int i=0;i<=1000;i++) { if(vis[i]>0) w.print(i+" "); }*/ //w.println(); for(int i=l;i<=r;i++) { if(vis[i]>0) { //w.print(i+" "); t++; } } w.println(t); w.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 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 j8(),"j8",1<<26).start(); } }
Java
["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"]
1 second
["3", "20"]
null
Java 8
standard input
[ "implementation" ]
aa77158bf4c0854624ddd89aa8b424b3
The first line contains four space-separated integers p, q, l, r (1 ≤  p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai &lt; bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj &lt; dj ≤ 1000). It's guaranteed that bi &lt; ai + 1 and dj &lt; cj + 1 for all valid i and j.
1,300
Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation.
standard output
PASSED
e046ae508a0e340f3122836eaa1b9787
train_002.jsonl
1411218000
Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that?
256 megabytes
import java.util.*; import java.io.*; public class Main { static StreamTokenizer st; static int[] zb, ze, xb, xe; public static void main(String[] args) throws IOException { st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); int p = ri(); int q = ri(); int l = ri(); int r = ri(); zb = new int[p]; ze = new int[p]; xb = new int[q]; xe = new int[q]; for(int i = 0; i < p; ++i) { zb[i] = ri(); ze[i] = ri(); } for(int i = 0; i < q; ++i) { xb[i] = ri(); xe[i] = ri(); } int res = 0; while (l <= r) { boolean con = true; for(int j = 0; j < q && con; ++j) { int cb = xb[j] + l; int ce = xe[j] + l; for(int k = 0; k < p; ++k) { if(!(cb > ze[k] || ce < zb[k])) { ++res; con = false; break; } } } ++l; } System.out.println(res); } static int ri() throws IOException { st.nextToken(); return (int) st.nval; } }
Java
["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"]
1 second
["3", "20"]
null
Java 8
standard input
[ "implementation" ]
aa77158bf4c0854624ddd89aa8b424b3
The first line contains four space-separated integers p, q, l, r (1 ≤  p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai &lt; bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj &lt; dj ≤ 1000). It's guaranteed that bi &lt; ai + 1 and dj &lt; cj + 1 for all valid i and j.
1,300
Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation.
standard output
PASSED
b1bef1c64ea9aba5d60249563cd2ffbc
train_002.jsonl
1411218000
Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that?
256 megabytes
import java.util.*; import java.io.*; public class B { Reader in; PrintWriter out; int i = 0, j = 0; void solve() { //START// int p = in.nextInt(), q = in.nextInt(), l = in.nextInt(), r = in.nextInt(); BitSet covered = new BitSet(1001); int[] a = new int[p]; int[] b = new int[p]; for (i = 0; i < p; i++) { a[i] = in.nextInt(); b[i] = in.nextInt(); } int curL = 0, curR = 0, low = 0, high = 0; for (i = 0; i < q; i++) { curL = in.nextInt(); curR = in.nextInt(); for (j = 0; j < p; j++) { if (curR <= a[j]) { low = a[j] - curR; high = b[j] - curL; covered.set(low, high+1); } else if (curR <= b[j]) { low = 0; high = b[j] - curL; covered.set(low, high+1); } else if (curL <= b[j]) { low = 0; high = b[j] - curL; covered.set(low, high+1); } } } int sol = (covered.get(l, r+1)).cardinality(); out.println(sol); //END } void runIO() { in = new Reader(); out = new PrintWriter(System.out, false); solve(); out.close(); } public static void main(String[] args) { new B().runIO(); } // input/output static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public final String next() { 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(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } 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 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 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; } public int[] readIntArray(int size) { int[] arr = new int[size]; for (int i = 0; i < size; i++) arr[i] = nextInt(); return arr; } public long[] readLongArray(int size) { long[] arr = new long[size]; for (int i = 0; i < size; i++) arr[i] = nextInt(); return arr; } private void fillBuffer() { try { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); } catch (IOException e) { } if (bytesRead == -1) buffer[0] = -1; } private byte read() { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } } }
Java
["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"]
1 second
["3", "20"]
null
Java 8
standard input
[ "implementation" ]
aa77158bf4c0854624ddd89aa8b424b3
The first line contains four space-separated integers p, q, l, r (1 ≤  p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai &lt; bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj &lt; dj ≤ 1000). It's guaranteed that bi &lt; ai + 1 and dj &lt; cj + 1 for all valid i and j.
1,300
Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation.
standard output
PASSED
98c2cf565169d91fad18a69d32634a9c
train_002.jsonl
1411218000
Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that?
256 megabytes
import java.util.Scanner; public class chatOnline { static int update[] = new int[1001]; public static void main(String args[]) { Scanner sc = new Scanner(System.in); int p = sc.nextInt(); int q = sc.nextInt(); int l = sc.nextInt(); int r = sc.nextInt(); int[][] z = new int[2][p]; int x[][] = new int[2][q]; int c = 0; int k = 0; for (int i = 0; i < p; i++) { for (int j = 0; j < 2; j++) { z[j][i] = sc.nextInt(); } } for (int i = 0; i < q; i++) { for (int j = 0; j < 2; j++) { x[j][i] = sc.nextInt(); } for (int t = l; t <= r; t++) { if (update[t] == 0) { for (int m = 0; m < p; m++) { if ((x[0][i] + t >= z[0][m] && x[0][i] + t <= z[1][m]) || (x[1][i] + t >= z[0][m] && x[1][i] + t <= z[1][m]) || (x[0][i] + t < z[0][m] && x[1][i] + t > z[1][m])) { update[t] = update[t] + 1; } } } } } for (int i = l; i <= r; i++) { if (update[i] >= 1) { k = k + 1; } } System.out.println(k); } }
Java
["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"]
1 second
["3", "20"]
null
Java 8
standard input
[ "implementation" ]
aa77158bf4c0854624ddd89aa8b424b3
The first line contains four space-separated integers p, q, l, r (1 ≤  p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai &lt; bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj &lt; dj ≤ 1000). It's guaranteed that bi &lt; ai + 1 and dj &lt; cj + 1 for all valid i and j.
1,300
Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation.
standard output
PASSED
26380ebd41501891ee142fff7c48c38f
train_002.jsonl
1411218000
Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that?
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collections; import java.util.InputMismatchException; public class Main1 { public static void main(String[] args) { FastScanner sc = new FastScanner(System.in); int p = sc.nextInt(); int q = sc.nextInt(); int l = sc.nextInt(); int r = sc.nextInt(); Interval[] Z = new Interval[p]; Interval[] X = new Interval[q]; for(int i = 0; i < p; i++){ int s = sc.nextInt(); int e = sc.nextInt(); Z[i] = new Interval(s, e); } for(int i = 0; i < q; i++){ int s = sc.nextInt(); int e = sc.nextInt(); X[i] = new Interval(s, e); } ArrayList<Interval> intervals = new ArrayList<>(); for(int i = 0; i<q; i++){ for(int j = 0; j<p; j++){ if(X[i].s <= Z[j].e){ int minPush = Math.max(0, Z[j].s - X[i].e); int maxPush = Z[j].e - X[i].s; if(maxPush >= 0){ intervals.add(new Interval(minPush, maxPush)); } } } } Collections.sort(intervals); if(intervals.isEmpty()){ System.out.println(0); } else{ ArrayList<Interval> final_intervals = new ArrayList<>(); final_intervals.add(intervals.get(0)); int p1 = 1; int p2 = 0; int size = intervals.size(); while(p1 < size){ Interval current = intervals.get(p1); Interval last = final_intervals.get(p2); if(current.s <= last.e){ //append last.e = Math.max(current.e, last.e); } else{ //new entry final_intervals.add(current); p2++; } p1++; } int total = 0; for(Interval i : final_intervals){ total += Math.max(0, Math.min(r, i.e) - Math.max(l, i.s) + 1);; } System.out.println(total); } } } class Interval implements Comparable<Interval>{ int s; int e; Interval(int start, int end){ s = start; e = end; } public int compareTo(Interval i) { return s - i.s; } } class FastScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public FastScanner(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 next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isLineEndChar(c)); return res.toString(); } 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 long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isLineEndChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
Java
["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"]
1 second
["3", "20"]
null
Java 8
standard input
[ "implementation" ]
aa77158bf4c0854624ddd89aa8b424b3
The first line contains four space-separated integers p, q, l, r (1 ≤  p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai &lt; bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj &lt; dj ≤ 1000). It's guaranteed that bi &lt; ai + 1 and dj &lt; cj + 1 for all valid i and j.
1,300
Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation.
standard output
PASSED
11fc57eea3e8119a7d37df13bcefc377
train_002.jsonl
1411218000
Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that?
256 megabytes
import java.io.*; import java.util.*; public class A25{ static class Pair{ int left; int right; } public static boolean intersect(Pair p1,Pair p2,int t){ if(p2.left+t <= p1.right && p2.left+t >=p1.left) return true; else if(p2.right+t <=p1.right && p2.right+t >=p1.left){ return true; } else if(p2.left+t <=p1.left && p1.left<=p2.right+t) return true; else return false; } public static void main(String args[]){ Scanner sc = new Scanner(System.in); int p = sc.nextInt(); int q = sc.nextInt(); int l = sc.nextInt(); int r = sc.nextInt(); Pair X[] = new Pair[p]; for(int i=0;i<p;i++){ X[i] = new Pair(); X[i].left = sc.nextInt(); X[i].right = sc.nextInt(); } Pair Z[] = new Pair[q]; for(int i=0;i<q;i++){ Z[i] = new Pair(); Z[i].left = sc.nextInt(); Z[i].right = sc.nextInt(); } int count=0; boolean flag=false; for(int i=l;i<=r;i++){ for(int j=0;j<p;j++){ flag=false; for(int k=0;k<q;k++){ if(intersect(X[j],Z[k],i)){ //System.out.println(i+" "+j+" "+k+" "+count); count++; flag = true; break; } } if(flag) break; } } System.out.println(count); } }
Java
["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"]
1 second
["3", "20"]
null
Java 8
standard input
[ "implementation" ]
aa77158bf4c0854624ddd89aa8b424b3
The first line contains four space-separated integers p, q, l, r (1 ≤  p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai &lt; bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj &lt; dj ≤ 1000). It's guaranteed that bi &lt; ai + 1 and dj &lt; cj + 1 for all valid i and j.
1,300
Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation.
standard output
PASSED
864c1c254de9c8a94c207e343a88c945
train_002.jsonl
1411218000
Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that?
256 megabytes
import java.util.Scanner; public class ChatOnline { public static void main(String[] args) { Scanner input = new Scanner(System.in); int p=input.nextInt(), q=input.nextInt(), l=input.nextInt(), r=input.nextInt(); int[] xs = new int[p], ys=new int[p]; boolean[] v = new boolean[1004]; int t = 0; for(int i=0;i<p;i++) { xs[i] = input.nextInt(); ys[i] = input.nextInt(); } for(int i=0;i<q;i++) { int c=input.nextInt(), d=input.nextInt(); for(int j=0;j<p;j++) { int x = xs[j]-d, y=ys[j]-c; if(l>y || x>r) continue; for(int k=Math.max(x, l);k<=Math.min(y, r);k++) { if(!v[k]) { v[k] = true; t++; } } } } System.out.println(t); } }
Java
["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"]
1 second
["3", "20"]
null
Java 8
standard input
[ "implementation" ]
aa77158bf4c0854624ddd89aa8b424b3
The first line contains four space-separated integers p, q, l, r (1 ≤  p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai &lt; bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj &lt; dj ≤ 1000). It's guaranteed that bi &lt; ai + 1 and dj &lt; cj + 1 for all valid i and j.
1,300
Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation.
standard output
PASSED
07c21ddd7f7de835f3ad523379f49b14
train_002.jsonl
1411218000
Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that?
256 megabytes
import java.util.*; public class CodeForces implements Runnable { private Scanner scanner; public static void main(String args[]) { new Thread(new CodeForces()).start(); } public void run() { scanner = new Scanner(System.in); solution(); scanner.close(); } //651B, 469A, 469B public void solution() { int p = scanner.nextInt(); int q = scanner.nextInt(); int l = scanner.nextInt(); int r = scanner.nextInt(); ArrayList<Schedule> xList = new ArrayList<Schedule>(); ArrayList<Schedule> zList = new ArrayList<Schedule>(); for (int i = 0; i < p; i++) { zList.add(new Schedule(scanner.nextInt(), scanner.nextInt())); } for (int i = 0; i < q; i++) { xList.add(new Schedule(scanner.nextInt(), scanner.nextInt())); } int counter = 0; for (int i = l; i <= r; i++) { loop: for (Schedule s: xList) { int x = s.x + i; int y = s.y + i; for (Schedule k: zList) { if ((x >= k.x && x <= k.y) || (y >= k.x && y <= k.y) || (k.x >= x && k.x <= y) || (k.y >= x && k.y <= y)) { counter++; break loop; } } } } System.out.println(counter); } class Schedule { private int x; private int y; public Schedule(int x, int y) { this.x = x; this.y = y; } } private boolean isPrime(int n) { if (n <= 1) { return false; } else if (n <= 3) { return true; } else if (n % 2 == 0 || n % 3 == 0) { return false; } int i = 5; while (i * i <= n) { if (n % i == 0 || n % (i + 2) == 0) { return false; } i = i + 6; } return true; } }
Java
["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"]
1 second
["3", "20"]
null
Java 8
standard input
[ "implementation" ]
aa77158bf4c0854624ddd89aa8b424b3
The first line contains four space-separated integers p, q, l, r (1 ≤  p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai &lt; bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj &lt; dj ≤ 1000). It's guaranteed that bi &lt; ai + 1 and dj &lt; cj + 1 for all valid i and j.
1,300
Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation.
standard output
PASSED
906683cf4774c6fa77c964a66cae3dfb
train_002.jsonl
1411218000
Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that?
256 megabytes
import java.util.*; public class Main{ public static void main(String args[]){ int i,j,c=0,k,a1,a2; Scanner sc=new Scanner(System.in); int p=sc.nextInt(); int q=sc.nextInt(); int l=sc.nextInt(); int r=sc.nextInt(); int a[][]=new int [p][2]; int b[][]=new int [q][2]; for(i=0;i<p;i++){ a[i][0]=sc.nextInt(); a[i][1]=sc.nextInt(); } for(i=0;i<q;i++){ b[i][0]=sc.nextInt(); b[i][1]=sc.nextInt(); } for(k=l;k<=r;k++){ z: for(i=0;i<q;i++){ a1=k+b[i][0]; a2=k+b[i][1]; for(j=0;j<p;j++){ if(a1>=a[j][0]&&a1<=a[j][1]||(a2>=a[j][0]&&a2<=a[j][1])||(a1<=a[j][0]&&a2>=a[j][1])) { c++; break z; } } } } System.out.println(c); } }
Java
["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"]
1 second
["3", "20"]
null
Java 8
standard input
[ "implementation" ]
aa77158bf4c0854624ddd89aa8b424b3
The first line contains four space-separated integers p, q, l, r (1 ≤  p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai &lt; bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj &lt; dj ≤ 1000). It's guaranteed that bi &lt; ai + 1 and dj &lt; cj + 1 for all valid i and j.
1,300
Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation.
standard output
PASSED
4992ee26fd9365ecef756f78c11bfab8
train_002.jsonl
1411218000
Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that?
256 megabytes
import javax.print.attribute.standard.PrinterIsAcceptingJobs; import java.io.*; import java.math.BigInteger; import java.sql.Array; import java.util.*; public class TaskC { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int p = sc.nextInt(); int q = sc.nextInt(); Pair[] Z = new Pair[p]; Pair[] X = new Pair[q]; int l = sc.nextInt(); int r = sc.nextInt(); for (int i = 0; i< p;i++) Z[i] = new Pair(sc.nextInt() , sc.nextInt()); for (int i = 0; i< q;i++) X[i] = new Pair(sc.nextInt() , sc.nextInt()); int ans = 0; for (int i = l; i <= r ; i++) { O: for (int x = 0; x < q; x++) { Pair t = new Pair(X[x].x + i, X[x].y + i); for (int z = 0; z < p; z++) { if (Z[z].overlap(t)) { ans++; break O; } } } } pw.print(ans); pw.close(); } private static int gcd(int a, int b) { if( b == 0) return a; return gcd(b , a%b); } static class Pair implements Comparable<Pair> { int x, y; public Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair o) { if(x == o.x) return y - o.y; return x - o.x; } public double dis(Pair a){ return (a.x - x)*(a.x - x) + (a.y-y)*(a.y-y); } public String toString() { return x + " " + y; } public boolean overlap(Pair a) { if((this.x >= a.x && this.x <= a.y) || (a.x >= this.x && a.x <= this.y)) return true; return false; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public boolean check() { if (!st.hasMoreTokens()) return false; return true; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(e); } } public double nextDouble() { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() { try { return br.ready(); } catch (IOException e) { throw new RuntimeException(e); } } } }
Java
["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"]
1 second
["3", "20"]
null
Java 8
standard input
[ "implementation" ]
aa77158bf4c0854624ddd89aa8b424b3
The first line contains four space-separated integers p, q, l, r (1 ≤  p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai &lt; bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj &lt; dj ≤ 1000). It's guaranteed that bi &lt; ai + 1 and dj &lt; cj + 1 for all valid i and j.
1,300
Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation.
standard output
PASSED
d631f7ef4d6fa2e46958648f4b76ccb3
train_002.jsonl
1411218000
Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that?
256 megabytes
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ //import com.sun.xml.internal.ws.util.StringUtils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; import java.util.stream.Collectors; /** * * @author george */ public class main { /* public static boolean isPrime(long n) { if(n < 2) return false; if(n == 2 || n == 3) return true; if(n%2 == 0 || n%3 == 0) return false; long sqrtN = (long)Math.sqrt(n)+1; for(long i = 6L; i <= sqrtN; i += 6) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; }*/ /* private static long f(long l) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } public static boolean contains(final int[] arr, final int key) { return Arrays.stream(arr).anyMatch(i -> i == key); } static boolean isSubSequence(String str1, String str2, int m, int n) { if (m == 0) return true; if (n == 0) return false; if (str1.charAt(m-1) == str2.charAt(n-1)) return isSubSequence(str1, str2, m-1, n-1); return isSubSequence(str1, str2, m, n-1); } static int gcdThing(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } public static boolean checkAnagram(String str1, String str2) { int i=0; for (char c : str1.toCharArray()) { i = str2.indexOf(c, i) + 1; if (i <= 0) { return false; } } return true; } public static Set primeFactors(int num) { Set<Integer>Prime=new HashSet<Integer>(); // Print the number of 2s that divide n int tw=2; while (num%2==0) { Prime.add(tw); num /= 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(num); i+= 2) { // While i divides n, print i and divide n while (num%i == 0) { Prime.add(i); num/= i; } } // This condition is to handle the case whien // n is a prime number greater than 2 if (num> 2) Prime.add(num); return Prime; } public static String Winner(int [] arr) { int maxsum=-1; int temp=0; List<Integer> L=Arrays.stream(arr).boxed().collect(Collectors.toList()); int count=0; String winner="First"; while(L.isEmpty()!=true){ if(count%2==0){winner="First"; //take max odd interval for (int i = 0; i < L.size(); i++) { } } else{winner="Second"; } count++; } return winner; } public static String changeCharInPosition(int position, char ch, String str){ char[] charArray = str.toCharArray(); charArray[position] = ch; return new String(charArray); } public static boolean CheckCanObtainYfromX(String y,String x){ boolean cond=true; String help=""; for (int i = 0; i < y.length(); i++) { help="";help+=y.charAt(i); if(y.contains(help)==false){x=x.replaceFirst(help,"A");cond=false;break;} } return cond; } public static int LongestPalindrome(String x){ int n=x.length(); boolean table[][] = new boolean[n][n]; int maxLength = 1; for (int i = 0; i < n; ++i) table[i][i] = true; int start = 0; for (int i = 0; i < n - 1; ++i) { if (x.charAt(i) == x.charAt(i + 1)) { table[i][i + 1] = true; start = i; maxLength = 2; } } for (int k = 3; k <= n; ++k) { for (int i = 0; i < n - k + 1; ++i) { int j = i + k - 1; if (table[i + 1][j - 1] && x.charAt(i) == x.charAt(j)) { table[i][j] = true; if (k > maxLength) { start = i; maxLength = k; } } } } return maxLength; } public static long choose(long total, long choose){ if(total < choose) return 0; if(choose == 0 || choose == total) return 1; return choose(total-1,choose-1)+choose(total-1,choose); } public static long bitwiseAdd(int n1, int n2) { int x = n1, y = n2; int xor, and, temp; and = x & y; xor = x ^ y; while (and != 0) { and <<= 1; temp = xor ^ and; and &= xor; xor = temp; } return (xor%2); } public static boolean compare(String a,String b){ for (int i = 0; i < a.length(); i++) { if(a.charAt(i)!=b.charAt(i)&&a.charAt(i)!='?'){return false;} } return true; }*/ /*public static boolean isUniqueChars(String str) { // short circuit - supposed to imply that // there are no more than 256 different characters. // this is broken, because in Java, char's are Unicode, // and 2-byte values so there are 32768 values // (or so - technically not all 32768 are valid chars) if (str.length() > 256) { return false; } // checker is used as a bitmap to indicate which characters // have been seen already int checker = 0; for (int i = 0; i < str.length(); i++) { // set val to be the difference between the char at i and 'a' // unicode 'a' is 97 // if you have an upper-case letter e.g. 'A' you will get a // negative 'val' which is illegal int val = str.charAt(i) - 'a'; // if this lowercase letter has been seen before, then // the corresponding bit in checker will have been set and // we can exit immediately. if ((checker & (1 << val)) > 0) return false; // set the bit to indicate we have now seen the letter. checker |= (1 << val); } // none of the characters has been seen more than once. return true; }*/ /*private static int [] PossibilitiesAsBinaryTruthTable(int n) { int rows = (int) Math.pow(2,n); int [] possibleTrack=new int[rows]; int trackPerRow; for (int i=0; i<rows; i++) { trackPerRow=0; for (int j=n-1; j>=0; j--) { if(((i/(int) Math.pow(2, j))%2)==0){trackPerRow--;} else if(((i/(int) Math.pow(2, j))%2)==1){trackPerRow++;} } possibleTrack[i]=trackPerRow; } return possibleTrack; }*/ public static void main (String [] args) throws IOException { Scanner s=new Scanner(System.in); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw=new PrintWriter(new OutputStreamWriter(System.out)); String line=br.readLine(); String[] input=line.split(" "); int p=Integer.parseInt(input[0]);int q=Integer.parseInt(input[1]);int l=Integer.parseInt(input[2]);int r=Integer.parseInt(input[3]); String [] z=new String[p]; String [] x=new String[q]; for (int i = 0; i < p; i++) { z[i]=br.readLine(); } for (int i = 0; i < q; i++) { x[i]=br.readLine(); } int [] za=new int[p]; int [] zb=new int[p]; String[]now; for (int i = 0; i < p; i++) { now=z[i].split(" "); za[i]=Integer.parseInt(now[0]);zb[i]=Integer.parseInt(now[1]); now[0]="";now[1]=""; } int [] xa=new int[q]; int [] xb=new int[q]; for (int i = 0; i < q; i++) { now=x[i].split(" "); xa[i]=Integer.parseInt(now[0]);xb[i]=Integer.parseInt(now[1]); now[0]="";now[1]=""; } //we need to check all the possibile integers from l to r inclusive //an integr is valid if it has at least an intersection int validTimeToWakeUp=0; int flag=0; int flag2=0; //we need to check if st lines intersect int x1,y1,x2,y2; for (;l<=r; l++) { for (int i = 0; i < q; i++) { for (int j = 0; j < p; j++) { x1=xa[i]+l;y1=xb[i]+l;x2=za[j];y2=zb[j]; if(Math.max(x1, x2)<=Math.min(y1, y2)){flag=1;break;} } if(flag==1){flag=0;validTimeToWakeUp++;break;} } } System.out.print(validTimeToWakeUp); } }
Java
["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"]
1 second
["3", "20"]
null
Java 8
standard input
[ "implementation" ]
aa77158bf4c0854624ddd89aa8b424b3
The first line contains four space-separated integers p, q, l, r (1 ≤  p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai &lt; bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj &lt; dj ≤ 1000). It's guaranteed that bi &lt; ai + 1 and dj &lt; cj + 1 for all valid i and j.
1,300
Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation.
standard output