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
8271439cc5ac756f9c7f540601048ce0
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.BufferedReader; import java.io.Reader; 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; MyInput in = new MyInput(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, MyInput in, PrintWriter out) { int t = in.nextInt(); while (t-- > 0) { int l = in.nextInt(); int r = in.nextInt(); out.println(l + " " + 2 * l); } } } static class MyInput { private final BufferedReader in; private static int pos; private static int readLen; private static final char[] buffer = new char[1024 * 8]; private static char[] str = new char[500 * 8 * 2]; private static boolean[] isDigit = new boolean[256]; private static boolean[] isSpace = new boolean[256]; private static boolean[] isLineSep = new boolean[256]; static { for (int i = 0; i < 10; i++) { isDigit['0' + i] = true; } isDigit['-'] = true; isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true; isLineSep['\r'] = isLineSep['\n'] = true; } public MyInput(InputStream is) { in = new BufferedReader(new InputStreamReader(is)); } public int read() { if (pos >= readLen) { pos = 0; try { readLen = in.read(buffer); } catch (IOException e) { throw new RuntimeException(); } if (readLen <= 0) { throw new MyInput.EndOfFileRuntimeException(); } } return buffer[pos++]; } public int nextInt() { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; int ret = 0; if (str[0] == '-') { i = 1; } for (; i < len; i++) ret = ret * 10 + str[i] - '0'; if (str[0] == '-') { ret = -ret; } return ret; } public char nextChar() { while (true) { final int c = read(); if (!isSpace[c]) { return (char) c; } } } int reads(int len, boolean[] accept) { try { while (true) { final int c = read(); if (accept[c]) { break; } if (str.length == len) { char[] rep = new char[str.length * 3 / 2]; System.arraycopy(str, 0, rep, 0, str.length); str = rep; } str[len++] = (char) c; } } catch (MyInput.EndOfFileRuntimeException e) { } return len; } static class EndOfFileRuntimeException extends RuntimeException { } } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
4d49e8e42ce192eda490b5162d0824de
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int T = scanner.nextInt(); while (T-- > 0) { int l = scanner.nextInt(); int r = scanner.nextInt(); System.out.println(l + " " + 2*l); } } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
cc50f9e1819eed36059af9e3aa7f49bf
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.util.Scanner; public class sisxth { public static void main(String ards[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t!=0) { long l,r,x,y; l=sc.nextLong(); r=sc.nextLong(); if(l==1) { x=1; y=r; } else { long mod=r%l; x=l; y=r-mod; } System.out.println(x+" "+y); t--; } } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
a5437cf46b7f66a061013ad809875b0b
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.util.Scanner; public class FindingVisible { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); for (int i = 0; i < n; i++) { int x= in.nextInt(),y=in.nextInt(); findVis(x,y); } } private static void findVis(int x, int y) { if (x == 1 || x == y || y % x == 0) { System.out.println(x + " " + y); return; } int q = y / x; System.out.println(x + " " + ((x * q))); } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
da260395ec635e6aa8f65edefcd91ca6
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.util.*; public class A { public static void main(String... args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while (t-- > 0) { int a, b; a = s.nextInt(); b = s.nextInt(); if (b%a==0) System.out.println(a+" "+b); else System.out.println(a+" "+(b-b%a)); } } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
61453b9d06c40eab8715d090bd64e14b
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.util.*; public class Div { public static void main(String args[]) throws Exception { Scanner scan=new Scanner(System.in); int t=scan.nextInt(); for(int i=1;i<=t;i++) { int l=scan.nextInt(); int r=scan.nextInt(); System.out.print(l+" "+(2*l)); System.out.println(); } } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
aba9cf44f58ea3dfa7902c6eb760af25
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner scan = new Scanner(System.in); int n = scan.nextInt(); while(n-->0){ int l = scan.nextInt(); int r = scan.nextInt(); System.out.println(l+" "+l*2); } } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
cf4220244132a2a5b261197f67e84e8e
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.util.*; public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); StringBuilder result = new StringBuilder(); for(int i = 0; i < t; i++) { int l = scan.nextInt(); int r = scan.nextInt(); int p = r % l; int x = Math.min((r-p), l); int y = Math.max((r-p), l); result.append(x + " " + y + "\n"); } System.out.println(result); } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
68d91422366d8b776f257240d526d6cb
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.util.*; public class main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int l=sc.nextInt(); int r=sc.nextInt(); System.out.println(l+" "+2*l); } } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
c5cf6ef73e9332832bc6593397c3befb
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner scan=new Scanner(System.in); int n=scan.nextInt(); for(int i=0;i<n;i++) { int l=scan.nextInt(); int r=scan.nextInt(); int y=2*l; if(r>=y) { System.out.println(l+" "+y); } } } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
4b09e422cd197ebceb6ab07925dcbf4f
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class FNDV { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); for(int k = 0;k<t;k++){ StringTokenizer st = new StringTokenizer(br.readLine()); int l = Integer.parseInt(st.nextToken()); int r = Integer.parseInt(st.nextToken()); outer: for(int x = l;x < r;x++){ for (int y = r;y > x;y= (y - y%x)){ if(y%x == 0){ System.out.println(x+" "+y); break outer; } } } } } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
d348bd76cbf9934933f482d2204ef775
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.util.Scanner; public class FindDivisible { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t=scan.nextInt(); while(t-->0) { int l=scan.nextInt(); int r=scan.nextInt(); if(r%l==0) System.out.println(l+" "+r); else { r=l*2; System.out.println(l+" "+r); } } } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
eb2baf10f519b74618e3ae54ffe05ef8
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.util.Scanner; public class Vasya_Vershini { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); long t, l, r; t = scanner.nextLong(); for (int i = 0; i < t; i++) { l = scanner.nextLong(); r = scanner.nextLong(); long x = l; p: while (true) { long k = 2; long y = r; y = x * k; k++; if (check(x, y)) { System.out.println(x + " " + y); break p; } x++; } } } private static boolean check(long x, long y) { return (x != y) && (y % x == 0); } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
13a4a6f3341e6cfd8fa54bb86a0a79ba
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.util.Scanner; public class Vasya_Vershini { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); long t, l, r; t = scanner.nextLong(); for (int i = 0; i < t; i++) { l = scanner.nextLong(); r = scanner.nextLong(); long x = l; p: while (true) { long k = 2; long y = r; while (y <= r) { y = x * k; k++; if (check(x, y)) { System.out.println(x + " " + y); break p; } } x++; } } } private static boolean check(long x, long y) { return (x != y) && (y % x == 0); } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
e7fc274ce50c8edd93210a82967f2d74
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.util.Scanner; public class App { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); int testcaseAmount = scanner.nextInt(); scanner.nextLine(); String[] testcases = new String[testcaseAmount]; for (int i = 0; i < testcases.length; i++) { testcases[i] = scanner.nextLine(); } for (String string : testcases) { System.out.println(printDivisblePair(string)); } scanner.close(); } private static String printDivisblePair(String string) { int l = Integer.parseInt(string.split(" ")[0]); return l + " " + (2 * l); } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
30aab179c42bfffd8176f0e305d6ad85
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.util.Scanner; public class App { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); int testcaseAmount = scanner.nextInt(); scanner.nextLine(); String[] testcases = new String[testcaseAmount]; for (int i = 0; i < testcases.length; i++) { testcases[i] = scanner.nextLine(); } for (String string : testcases) { System.out.println(printDivisblePair(string)); } scanner.close(); } private static String printDivisblePair(String string) { String[] arr = string.split(" "); int l = Integer.parseInt(arr[0]); int r = Integer.parseInt(arr[1]); for (int i = l; i <= r; i++) { if ((2 * i) <= r && i != 0) { return i + " " + (2 * i); } } return ""; } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
615326484a4be4821662bf676fda6526
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.util.Scanner; public class probA1096 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n= sc.nextInt(); for(int i=0;i<n;i++) { int l= sc.nextInt(); int r= sc.nextInt(); System.out.println(l+" "+l*2); } } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
4f7c9225919a0b51673ba411a8b84d43
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.util.Scanner; public class CF_contest_1096A { public static void main(String[] args) { Scanner input = new Scanner(System.in); int t = input.nextInt(); for (int i = 1; i <= t; i++) { int l = input.nextInt(); int r = input.nextInt(); System.out.println(l + " " + 2*l); } } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
7edff86aef5c5bc5addbaed0da0e1ade
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; public class A1096 { public static void main(String[] args) throws FileNotFoundException { Scanner scan = new Scanner(System.in); int queries = scan.nextInt(); for(int i =0 ; i < queries; i ++) { int inner = scan.nextInt(); int outer = scan.nextInt(); if(outer % inner == 0) { System.out.println(inner + " " + outer); }else { System.out.println(inner + " " + inner * 2); } } } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
2162786ecc71b51513315797392b7063
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.util.*; public class Solution{ public static void main(String[] args){ Scanner s=new Scanner(System.in); int t=s.nextInt(); while(t-->0){ int a=s.nextInt(),b=s.nextInt(); int x=a,y=2*a; System.out.println(""+x+" "+y); } } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
9f3ce18dc3ab9612fc510f7fd8b6cc9a
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
/* A. Find Divisible http://codeforces.com/problemset/problem/1096/A */ import java.util.Scanner; public class A08 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); for (int k = 0; k < n; k++) { int r = in.nextInt(); int l = in.nextInt(); System.out.println(r + " " + r * 2); } } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
ded1bd4f804a1c24a384cb7997e2d4c8
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.util.Scanner; public class AA { public static void main(String args[]) { Scanner scan=new Scanner(System.in); int n=scan.nextInt(); for(int i=0;i<n;i++) { boolean found=false; int l=scan.nextInt(); int r=scan.nextInt(); for(int q=l;q<=r;q++) { if(q*2<=r){ System.out.println(q+" "+q*2); break; } } } } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
185e88d3b9bf3690c52e41f5483622ba
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
public class A { public static void main (String arg[]){ java.util.Scanner sc = new java.util.Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int l = sc.nextInt(); int r = sc.nextInt(); System.out.println(l+" "+2*l); } } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
9e0f9ebb7585a73268197300c07e1d20
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.net.*; import java.applet.*; import java.awt.*; import java.awt.event.*; public class cf { public static void main(String args[]) { Scanner in=new Scanner(System.in); int t=in.nextInt(); while(t>0) { t--; int x=in.nextInt(); int y=in.nextInt(); System.out.println(x+" "+(x*2)); } } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
2a0f971ea12a7d90b906d63b87116dd7
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.util.*; public class find { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++) { long x=sc.nextLong(); long y=sc.nextLong(); System.out.println(x+" "+(2*x)); } } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
e8ed89f87e2bf5dbbdde4c0e5ad5de35
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.util.*; public class xbyy { public static void main(String ag[]) { Scanner sc=new Scanner(System.in); int T,l,r,i; T=sc.nextInt(); while((T--)>0) { l=sc.nextInt(); r=sc.nextInt(); for(i=l;i<=r/2;i++) { if((2*i)%i==0&&(2*i)<=r) { System.out.println(i+" "+(2*i)); break; } } } } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
c9bffe5aaa3059dca20f63f0ecc010c0
train_002.jsonl
1546007700
You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.
256 megabytes
import java.util.Scanner; public class main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int num = in.nextInt(); for (int i = 0; i < num; i++) { int l = in.nextInt(); int r = in.nextInt(); System.out.print(l + " "+ l*2+ "\n"); } } }
Java
["3\n1 10\n3 14\n1 10"]
2 seconds
["1 7\n3 9\n5 10"]
null
Java 8
standard input
[ "implementation", "greedy", "math" ]
a9cd97046e27d799c894d8514e90a377
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) β€” the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) β€” inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.
800
Print $$$T$$$ lines, each line should contain the answer β€” two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.
standard output
PASSED
d3466e19ad9225660b375a3be6df8d44
train_002.jsonl
1293552000
Cheaterius is a famous in all the Berland astrologist, magician and wizard, and he also is a liar and a cheater. One of his latest inventions is Cheaterius' amulets! They bring luck and wealth, but are rather expensive. Cheaterius makes them himself. The technology of their making is kept secret. But we know that throughout long nights Cheaterius glues together domino pairs with super glue to get squares 2 × 2 which are the Cheaterius' magic amulets! That's what one of Cheaterius's amulets looks like After a hard night Cheaterius made n amulets. Everyone of them represents a square 2 × 2, every quarter contains 1 to 6 dots. Now he wants sort them into piles, every pile must contain similar amulets. Two amulets are called similar if they can be rotated by 90, 180 or 270 degrees so that the following condition is met: the numbers of dots in the corresponding quarters should be the same. It is forbidden to turn over the amulets.Write a program that by the given amulets will find the number of piles on Cheaterius' desk.
256 megabytes
import java.util.*; public class FortuneTelling { static Set<Integer> s = new HashSet<Integer>(); static void permute(char[] a, char[] b) { s.add(Integer.parseInt(""+a[1]+a[0]+b[0]+b[1])); s.add(Integer.parseInt(""+a[0]+b[0]+b[1]+a[1])); // 90 s.add(Integer.parseInt(""+b[0]+b[1]+a[1]+a[0])); // 180 s.add(Integer.parseInt(""+b[1]+a[1]+a[0]+b[0])); // 270 } static void solve(){ Scanner sc = new Scanner(System.in); int NOC=sc.nextInt(), n=NOC; while (NOC-->0) { String a = sc.next(); String b = sc.next(); String c = new StringBuilder(a).reverse().toString(); if (!s.contains(Integer.parseInt(c+b))) permute(a.toCharArray(),b.toCharArray()); else n--; if(NOC>0) sc.next(); } System.out.println(n); sc.close(); } public static void main(String args[]) { solve(); } }
Java
["4\n31\n23\n**\n31\n23\n**\n13\n32\n**\n32\n13", "4\n51\n26\n**\n54\n35\n**\n25\n61\n**\n45\n53"]
2 seconds
["1", "2"]
null
Java 11
standard input
[ "implementation" ]
3de36638239deacac56e104d3dce1670
The first line contains an integer n (1 ≀ n ≀ 1000), where n is the number of amulets. Then the amulet's descriptions are contained. Every description occupies two lines and contains two numbers (from 1 to 6) in each line. Between every pair of amulets the line "**" is located.
1,300
Print the required number of piles.
standard output
PASSED
8a71c8caf64a04712a6647d96938cda9
train_002.jsonl
1531578900
Maxim wants to buy some games at the local game shop. There are $$$n$$$ games in the shop, the $$$i$$$-th game costs $$$c_i$$$.Maxim has a wallet which can be represented as an array of integers. His wallet contains $$$m$$$ bills, the $$$j$$$-th bill has value $$$a_j$$$.Games in the shop are ordered from left to right, Maxim tries to buy every game in that order.When Maxim stands at the position $$$i$$$ in the shop, he takes the first bill from his wallet (if his wallet is empty then he proceeds to the next position immediately) and tries to buy the $$$i$$$-th game using this bill. After Maxim tried to buy the $$$n$$$-th game, he leaves the shop.Maxim buys the $$$i$$$-th game if and only if the value of the first bill (which he takes) from his wallet is greater or equal to the cost of the $$$i$$$-th game. If he successfully buys the $$$i$$$-th game, the first bill from his wallet disappears and the next bill becomes first. Otherwise Maxim leaves the first bill in his wallet (this bill still remains the first one) and proceeds to the next game.For example, for array $$$c = [2, 4, 5, 2, 4]$$$ and array $$$a = [5, 3, 4, 6]$$$ the following process takes place: Maxim buys the first game using the first bill (its value is $$$5$$$), the bill disappears, after that the second bill (with value $$$3$$$) becomes the first one in Maxim's wallet, then Maxim doesn't buy the second game because $$$c_2 &gt; a_2$$$, the same with the third game, then he buys the fourth game using the bill of value $$$a_2$$$ (the third bill becomes the first one in Maxim's wallet) and buys the fifth game using the bill of value $$$a_3$$$.Your task is to get the number of games Maxim will buy.
256 megabytes
import java.util.*; public class Main { public static void main(String args[]){ Scanner in = new Scanner(System.in); int n = in.nextInt(), m = in.nextInt(), gam[] = new int[n], mon[] = new int[m]; for (int i = 0; i < n; i++) gam[i] = in.nextInt(); for (int i = 0; i < m; i++) mon[i] = in.nextInt(); int a = 0, b = 0, count = 0; while (n > 0 && m > 0){ if (mon[b] >= gam[a]){ a++; b++; count++; n--; m--; } else { a++; n--; } } System.out.println(count); } }
Java
["5 4\n2 4 5 2 4\n5 3 4 6", "5 2\n20 40 50 20 40\n19 20", "6 4\n4 8 15 16 23 42\n1000 1000 1000 1000"]
1 second
["3", "0", "4"]
NoteThe first example is described in the problem statement.In the second example Maxim cannot buy any game because the value of the first bill in his wallet is smaller than the cost of any game in the shop.In the third example the values of the bills in Maxim's wallet are large enough to buy any game he encounter until he runs out of bills in his wallet.
Java 8
standard input
[ "implementation" ]
c3f080681e3da5e1290ef935ff91f364
The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1000$$$) β€” the number of games and the number of bills in Maxim's wallet. The second line of the input contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 1000$$$), where $$$c_i$$$ is the cost of the $$$i$$$-th game. The third line of the input contains $$$m$$$ integers $$$a_1, a_2, \dots, a_m$$$ ($$$1 \le a_j \le 1000$$$), where $$$a_j$$$ is the value of the $$$j$$$-th bill from the Maxim's wallet.
800
Print a single integer β€” the number of games Maxim will buy.
standard output
PASSED
7b25453dbe3e5576b291ca905cd1acb3
train_002.jsonl
1531578900
Maxim wants to buy some games at the local game shop. There are $$$n$$$ games in the shop, the $$$i$$$-th game costs $$$c_i$$$.Maxim has a wallet which can be represented as an array of integers. His wallet contains $$$m$$$ bills, the $$$j$$$-th bill has value $$$a_j$$$.Games in the shop are ordered from left to right, Maxim tries to buy every game in that order.When Maxim stands at the position $$$i$$$ in the shop, he takes the first bill from his wallet (if his wallet is empty then he proceeds to the next position immediately) and tries to buy the $$$i$$$-th game using this bill. After Maxim tried to buy the $$$n$$$-th game, he leaves the shop.Maxim buys the $$$i$$$-th game if and only if the value of the first bill (which he takes) from his wallet is greater or equal to the cost of the $$$i$$$-th game. If he successfully buys the $$$i$$$-th game, the first bill from his wallet disappears and the next bill becomes first. Otherwise Maxim leaves the first bill in his wallet (this bill still remains the first one) and proceeds to the next game.For example, for array $$$c = [2, 4, 5, 2, 4]$$$ and array $$$a = [5, 3, 4, 6]$$$ the following process takes place: Maxim buys the first game using the first bill (its value is $$$5$$$), the bill disappears, after that the second bill (with value $$$3$$$) becomes the first one in Maxim's wallet, then Maxim doesn't buy the second game because $$$c_2 &gt; a_2$$$, the same with the third game, then he buys the fourth game using the bill of value $$$a_2$$$ (the third bill becomes the first one in Maxim's wallet) and buys the fifth game using the bill of value $$$a_3$$$.Your task is to get the number of games Maxim will buy.
256 megabytes
import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; public class Main{ public static void main(String[] args)throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); String s[] = br.readLine().split(" "); int n = Integer.parseInt(s[0]); int m = Integer.parseInt(s[1]); String p[] = br.readLine().split(" "); int v[] = new int[n]; for(int i = 0; i<n;i++){ v[i] = Integer.parseInt(p[i]); } p = br.readLine().split(" "); int c[] = new int[m]; for(int i = 0; i<m;i++){ c[i] = Integer.parseInt(p[i]); } int ans = 0, cu = 0; for(int i = 0; i<m;i++){ boolean f = false; for(int j = 0; j<n && cu<n;j++){ if(v[cu]<=c[i] && v[cu]!=-10){ ans++; v[cu] =-10; f = true; break; } cu++; } if(!f){ break; } } out.println(ans); out.close(); } }
Java
["5 4\n2 4 5 2 4\n5 3 4 6", "5 2\n20 40 50 20 40\n19 20", "6 4\n4 8 15 16 23 42\n1000 1000 1000 1000"]
1 second
["3", "0", "4"]
NoteThe first example is described in the problem statement.In the second example Maxim cannot buy any game because the value of the first bill in his wallet is smaller than the cost of any game in the shop.In the third example the values of the bills in Maxim's wallet are large enough to buy any game he encounter until he runs out of bills in his wallet.
Java 8
standard input
[ "implementation" ]
c3f080681e3da5e1290ef935ff91f364
The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1000$$$) β€” the number of games and the number of bills in Maxim's wallet. The second line of the input contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 1000$$$), where $$$c_i$$$ is the cost of the $$$i$$$-th game. The third line of the input contains $$$m$$$ integers $$$a_1, a_2, \dots, a_m$$$ ($$$1 \le a_j \le 1000$$$), where $$$a_j$$$ is the value of the $$$j$$$-th bill from the Maxim's wallet.
800
Print a single integer β€” the number of games Maxim will buy.
standard output
PASSED
fe2b70e9a7a5061867c2cb52ce5ef493
train_002.jsonl
1531578900
Maxim wants to buy some games at the local game shop. There are $$$n$$$ games in the shop, the $$$i$$$-th game costs $$$c_i$$$.Maxim has a wallet which can be represented as an array of integers. His wallet contains $$$m$$$ bills, the $$$j$$$-th bill has value $$$a_j$$$.Games in the shop are ordered from left to right, Maxim tries to buy every game in that order.When Maxim stands at the position $$$i$$$ in the shop, he takes the first bill from his wallet (if his wallet is empty then he proceeds to the next position immediately) and tries to buy the $$$i$$$-th game using this bill. After Maxim tried to buy the $$$n$$$-th game, he leaves the shop.Maxim buys the $$$i$$$-th game if and only if the value of the first bill (which he takes) from his wallet is greater or equal to the cost of the $$$i$$$-th game. If he successfully buys the $$$i$$$-th game, the first bill from his wallet disappears and the next bill becomes first. Otherwise Maxim leaves the first bill in his wallet (this bill still remains the first one) and proceeds to the next game.For example, for array $$$c = [2, 4, 5, 2, 4]$$$ and array $$$a = [5, 3, 4, 6]$$$ the following process takes place: Maxim buys the first game using the first bill (its value is $$$5$$$), the bill disappears, after that the second bill (with value $$$3$$$) becomes the first one in Maxim's wallet, then Maxim doesn't buy the second game because $$$c_2 &gt; a_2$$$, the same with the third game, then he buys the fourth game using the bill of value $$$a_2$$$ (the third bill becomes the first one in Maxim's wallet) and buys the fifth game using the bill of value $$$a_3$$$.Your task is to get the number of games Maxim will buy.
256 megabytes
import java.io.*; import java.util.*; public class GameShopping { public static void main(String[] args) throws 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()); st = new StringTokenizer(br.readLine()); int[] c = new int[n]; ArrayList<Integer> a = new ArrayList<Integer>(); for(int i = 0; i < n; i++) { c[i] = Integer.parseInt(st.nextToken()); } st = new StringTokenizer(br.readLine()); for(int i = 0; i < m; i++) { a.add(Integer.parseInt(st.nextToken())); } int count = 0; for(int i = 0; i < n; i++) { if(a.size() == 0)break; if(c[i] <= a.get(0)) { count++; a.remove(0); } } System.out.println(count); } }
Java
["5 4\n2 4 5 2 4\n5 3 4 6", "5 2\n20 40 50 20 40\n19 20", "6 4\n4 8 15 16 23 42\n1000 1000 1000 1000"]
1 second
["3", "0", "4"]
NoteThe first example is described in the problem statement.In the second example Maxim cannot buy any game because the value of the first bill in his wallet is smaller than the cost of any game in the shop.In the third example the values of the bills in Maxim's wallet are large enough to buy any game he encounter until he runs out of bills in his wallet.
Java 8
standard input
[ "implementation" ]
c3f080681e3da5e1290ef935ff91f364
The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1000$$$) β€” the number of games and the number of bills in Maxim's wallet. The second line of the input contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 1000$$$), where $$$c_i$$$ is the cost of the $$$i$$$-th game. The third line of the input contains $$$m$$$ integers $$$a_1, a_2, \dots, a_m$$$ ($$$1 \le a_j \le 1000$$$), where $$$a_j$$$ is the value of the $$$j$$$-th bill from the Maxim's wallet.
800
Print a single integer β€” the number of games Maxim will buy.
standard output
PASSED
1566eeefcb0c41737a597ceb7da0e683
train_002.jsonl
1531578900
Maxim wants to buy some games at the local game shop. There are $$$n$$$ games in the shop, the $$$i$$$-th game costs $$$c_i$$$.Maxim has a wallet which can be represented as an array of integers. His wallet contains $$$m$$$ bills, the $$$j$$$-th bill has value $$$a_j$$$.Games in the shop are ordered from left to right, Maxim tries to buy every game in that order.When Maxim stands at the position $$$i$$$ in the shop, he takes the first bill from his wallet (if his wallet is empty then he proceeds to the next position immediately) and tries to buy the $$$i$$$-th game using this bill. After Maxim tried to buy the $$$n$$$-th game, he leaves the shop.Maxim buys the $$$i$$$-th game if and only if the value of the first bill (which he takes) from his wallet is greater or equal to the cost of the $$$i$$$-th game. If he successfully buys the $$$i$$$-th game, the first bill from his wallet disappears and the next bill becomes first. Otherwise Maxim leaves the first bill in his wallet (this bill still remains the first one) and proceeds to the next game.For example, for array $$$c = [2, 4, 5, 2, 4]$$$ and array $$$a = [5, 3, 4, 6]$$$ the following process takes place: Maxim buys the first game using the first bill (its value is $$$5$$$), the bill disappears, after that the second bill (with value $$$3$$$) becomes the first one in Maxim's wallet, then Maxim doesn't buy the second game because $$$c_2 &gt; a_2$$$, the same with the third game, then he buys the fourth game using the bill of value $$$a_2$$$ (the third bill becomes the first one in Maxim's wallet) and buys the fifth game using the bill of value $$$a_3$$$.Your task is to get the number of games Maxim will buy.
256 megabytes
import java.util.Scanner; public class Ishu { public static void main(String[] args) { Scanner scan=new Scanner(System.in); int n,m,i,j=0,games=0; int[] c=new int[1000]; int[] a=new int[1000]; n=scan.nextInt(); m=scan.nextInt(); for(i=0;i<n;++i) c[i]=scan.nextInt(); for(i=0;i<m;++i) a[i]=scan.nextInt(); for(i=0;i<n;++i) { if(j<m) { if(c[i]<=a[j]) { ++games; ++j; } } } System.out.println(games); } }
Java
["5 4\n2 4 5 2 4\n5 3 4 6", "5 2\n20 40 50 20 40\n19 20", "6 4\n4 8 15 16 23 42\n1000 1000 1000 1000"]
1 second
["3", "0", "4"]
NoteThe first example is described in the problem statement.In the second example Maxim cannot buy any game because the value of the first bill in his wallet is smaller than the cost of any game in the shop.In the third example the values of the bills in Maxim's wallet are large enough to buy any game he encounter until he runs out of bills in his wallet.
Java 8
standard input
[ "implementation" ]
c3f080681e3da5e1290ef935ff91f364
The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1000$$$) β€” the number of games and the number of bills in Maxim's wallet. The second line of the input contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 1000$$$), where $$$c_i$$$ is the cost of the $$$i$$$-th game. The third line of the input contains $$$m$$$ integers $$$a_1, a_2, \dots, a_m$$$ ($$$1 \le a_j \le 1000$$$), where $$$a_j$$$ is the value of the $$$j$$$-th bill from the Maxim's wallet.
800
Print a single integer β€” the number of games Maxim will buy.
standard output
PASSED
9511648e68db38a80f9f222f28530c78
train_002.jsonl
1531578900
Maxim wants to buy some games at the local game shop. There are $$$n$$$ games in the shop, the $$$i$$$-th game costs $$$c_i$$$.Maxim has a wallet which can be represented as an array of integers. His wallet contains $$$m$$$ bills, the $$$j$$$-th bill has value $$$a_j$$$.Games in the shop are ordered from left to right, Maxim tries to buy every game in that order.When Maxim stands at the position $$$i$$$ in the shop, he takes the first bill from his wallet (if his wallet is empty then he proceeds to the next position immediately) and tries to buy the $$$i$$$-th game using this bill. After Maxim tried to buy the $$$n$$$-th game, he leaves the shop.Maxim buys the $$$i$$$-th game if and only if the value of the first bill (which he takes) from his wallet is greater or equal to the cost of the $$$i$$$-th game. If he successfully buys the $$$i$$$-th game, the first bill from his wallet disappears and the next bill becomes first. Otherwise Maxim leaves the first bill in his wallet (this bill still remains the first one) and proceeds to the next game.For example, for array $$$c = [2, 4, 5, 2, 4]$$$ and array $$$a = [5, 3, 4, 6]$$$ the following process takes place: Maxim buys the first game using the first bill (its value is $$$5$$$), the bill disappears, after that the second bill (with value $$$3$$$) becomes the first one in Maxim's wallet, then Maxim doesn't buy the second game because $$$c_2 &gt; a_2$$$, the same with the third game, then he buys the fourth game using the bill of value $$$a_2$$$ (the third bill becomes the first one in Maxim's wallet) and buys the fifth game using the bill of value $$$a_3$$$.Your task is to get the number of games Maxim will buy.
256 megabytes
import java.io.PrintWriter; import java.util.ArrayList; import java.util.Scanner; public class EducationalCodeforcesRound47_A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int[] kolVo = new int[sc.nextInt()]; int[] kolVoDeneg = new int[sc.nextInt()]; ArrayList<String> games = new ArrayList<String>(); for(int i = 0; i < kolVo.length; i++) { games.add(sc.next()); } // System.out.println(games); ArrayList<String> money = new ArrayList<String>(); for(int i = 0; i < kolVoDeneg.length; i++) { money.add(sc.next()); } // System.out.println(money); int otvet = 0; for(int i = 0; i < kolVo.length; i++) { String gamesOne = (String) games.get(i); String moneyOne = (String) money.get(0); // gamesOne = (String) games.get(i); // moneyOne = (String) money.get(0); if(Integer.parseInt(gamesOne) <= Integer.parseInt(moneyOne)) { otvet++; money.remove(0); } if (money.size() == 0) { break; } } out.println(otvet); out.flush(); } }
Java
["5 4\n2 4 5 2 4\n5 3 4 6", "5 2\n20 40 50 20 40\n19 20", "6 4\n4 8 15 16 23 42\n1000 1000 1000 1000"]
1 second
["3", "0", "4"]
NoteThe first example is described in the problem statement.In the second example Maxim cannot buy any game because the value of the first bill in his wallet is smaller than the cost of any game in the shop.In the third example the values of the bills in Maxim's wallet are large enough to buy any game he encounter until he runs out of bills in his wallet.
Java 8
standard input
[ "implementation" ]
c3f080681e3da5e1290ef935ff91f364
The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1000$$$) β€” the number of games and the number of bills in Maxim's wallet. The second line of the input contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 1000$$$), where $$$c_i$$$ is the cost of the $$$i$$$-th game. The third line of the input contains $$$m$$$ integers $$$a_1, a_2, \dots, a_m$$$ ($$$1 \le a_j \le 1000$$$), where $$$a_j$$$ is the value of the $$$j$$$-th bill from the Maxim's wallet.
800
Print a single integer β€” the number of games Maxim will buy.
standard output
PASSED
29b2e2674919bf8642e350da279494fc
train_002.jsonl
1531578900
Maxim wants to buy some games at the local game shop. There are $$$n$$$ games in the shop, the $$$i$$$-th game costs $$$c_i$$$.Maxim has a wallet which can be represented as an array of integers. His wallet contains $$$m$$$ bills, the $$$j$$$-th bill has value $$$a_j$$$.Games in the shop are ordered from left to right, Maxim tries to buy every game in that order.When Maxim stands at the position $$$i$$$ in the shop, he takes the first bill from his wallet (if his wallet is empty then he proceeds to the next position immediately) and tries to buy the $$$i$$$-th game using this bill. After Maxim tried to buy the $$$n$$$-th game, he leaves the shop.Maxim buys the $$$i$$$-th game if and only if the value of the first bill (which he takes) from his wallet is greater or equal to the cost of the $$$i$$$-th game. If he successfully buys the $$$i$$$-th game, the first bill from his wallet disappears and the next bill becomes first. Otherwise Maxim leaves the first bill in his wallet (this bill still remains the first one) and proceeds to the next game.For example, for array $$$c = [2, 4, 5, 2, 4]$$$ and array $$$a = [5, 3, 4, 6]$$$ the following process takes place: Maxim buys the first game using the first bill (its value is $$$5$$$), the bill disappears, after that the second bill (with value $$$3$$$) becomes the first one in Maxim's wallet, then Maxim doesn't buy the second game because $$$c_2 &gt; a_2$$$, the same with the third game, then he buys the fourth game using the bill of value $$$a_2$$$ (the third bill becomes the first one in Maxim's wallet) and buys the fifth game using the bill of value $$$a_3$$$.Your task is to get the number of games Maxim will buy.
256 megabytes
//package problem; import java.util.Scanner; public class array { public static void main(String[] args) { Scanner in_t=new Scanner(System.in); //Scanner in_t2=new Scanner(System.in); int t1=in_t.nextInt(); int t2=in_t.nextInt(); int count=0; //System.out.println(t1+" "+t2); int[] a1=new int[t1+1]; int[] a2=new int[t2+1]; for(int i=1;i<=t1;i++) a1[i]=in_t.nextInt(); for(int i=1;i<=t2;i++) a2[i]=in_t.nextInt(); int j=1; for(int i=1;i<=t1&&j<=t2;i++) { if(a2[j]>=a1[i]) {count++;j++;} } System.out.println(count); in_t.close(); //in_t2.close(); } }
Java
["5 4\n2 4 5 2 4\n5 3 4 6", "5 2\n20 40 50 20 40\n19 20", "6 4\n4 8 15 16 23 42\n1000 1000 1000 1000"]
1 second
["3", "0", "4"]
NoteThe first example is described in the problem statement.In the second example Maxim cannot buy any game because the value of the first bill in his wallet is smaller than the cost of any game in the shop.In the third example the values of the bills in Maxim's wallet are large enough to buy any game he encounter until he runs out of bills in his wallet.
Java 8
standard input
[ "implementation" ]
c3f080681e3da5e1290ef935ff91f364
The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1000$$$) β€” the number of games and the number of bills in Maxim's wallet. The second line of the input contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 1000$$$), where $$$c_i$$$ is the cost of the $$$i$$$-th game. The third line of the input contains $$$m$$$ integers $$$a_1, a_2, \dots, a_m$$$ ($$$1 \le a_j \le 1000$$$), where $$$a_j$$$ is the value of the $$$j$$$-th bill from the Maxim's wallet.
800
Print a single integer β€” the number of games Maxim will buy.
standard output
PASSED
c692f747e08970355f0fd72bede466be
train_002.jsonl
1531578900
Maxim wants to buy some games at the local game shop. There are $$$n$$$ games in the shop, the $$$i$$$-th game costs $$$c_i$$$.Maxim has a wallet which can be represented as an array of integers. His wallet contains $$$m$$$ bills, the $$$j$$$-th bill has value $$$a_j$$$.Games in the shop are ordered from left to right, Maxim tries to buy every game in that order.When Maxim stands at the position $$$i$$$ in the shop, he takes the first bill from his wallet (if his wallet is empty then he proceeds to the next position immediately) and tries to buy the $$$i$$$-th game using this bill. After Maxim tried to buy the $$$n$$$-th game, he leaves the shop.Maxim buys the $$$i$$$-th game if and only if the value of the first bill (which he takes) from his wallet is greater or equal to the cost of the $$$i$$$-th game. If he successfully buys the $$$i$$$-th game, the first bill from his wallet disappears and the next bill becomes first. Otherwise Maxim leaves the first bill in his wallet (this bill still remains the first one) and proceeds to the next game.For example, for array $$$c = [2, 4, 5, 2, 4]$$$ and array $$$a = [5, 3, 4, 6]$$$ the following process takes place: Maxim buys the first game using the first bill (its value is $$$5$$$), the bill disappears, after that the second bill (with value $$$3$$$) becomes the first one in Maxim's wallet, then Maxim doesn't buy the second game because $$$c_2 &gt; a_2$$$, the same with the third game, then he buys the fourth game using the bill of value $$$a_2$$$ (the third bill becomes the first one in Maxim's wallet) and buys the fifth game using the bill of value $$$a_3$$$.Your task is to get the number of games Maxim will buy.
256 megabytes
import java.util.Scanner; import java.util.Arrays; public class x{ public static void main(String[] args){ Scanner in=new Scanner(System.in); int n=in.nextInt(),m=in.nextInt(); int[] a=new int[n],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 res=0; int k=b[0]; int j=0; for(int i=0;i<n;i++){ if(k>=a[i]){ res++; if(j+1<m){ k=b[++j]; continue; } else{ System.out.println(res); return; } } } System.out.println(res); } }
Java
["5 4\n2 4 5 2 4\n5 3 4 6", "5 2\n20 40 50 20 40\n19 20", "6 4\n4 8 15 16 23 42\n1000 1000 1000 1000"]
1 second
["3", "0", "4"]
NoteThe first example is described in the problem statement.In the second example Maxim cannot buy any game because the value of the first bill in his wallet is smaller than the cost of any game in the shop.In the third example the values of the bills in Maxim's wallet are large enough to buy any game he encounter until he runs out of bills in his wallet.
Java 8
standard input
[ "implementation" ]
c3f080681e3da5e1290ef935ff91f364
The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1000$$$) β€” the number of games and the number of bills in Maxim's wallet. The second line of the input contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 1000$$$), where $$$c_i$$$ is the cost of the $$$i$$$-th game. The third line of the input contains $$$m$$$ integers $$$a_1, a_2, \dots, a_m$$$ ($$$1 \le a_j \le 1000$$$), where $$$a_j$$$ is the value of the $$$j$$$-th bill from the Maxim's wallet.
800
Print a single integer β€” the number of games Maxim will buy.
standard output
PASSED
85f24ce3f83ac24d3aa718fd7ad73f45
train_002.jsonl
1531578900
Maxim wants to buy some games at the local game shop. There are $$$n$$$ games in the shop, the $$$i$$$-th game costs $$$c_i$$$.Maxim has a wallet which can be represented as an array of integers. His wallet contains $$$m$$$ bills, the $$$j$$$-th bill has value $$$a_j$$$.Games in the shop are ordered from left to right, Maxim tries to buy every game in that order.When Maxim stands at the position $$$i$$$ in the shop, he takes the first bill from his wallet (if his wallet is empty then he proceeds to the next position immediately) and tries to buy the $$$i$$$-th game using this bill. After Maxim tried to buy the $$$n$$$-th game, he leaves the shop.Maxim buys the $$$i$$$-th game if and only if the value of the first bill (which he takes) from his wallet is greater or equal to the cost of the $$$i$$$-th game. If he successfully buys the $$$i$$$-th game, the first bill from his wallet disappears and the next bill becomes first. Otherwise Maxim leaves the first bill in his wallet (this bill still remains the first one) and proceeds to the next game.For example, for array $$$c = [2, 4, 5, 2, 4]$$$ and array $$$a = [5, 3, 4, 6]$$$ the following process takes place: Maxim buys the first game using the first bill (its value is $$$5$$$), the bill disappears, after that the second bill (with value $$$3$$$) becomes the first one in Maxim's wallet, then Maxim doesn't buy the second game because $$$c_2 &gt; a_2$$$, the same with the third game, then he buys the fourth game using the bill of value $$$a_2$$$ (the third bill becomes the first one in Maxim's wallet) and buys the fifth game using the bill of value $$$a_3$$$.Your task is to get the number of games Maxim will buy.
256 megabytes
import java.util.Scanner; import java.util.Arrays; public class x{ public static void main(String[] args){ Scanner in=new Scanner(System.in); int n=in.nextInt(),m=in.nextInt(); int[] a=new int[n],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 res=0; int k=b[0]; int j=0; for(int i=0;i<n;i++){ if(k>=a[i]){ res++; if(j+1<m){ k=b[++j]; continue; } else{ System.out.println(res); return; } } } System.out.println(res); } }
Java
["5 4\n2 4 5 2 4\n5 3 4 6", "5 2\n20 40 50 20 40\n19 20", "6 4\n4 8 15 16 23 42\n1000 1000 1000 1000"]
1 second
["3", "0", "4"]
NoteThe first example is described in the problem statement.In the second example Maxim cannot buy any game because the value of the first bill in his wallet is smaller than the cost of any game in the shop.In the third example the values of the bills in Maxim's wallet are large enough to buy any game he encounter until he runs out of bills in his wallet.
Java 8
standard input
[ "implementation" ]
c3f080681e3da5e1290ef935ff91f364
The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1000$$$) β€” the number of games and the number of bills in Maxim's wallet. The second line of the input contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 1000$$$), where $$$c_i$$$ is the cost of the $$$i$$$-th game. The third line of the input contains $$$m$$$ integers $$$a_1, a_2, \dots, a_m$$$ ($$$1 \le a_j \le 1000$$$), where $$$a_j$$$ is the value of the $$$j$$$-th bill from the Maxim's wallet.
800
Print a single integer β€” the number of games Maxim will buy.
standard output
PASSED
6e1eaaa7a92371f62c7a88a6390bb284
train_002.jsonl
1531578900
Maxim wants to buy some games at the local game shop. There are $$$n$$$ games in the shop, the $$$i$$$-th game costs $$$c_i$$$.Maxim has a wallet which can be represented as an array of integers. His wallet contains $$$m$$$ bills, the $$$j$$$-th bill has value $$$a_j$$$.Games in the shop are ordered from left to right, Maxim tries to buy every game in that order.When Maxim stands at the position $$$i$$$ in the shop, he takes the first bill from his wallet (if his wallet is empty then he proceeds to the next position immediately) and tries to buy the $$$i$$$-th game using this bill. After Maxim tried to buy the $$$n$$$-th game, he leaves the shop.Maxim buys the $$$i$$$-th game if and only if the value of the first bill (which he takes) from his wallet is greater or equal to the cost of the $$$i$$$-th game. If he successfully buys the $$$i$$$-th game, the first bill from his wallet disappears and the next bill becomes first. Otherwise Maxim leaves the first bill in his wallet (this bill still remains the first one) and proceeds to the next game.For example, for array $$$c = [2, 4, 5, 2, 4]$$$ and array $$$a = [5, 3, 4, 6]$$$ the following process takes place: Maxim buys the first game using the first bill (its value is $$$5$$$), the bill disappears, after that the second bill (with value $$$3$$$) becomes the first one in Maxim's wallet, then Maxim doesn't buy the second game because $$$c_2 &gt; a_2$$$, the same with the third game, then he buys the fourth game using the bill of value $$$a_2$$$ (the third bill becomes the first one in Maxim's wallet) and buys the fifth game using the bill of value $$$a_3$$$.Your task is to get the number of games Maxim will buy.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String []args) { Scanner in = new Scanner(System.in); int numOfGames = in.nextInt(); int numOfBills = in.nextInt(); int [] gameCost = new int [numOfGames]; int [] billValue = new int [numOfBills]; for(int i=0;i<numOfGames;i++) { gameCost[i] = in.nextInt(); } for(int i=0;i<numOfBills;i++) { billValue[i] = in.nextInt(); } int j=0,boughtGames=0; for(int i=0;i<numOfGames&&j<numOfBills;i++) { if(billValue[j]>=gameCost[i]) { j++; boughtGames++; } } System.out.println(boughtGames); in.close(); } }
Java
["5 4\n2 4 5 2 4\n5 3 4 6", "5 2\n20 40 50 20 40\n19 20", "6 4\n4 8 15 16 23 42\n1000 1000 1000 1000"]
1 second
["3", "0", "4"]
NoteThe first example is described in the problem statement.In the second example Maxim cannot buy any game because the value of the first bill in his wallet is smaller than the cost of any game in the shop.In the third example the values of the bills in Maxim's wallet are large enough to buy any game he encounter until he runs out of bills in his wallet.
Java 8
standard input
[ "implementation" ]
c3f080681e3da5e1290ef935ff91f364
The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1000$$$) β€” the number of games and the number of bills in Maxim's wallet. The second line of the input contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 1000$$$), where $$$c_i$$$ is the cost of the $$$i$$$-th game. The third line of the input contains $$$m$$$ integers $$$a_1, a_2, \dots, a_m$$$ ($$$1 \le a_j \le 1000$$$), where $$$a_j$$$ is the value of the $$$j$$$-th bill from the Maxim's wallet.
800
Print a single integer β€” the number of games Maxim will buy.
standard output
PASSED
c2a8148edf6a48d5a974856b40143839
train_002.jsonl
1531578900
Maxim wants to buy some games at the local game shop. There are $$$n$$$ games in the shop, the $$$i$$$-th game costs $$$c_i$$$.Maxim has a wallet which can be represented as an array of integers. His wallet contains $$$m$$$ bills, the $$$j$$$-th bill has value $$$a_j$$$.Games in the shop are ordered from left to right, Maxim tries to buy every game in that order.When Maxim stands at the position $$$i$$$ in the shop, he takes the first bill from his wallet (if his wallet is empty then he proceeds to the next position immediately) and tries to buy the $$$i$$$-th game using this bill. After Maxim tried to buy the $$$n$$$-th game, he leaves the shop.Maxim buys the $$$i$$$-th game if and only if the value of the first bill (which he takes) from his wallet is greater or equal to the cost of the $$$i$$$-th game. If he successfully buys the $$$i$$$-th game, the first bill from his wallet disappears and the next bill becomes first. Otherwise Maxim leaves the first bill in his wallet (this bill still remains the first one) and proceeds to the next game.For example, for array $$$c = [2, 4, 5, 2, 4]$$$ and array $$$a = [5, 3, 4, 6]$$$ the following process takes place: Maxim buys the first game using the first bill (its value is $$$5$$$), the bill disappears, after that the second bill (with value $$$3$$$) becomes the first one in Maxim's wallet, then Maxim doesn't buy the second game because $$$c_2 &gt; a_2$$$, the same with the third game, then he buys the fourth game using the bill of value $$$a_2$$$ (the third bill becomes the first one in Maxim's wallet) and buys the fifth game using the bill of value $$$a_3$$$.Your task is to get the number of games Maxim will buy.
256 megabytes
import javafx.util.*; import java.util.*; import java.io.*; import java.math.*; public class Test5 { static StreamTokenizer st = new StreamTokenizer(new InputStreamReader(System.in)); public static void main(String[] z) throws Exception { Scanner s = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int o=0, a = ni(), b=ni(), bnk=0; int[] m = new int[a], n = new int[b]; for(int q=0; q<a; q++) m[q] = ni(); for(int q=0; q<b; q++) n[q] = ni(); for(int q=0; q<a && bnk<b; q++){ if(n[bnk]>=m[q]){ o++; bnk++; } } System.out.println(o); pw.flush(); } static class PyraSort { private static int heapSize; public static void sort(int[] a) { buildHeap(a); while (heapSize > 1) { swap(a, 0, heapSize - 1); heapSize--; heapify(a, 0); } } private static void buildHeap(int[] a) { heapSize = a.length; for (int i = a.length / 2; i >= 0; i--) { heapify(a, i); } } private static void heapify(int[] a, int i) { int l = 2 * i + 2; int r = 2 * i + 1; int largest = i; if (l < heapSize && a[i] < a[l]) { largest = l; } if (r < heapSize && a[largest] < a[r]) { largest = r; } if (i != largest) { swap(a, i, largest); heapify(a, largest); } } private static void swap(int[] a, int i, int j) { a[i] ^= a[j] ^= a[i]; a[j] ^= a[i]; } } static long gcd(long a, long b){ a = Math.abs(a); b = Math.abs(b); for(; a>0 && b>0;) if(a>b) a%=b; else b%=a; return a+b; } static class mrgs{ static int[] sort(int[] m){ if(m.length<2) return m; int d = m.length, mid = d>>1; return merge(sort(Arrays.copyOfRange(m,0,mid)),sort(Arrays.copyOfRange(m,mid,d))); } static int[] merge(int[] m1, int[] m2){ int d1 = m1.length, d2 = m2.length, c1 = 0, c2=0, nl=d1+d2; int[] ans = new int[nl]; for(int q=0; q<nl; q++){ if(c1<d1 && c2<d2){ if(m1[c1]<m2[c2]) ans[q] = m1[c1++]; else ans[q] = m2[c2++]; } else if(c1<d1) ans[q] = m1[c1++]; else ans[q] = m2[c2++]; } return ans; } } static int ni() throws Exception{ st.nextToken(); return (int)st.nval; } static double nd() throws Exception{ st.nextToken(); return st.nval; } }
Java
["5 4\n2 4 5 2 4\n5 3 4 6", "5 2\n20 40 50 20 40\n19 20", "6 4\n4 8 15 16 23 42\n1000 1000 1000 1000"]
1 second
["3", "0", "4"]
NoteThe first example is described in the problem statement.In the second example Maxim cannot buy any game because the value of the first bill in his wallet is smaller than the cost of any game in the shop.In the third example the values of the bills in Maxim's wallet are large enough to buy any game he encounter until he runs out of bills in his wallet.
Java 8
standard input
[ "implementation" ]
c3f080681e3da5e1290ef935ff91f364
The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1000$$$) β€” the number of games and the number of bills in Maxim's wallet. The second line of the input contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 1000$$$), where $$$c_i$$$ is the cost of the $$$i$$$-th game. The third line of the input contains $$$m$$$ integers $$$a_1, a_2, \dots, a_m$$$ ($$$1 \le a_j \le 1000$$$), where $$$a_j$$$ is the value of the $$$j$$$-th bill from the Maxim's wallet.
800
Print a single integer β€” the number of games Maxim will buy.
standard output
PASSED
b246437a6079c67c0ebabde2bcfbbaa2
train_002.jsonl
1531578900
Maxim wants to buy some games at the local game shop. There are $$$n$$$ games in the shop, the $$$i$$$-th game costs $$$c_i$$$.Maxim has a wallet which can be represented as an array of integers. His wallet contains $$$m$$$ bills, the $$$j$$$-th bill has value $$$a_j$$$.Games in the shop are ordered from left to right, Maxim tries to buy every game in that order.When Maxim stands at the position $$$i$$$ in the shop, he takes the first bill from his wallet (if his wallet is empty then he proceeds to the next position immediately) and tries to buy the $$$i$$$-th game using this bill. After Maxim tried to buy the $$$n$$$-th game, he leaves the shop.Maxim buys the $$$i$$$-th game if and only if the value of the first bill (which he takes) from his wallet is greater or equal to the cost of the $$$i$$$-th game. If he successfully buys the $$$i$$$-th game, the first bill from his wallet disappears and the next bill becomes first. Otherwise Maxim leaves the first bill in his wallet (this bill still remains the first one) and proceeds to the next game.For example, for array $$$c = [2, 4, 5, 2, 4]$$$ and array $$$a = [5, 3, 4, 6]$$$ the following process takes place: Maxim buys the first game using the first bill (its value is $$$5$$$), the bill disappears, after that the second bill (with value $$$3$$$) becomes the first one in Maxim's wallet, then Maxim doesn't buy the second game because $$$c_2 &gt; a_2$$$, the same with the third game, then he buys the fourth game using the bill of value $$$a_2$$$ (the third bill becomes the first one in Maxim's wallet) and buys the fifth game using the bill of value $$$a_3$$$.Your task is to get the number of games Maxim will buy.
256 megabytes
import java.util.Scanner; public class testt { public static void main(String[] args) { Scanner in = new Scanner(System.in); int NumberOfGames,NumberOfBills,Counter=0,k=0; NumberOfGames=in.nextInt(); NumberOfBills=in.nextInt(); int[] Games = new int[NumberOfGames]; int[] Bills = new int[NumberOfBills]; for(int i=0;i<NumberOfGames;i++) { Games[i] = in.nextInt();} for(int i=0;i<NumberOfBills;i++) { Bills[i] = in.nextInt();} for(int i=0;i<NumberOfGames;i++) { if(Bills[k] > Games[i] || Bills[k] == Games[i]) {Counter++;k++;} if(k==NumberOfBills) {break;} } System.out.println(Counter); } }
Java
["5 4\n2 4 5 2 4\n5 3 4 6", "5 2\n20 40 50 20 40\n19 20", "6 4\n4 8 15 16 23 42\n1000 1000 1000 1000"]
1 second
["3", "0", "4"]
NoteThe first example is described in the problem statement.In the second example Maxim cannot buy any game because the value of the first bill in his wallet is smaller than the cost of any game in the shop.In the third example the values of the bills in Maxim's wallet are large enough to buy any game he encounter until he runs out of bills in his wallet.
Java 8
standard input
[ "implementation" ]
c3f080681e3da5e1290ef935ff91f364
The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1000$$$) β€” the number of games and the number of bills in Maxim's wallet. The second line of the input contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 1000$$$), where $$$c_i$$$ is the cost of the $$$i$$$-th game. The third line of the input contains $$$m$$$ integers $$$a_1, a_2, \dots, a_m$$$ ($$$1 \le a_j \le 1000$$$), where $$$a_j$$$ is the value of the $$$j$$$-th bill from the Maxim's wallet.
800
Print a single integer β€” the number of games Maxim will buy.
standard output
PASSED
4355837fd46de6fda4123d8688d6bcdc
train_002.jsonl
1531578900
Maxim wants to buy some games at the local game shop. There are $$$n$$$ games in the shop, the $$$i$$$-th game costs $$$c_i$$$.Maxim has a wallet which can be represented as an array of integers. His wallet contains $$$m$$$ bills, the $$$j$$$-th bill has value $$$a_j$$$.Games in the shop are ordered from left to right, Maxim tries to buy every game in that order.When Maxim stands at the position $$$i$$$ in the shop, he takes the first bill from his wallet (if his wallet is empty then he proceeds to the next position immediately) and tries to buy the $$$i$$$-th game using this bill. After Maxim tried to buy the $$$n$$$-th game, he leaves the shop.Maxim buys the $$$i$$$-th game if and only if the value of the first bill (which he takes) from his wallet is greater or equal to the cost of the $$$i$$$-th game. If he successfully buys the $$$i$$$-th game, the first bill from his wallet disappears and the next bill becomes first. Otherwise Maxim leaves the first bill in his wallet (this bill still remains the first one) and proceeds to the next game.For example, for array $$$c = [2, 4, 5, 2, 4]$$$ and array $$$a = [5, 3, 4, 6]$$$ the following process takes place: Maxim buys the first game using the first bill (its value is $$$5$$$), the bill disappears, after that the second bill (with value $$$3$$$) becomes the first one in Maxim's wallet, then Maxim doesn't buy the second game because $$$c_2 &gt; a_2$$$, the same with the third game, then he buys the fourth game using the bill of value $$$a_2$$$ (the third bill becomes the first one in Maxim's wallet) and buys the fifth game using the bill of value $$$a_3$$$.Your task is to get the number of games Maxim will buy.
256 megabytes
import java.util.LinkedList; import java.util.Scanner; public class A1009 { public static void main(String q[]){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(),m=sc.nextInt(); int ar[]=new int[n]; for (int i=0;i<n;i++) ar[i]=sc.nextInt(); LinkedList<Integer> li=new LinkedList<Integer>(); for (int i=0;i<m;i++) li.add(sc.nextInt()); int cnt=0; for (int i=0;i<n;i++){ int first=0; if(!li.isEmpty()) first=li.getFirst(); if(first>=ar[i] && !li.isEmpty()){ cnt++; li.removeFirst(); } } System.out.println(cnt); } }
Java
["5 4\n2 4 5 2 4\n5 3 4 6", "5 2\n20 40 50 20 40\n19 20", "6 4\n4 8 15 16 23 42\n1000 1000 1000 1000"]
1 second
["3", "0", "4"]
NoteThe first example is described in the problem statement.In the second example Maxim cannot buy any game because the value of the first bill in his wallet is smaller than the cost of any game in the shop.In the third example the values of the bills in Maxim's wallet are large enough to buy any game he encounter until he runs out of bills in his wallet.
Java 8
standard input
[ "implementation" ]
c3f080681e3da5e1290ef935ff91f364
The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1000$$$) β€” the number of games and the number of bills in Maxim's wallet. The second line of the input contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 1000$$$), where $$$c_i$$$ is the cost of the $$$i$$$-th game. The third line of the input contains $$$m$$$ integers $$$a_1, a_2, \dots, a_m$$$ ($$$1 \le a_j \le 1000$$$), where $$$a_j$$$ is the value of the $$$j$$$-th bill from the Maxim's wallet.
800
Print a single integer β€” the number of games Maxim will buy.
standard output
PASSED
e626e6c679988cca4e2a8c256deaec05
train_002.jsonl
1531578900
Maxim wants to buy some games at the local game shop. There are $$$n$$$ games in the shop, the $$$i$$$-th game costs $$$c_i$$$.Maxim has a wallet which can be represented as an array of integers. His wallet contains $$$m$$$ bills, the $$$j$$$-th bill has value $$$a_j$$$.Games in the shop are ordered from left to right, Maxim tries to buy every game in that order.When Maxim stands at the position $$$i$$$ in the shop, he takes the first bill from his wallet (if his wallet is empty then he proceeds to the next position immediately) and tries to buy the $$$i$$$-th game using this bill. After Maxim tried to buy the $$$n$$$-th game, he leaves the shop.Maxim buys the $$$i$$$-th game if and only if the value of the first bill (which he takes) from his wallet is greater or equal to the cost of the $$$i$$$-th game. If he successfully buys the $$$i$$$-th game, the first bill from his wallet disappears and the next bill becomes first. Otherwise Maxim leaves the first bill in his wallet (this bill still remains the first one) and proceeds to the next game.For example, for array $$$c = [2, 4, 5, 2, 4]$$$ and array $$$a = [5, 3, 4, 6]$$$ the following process takes place: Maxim buys the first game using the first bill (its value is $$$5$$$), the bill disappears, after that the second bill (with value $$$3$$$) becomes the first one in Maxim's wallet, then Maxim doesn't buy the second game because $$$c_2 &gt; a_2$$$, the same with the third game, then he buys the fourth game using the bill of value $$$a_2$$$ (the third bill becomes the first one in Maxim's wallet) and buys the fifth game using the bill of value $$$a_3$$$.Your task is to get the number of games Maxim will buy.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Game { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int cost[] = new int[n]; int wallet[] = new int[m]; for (int i=0; i<n; i++) { cost[i]= sc.nextInt(); } for (int i=0; i<m; i++) { wallet[i]= sc.nextInt(); } int count=0; for(int i=0,j=0;i<n&&j<m;i++) { if(cost[i]<=wallet[j]) { count++; j++; } } System.out.println(count); } }
Java
["5 4\n2 4 5 2 4\n5 3 4 6", "5 2\n20 40 50 20 40\n19 20", "6 4\n4 8 15 16 23 42\n1000 1000 1000 1000"]
1 second
["3", "0", "4"]
NoteThe first example is described in the problem statement.In the second example Maxim cannot buy any game because the value of the first bill in his wallet is smaller than the cost of any game in the shop.In the third example the values of the bills in Maxim's wallet are large enough to buy any game he encounter until he runs out of bills in his wallet.
Java 8
standard input
[ "implementation" ]
c3f080681e3da5e1290ef935ff91f364
The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1000$$$) β€” the number of games and the number of bills in Maxim's wallet. The second line of the input contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 1000$$$), where $$$c_i$$$ is the cost of the $$$i$$$-th game. The third line of the input contains $$$m$$$ integers $$$a_1, a_2, \dots, a_m$$$ ($$$1 \le a_j \le 1000$$$), where $$$a_j$$$ is the value of the $$$j$$$-th bill from the Maxim's wallet.
800
Print a single integer β€” the number of games Maxim will buy.
standard output
PASSED
4e09c74866d5360dc5de12e35806e074
train_002.jsonl
1531578900
Maxim wants to buy some games at the local game shop. There are $$$n$$$ games in the shop, the $$$i$$$-th game costs $$$c_i$$$.Maxim has a wallet which can be represented as an array of integers. His wallet contains $$$m$$$ bills, the $$$j$$$-th bill has value $$$a_j$$$.Games in the shop are ordered from left to right, Maxim tries to buy every game in that order.When Maxim stands at the position $$$i$$$ in the shop, he takes the first bill from his wallet (if his wallet is empty then he proceeds to the next position immediately) and tries to buy the $$$i$$$-th game using this bill. After Maxim tried to buy the $$$n$$$-th game, he leaves the shop.Maxim buys the $$$i$$$-th game if and only if the value of the first bill (which he takes) from his wallet is greater or equal to the cost of the $$$i$$$-th game. If he successfully buys the $$$i$$$-th game, the first bill from his wallet disappears and the next bill becomes first. Otherwise Maxim leaves the first bill in his wallet (this bill still remains the first one) and proceeds to the next game.For example, for array $$$c = [2, 4, 5, 2, 4]$$$ and array $$$a = [5, 3, 4, 6]$$$ the following process takes place: Maxim buys the first game using the first bill (its value is $$$5$$$), the bill disappears, after that the second bill (with value $$$3$$$) becomes the first one in Maxim's wallet, then Maxim doesn't buy the second game because $$$c_2 &gt; a_2$$$, the same with the third game, then he buys the fourth game using the bill of value $$$a_2$$$ (the third bill becomes the first one in Maxim's wallet) and buys the fifth game using the bill of value $$$a_3$$$.Your task is to get the number of games Maxim will buy.
256 megabytes
import java.util.Arrays; import java.util.Scanner; import java.io.*; import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.Scanner; import java.util.*; import java.util.LinkedList; public class hackerearth{ public static void main (String[] args) throws Exception{ Reader s = new Reader(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pr = new PrintWriter(System.out); int n = s.nextInt(); int m = s.nextInt(); int a[] =new int[n]; for(int i = 0;i<n;i++) { a[i] =s.nextInt(); } Queue<Integer> q = new LinkedList<>(); for(int i = 0 ;i< m;i++) { q.add(s.nextInt()); } int count=0; for(int i =0;i<n ;i++) { if(a[i] <= q.peek()) { q.remove(); count++; a[i]=-1; } if(q.size()==0) { break; } } pr.println(count); pr.close();}} class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } class Writer{ BufferedWriter wr = null; Writer(){} public Writer(BufferedWriter wr) { this.wr = wr; } public void writeInt(int value) throws IOException { wr.write(value + "\n"); } public void writeString(String value) throws IOException { wr.write(value + "\n"); } public void flush() throws IOException { wr.flush(); wr.close(); } }
Java
["5 4\n2 4 5 2 4\n5 3 4 6", "5 2\n20 40 50 20 40\n19 20", "6 4\n4 8 15 16 23 42\n1000 1000 1000 1000"]
1 second
["3", "0", "4"]
NoteThe first example is described in the problem statement.In the second example Maxim cannot buy any game because the value of the first bill in his wallet is smaller than the cost of any game in the shop.In the third example the values of the bills in Maxim's wallet are large enough to buy any game he encounter until he runs out of bills in his wallet.
Java 8
standard input
[ "implementation" ]
c3f080681e3da5e1290ef935ff91f364
The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1000$$$) β€” the number of games and the number of bills in Maxim's wallet. The second line of the input contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 1000$$$), where $$$c_i$$$ is the cost of the $$$i$$$-th game. The third line of the input contains $$$m$$$ integers $$$a_1, a_2, \dots, a_m$$$ ($$$1 \le a_j \le 1000$$$), where $$$a_j$$$ is the value of the $$$j$$$-th bill from the Maxim's wallet.
800
Print a single integer β€” the number of games Maxim will buy.
standard output
PASSED
e0c15d4b1f41112803f280f8ac0cf904
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
import java.io.IOException; import java.util.ArrayList; import java.util.InputMismatchException; public class MarinaAndVasya { public static void main(String[] args) { FasterScanner sc = new FasterScanner(); int N = sc.nextInt(); int T = sc.nextInt(); char[] S1 = sc.nextLine().toCharArray(); char[] S2 = sc.nextLine().toCharArray(); ArrayList<Integer> same = new ArrayList<Integer>(); ArrayList<Integer> diff = new ArrayList<Integer>(); for (int i = 0; i < N; i++) { if (S1[i] == S2[i]) { same.add(i); } else { diff.add(i); } } if (diff.size() > 2 * T) { System.out.println(-1); return; } char[] S3 = new char[N]; int chng = Math.max(0, diff.size() - T); int diffCnt = 0; for (int i = 0; i < 2 * chng; i += 2) { int idx1 = diff.get(i); int idx2 = diff.get(i + 1); S3[idx1] = S1[idx1]; S3[idx2] = S2[idx2]; diffCnt++; } for (int i = 2 * chng; i < diff.size(); i++) { int idx = diff.get(i); S3[idx] = diffChar(S1[idx], S2[idx]); diffCnt++; } for (int i = 0; i < same.size(); i++) { int idx = same.get(i); if (diffCnt < T) { S3[idx] = diffChar(S1[idx]); diffCnt++; } else { S3[idx] = S1[idx]; } } System.out.println(new String(S3)); } private static char diffChar(char... arr) { outer: for (char c = 'a'; c <= 'z'; c++) { for (char a : arr) { if (c == a) { continue outer; } } return c; } return '\0'; } 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; } } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
ac9670c510739365b4ab48230a08af70
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
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.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author I_love_PloadyFree */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(); int t = in.readInt(); char[] s1 = in.next().toCharArray(); char[] s2 = in.next().toCharArray(); char[] s = new char[n]; List<Integer> dif = new ArrayList<>(); for (int i = 0; i < n; i++) if (s1[i] == s2[i]) s[i] = s1[i]; else dif.add(i); int curDif = (dif.size() + 1) / 2; if (curDif > t) { out.print(-1); return; } for (int i = 0; i < dif.size(); i++) { int idx = dif.get(i); s[idx] = i % 2 == 0 ? s1[idx] : s2[idx]; } if (dif.size() % 2 == 1) { int idx = dif.get(0); s[idx] = getChar(s1[idx], s2[idx]); } int idxInDif = 0; for (int i = 0; i < n && curDif < t; i++) { if (idxInDif == dif.size() || dif.get(idxInDif) != i) { s[i] = getChar(s1[i], s2[i]); curDif++; } else { idxInDif++; } } idxInDif = dif.size() % 2; while (curDif < t) { int idx1 = dif.get(idxInDif++); int idx2 = dif.get(idxInDif++); s[idx1] = getChar(s1[idx1], s2[idx1]); s[idx2] = getChar(s1[idx2], s2[idx2]); curDif++; } out.print(s); } private char getChar(char c1, char c2) { for (char c = 'a'; c <= 'z'; c++) if (c != c1 && c != c2) return c; return 0; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int 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 String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(char[] array) { writer.print(array); } public void close() { writer.close(); } public void print(int i) { writer.print(i); } } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
4ad3c1a79c0fd68623c6864a6c283f15
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * Created by pallavi on 6/10/15. */ public class B584 { static int n; static int t; public static char df(char a, char b) { int aa = a-97; int bb = b-97; int cc = (aa+1)%26; if (bb == cc) cc = (aa+2)%26; return (char) (cc+97); } public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String[] inp = reader.readLine().split(" "); n = Integer.parseInt(inp[0]); t = Integer.parseInt(inp[1]); String s1 = reader.readLine(); String s2 = reader.readLine(); char[] res = new char[n]; int diff = 0; for (int i = 0; i < n; i++) { if (s1.charAt(i) != s2.charAt(i)) diff+=1; } if (2*t<diff) { System.out.println(-1); return; } boolean fl = true; for (int i = 0; i < n; i++) { if (s1.charAt(i) == s2.charAt(i)) continue; if (2*t>diff) { res[i] = df(s1.charAt(i), s2.charAt(i)); diff-=1; t-=1; } else { if (fl) { res[i] = s1.charAt(i); fl = false; } else { res[i] = s2.charAt(i); fl = true; t-=1; diff-=2; } } } for (int i = 0; i < n; i++) { if (s1.charAt(i) != s2.charAt(i)) continue; if (t>0) { res[i] = df(s1.charAt(i), s2.charAt(i)); t-=1; } else { res[i] = s1.charAt(i); } } System.out.println(String.valueOf(res)); } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
b67502c488276e65eadc3d363124f325
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class TaskC { public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); public static StringTokenizer tok; public static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public static long nextLong() throws IOException { return Long.parseLong(nextToken()); } public static double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public static String nextToken() throws IOException { if (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } public static char diff(char a, char b) { if (a != 'a' && b != 'a') return 'a'; if (a != 'b' && b != 'b') return 'b'; return 'c'; } public static void main(String[] args) throws IOException { int n = nextInt(); int t = nextInt(); String s1 = nextToken(); String s2 = nextToken(); int k = 0; for (int i = 0; i < n; i++) { if (s1.charAt(i) == s2.charAt(i)) k++; } if (n - k > 2 * t) { System.out.println(-1); return; } if (n - k > t && n - k <= 2 * t) { int counter = 0; for (int i = 0; i < n; i++) { char a = s1.charAt(i); char b = s2.charAt(i); if (a == b) { System.out.print(a); } else { if (counter < n - k - t) { System.out.print(b); } else if (counter < t) { System.out.print(diff(a, b)); } else { System.out.print(a); } counter++; } } } else { int diffInKLeft = t - (n - k); for (int i = 0; i < n; i++) { char a = s1.charAt(i); char b = s2.charAt(i); if (a == b) { if (diffInKLeft > 0) { System.out.print(diff(a, b)); diffInKLeft--; } else { System.out.print(a); } } else { System.out.print(diff(a, b)); } } } // if (n - k > 2 * t || (n - k) % 2 == 1 || t - (n - k) / 2 > k) { // System.out.println(-1); // return; // } // if (t * 2 + k < n || n < 2 * t && k < 2 * t - n) { // System.out.println(-1); // return; // } // int matchFirstLeft = (n - k) / 2; // int diffInKLeft = t - (n - k) / 2; // //int left = n - 2 * t; // //int counter = 0, edge = Math.min(n, t * 2); // //char[] res = new char[n]; // for (int i = 0; i < n; i++) { // char a = s1.charAt(i); // char b = s2.charAt(i); // if (a == b) { // if (diffInKLeft > 0) { // System.out.print(diff(a, b)); // diffInKLeft--; // } else { // System.out.print(a); // } // } else { // if (matchFirstLeft > 0) { // System.out.print(a); // matchFirstLeft--; // } else { // System.out.print(b); // } // } // if (s1.charAt(i) == s2.charAt(i) && left > 0) { // //res[i] = s1.charAt(i); // System.out.print(s1.charAt(i)); // left--; // } else if (counter < t && counter >= edge - t){ // //res[i] = diff(s1.charAt(i), s2.charAt(i)); // System.out.print(diff(s1.charAt(i), s2.charAt(i))); // counter++; // } else if (counter < edge - t) { // //res[i] = s2.charAt(i); // System.out.print(s2.charAt(i)); // counter++; // } else { // //res[i] = s1.charAt(i); // System.out.print(s1.charAt(i)); // counter++; // } // } } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
d66dd2528830890554300c2e7bb5787f
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; public class Prob03 { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] s0=br.readLine().split(" ");int n = Integer.parseInt(s0[0]);int t = Integer.parseInt(s0[1]); String s1=br.readLine();String s2=br.readLine(); StringBuilder result = new StringBuilder(); boolean first = true; int last = 0; int max = 0; for (int i = 0; i < n; i++) { if (s1.charAt(i)==s2.charAt(i)) {result.append(s1.charAt(i)); max++; continue;} if (first) {result.append(s1.charAt(i)); first=false;last=i;} else {result.append(s2.charAt(i)); first=true; max++;} } if (!first) { if (s1.charAt(last)!='a' && s2.charAt(last)!='a') result.replace(last, last+1, "a"); else if (s1.charAt(last)!='b' && s2.charAt(last)!='b') result.replace(last, last+1, "b"); else result.replace(last, last+1, "c"); } int eq = n-t; if (eq > max) {System.out.println(-1);return;} int i=0; int eee=0; first = true; last = 0; while(true) { if (eee==eq) break; if (i==n) {System.out.println("-1");return;} if (s1.charAt(i)==s2.charAt(i)) eee++; else if (first) {first=false;last=i;} else {first=true;eee++;} i++; } if (!first) { if (s1.charAt(last)!='a' && s2.charAt(last)!='a') result.replace(last, last+1, "a"); else if (s1.charAt(last)!='b' && s2.charAt(last)!='b') result.replace(last, last+1, "b"); else result.replace(last, last+1, "c"); } while (i < n) { if (s1.charAt(i)!='a' && s2.charAt(i)!='a') result.replace(i, i+1, "a"); else if (s1.charAt(i)!='b' && s2.charAt(i)!='b') result.replace(i, i+1, "b"); else result.replace(i, i+1, "c"); i++; } System.out.println(result.toString()); } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
9acf1690dd82f30fd4c0fd91bf4703b4
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
import java.io.*; import java.util.*; public class Main { static InputReader in; static PrintWriter out; public static void main(String[] args) throws IOException{ //InputStream ins = new FileInputStream("E:\\rush.txt"); InputStream ins = System.in; in = new InputReader(ins); out = new PrintWriter(System.out); //code start from here new Task().solve(in, out); out.close(); } static int N = 50000; static class Task{ int n,t; String s1,s2; StringBuilder sb; int cnt = 0; public char v(char x,char y) { for (char key = 'a';key<='z';key++){ if (key!=x && key!=y) return key; } return '2'; } public void solve(InputReader in,PrintWriter out) { n = in.nextInt();t = in.nextInt(); s1 = in.next();s2 = in.next(); for (int i = 0;i < n;i++) { if (s1.charAt(i)!=s2.charAt(i)) { cnt++; } } if (cnt>2*t) { out.println(-1); return; } if (cnt>=t) { int num = cnt-t; int cl = 0; for (int i = 0;i < n;i++) { if (s1.charAt(i)==s2.charAt(i)) { out.print(s1.charAt(i)); }else { cl++; if (cl<=num) { out.print(s2.charAt(i)); }else if (cl>num && cl <=2*num) { out.print(s1.charAt(i)); }else { out.print(v(s1.charAt(i),s2.charAt(i))); } } } }else { int num = t-cnt; for (int i = 0;i < n;i++) { if (s1.charAt(i)==s2.charAt(i)) { if (num>0) { num--; out.print(v(s1.charAt(i),s2.charAt(i))); }else { out.print(s1.charAt(i)); } }else { out.print(v(s1.charAt(i),s2.charAt(i))); } } } } } static class InputReader{ public BufferedReader br; public StringTokenizer tokenizer; public InputReader(InputStream ins) { br = new BufferedReader(new InputStreamReader(ins)); tokenizer = null; } public String next(){ while (tokenizer==null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(br.readLine()); }catch(IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
808e46f140029d6b5209e521c22241e2
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class Div2_324_B { public static void main(String[] args) throws IOException { BufferedInputStream bis = new BufferedInputStream(System.in); BufferedReader br = new BufferedReader(new InputStreamReader(bis)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int t = Integer.parseInt(st.nextToken()); int same = 0, diff = 0; String s1 = br.readLine().trim(); String s2 = br.readLine().trim(); char [] res = new char [n]; for (int i = 0; i < s1.length(); i++) { if(s1.charAt(i) != s2.charAt(i)) diff++; else same++; } int cnt = 0, part = diff/2; for (int i = 0; i < res.length; i++) { if(s1.charAt(i) != s2.charAt(i)) { res[i] = getDiff(s1.charAt(i), s2.charAt(i)); t--; } else res [i] = s1.charAt(i); } for (int i = 0; i < res.length && t < 0; i++) { if(s1.charAt(i) != s2.charAt(i)) { if(cnt%2 == 0) { res[i] = s1.charAt(i); } else { res[i] = s2.charAt(i); t++; } cnt++; } } for(int i = 0;i < res.length && t > 0;i++) { if (s1.charAt(i) == s2.charAt(i)) { t--; if(s1.charAt(i) == 'z') res[i] = 'a'; else res[i] = (char)(s1.charAt(i)+1); } } if(t == 0) System.out.println(new String(res)); else System.out.println(-1); } public static char getDiff(char a, char b) { if(a > b) { char t = a; a = b; b = t; } if(b-a > 1) return (char)(a+1); if(b < 'z') return 'z'; return 'a'; } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
da392e17c2131a6825c78e173b0d3418
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
import javax.swing.text.MutableAttributeSet; import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { Scanner input = new Scanner(System.in); int n = input.nextInt(); int t = input.nextInt(); input.nextLine(); String str1 = input.nextLine(); String str2 = input.nextLine(); int counter = 0; for (int i = 0; i < n; i++) { if (str1.charAt(i) != str2.charAt(i)) counter++; } if(t == 0 && counter == 1) { System.out.println(-1); return; } if(t < (counter+1)/2) { System.out.println(-1); return; } int count = 0; boolean first = true; int i = 0; StringBuilder ans = new StringBuilder(str1); while (counter - count/2 > t) { if (str1.charAt(i) != str2.charAt(i)) { if(first)ans.setCharAt(i,str1.charAt(i)); else ans.setCharAt(i,str2.charAt(i)); first ^= true; count++; } i++; } t -= count/2; for (;i < n; i++) { if (str1.charAt(i) != str2.charAt(i)) { for (int j = 0; j < 26; j++) if (j + 'a' != str1.charAt(i) && j+'a' != str2.charAt(i)) { ans.setCharAt(i, (char) (j + 'a')); t--; break; } } } for ( i = 0; i < n; i++) { if (str1.charAt(i) == str2.charAt(i) && t > 0) { for (int j = 0; j < 26; j++) if (j + 'a' != str1.charAt(i)) { ans.setCharAt(i, (char) (j + 'a')); t--; break; } } } System.out.println(ans); } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
7cc033fc2b9f285e6beafc82a7a4387d
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; import java.lang.*; public class Third { //static long m=1000000007; static BigInteger ways(int N, int K) { BigInteger ret = BigInteger.ONE; for(int i=N;i>=N-K+1;i--) { ret = ret.multiply(BigInteger.valueOf(i)); } for (int j = 1; j<=K; j++) { ret = ret.divide(BigInteger.valueOf(j)); } ret=ret.mod(BigInteger.valueOf(1000000007)); return ret; } public static int prime(int n) { int f=1; if(n==1) return 0; for(int i=2;i<=(Math.sqrt(n));) { if(n%i==0) { f=0; break; } if(i==2) i++; else i+=2; } if(f==1) return 1; else return 0; } /*public static long gcd(long x,long y) { if(x%y==0) return y; else return gcd(y,x%y); }*/ public static BigInteger fact(int n) { BigInteger f=BigInteger.ONE; for(int i=1;i<=n;i++) { f=f.multiply(BigInteger.valueOf(i)); } //f=f.mod(BigInteger.valueOf(m)); return f; } public static int gcd(int x,int y) { if(x%y==0) return y; else return gcd(y,x%y); } public static int max(int a,int b) { if(a>b) return a; else return b; } public static int min(int a,int b) { if(a>b) return b; else return a; } static long ncr(long n,long k) { long w=1; for(long i=n,j=1;i>=n-k+1;i--,j++) { w=w*i; w=w/j; w%=1000000007; } return w; } static class Pair implements Comparable<Pair> { int a,b; Pair (int a,int b) { this.a=a; this.b=b; } public int compareTo(Pair o) { // TODO Auto-generated method stub if(this.a!=o.a) return Integer.compare(this.a,o.a); else return Integer.compare(this.b, o.b); //return 0; } } public static void main(String[] args) throws Exception{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb=new StringBuilder(""); //InputReader in = new InputReader(System.in); // OutputWriter out = new OutputWriter(System.out); //PrintWriter pw=new PrintWriter(System.out); //String line=br.readLine().trim(); //int t=Integer.parseInt(br.readLine()); // while(t-->0) //{ //int n=Integer.parseInt(br.readLine()); //long n=Long.parseLong(br.readLine()); //line=br.readLine().trim(); //String l[]=br.readLine().split(" "); //int m=Integer.parseInt(l[0]); //int k=Integer.parseInt(l[1]); //l=br.readLine().split(" "); //long m=Long.parseLong(l[0]); //long n=Long.parseLong(l[1]); //char ch=a.charAt(); // char c[]=new char[n]; //String l[]=br.readLine().split(" "); //l=br.readLine().split(" "); /*int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=Integer.parseInt(l[i]); }*/ /*long a[]=new long[n]; for(int i=0;i<n;i++) { a[i]=Long.parseLong(l[i]); }*/ /*int a[][]=new int[n][n]; for(int i=0;i<n;i++) { l=br.readLine().split(" "); for(int j=0;j<n;j++) { a[i][j]=Long.parseLong(l[j]); } }*/ // String a=br.readLine(); // char a[]=c.toCharArray(); //char ch=l[0].charAt(0); //HashMap<Integer,Integer>hm=new HashMap<Integer,Integer>(); //HashMap<Integer,String>hm=new HashMap<Integer,String>(); //HashMap<Integer,Long>hm=new HashMap<Integer,Long>(); //hm.put(1,1); //HashSet<Integer>hs=new HashSet<Integer>(); //HashSet<Long>hs=new HashSet<Long>(); //HashSet<String>hs=new HashSet<String>(); //hs.add(x); //Stack<Integer>s=new Stack<Integer>(); //s.push(x); //s.pop(x); //Queue<Integer>q=new LinkedList<Integer>(); //q.add(x); //q.remove(x); //ArrayList<Integer>ar=new ArrayList<Integer>(); //long x=Long.parseLong(l[0]); //int min=100000000; //long c1=(long)Math.ceil(n/(double)a); //Arrays.sort(a); // int f1[]=new int[26]; //int f2[]=new int[26]; /*for(int i=0;i<n;i++) { int x=a.charAt(0); f1[x-97]++; }*/ /*for(int i=0;i<n;i++) { }*/ /*for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { } }*/ /*while(q--) { l=br.readLine().split(" "); int n=Integer.parseInt(l[0]); int k=Integer.parseInt(l[1]); } */ /*if(f==1) System.out.println("Yes"); else System.out.println("No");*/ //System.out.print(""); //sb.append(""); //sb.append("").append("\n"); String l[]=br.readLine().split(" "); int n=Integer.parseInt(l[0]); int t=Integer.parseInt(l[1]); String a=br.readLine(); String b=br.readLine(); char c[]=a.toCharArray(); char d[]=b.toCharArray(); int g[]=new int[26]; int h[]=new int[26]; // Arrays.sort(c); // Arrays.sort(d); char e[]=new char[c.length]; for(int i=0;i<c.length;i++) { g[c[i]-97]++; } for(int i=0;i<c.length;i++) { h[d[i]-97]++; } int f=1; if(c.length!=d.length) f=0; else { /*for(int i=0;i<26;i++) { }*/ int c1=0; for(int i=0;i<c.length;i++) { if(c[i]!=d[i]) c1++; } int ds=(int)Math.ceil(c1/2.0); if(t>=ds) { int j=1; /*for(int i=0;i<c.length;i++) { if(c[i]==d[i]) { e[i]=c[i]; } else { if(c[i]=='a') { if(d[i]=='b') e[i]='c'; else e[i]='b'; } else if(d[i]=='a') { if(c[i]=='b') e[i]='c'; else e[i]='b'; } else e[i]='a'; } }*/ int s=n-t; for(int i=0;i<c.length;i++) { if(c[i]==d[i]&&s>0) { e[i]=c[i]; //System.out.println(e[i]); s--; } } int sf=s; //int df=(int)Math.ceil(s/2.0); int df=s; for(int i=0;i<n;i++) { if(e[i]=='\0'&&sf>0) { e[i]=c[i]; sf--; } } for(int i=0;i<n;i++) { if(e[i]=='\0'&&df>0) { e[i]=d[i]; df--; } } for(int i=0;i<n;i++) { if(e[i]=='\0') { if(c[i]=='a') { if(d[i]=='b') e[i]='c'; else e[i]='b'; } else if(d[i]=='a') { if(c[i]=='b') e[i]='c'; else e[i]='b'; } else e[i]='a'; } } } else f=0; } if(f==0) System.out.println("-1"); else { for(int i=0;i<c.length;i++) System.out.print(e[i]); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int 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 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 double readDouble() { 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, readInt()); 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, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } 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 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); } } 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(); } } /* USAGE //initialize InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); //read int int i = in.readInt(); //read string String s = in.readString(); //read int array of size N int[] x = IOUtils.readIntArray(in,N); //printline out.printLine("X"); //flush output out.flush(); //remember to close the //outputstream, at the end out.close(); */ static class IOUtils { 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 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
371733cf309334673a444d724163c269
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
import java.util.*; public class C { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = n - in.nextInt(); in.nextLine(); String s1 = in.nextLine(); String s2 = in.nextLine(); int d = 0; for (int i = 0; i < n; i++) if (s1.charAt(i) != s2.charAt(i)) d++; int len = d >> 1; if (len >= k) { int count = 1; for (int i = 0; i < n; i++) { if (s1.charAt(i) != s2.charAt(i)) { if (count <= k) System.out.print(s1.charAt(i)); else if (count <= 2*k) System.out.print(s2.charAt(i)); else System.out.print(difChar(s1.charAt(i), s2.charAt(i))); count++; } else System.out.print(difChar(s1.charAt(i), s2.charAt(i))); } } else { if (len - d + n < k) { System.out.println(-1); return; } int count = 1; int count2 = len + 1; for (int i = 0; i < n; i++) { if (s1.charAt(i) != s2.charAt(i)) { if (count <= len) System.out.print(s1.charAt(i)); else if (count <= 2*len) System.out.print(s2.charAt(i)); else System.out.print(difChar(s1.charAt(i), s2.charAt(i))); count++; } else { if (count2 <= k) System.out.print(s1.charAt(i)); else System.out.print(difChar(s1.charAt(i), s2.charAt(i))); count2++; } } } System.out.println(); } private static char difChar(char a, char b) { for (int i = 97; i <= 122; i++) { if ((i == (int)a) || (i == (int)b)) continue; return (char)i; } return 'z'; } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
0f9c74bddf896e11f339792d30c73369
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeSet; public class C { public static char diff_char(final char a, final char b){ for(char c = 'a'; c <= 'z'; c++){ if(c != a && c != b){ return c; } } return '\0'; } public static boolean is_valid(char[] a){ for(final char c : a){ if(c == '\0'){ return false; } } return true; } public static int check_diff(final int n, final char[] a, final char[] b){ int count = 0; for(int i = 0; i < n; i++){ if(a[i] != b[i]){ count++; } } return count; } public static void main(String[] args) { try (final Scanner sc = new Scanner(System.in)) { final int len = sc.nextInt(); final int t = sc.nextInt(); final String s1_str = sc.next(); final String s2_str = sc.next(); char[] s1 = s1_str.toCharArray(); char[] s2 = s2_str.toCharArray(); int both = 0, either = 0; for(int i = 0; i < len; i++){ if(s1[i] == s2[i]){ both++; }else{ either++; } } if(t == 0){ if(both == len){ System.out.println(s1); }else{ System.out.println(-1); } return; } char[] answer = new char[len]; boolean force_use = either % 2 != 0; int g_count = 0; if(force_use){ for(int i = 0; i < len; i++){ if(s1[i] != s2[i]){ g_count++; answer[i] = diff_char(s1[i], s2[i]); break; } } } //System.out.println(either + " " + t); final int forced_t = t - (force_use ? 1 : 0); final int forced_either = either - (force_use ? 1 : 0); final int either_either_diff = Math.max(0, forced_either - forced_t); final int either_both_diff = forced_either - either_either_diff * 2; //System.out.println(either_either_diff + " " + either_both_diff); if(either_both_diff < 0){ System.out.println(-1); return; } // for(int i = 0, count = 0; i < len && count < either_both_diff; i++){ if(s1[i] != s2[i] && answer[i] == '\0'){ count++; g_count++; answer[i] = diff_char(s1[i], s2[i]); } } for(int i = 0, count = 0; i < len && count < either_either_diff; i++){ if(s1[i] != s2[i] && answer[i] == '\0'){ count++; g_count++; answer[i] = s2[i]; } } for(int i = 0, count = 0; i < len && count < either_either_diff; i++){ if(s1[i] != s2[i] && answer[i] == '\0'){ count++; answer[i] = s1[i]; } } final int both_diff = t - g_count; for(int i = 0, count = 0; i < len && count < both_diff; i++){ if(s1[i] == s2[i] && answer[i] == '\0'){ count++; answer[i] = diff_char(s1[i], s2[i]); } } for(int i = 0; i < len; i++){ if(s1[i] == s2[i] && answer[i] == '\0'){ answer[i] = s1[i]; } } final int s1_diff = check_diff(len, s1, answer); final int s2_diff = check_diff(len, s2, answer); //System.out.println(Arrays.toString(answer) + " " + s1_diff + " " + s2_diff); if(is_valid(answer) && s1_diff == t && s2_diff == t){ for(int i = 0; i < len; i++){ System.out.print(answer[i]); } System.out.println(); }else{ System.out.println(-1); } } } public static class Scanner implements Closeable { private BufferedReader br; private StringTokenizer tok; public Scanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } private void getLine() { try { while (!hasNext()) { tok = new StringTokenizer(br.readLine()); } } catch (IOException e) { /* ignore */ } } private boolean hasNext() { return tok != null && tok.hasMoreTokens(); } public String next() { getLine(); return tok.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public void close() { try { br.close(); } catch (IOException e) { /* ignore */ } } } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
e69f4551a5821eb17d90185f00181668
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; public class MarinaAndVasya { public static char diffChar(char c1,char c2) { if (c1==c2) { if (c1 == 'a') return 'b'; return 'a'; } if (c1!='a' && c2 != 'a') return 'a'; if (c1!='b' && c2 != 'b') return 'b'; return 'c'; } public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int t = Integer.parseInt(st.nextToken()); String s1 = br.readLine(); String s2 = br.readLine(); int similar = 0,diff; ArrayList<Integer> sim = new ArrayList<Integer>(); ArrayList<Integer> dif = new ArrayList<Integer>(); for (int i=0;i<n;i++) { if (s1.charAt(i)==s2.charAt(i)) { sim.add(i); similar++; } else dif.add(i); } diff = n - similar; if (n-t>similar+diff/2) { System.out.println(-1); return; } char[] s = new char[n]; if (diff<=t) { for (int i=0;i<diff;i++) { int index = dif.get(i); s[index] = diffChar(s1.charAt(index), s2.charAt(index)); } int it = 0; for (int i=diff;i<t;i++) { int index = sim.get(it++); s[index] = diffChar(s1.charAt(index), s2.charAt(index)); } while (it<similar) { int index = sim.get(it++); s[index] = s1.charAt(index); } } else { int simIt = 0, difIt = 0; for (int i=0;i<n-t;i++) { if (difIt+2<=diff) { int index = dif.get(difIt++); s[index] = s1.charAt(index); index = dif.get(difIt++); s[index] = s2.charAt(index); } else { int index = sim.get(simIt++); s[index] = s1.charAt(index); } } while (difIt<diff) { int index = dif.get(difIt++); s[index] = diffChar(s1.charAt(index), s2.charAt(index)); } while (simIt<similar) { int index = sim.get(simIt++); s[index] = diffChar(s1.charAt(index), s2.charAt(index)); } } StringBuilder sb = new StringBuilder(); for (int i=0;i<n;i++) sb.append(s[i]); System.out.println(sb); } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
6d5f876bcf925ba79159dc58faec24fe
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(), m = in.nextInt(); String S1 = in.next(), S2 = in.next(); int count = 0; for (int i = 0; i < n; ++i) if (S1.charAt(i) == S2.charAt(i)) count++; m = n - m; if (count + 2 * (m - count) > n) { out.println(-1); return; } int m1 = m, m2 = m; char[] ans = new char[n]; for (int i = 0; i < n; ++i) { char c1 = S1.charAt(i), c2 = S2.charAt(i); if (m1 > 0 && m2 > 0 && c1 == c2) { ans[i] = c1; m1--; m2--; } } for (int i = 0; i < n; ++i) { char c1 = S1.charAt(i), c2 = S2.charAt(i); if (m1 > 0 && c1 != c2) { ans[i] = c1; m1--; } else if (m2 > 0 && c1 != c2) { ans[i] = c2; m2--; } else if (ans[i] == '\0') { char ap = 'a'; while (ap == c1 || ap == c2) ap++; ans[i] = ap; } } out.println(new String(ans)); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new UnknownError(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new UnknownError(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { return Integer.parseInt(next()); } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
6202e1f7eab43e4a29f5c2547b0623ae
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main1 { static class Reader { private InputStream mIs;private byte[] buf = new byte[1024];private int curChar,numChars;public Reader() { this(System.in); }public Reader(InputStream is) { mIs = is;} public int read() {if (numChars == -1) throw new InputMismatchException();if (curChar >= numChars) {curChar = 0;try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();}if (numChars <= 0) return -1; }return buf[curChar++];} public String nextLine(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isEndOfLine(c));return res.toString() ;} public String s(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isSpaceChar(c));return res.toString();} public long l(){int c = read();while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }long res = 0; do{ if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read();}while(!isSpaceChar(c));return res * sgn;} public int i(){int c = read() ;while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }int res = 0;do{if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read() ;}while(!isSpaceChar(c));return res * sgn;} public double d() throws IOException {return Double.parseDouble(s()) ;} public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } /////////////////////////////////////////////////////////////////////////////////////////// // RRRRRRRRR AAA HHH HHH IIIIIIIIIIIII LLL // // RR RRR AAAAA HHH HHH IIIIIIIIIII LLL // // RR RRR AAAAAAA HHH HHH III LLL // // RR RRR AAA AAA HHHHHHHHHHH III LLL // // RRRRRR AAA AAA HHHHHHHHHHH III LLL // // RR RRR AAAAAAAAAAAAA HHH HHH III LLL // // RR RRR AAA AAA HHH HHH IIIIIIIIIII LLLLLLLLLLLL // // RR RRR AAA AAA HHH HHH IIIIIIIIIIIII LLLLLLLLLLLL // /////////////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args)throws IOException { PrintWriter out= new PrintWriter(System.out); Reader sc=new Reader(); int n=sc.i(); int t=sc.i(); String s1=sc.s(); String s2=sc.s(); int q=0; for(int i=0;i<n;i++)if(s1.charAt(i)==s2.charAt(i))q++; int k=n-t; if(n-q<2*(k-q)) { System.out.println(-1); System.exit(0); } if(k<=q) { int count=0; for(int i=0;i<n;i++) if(s1.charAt(i)==s2.charAt(i)&&count!=k) { count++; out.print(s1.charAt(i)); } else { int mark[]=new int[26]; mark[s1.charAt(i)-97]++;mark[s2.charAt(i)-97]++; for(int j=0;j<26;j++) { if(mark[j]==0) { out.print((char)(j+97)); break; } } } out.flush(); System.exit(0); } k-=q; int count1=0; int count2=0; for(int i=0;i<n;i++) { if(s1.charAt(i)==s2.charAt(i)) out.print(s1.charAt(i)); else { if(count1!=k) { out.print(s1.charAt(i)); count1++; } else if(count2!=k) { out.print(s2.charAt(i)); count2++; } else { int mark[]=new int[26]; mark[s1.charAt(i)-97]++;mark[s2.charAt(i)-97]++; for(int j=0;j<26;j++) { if(mark[j]==0) { out.print((char)(j+97)); break; } } } } } out.flush(); } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
7f3696dfffa9ab3ec1f2594502520d14
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; import java.util.LinkedList; import java.io.InputStream; import java.io.BufferedReader; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStreamReader; import java.io.IOException; /** * Built using CHelper plug-in * Actual solution is at the top * * @author shu_mj @ http://shu-mj.com */ 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); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { Scanner in; PrintWriter out; public void solve(int testNumber, Scanner in, PrintWriter out) { this.in = in; this.out = out; run(); } void run() { int n = in.nextInt(); int t = in.nextInt(); char[] a = in.next().toCharArray(); char[] b = in.next().toCharArray(); int sc = 0; LinkedList<Integer> que = new LinkedList<>(); for (int i = 0; i < n; i++) { if (a[i] == b[i]) { sc++; que.addLast(i); } else { que.addFirst(i); } } t = n - t; if (t > sc + (n - sc) / 2) { out.println(-1); } else { if ((n - sc) % 2 == 1) { que.addLast(que.pollFirst()); } int l = 0, r = 0; char[] s = new char[n]; for (int i : que) { if (a[i] == b[i]) { if (l < t && r < t) { s[i] = a[i]; l++; r++; } else { for (char c = 'a'; ; c++) { if (c != a[i]) { s[i] = c; break; } } } } else { if (l <= r && l < t) { s[i] = a[i]; l++; } else if (r <= l && r < t) { s[i] = b[i]; r++; } else { for (char c = 'a'; ; c++) { if (c != a[i] && c != b[i]) { s[i] = c; break; } } } } } out.println(s); } } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); eat(""); } private void eat(String s) { st = new StringTokenizer(s); } public String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } public boolean hasNext() { while (!st.hasMoreTokens()) { String s = nextLine(); if (s == null) return false; eat(s); } return true; } public String next() { hasNext(); return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
2144111872730da089d10cf2e685bad2
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; public class C { public static void main(String[] args) throws Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String[] l = bf.readLine().split(" "); int n = Integer.parseInt(l[0]); int t = Integer.parseInt(l[1]); char[] s1 = bf.readLine().toCharArray(); char[] s2 = bf.readLine().toCharArray(); char[] s3 = new char[n]; int diff = 0; boolean f = false; int last = -1; for (int i = 0; i < n; i++) { if (s1[i] != s2[i]) { s3[i] = f ? s1[i] : s2[i]; diff++; last = i; f = !f; } } if (diff % 2 != 0) { s3[last] = somethingDiff(s1[last], s2[last]); diff++; } diff /= 2; if (diff > t) { System.out.println(-1); return; } int prev = -1; for (int i = 0; i < s3.length; i++) { if (diff == t) break; if (s1[i] != s2[i]) { if (prev != -1) { s3[i] = somethingDiff(s1[i], s2[i]); s3[prev] = somethingDiff(s1[prev], s2[prev]); diff++; prev = -1; } else { prev = i; } } } boolean changed[] = new boolean[n]; for (int i = 0; i < s3.length; i++) { if (diff == t) break; if (s1[i] == s2[i]) { s3[i] = somethingDiff(s1[i], s2[i]); changed[i] = true; diff++; } } if (diff < t) { System.out.println(-1); return; } for (int i = 0; i < s3.length; i++) { if (s1[i] == s2[i] && !changed[i]) { s3[i] = s1[i]; } } System.out.println(String.valueOf(s3)); } static char[] abc = "abcdefghijklmnopqrstuvwxyz".toCharArray(); static char somethingDiff(char c, char d) { for (int i = 0; i < abc.length; i++) { if (abc[i] != c && abc[i] != d) { return abc[i]; } } return (char) (((c + d) % 26 + 1) + 'a'); } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
03e4417b4c35d3359fc186585e471522
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
import java.util.Scanner; public class MarinaandVasya { private static int countDifferences (String s1, String s2) { int count = 0; for (int i = 0 ; i < s1.length(); i++) { if (s1.charAt(i) != s2.charAt(i)) { count++; } } return count ++; } static String solveMarinAndVasya (String s1, String s2, int t) { StringBuilder result = new StringBuilder(); int differences = countDifferences(s1, s2); if (t < ((differences + 1) / 2 )) { //System.out.println("here 1"); return "-1"; } int similarties = s1.length() - t; boolean odd = true; for (int i = 0; i < s1.length(); i++) { if (t == 0 && similarties <= 0) { //System.out.println("here 2"); return "-1"; } if (s1.charAt(i) == s2.charAt(i) ) { if (similarties > 0) { result.append(s1.charAt(i)); similarties--; } else { char newChar = (char) (s1.charAt(i) + 1); if (newChar > 122) newChar -=26; result.append(newChar); t--; } } else { if (differences > t || differences == t && !odd) { //System.out.println(">>>>>"); if (odd) { result.append(s1.charAt(i)); odd = false; } else { result.append(s2.charAt(i)); t--; odd = true; } } else { t--; //System.out.println("zewww"); char newChar = (char) (s1.charAt(i) + 1); if (newChar > 122) newChar -=26; if ( newChar == s2.charAt(i)) { newChar = (char) (s2.charAt(i) + 1); } if (newChar > 122) newChar -=26; result.append(newChar); } differences--; } } if (!odd) {/*System.out.println(result.toString()); System.out.println("here 3");*/ return "-1"; } return result.toString(); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int stringLength = scanner.nextInt(); int t = scanner.nextInt(); scanner.nextLine(); String s1 = scanner.nextLine(); String s2 = scanner.nextLine(); System.out.println(solveMarinAndVasya(s1, s2, t)); scanner.close(); } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
3a4e51cab171c340fc6ed5a01d2f00d0
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; public class Main { public static long mod= (long) (1e9 +7); public static void main(String args[]) { InputReader s= new InputReader(System.in); OutputStream outputStream= System.out; PrintWriter out= new PrintWriter(outputStream); int n= s.nextInt(); int t= s.nextInt(); int same = n-t; char s1[]= s.next().toCharArray(); char s2[]= s.next().toCharArray(); char ans[]= new char[n]; int count= 0; for(int i=0;i<n;i++){ if(s1[i]==s2[i]) count++; } int maxallowed= count + (n-count)/2; if(same>maxallowed){ out.println("-1"); } else{ for(int i=0;i<n;i++){ if(s1[i]==s2[i]){ if(same>0){ ans[i]=s1[i]; same--; } else{ ans[i]= findDiff(s1[i],s2[i]); } } } int a= same, b=same; for(int i=0;i<n;i++){ if(s1[i]==s2[i]) continue; if(a==0 && b==0){ ans[i]= findDiff(s1[i],s2[i]); } else if(a>=b){ a--; ans[i]= s1[i]; } else{ ans[i]=s2[i]; b--; } } out.print(new String(ans)); } out.close(); } static char findDiff(char x,char y){ if(x!='a' && y!= 'a'){ return 'a'; } if(x!='b' && y!= 'b'){ return 'b'; } return 'c'; } static ArrayList<Integer> primeFactors(int n) { // Print the number of 2s that divide n ArrayList<Integer> arr= new ArrayList<>(); while (n%2 == 0) { arr.add(2); n = n/2; } // n must be odd at this point. So we can skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i = i+2) { // While i divides n, print i and divide n while (n%i == 0) { arr.add(i); n = n/i; } } // This condition is to handle the case when n is a prime number // greater than 2 if (n > 2) arr.add(n); return arr; } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream inputstream) { reader = new BufferedReader(new InputStreamReader(inputstream)); tokenizer = null; } public String nextLine(){ String fullLine=null; while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { fullLine=reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return fullLine; } return fullLine; } 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()); } } public static int expo(int a, int b){ if (b==0) return 1; if (b==1) return a; if (b==2) return a*a; if (b%2==0){ return expo(expo(a,b/2),2); } else{ return a*expo(expo(a,(b-1)/2),2); } } static class Pair implements Comparable<Pair> { long f,s; Pair(long ii, long cc) { f=ii; s=cc; } public int compareTo(Pair o) { return Long.compare(this.f, o.f); } } public static int gcd(int number1, int number2) { if(number2 == 0){ return number1; } return gcd(number2, number1%number2); } public static int combinations(int n,int r){ if(n==r) return 1; if(r==1) return n; if(r==0) return 1; return combinations(n-1,r)+ combinations(n-1,r-1); } public static long binomialCoeff(int n, int k) { long C[][]= new long[n+1][k+1]; int i, j; // Caculate value of Binomial Coefficient in bottom up manner for (i = 0; i <= n; i++) { for (j = 0; j <= Math.min(i, k); j++) { // Base Cases if (j == 0 || j == i) C[i][j] = 1; // Calculate value using previosly stored values else C[i][j] = C[i-1][j-1] + C[i-1][j]; } } return C[n][k]; } public static int[] sieve(int N){ int arr[]= new int[N+1]; for(int i=2;i<Math.sqrt(N);i++){ if(arr[i]==0){ for(int j= i*i;j<= N;j= j+i){ arr[j]=1; } } } return arr; // All the i for which arr[i]==0 are prime numbers. } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
8942fcdb43eaf8040e195cac70a7482d
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
import java.io.*; import java.util.*; public class Main { BufferedReader br; PrintWriter out; StringTokenizer st; String next() throws IOException { while (st == null || !st.hasMoreTokens()) { String s = br.readLine(); st = new StringTokenizer(s); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } private void solve() throws IOException { int n = nextInt(), t = nextInt(); String a = next(), b = next(); int s = n - t; int fa, fb; char c[] = new char[n]; for (int i = 0; i < n; i++) { c[i] = '#'; } for (int i = 0; i < n; i++) { if (s == 0) break; if (a.charAt(i) == b.charAt(i)) { s--; c[i] = a.charAt(i); } } fa = fb = s; for (int i = 0; i < n; i++) { if (fa == 0) break; if (c[i] == '#') { c[i] = a.charAt(i); fa--; } } for (int i = 0; i < n; i++) { if (fb == 0) break; if (c[i] == '#') { c[i] = b.charAt(i); fb--; } } if (fa != 0 || fb != 0) { out.println(-1); return; } for (int i = 0; i < n; i++) { if (c[i] == '#') { for (int j = 'a'; j <= 'z'; j++) { if (a.charAt(i) != j && b.charAt(i) != j) { c[i] = (char) j; break; } } } out.print(c[i]); } } private void run() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); br.close(); out.close(); } public static void main(String[] args) throws IOException { new Main().run(); } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
770becd08e8f1a7517010b08f7ec4833
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
import java.util.*; import java.math.*; import java.io.*; public class Luck{ public static InputReader sc; public static PrintWriter out; public static final long MOD = (long) (1e9 + 7); static int n; static int m; static int q; public static void main(String[] args){ sc=new InputReader(System.in); out=new PrintWriter(System.out); int n=sc.nextInt(); int t=sc.nextInt(); String a=sc.readString(); String b=sc.readString(); char[] ch=new char[n]; int count=0; Arrays.fill(ch, '!'); for(int i=0;i<n;i++){ if(count==n-t){ break; } if(a.charAt(i)==b.charAt(i)){ ch[i]=a.charAt(i); count+=1; } } int val1=count; int val2=count; if(count<n-t){ for(int i=0;i<n;i++){ if(val1==n-t){ break; } if(ch[i]=='!'){ ch[i]=a.charAt(i); val1+=1; } } for(int i=0;i<n;i++){ if(val2==n-t){ break; } if(ch[i]=='!'){ ch[i]=b.charAt(i); val2+=1; } } } if(val1<n-t || val2<n-t){ out.println(-1); } else{ for(int i=0;i<n;i++){ if(ch[i]=='!'){ for(char j='a';j<='z';j++){ if(a.charAt(i)!=j && b.charAt(i)!=j){ ch[i]=j; break; } } } out.print(ch[i]); } } out.close(); } static class Pair{ int x; int y; Pair(){ this.x=-1; this.y=-1; } Pair(int profit,int val){ this.x=profit; this.y=val; } public static Comparator<Pair> Val=new Comparator<Pair>(){ public int compare(Pair p1,Pair p2){ if(p1.x==p2.y){ return p1.y-p2.y; } return (int)p1.x-(int)p2.x; } }; public int hashCode() { final int prime = 31; int result = 1; result = prime * result + x; result = prime * result + x; return result; } public boolean equals(Object obj){ if (this == obj) return true; if (!(obj instanceof Pair)) { return false; } Pair p = (Pair) obj; if(p.x!=this.x){ return false; } else if(p.y!=p.y){ return false; } return true; } } static class DisjointSet{ int n; int[] par; int[] rank; DisjointSet(int n){ this.n=n; this.par=new int[n]; this.rank=new int[n]; makeSet(); } void makeSet(){ for(int i=1;i<n;i++){ par[i]=i; rank[i]=1; } } void union(int x,int y){ int parX=parent(x); int parY=parent(y); if(parX!=parY){ if(rank[parX]>=rank[parY]){ rank[parX]+=rank[parY]; rank[parY]=0; par[parY]=parX; } else{ rank[parY]+=rank[parX]; rank[parX]=0; par[parX]=parY; } } } int parent(int c){ int i=c; while(i!=par[i]){ i=par[i]; } return i; } } static int gcd(int a,int b){ if(b==0){ return a; } return gcd(b,a%b); } static int lcm(int a,int b){ int g; if(a<b){ g=gcd(b,a); } else{ g=gcd(a,b); } return (a*b)/g; } static long bigmod ( long a, long p, long m ) { long res = 1 % m, x = a % m; while ( p>0 ) { if ( (p & 1)==1 ) res = ( res * x ) % m; x = ( x * x ) % m; p >>= 1; } return res; } static boolean isPrime(int n){ if (n == 2) return true; for (long i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } static void shuffle(int[] A){ for(int i=A.length-1;i>0;i--){ int j=(int)(Math.random()*(i+1)); int temp=A[j]; A[j]=A[i]; A[i]=temp; } } // public static class Node implements Comparable<Node>{ // int u; // int v; // public Node(){ // ; // } // public Node (int u, int v) { // this.u = u; // this.v = v; // } // // public void print() { // out.println(v + " " + u + " "); // } // // public int compareTo(Node n1){ // return this.u-n1.u; // } // } public static long pow(long base,long exp,long mod){ if(exp==0){ return 1; } if(exp==1){ return base; } long temp=exp/2; long val=pow(base,temp,mod); long result=val*val; result=(result%mod); long AND=exp&1; if(AND==1){ result*=base; result=(result%mod); } return result; } public static BigInteger pow(BigInteger base, BigInteger exp) { if(exp.equals(new BigInteger(String.valueOf(0)))){ return new BigInteger(String.valueOf(1)); } if(exp.equals(new BigInteger(String.valueOf(1)))) return base; BigInteger temp=exp.divide(new BigInteger(String.valueOf(2))); BigInteger val = pow(base, temp); BigInteger result = val.multiply(val); result=result.remainder(new BigInteger(String.valueOf(MOD))); BigInteger AND=exp.and(new BigInteger(String.valueOf(1))); if(AND.equals(new BigInteger(String.valueOf(1)))){ result = result.multiply(base); result=result.remainder(new BigInteger(String.valueOf(MOD))); } return result; } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
1f2a200f320e7be2675896e7e4336ca4
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Random; import java.util.StringTokenizer; public class MarinaAndVasya { public static void main(String[] args) { FastReader fs = new FastReader(); int n = fs.nextInt(), t = fs.nextInt(); String a = fs.nextLine(), b = fs.nextLine(); char[] out = new char[n]; int similar = (n - t) * 2; for(int i = 0; i < n && similar > 0; i++) { if(a.charAt(i) == b.charAt(i)) { out[i] = a.charAt(i); similar -= 2; } } boolean aTurn = true; for(int i = 0; i < n && similar > 0; i++) { if(a.charAt(i) != b.charAt(i)) { out[i] = aTurn? a.charAt(i) : b.charAt(i); aTurn = !aTurn; if(aTurn) { similar -= 2; t--; } } } for(int i = 0; i < n; i++) { if(out[i] == 0) { Random r = new Random(); while(true) { char c = (char) (r.nextInt(26) + 97); if(c != a.charAt(i) && c != b.charAt(i)) { out[i] = c; t--; break; } } } } if(t > 0 || similar > 0) System.out.println(-1); else System.out.println(String.copyValueOf(out)); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
a3bc6e1435658cab76aa7e2f08d5a524
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int t = in.nextInt(); char[] a = in.next().toCharArray(); char[] b = in.next().toCharArray(); int dif = 0; for (int i = 0; i < n; i++) { if (a[i] != b[i]) ++dif; } char[] c = new char[n]; if (t < dif) { int count = 0; int count2 = 0; int need = dif - t; for (int i = 0; i < n; i++) { if (a[i] != b[i]) { if (count < need) { c[i] = a[i]; ++count; } else if (count2 < need) { c[i] = b[i]; ++count2; } else { char cur = 'a'; while (cur == a[i] || cur == b[i]) { ++cur; } c[i] = cur; } } else { c[i] = a[i]; } } int recal1 = 0; int recal2 = 0; for (int i = 0; i < n; i++) { if (a[i] != c[i]) ++recal1; if (b[i] != c[i]) ++recal2; } if (recal1 != t || recal2 != t) { out.println(-1); return; } out.println(new String(c)); return; } int count = t - dif; for (int i = 0; i < n; i++) { if (a[i] == b[i]) { if (count > 0) { char cur = 'a'; while (cur == a[i]) { cur++; } c[i] = cur; --count; } else { c[i] = a[i]; } } else { char cur = 'a'; while (cur == a[i] || cur == b[i]) { ++cur; } c[i] = cur; } } out.println(new String(c)); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
94cb96611cdc5c508c5056313e848381
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
/** * Created by ckboss on 15-10-7. */ import java.util.*; public class CF584C { int n,t; int t1,t2; String s1,s2; StringBuffer s3; char getChar(char c1,char c2) { char c; for(c='a';c<='z';c++) { if(c!=c1&&c!=c2) { break; } } return c; } CF584C() { Scanner in = new Scanner(System.in); n=in.nextInt(); t=in.nextInt(); s1=in.next(); s2=in.next(); s3=new StringBuffer(); t1=0; t2=0; for(int i=0;i<n;i++) { char c1=s1.charAt(i),c2=s2.charAt(i); if(c1!=c2) { if (t1 <= t2) { s3.append(c2); t1++; } else { s3.append(c1); t2++; } } else s3.append(c1); } //System.out.println("---> "+s3+" t1: "+t1+" t2: "+t2); if(t1>t||t2>t) { System.out.println("-1"); return ; } boolean flag=false; for(int i=0;i<n;i++) { char c1=s1.charAt(i),c2=s2.charAt(i); if(c1!=c2) { char oc = s3.charAt(i); if(oc==c1&&t1<t) { s3.setCharAt(i, getChar(c1, c2)); t1++; } if(oc==c2&&t2<t) { s3.setCharAt(i, getChar(c1, c2)); t2++; } } if (t1 == t && t2 == t) { flag = true; break; } } for(int i=0;i<n&&flag==false;i++) { char c1=s1.charAt(i),c2=s2.charAt(i); if(c1==c2) { s3.setCharAt(i, getChar(c1, c2)); t1++; t2++; } if (t1 == t && t2 == t) { flag = true; break; } } if(flag==false) { System.out.println("-1"); } else { System.out.println(s3); } } public static void main(String[] args) { new CF584C(); } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
a733b157fe382c7e7de16da4eeda9385
train_002.jsonl
1444149000
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; 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.LinkedList; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Random; import java.util.Set; import java.util.Stack; public class test { private static long GCD(long a, long b) { if(b == 0) return a; return GCD(b, a%b); } public static void main(String[] args) throws InterruptedException { //new careercup().run(); //new CC().run(); //System.out.println(Integer.MAX_VALUE); try{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] s = br.readLine().split(" "); int n=Integer.parseInt(s[0]), t=Integer.parseInt(s[1]); String s1 = br.readLine(); String s2 = br.readLine(); Queue<Integer> same = new LinkedList<>(); for(int i=0; i<n; i++) { if(s1.charAt(i) == s2.charAt(i)) same.offer(i); } char[] res = new char[n]; int i=0, size=same.size(); for(; i<Math.min(n-t, size); i++) { int p = same.poll(); res[p] = s1.charAt(p); } int p = 0; for(; i<n-t; i++) { while(p<n && res[p]!=0) p++; if(p == n) { System.out.println(-1); return; } res[p] = s1.charAt(p); while(p<n && res[p]!=0) p++; if(p == n) { System.out.println(-1); return; } res[p] = s2.charAt(p); } for(i=0; i<n; i++) { if(res[i] != 0) continue; char c = 'a'; for(c='a'; c<='z'; c++) { if(c!=s1.charAt(i) && c!=s2.charAt(i)) { res[i] = c; break; } } } System.out.println(new String(res)); }catch(IOException io){ io.printStackTrace(); } } }
Java
["3 2\nabc\nxyc", "1 0\nc\nb"]
1 second
["ayd", "-1"]
null
Java 8
standard input
[ "constructive algorithms", "greedy", "strings" ]
8ba3a7f7cb955478481c74cd4a4eed14
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
1,700
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
standard output
PASSED
b3da0ea13928acc2eb09bf4d627edf87
train_002.jsonl
1505833500
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .
256 megabytes
import java.io.*; import java.util.*; public class B { Inputter in = new Inputter(new BufferedReader(new InputStreamReader(System.in))); PrintWriter out = new PrintWriter(System.out); boolean debug = false; private void solve() { int n = in.nextInt(); List[] edges = new List[n + 1]; for (int i = 1; i <= n; i++) { edges[i] = new LinkedList<Integer>(); } for (int i = 1; i < n; i++) { StringTokenizer tokenizer = new StringTokenizer(in.readLine(), " "); int u = Integer.parseInt(tokenizer.nextToken()); int v = Integer.parseInt(tokenizer.nextToken()); edges[u].add(v); edges[v].add(u); } Boolean[] color = new Boolean[n + 1]; coloring(1, true, edges, color); int countTrue = 0, countFalse = 0; for (int u = 1; u <= n; u++) { if (color[u]) { countTrue++; } else { countFalse++; } } long count = 0L; for (int u = 1; u <= n; u++) { count += (color[u] ? countFalse : countTrue) - edges[u].size(); } out.println(count/2); } private void coloring(int u, Boolean c, List[] edges, Boolean[] color) { color[u] = c; for (int v : (List<Integer>) edges[u]) { if (color[v] == null) { coloring(v, !c, edges, color); } } } public static void main(String[] args) { B app = new B(); app.solve(); app.out.flush(); app.out.close(); app.in.close(); } } class Inputter { BufferedReader reader; StringTokenizer tokenizer; Inputter(BufferedReader reader) { this.reader = reader; } String nextString() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(readLine(), " "); } return tokenizer.nextToken(); } int nextInt() { return Integer.parseInt(nextString()); } long nextLong() { return Long.parseLong(nextString()); } double nextDouble() { return Double.parseDouble(nextString()); } int[] nextArrayInt(int n, int offset) { int[] a = new int[n + offset]; tokenizer = new StringTokenizer(readLine(), " "); int i = offset; while (i < n + offset && tokenizer.hasMoreTokens()) { a[i++] = Integer.parseInt(tokenizer.nextToken()); } return a; } String readLine() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } void close() { try { reader.close(); } catch (IOException e) { throw new RuntimeException(e); } } }
Java
["3\n1 2\n1 3", "5\n1 2\n2 3\n3 4\n4 5"]
2 seconds
["0", "2"]
NoteTree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graphIn the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
44b9adcc9d672221bda3a1cada81b3d0
The first line of input contains an integer nΒ β€” the number of nodes in the tree (1 ≀ n ≀ 105). The next n - 1 lines contain integers u and v (1 ≀ u, v ≀ n, u ≠ v)Β β€” the description of the edges of the tree. It's guaranteed that the given graph is a tree.
1,300
Output one integerΒ β€” the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
standard output
PASSED
628c928b0be23457542a2c7059d351b0
train_002.jsonl
1505833500
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .
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.StringTokenizer; public class B { public static void main(String[] args) { FastScanner scan = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int n = scan.nextInt(); adj = new ArrayList[n]; for(int i = 0; i < n; i++) adj[i] = new ArrayList<Integer>(); for(int i = 0; i < n-1; i++) { int u = scan.nextInt()-1, v = scan.nextInt()-1; adj[u].add(v); adj[v].add(u); } c = new int[n]; v = new boolean[n]; go(0, 0); int a = 0, b = 0; for(int i = 0; i < n; i++) { if(c[i] == 0) a++; else b++; } out.println((long)a*(long)b-n+1); out.close(); } static ArrayList<Integer>[] adj; static int[] c; static boolean[] v; static void go(int at, int k) { c[at] = k; v[at] = true; for(int i : adj[at]) if(!v[i]) go(i, k^1); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(br.readLine()); } catch (Exception e){e.printStackTrace();} } public String next() { if (st.hasMoreTokens()) return st.nextToken(); 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() { String line = ""; if(st.hasMoreTokens()) line = st.nextToken(); else try {return br.readLine();}catch(IOException e){e.printStackTrace();} while(st.hasMoreTokens()) line += " "+st.nextToken(); return line; } public int[] nextIntArray(int n) { int[] a = new int[n]; for(int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n){ long[] a = new long[n]; for(int i = 0; i < n; i++) a[i] = nextLong(); return a; } public double[] nextDoubleArray(int n){ double[] a = new double[n]; for(int i = 0; i < n; i++) a[i] = nextDouble(); return a; } public char[][] nextGrid(int n, int m){ char[][] grid = new char[n][m]; for(int i = 0; i < n; i++) grid[i] = next().toCharArray(); return grid; } } }
Java
["3\n1 2\n1 3", "5\n1 2\n2 3\n3 4\n4 5"]
2 seconds
["0", "2"]
NoteTree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graphIn the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
44b9adcc9d672221bda3a1cada81b3d0
The first line of input contains an integer nΒ β€” the number of nodes in the tree (1 ≀ n ≀ 105). The next n - 1 lines contain integers u and v (1 ≀ u, v ≀ n, u ≠ v)Β β€” the description of the edges of the tree. It's guaranteed that the given graph is a tree.
1,300
Output one integerΒ β€” the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
standard output
PASSED
1b6ffea79cf5286c50ea270b74735ae1
train_002.jsonl
1505833500
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; public class Solution2 { ArrayList<ArrayList<Integer>> arr; long [] color = new long[2]; boolean[] visited; void dfs (int c , int source) { visited[source] = true; color [c] ++; for (int i = 0; i < arr.get(source).size(); i++) { if (!visited[arr.get(source).get(i)]) { dfs((c+1)%2 , arr.get(source).get(i)); } } } public static void main(String[] args) { InputReader in = new InputReader(System.in); int n = in.nextInt(); Solution2 x = new Solution2(); x.arr = new ArrayList<ArrayList<Integer>>(n+1); for (int i = 0; i < n+1; i++) { x.arr.add(new ArrayList<Integer>()); } x.visited = new boolean[n+1]; for (int i = 0; i < n - 1; i++) { int a =in.nextInt(); int b =in.nextInt(); x.arr.get(a).add(b); x.arr.get(b).add(a); } x.dfs(1,1); System.out.println(x.color[0] * x.color[1] - n + 1); } } class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } }
Java
["3\n1 2\n1 3", "5\n1 2\n2 3\n3 4\n4 5"]
2 seconds
["0", "2"]
NoteTree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graphIn the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
44b9adcc9d672221bda3a1cada81b3d0
The first line of input contains an integer nΒ β€” the number of nodes in the tree (1 ≀ n ≀ 105). The next n - 1 lines contain integers u and v (1 ≀ u, v ≀ n, u ≠ v)Β β€” the description of the edges of the tree. It's guaranteed that the given graph is a tree.
1,300
Output one integerΒ β€” the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
standard output
PASSED
6c06f5f2bb680659b7e89f182817c36f
train_002.jsonl
1505833500
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; public class Solution2 { ArrayList<ArrayList<Integer>> arr; long [] color = new long[2]; boolean[] visited; void dfs (int c , int source) { visited[source] = true; color [c] ++; for (int i = 0; i < arr.get(source).size(); i++) { if (!visited[arr.get(source).get(i)]) { dfs((c+1)%2 , arr.get(source).get(i)); } } } public static void main(String[] args) { InputReader in = new InputReader(System.in); int n = in.nextInt(); Solution2 x = new Solution2(); x.arr = new ArrayList<ArrayList<Integer>>(n+1); for (int i = 0; i < n+1; i++) { x.arr.add(new ArrayList<Integer>()); } x.visited = new boolean[n+1]; for (int i = 0; i < n - 1; i++) { int a =in.nextInt(); int b =in.nextInt(); x.arr.get(a).add(b); x.arr.get(b).add(a); } x.dfs(1,1); System.out.println(x.color[0] * x.color[1] - n + 1); } } class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } }
Java
["3\n1 2\n1 3", "5\n1 2\n2 3\n3 4\n4 5"]
2 seconds
["0", "2"]
NoteTree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graphIn the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
44b9adcc9d672221bda3a1cada81b3d0
The first line of input contains an integer nΒ β€” the number of nodes in the tree (1 ≀ n ≀ 105). The next n - 1 lines contain integers u and v (1 ≀ u, v ≀ n, u ≠ v)Β β€” the description of the edges of the tree. It's guaranteed that the given graph is a tree.
1,300
Output one integerΒ β€” the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
standard output
PASSED
0118752057bbbb69debd34fed9567096
train_002.jsonl
1505833500
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class loser { static class InputReader { public BufferedReader br; public StringTokenizer token; public InputReader(InputStream stream) { br=new BufferedReader(new InputStreamReader(stream),32768); token=null; } public String next() { while(token==null || !token.hasMoreTokens()) { try { token=new StringTokenizer(br.readLine()); } catch(IOException e) { throw new RuntimeException(e); } } return token.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } static class card{ String s; int l; public card(String s,int i) { this.s=s; this.l=i; } } static class sort implements Comparator<card> { public int compare(card o1,card o2) { if(o1.l!=o2.l) return (o1.l-o2.l); else return o1.s.compareTo(o2.s); } } static void shuffle(int a[]) { List<Integer> l=new ArrayList<>(); for(int i=0;i<a.length;i++) l.add(a[i]); Collections.shuffle(l); for(int i=0;i<a.length;i++) a[i]=l.get(i); } /*static long gcd(long a,long b) { if(b==0) return a; else return gcd(b,a%b); } static boolean valid(int i,int j,int r,int c) { if(i<r && i>=0 && j<c && j>=0) return true; else return false; }*/ static class Pair { int a;int b; public Pair(int a,int b) { this.a =a; this.b =b; } } static boolean v[]=new boolean[100005]; static long color[]=new long[2]; static void dfs(int s,List<Integer> l[],int d) { //System.out.println(s+" "+d); if(!v[s]) { v[s]=true; color[d]++; for(int i=0;i<l[s].size();i++) { if(!v[l[s].get(i)]) { dfs(l[s].get(i),l,(d==0)?1:0); } } } } public static void main(String[] args) { InputReader sc=new InputReader(System.in); int n=sc.nextInt(); List<Integer> l[]=new ArrayList[n+1]; for(int i=0;i<n+1;i++) l[i]=new ArrayList<>(); for(int i=0;i<n-1;i++) { int x=sc.nextInt(); int y=sc.nextInt(); l[x].add(y); l[y].add(x); } dfs(1,l,0); System.out.println(color[0]*color[1]-n+1); } }
Java
["3\n1 2\n1 3", "5\n1 2\n2 3\n3 4\n4 5"]
2 seconds
["0", "2"]
NoteTree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graphIn the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
44b9adcc9d672221bda3a1cada81b3d0
The first line of input contains an integer nΒ β€” the number of nodes in the tree (1 ≀ n ≀ 105). The next n - 1 lines contain integers u and v (1 ≀ u, v ≀ n, u ≠ v)Β β€” the description of the edges of the tree. It's guaranteed that the given graph is a tree.
1,300
Output one integerΒ β€” the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
standard output
PASSED
1198fdceba3edc18f4d0e5240c4901de
train_002.jsonl
1505833500
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .
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.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.StringTokenizer; @SuppressWarnings("ALL") public class B { static class ProblemSolver { private static long[] count = new long[2]; private static List<Integer> [] graph; private static int[] color; public void solveTheProblem(InputReader in,PrintWriter out){ int n = in.nextInt(); color = new int[n]; Arrays.fill(color, -1); graph = new List[n]; for (int i = 0; i < n; i++) { graph[i] = new ArrayList<>(); } for (int i = 0; i < n-1; i++) { int u = in.nextInt() - 1; int v = in.nextInt() - 1; graph[u].add(v); graph[v].add(u); } dfs(0, 0); System.out.println(count[0] * count[1] - n + 1); } private void dfs(int u, int c) { color[u] = c; count[c]++; for (int v: graph[u]) { if (color[v] == -1) { dfs(v, 1 - c); } } } } //Default template for all the codes static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream),32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } //Main method for all the codes public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); ProblemSolver problemSolver = new ProblemSolver(); problemSolver.solveTheProblem(in, out); out.close(); } }
Java
["3\n1 2\n1 3", "5\n1 2\n2 3\n3 4\n4 5"]
2 seconds
["0", "2"]
NoteTree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graphIn the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
44b9adcc9d672221bda3a1cada81b3d0
The first line of input contains an integer nΒ β€” the number of nodes in the tree (1 ≀ n ≀ 105). The next n - 1 lines contain integers u and v (1 ≀ u, v ≀ n, u ≠ v)Β β€” the description of the edges of the tree. It's guaranteed that the given graph is a tree.
1,300
Output one integerΒ β€” the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
standard output
PASSED
fa2be4e848b70a3dfe02b180e5e2a3ea
train_002.jsonl
1505833500
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; import java.text.DateFormat; import java.text.SimpleDateFormat; public class Main{ ArrayList<Integer>[] graph; Set<Integer>[] setGraph; public Main(int n){ graph = new ArrayList[n+1]; setGraph = new HashSet[n+1]; for(int i=1; i<n+1; i++){ graph[i] = new ArrayList<>(); setGraph[i] = new HashSet<>(); } //System.out.println("hello"); } static Set<Integer> set1 = new HashSet<Integer>(); static Set<Integer> set2 = new HashSet<Integer>(); public void cunt2(int n, int index, boolean[] mark, int[] setNo){ mark[index] = true; //System.out.print(index+" "); for(int g : graph[index]){ if(!mark[g]){ //System.out.println(index); if(setNo[index]==1){ setNo[g] = 2; set2.add(g); }else{ setNo[g] = 1; set1.add(g); } cunt2( n, g, mark,setNo); } } } public int[] cunt(int n){ int[] setNo= new int[n+1]; int count =0; boolean[] mark = new boolean[n+1]; for(int i=1; i<n+1; i++){ if(!mark[i]){ setNo[i] = 1; set1.add(i); cunt2(n,i,mark,setNo); count++; } } return setNo; } public static void main (String[] args)throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); // String[] ar = new String[n]; Main tre = new Main(n); for(int i=0; i<n-1; i++){ String[] ar = br.readLine().split(" "); int u = Integer.parseInt(ar[0]); int v = Integer.parseInt(ar[1]); tre.graph[u].add(v); tre.graph[v].add(u); tre.setGraph[u].add(v); tre.setGraph[v].add(u); } int[] setNo = new int[n+1]; setNo = tre.cunt(n); long count =0; for(int i :set1){ count+= set2.size()-(tre.setGraph[i].size()); } System.out.println(count); //int jack = 40; } }
Java
["3\n1 2\n1 3", "5\n1 2\n2 3\n3 4\n4 5"]
2 seconds
["0", "2"]
NoteTree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graphIn the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
44b9adcc9d672221bda3a1cada81b3d0
The first line of input contains an integer nΒ β€” the number of nodes in the tree (1 ≀ n ≀ 105). The next n - 1 lines contain integers u and v (1 ≀ u, v ≀ n, u ≠ v)Β β€” the description of the edges of the tree. It's guaranteed that the given graph is a tree.
1,300
Output one integerΒ β€” the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
standard output
PASSED
c5e4c8d4208beb7c8b7ef749ca00fd0b
train_002.jsonl
1505833500
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .
256 megabytes
import java.util.*; import java.io.*; import java.math.BigDecimal; import java.math.BigInteger; import java.math.MathContext; public class Es { static class Pair implements Comparable { int first; double second; public Pair(int f, double s) { first = f; second = s; } public String toString() { return first + " " + second; } public int compareTo(Object o) { Pair p2 = (Pair)o; if(this.first < p2.first) return -1; if(this.first > p2.first) return 1; if(this.second < p2.second) return -1; if(this.second > p2.second) return 1; return 0; } } static int log2(double a) { return (int)(Math.log(a) / Math.log(2)); } static long gcd(long a, long b) { long r; while((r = a % b) > 0) { a = b; b = r; } return b; } static long c1 = 0, c2 = 0; static boolean[] cc; static int n; static Vector <Integer>[] tree; static void dfs(int c, int p, boolean b) { cc[c] = b; if(b) c1 ++; else c2 ++; for(int i = 0; i < tree[c].size(); i ++) { if(tree[c].get(i) == p) continue; dfs(tree[c].get(i), c, !b); } } public static void main(String[] args) throws IOException { n = in.nextInt(); tree = new Vector[n + 1]; cc = new boolean[n + 1]; for(int i = 0; i <= n; i ++) tree[i] = new Vector <Integer>(); for(int i = 0; i < n - 1; i ++) { int u = in.nextInt(); int v = in.nextInt(); tree[u].add(v); tree[v].add(u); } dfs(1, -1, true); long ans = 0; for(int i = 1; i <= n; i ++) { if(cc[i]) { ans += c2 - tree[i].size(); }else { ans += c1 - tree[i].size(); } } sop(ans / 2); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static FastReader in = new FastReader(); public static void sop(Object o) { System.out.print(o); } }
Java
["3\n1 2\n1 3", "5\n1 2\n2 3\n3 4\n4 5"]
2 seconds
["0", "2"]
NoteTree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graphIn the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
44b9adcc9d672221bda3a1cada81b3d0
The first line of input contains an integer nΒ β€” the number of nodes in the tree (1 ≀ n ≀ 105). The next n - 1 lines contain integers u and v (1 ≀ u, v ≀ n, u ≠ v)Β β€” the description of the edges of the tree. It's guaranteed that the given graph is a tree.
1,300
Output one integerΒ β€” the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
standard output
PASSED
aae949f11193f68efd0ebd426019dec6
train_002.jsonl
1505833500
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.List; import java.util.Scanner; import java.util.ArrayList; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Mouna Cheikhna */ 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); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { List<Edge>[] adjList; long nbA = 0; long nbB = 0; private boolean[] visited; public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); adjList = new ArrayList[n]; for (int i = 0; i < n; i++) { adjList[i] = new ArrayList<>(); } for (int i = 0; i < n - 1; i++) { int u = in.nextInt() - 1; int v = in.nextInt() - 1; adjList[u].add(new Edge(v)); adjList[v].add(new Edge(u)); } visited = new boolean[n]; dfs(0, true); long max = nbA * nbB; long res = max - (n - 1); out.println(res); } private void dfs(int start, boolean isA) { visited[start] = true; if (isA) { nbA++; } else { nbB++; } for (Edge neighbor : adjList[start]) { if (!visited[neighbor.dest]) { dfs(neighbor.dest, !isA); } } } class Edge { int dest; public Edge(int dest) { this.dest = dest; } } } }
Java
["3\n1 2\n1 3", "5\n1 2\n2 3\n3 4\n4 5"]
2 seconds
["0", "2"]
NoteTree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graphIn the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
44b9adcc9d672221bda3a1cada81b3d0
The first line of input contains an integer nΒ β€” the number of nodes in the tree (1 ≀ n ≀ 105). The next n - 1 lines contain integers u and v (1 ≀ u, v ≀ n, u ≠ v)Β β€” the description of the edges of the tree. It's guaranteed that the given graph is a tree.
1,300
Output one integerΒ β€” the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
standard output
PASSED
604b44acb821433fa1633a9ac0c5063a
train_002.jsonl
1505833500
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); List<Integer> adj[] = new ArrayList[n + 2]; for(int i = 0; i <= n; i++){ adj[i] = new ArrayList<>(); } for(int i = 1; i <= n - 1; i++){ int u = sc.nextInt(); int v = sc.nextInt(); adj[u].add(v); adj[v].add(u); } boolean[] visited = new boolean[n + 2]; long[] color = new long[n + 2]; List<Long> set1 = new ArrayList<>(); List<Long> set2 = new ArrayList<>(); dfs(1, adj, visited, set1, set2, color); // System.out.println(set1.size() + " " + set2.size()); long set1Size = set1.size(); long set2Size = set2.size(); long ans = set1Size * set2Size; ans = (ans - (n - 1)); System.out.println(ans); sc.close(); } private static void dfs(int u, List<Integer>[] adj, boolean[] visited, List<Long> set1 , List<Long> set2, long[] color) { visited[u] = true; if(color[u] == 0){ color[u] = 1; set1.add((long) u); } for(Integer x : adj[u]){ if(!visited[x]){ if(color[u] == 1){ set2.add((long)x); color[x] = 2; } else if(color[u] == 2){ set1.add((long)x); color[x] = 1; } dfs(x, adj, visited, set1, set2, color); } } } }
Java
["3\n1 2\n1 3", "5\n1 2\n2 3\n3 4\n4 5"]
2 seconds
["0", "2"]
NoteTree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graphIn the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
44b9adcc9d672221bda3a1cada81b3d0
The first line of input contains an integer nΒ β€” the number of nodes in the tree (1 ≀ n ≀ 105). The next n - 1 lines contain integers u and v (1 ≀ u, v ≀ n, u ≠ v)Β β€” the description of the edges of the tree. It's guaranteed that the given graph is a tree.
1,300
Output one integerΒ β€” the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
standard output
PASSED
f0eff5948e3610eb93dc5c2b14503e9c
train_002.jsonl
1505833500
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .
256 megabytes
/** https://codeforces.com/problemset/problem/862/B * idea: dfs and similar: create sets of graph, * and check number of vertexes in set 2 that each vertex in set 1 can connect with. */ import java.util.Scanner; import java.util.HashSet; import java.util.ArrayList; import java.util.Deque; import java.util.LinkedList; public class Bipartiteness { public static void createPartSet(ArrayList<ArrayList<Integer>> graph, HashSet[] partSet) { int startNode = 1; int sz = graph.size(); boolean[] visitedArr = new boolean[sz]; Deque<Integer> dq = new LinkedList<Integer>(); dq.addLast(startNode); visitedArr[startNode] = true; partSet[0].add(startNode); int setIdx, node; while(!dq.isEmpty()) { node = dq.pollLast(); if (partSet[0].contains(node)) { setIdx = 0; } else { setIdx = 1; } for (Integer neighbour : graph.get(node)) { if (!visitedArr[neighbour]) { visitedArr[neighbour] = true; partSet[1-setIdx].add(neighbour); dq.add(neighbour); } } } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int sz = n + 1; ArrayList<ArrayList<Integer>> graph = new ArrayList<>(); for (int i=0; i < sz; i++) { graph.add(new ArrayList<Integer>()); } int u,v; for (int i=0; i < n-1; i++) { u = sc.nextInt(); v = sc.nextInt(); graph.get(u).add(v); graph.get(v).add(u); } // create bipartiteness HashSet<Integer>[] partSet = new HashSet[2]; partSet[0] = new HashSet<>(); partSet[1] = new HashSet<>(); createPartSet(graph, partSet); // count the number of edges can be added. int setIdx; if (partSet[0].size() <= partSet[1].size()){ setIdx = 0; } else { setIdx = 1; } long ans = 0; for (int node : partSet[setIdx]) { ans += n - 1 - (partSet[setIdx].size() - 1) - graph.get(node).size(); } System.out.println(ans); } }
Java
["3\n1 2\n1 3", "5\n1 2\n2 3\n3 4\n4 5"]
2 seconds
["0", "2"]
NoteTree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graphIn the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
44b9adcc9d672221bda3a1cada81b3d0
The first line of input contains an integer nΒ β€” the number of nodes in the tree (1 ≀ n ≀ 105). The next n - 1 lines contain integers u and v (1 ≀ u, v ≀ n, u ≠ v)Β β€” the description of the edges of the tree. It's guaranteed that the given graph is a tree.
1,300
Output one integerΒ β€” the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
standard output
PASSED
2791938e004aa7b94012b9fe4120c759
train_002.jsonl
1505833500
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .
256 megabytes
import java.io.*; import java.lang.annotation.Native; import java.math.BigInteger; import java.util.*; import java.util.ArrayList; import java.util.Scanner; 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 int mod = 1000000007; static final int mod1 = 1073741824; public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static boolean prime[] = new boolean[1000001]; 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][2]; long R[][] = new long[n2][2]; /*Copy data to temp arrays*/ for (int i = 0; i < n1; ++i) { L[i][0] = arr[l + i][0]; L[i][1] = arr[l + i][1]; } for (int j = 0; j < n2; ++j) { R[j][0] = arr[m + 1 + j][0]; R[j][1] = arr[m + 1 + j][1]; } /* 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][0] <= R[j][0]) { arr[k][0] = L[i][0]; arr[k][1] = L[i][1]; i++; } else { arr[k][0] = R[j][0]; arr[k][1] = R[j][1]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k][0] = L[i][0]; arr[k][1] = L[i][1]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k][0] = R[j][0]; arr[k][1] = R[j][1]; j++; k++; } } public static long[][] sort2elementarray(long arr[][], int l, int r) { if (l < r) { // Find the middle point int m = (l + r) / 2; // Sort first and second halves sort2elementarray(arr, l, m); sort2elementarray(arr, m + 1, r); // Merge the sorted halves merge(arr, l, m, r); } return arr; } static int f=1; static int vertices=0; static int edges=0; public static void dfs(ArrayList<Integer>[]li,int v,HashMap<Integer,Integer>hm) { hm.put(v,1); //System.out.print(v+" "); //System.out.println(a[v]+" "+cost); vertices++; edges+=li[v].size(); for (int j = 0; j < li[v].size(); j++) { if (hm.get(li[v].get(j)) == null) { dfs(li, li[v].get(j), hm); } } } public static void dfsutil(ArrayList<Integer>[]li,int n,int v) { HashMap<Integer,Integer>hm=new HashMap<>(); int c=0; for(int j=1;j<=n;j++) { if(hm.get(j)==null) { vertices=0; edges=0; dfs(li,j,hm); if(edges!=vertices*(vertices-1)) { System.out.println("NO"); System.exit(0); } } } System.out.println("YES"); } public static void bipartite(ArrayList<Integer>[]li,int n) { int f = 1; int col[] = new int[n + 1]; int fl = 1; Arrays.fill(col, -1); for (int i = 1; i <= n; i++) { if (col[i] == -1) { col[i] = 1; Queue<Integer> q = new LinkedList<>(); q.add(i); while (!q.isEmpty()) { int x = q.peek(); q.remove(); for (int j = 0; j < li[x].size(); j++) { if (col[li[x].get(j)] == -1) { col[li[x].get(j)] = 1 - col[x]; q.add(li[x].get(j)); } else if (col[li[x].get(j)] == col[x]) { f = 0; break; } } } if (f == 0) { fl = 0; break; } } } /*if(fl==0) // System.out.println(-1); //else {*/ long l1 = 0; long l2 = 0; for (int j = 1; j <= n; j++) { if (col[j] == 1) { l1++; //sb.append(j+" "); } else { l2++; //sb1.append(j+" "); } } long ans=(l1*l2)-(n-1); System.out.println(ans); //} } public static void main(String[] args) { FastReader sc = new FastReader(); int n=sc.nextInt(); //int m=sc.nextInt(); ArrayList<Integer>[]li=new ArrayList[n+1]; for(int j=1;j<=n;j++) { li[j]=new ArrayList<>(); } for(int j=0;j<n-1;j++) { int u=sc.nextInt(); int v=sc.nextInt(); li[u].add(v); li[v].add(u); } //dfsutil(li,n,0); bipartite(li,n); } }
Java
["3\n1 2\n1 3", "5\n1 2\n2 3\n3 4\n4 5"]
2 seconds
["0", "2"]
NoteTree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graphIn the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
44b9adcc9d672221bda3a1cada81b3d0
The first line of input contains an integer nΒ β€” the number of nodes in the tree (1 ≀ n ≀ 105). The next n - 1 lines contain integers u and v (1 ≀ u, v ≀ n, u ≠ v)Β β€” the description of the edges of the tree. It's guaranteed that the given graph is a tree.
1,300
Output one integerΒ β€” the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
standard output
PASSED
0bd8a4a0cda5118ea4b99b42254fe72a
train_002.jsonl
1505833500
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .
256 megabytes
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; import java.util.Arrays; import java.util.ArrayList; import java.lang.Math; import java.util.Arrays; import java.util.Comparator; 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 int binarySearch(int a[] ,int k,int l,int h){ while(l<=h){ int mid = (l+h)/2; if(a[mid]==k) return mid; else if(a[mid]>k) h=mid-1; else if(a[mid]<k) l=mid+1; } return -1; } static int binarySearch(ArrayList<Integer> a ,int k,int l,int h){ while(l<=h){ int mid = (l+h)/2; if(a.get(mid)==k) return mid; else if(a.get(mid)>k) h=mid-1; else if(a.get(mid)<k) l=mid+1; } return -1; } static String reverse(String input) { char[] a = input.toCharArray(); int l, r = 0; r = a.length - 1; for (l = 0; l < r; l++, r--) { // Swap values of l and r char temp = a[l]; a[l] = a[r]; a[r] = temp; } return String.valueOf(a); } static int gcd(int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; if (a == b) return a; if (a > b) return gcd(a-b, b); return gcd(a, b-a); } static int lcm(int a, int b) { return (a*b)/gcd(a, b); } static int solve(int A, int B) { int count = 0; for (int i = 0; i < 21; i++) { if (((A >> i) & 1) != ((B >> i) & 1)) { count++; } } return count; } static long nCr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); } static long fact(int n) { long res = 1; for (int i = 2; i <= n; i++) res = res * i; return res; } public static long count(long k) { return k * (k - 1) / 2; } static boolean isPrime(int n) { // if(n==1) return false; if(n==2) return true; if (n%2==0) return false; int l = (int)Math.sqrt(n); for(int i=3;i<=l;i+=2) { if(n%i==0) return false; } return true; } static int negMod(int n){ int a = (n % 1000000007 + 1000000007) % 1000000007; return a; } public static int sum(long x) { int sum = 0; while (x > 0) { sum += x % 10; x /= 10; } return sum; } static ArrayList<Long> a = new ArrayList<>(); static void genLucky(long val , int f , int s){ if(val>10000000000l) return ; if(f==s) a.add(val); genLucky(val*10+4, f+1, s); genLucky(val*10+7, f, s+1); } public static int max(int x, int y, int z) { return (int)Math.max(Math.max(x, y), z); } public static int min(int x, int y, int z) { return (int)Math.min(Math.min(x, y), z); } static void dfs(int a){ visited[a] = 1; Iterator<Integer> i = adj.get(a).listIterator(); while (i.hasNext()) { int n = i.next(); if (visited[n]==0) dfs(n); } } static void bipartite(int n,int c,int color[]){ visited[n] = 1; color[n] = c; Iterator<Integer> i = adj.get(n).listIterator(); while (i.hasNext()) { int a = i.next(); if (visited[a]==0) bipartite(a,c^1,color); } } static void addEdge(int u, int v) { adj.get(u).add(v); adj.get(v).add(u); } static ArrayList<ArrayList<Integer>> adj = new ArrayList<ArrayList<Integer>>(); static int visited[] = new int[100002]; static int mod=1000003; public static void main(String[] args) throws Exception { OutputStream outputStream = System.out; PrintWriter w = new PrintWriter(outputStream); FastReader sc = new FastReader(); // Scanner sc = new Scanner(new File("input.txt")); // PrintWriter out = new PrintWriter(new File("output.txt")); int i,j; int t = 1; while(t>0){ int n = sc.nextInt(); for(i=0;i<=n;i++) adj.add(new ArrayList<Integer>()); if(n==1) w.println("0"); else{ for(i=1;i<=n-1;i++){ int u = sc.nextInt(); int v = sc.nextInt(); addEdge(u, v); } int color[] = new int[n+1]; bipartite(1,0,color); long h = 0 , l = 0; for(i=1;i<=n;i++){ if(color[i]==0) h++; else l++; } long k = h*l; k-=(n-1); w.println(k); } t--; } w.close(); } } // System.out.println();
Java
["3\n1 2\n1 3", "5\n1 2\n2 3\n3 4\n4 5"]
2 seconds
["0", "2"]
NoteTree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graphIn the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
44b9adcc9d672221bda3a1cada81b3d0
The first line of input contains an integer nΒ β€” the number of nodes in the tree (1 ≀ n ≀ 105). The next n - 1 lines contain integers u and v (1 ≀ u, v ≀ n, u ≠ v)Β β€” the description of the edges of the tree. It's guaranteed that the given graph is a tree.
1,300
Output one integerΒ β€” the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
standard output
PASSED
17b6d387c5b5a4700ab0e6ba3d175a01
train_002.jsonl
1505833500
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .
256 megabytes
import java.util.Scanner; import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; public class thing{ static class pair { int a,b; pair(int a,int b) { this.a = a; this.b = b; } } public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); ArrayList<Integer>[] edgelist = new ArrayList[n+1]; for (int i = 1; i <= n; i++) { edgelist[i] = new ArrayList<Integer>(); } for(int i=0;i<n-1;i++) { int a = in.nextInt(), b = in.nextInt(); edgelist[a].add(b); edgelist[b].add(a); } Queue<pair> q = new LinkedList<>(); q.add(new pair(1,0)); long left = 0, right = 0; boolean[] visited = new boolean[n+1]; while(!(q.isEmpty())) { pair x = q.remove(); int curr = x.a; if(x.b == 0) { left++; } else { right++; } visited[curr] = true; for(Integer i:edgelist[curr]) { if(!visited[i]) { q.add(new pair(i,(x.b+1)%2)); } } } System.out.println(left*right-n+1); } }
Java
["3\n1 2\n1 3", "5\n1 2\n2 3\n3 4\n4 5"]
2 seconds
["0", "2"]
NoteTree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graphIn the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
44b9adcc9d672221bda3a1cada81b3d0
The first line of input contains an integer nΒ β€” the number of nodes in the tree (1 ≀ n ≀ 105). The next n - 1 lines contain integers u and v (1 ≀ u, v ≀ n, u ≠ v)Β β€” the description of the edges of the tree. It's guaranteed that the given graph is a tree.
1,300
Output one integerΒ β€” the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
standard output
PASSED
942571506c3e1bc553c34dad0ccc07de
train_002.jsonl
1505833500
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .
256 megabytes
import java.io.*; import java.util.*; public class CF4352 { static long[] count=new long[2]; static LinkedList<Integer> a[]=new LinkedList[100005]; static int visited[]=new int[100005]; public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(br.readLine()); for(int i=0;i<100005;i++) { a[i]=new LinkedList(); } for(int j=0;j<n-1;j++) { String t1=br.readLine(); String[] t2=t1.split(" "); int b=Integer.parseInt(t2[0]); int c=Integer.parseInt(t2[1]); a[b].add(c); a[c].add(b); } visited[1]=1; dfs(1,0); long res=count[0]*count[1]; System.out.println(res-n+1); } public static void dfs(int node,int color) { count[color]++; visited[node]=1; Iterator<Integer> it=a[node].listIterator(); while(it.hasNext()) { int n=it.next(); if(visited[n]==0) { dfs(n,(color*-1)+1); } } } } /* import java.io.*; import java.util.*; public class CF4352 { static class Edge { int l; int r; Edge(int x,int y) { l=x; r=y; } } public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(br.readLine()); Edge[] ed=new Edge[n-1]; for(int i=0;i<n-1;i++) { String t1=br.readLine(); String[] t2=t1.split(" "); ed[i]=new Edge(Integer.parseInt(t2[0]),Integer.parseInt(t2[1])); } Arrays.sort(ed,new Comparator<Edge>() { public int compare(Edge e1,Edge e2) { return (int)(e1.l-e2.l); } }); ArrayList<Integer> a1=new ArrayList<>(); ArrayList<Integer> a2=new ArrayList<>(); a1.add(ed[0].l); a2.add(ed[0].r); for(int k=1;k<n-1;k++) { if(a1.contains(ed[k].l)&&a2.contains(ed[k].r)||a1.contains(ed[k].r)&&a2.contains(ed[k].l)) { continue; } else if(a1.contains(ed[k].l)&&!a2.contains(ed[k].r)) { a2.add(ed[k].r); } else if(a1.contains(ed[k].r)&&!a2.contains(ed[k].l)) { a2.add(ed[k].l); } else if(a2.contains(ed[k].l)&&!a1.contains(ed[k].r)) { a1.add(ed[k].r); } else if(a2.contains(ed[k].r)&&!a1.contains(ed[k].l)) { a1.add(ed[k].l); } } for(int h:a1) { System.out.print(h+" "); } System.out.println(); for(int h:a2) { System.out.print(h+" "); } System.out.println(); int set1=a1.size(); int set2=a2.size(); int answer=(set1*set2)-(n-1); System.out.println(answer); } } } */
Java
["3\n1 2\n1 3", "5\n1 2\n2 3\n3 4\n4 5"]
2 seconds
["0", "2"]
NoteTree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graphIn the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
44b9adcc9d672221bda3a1cada81b3d0
The first line of input contains an integer nΒ β€” the number of nodes in the tree (1 ≀ n ≀ 105). The next n - 1 lines contain integers u and v (1 ≀ u, v ≀ n, u ≠ v)Β β€” the description of the edges of the tree. It's guaranteed that the given graph is a tree.
1,300
Output one integerΒ β€” the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
standard output
PASSED
393126c6a4e41fc5a37397044e96a2bb
train_002.jsonl
1505833500
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .
256 megabytes
import java.util.*; public class MahmoudBipartiteness { static ArrayList<Integer>[] graph; static int s1; static int s2; static boolean[] visited; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); graph = new ArrayList[N]; visited = new boolean[N]; s1 = 0; s2 = 0; for(int i = 0; i < N; i++) { graph[i] = new ArrayList<Integer>(); } for(int i = 1; i < N; i++) { int t1 = scanner.nextInt()-1; int t2 = scanner.nextInt()-1; graph[t1].add(t2); graph[t2].add(t1); } visited[0] = true; dfs(0, 0); long ans = ((long)s1*(long)s2)-(long)N+1L; System.out.println(ans); } public static void dfs(int level, int node) { if (level % 2 == 0) { s1++; } else { s2++; } for (int i : graph[node]) { if (visited[i]) {continue;} visited[i] = true; dfs(level+1, i); } } }
Java
["3\n1 2\n1 3", "5\n1 2\n2 3\n3 4\n4 5"]
2 seconds
["0", "2"]
NoteTree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graphIn the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
44b9adcc9d672221bda3a1cada81b3d0
The first line of input contains an integer nΒ β€” the number of nodes in the tree (1 ≀ n ≀ 105). The next n - 1 lines contain integers u and v (1 ≀ u, v ≀ n, u ≠ v)Β β€” the description of the edges of the tree. It's guaranteed that the given graph is a tree.
1,300
Output one integerΒ β€” the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
standard output
PASSED
af65e63e381de7c49ce1d0b14d3ce7a8
train_002.jsonl
1505833500
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .
256 megabytes
//package codeforce3; import java.math.BigInteger; import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util.TreeSet; public class gragh { static class node { int v; boolean red; public node(int v, boolean red) { this.v = v; this.red = red; } } public static void main(String agrs[]) { Scanner in = new Scanner(System.in); int n = in.nextInt(); LinkedList<Integer>[] gragh = new LinkedList[n]; for(int i = 0; i < n; i++) { gragh[i] = new LinkedList<Integer>(); } for(int i = 0; i < n-1; i++) { int s = in.nextInt(); int d = in.nextInt(); gragh[s-1].add(d-1); gragh[d-1].add(s-1); } boolean visited[] = new boolean[n]; //visited[0] = true; LinkedList<node> queue = new LinkedList<node>(); queue.add(new node(0, true)); ArrayList<Integer> red = new ArrayList<Integer>(); ArrayList<Integer> blue = new ArrayList<Integer>(); //boolean[] sets = new boolean[n]; while(!queue.isEmpty()) { node current = queue.removeFirst(); //sets[current.v] = current.red; visited[current.v] = true; if(current.red) { red.add(current.v); } else { blue.add(current.v); } for(int k : gragh[current.v]) { if(!visited[k]) { queue.add(new node(k, !current.red)); } } } //System.out.println(red.size() + " " + blue.size()); BigInteger rs = BigInteger.valueOf(red.size()); BigInteger bs = BigInteger.valueOf(blue.size()); BigInteger result = (rs.multiply(bs)).subtract(BigInteger.valueOf(n-1)); /** for(int i = 0; i < n; i++) { TreeSet<Integer> set = new TreeSet<Integer>(); for(int j = 0; j < n; j++) { if(j != i) { set.add(j); } } set.removeAll(gragh[i]); if(sets[i]) { for(int j : blue) { if(set.contains(j)) { result++; gragh[j].add(i); //gragh[j][i] = 1; } } } else { for(int j : red) { if(set.contains(j)) { result++; gragh[j].add(i); //gragh[j][i] = 1; } } } } **/ System.out.println(result); } }
Java
["3\n1 2\n1 3", "5\n1 2\n2 3\n3 4\n4 5"]
2 seconds
["0", "2"]
NoteTree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graphIn the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
44b9adcc9d672221bda3a1cada81b3d0
The first line of input contains an integer nΒ β€” the number of nodes in the tree (1 ≀ n ≀ 105). The next n - 1 lines contain integers u and v (1 ≀ u, v ≀ n, u ≠ v)Β β€” the description of the edges of the tree. It's guaranteed that the given graph is a tree.
1,300
Output one integerΒ β€” the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
standard output
PASSED
1db9dd037674a7e3a6ab298f5a7bdffe
train_002.jsonl
1363188600
The classic programming language of Bitland is Bit++. This language is so peculiar and complicated.The language is that peculiar as it has exactly one variable, called x. Also, there are two operations: Operation ++ increases the value of variable x by 1. Operation -- decreases the value of variable x by 1. A statement in language Bit++ is a sequence, consisting of exactly one operation and one variable x. The statement is written without spaces, that is, it can only contain characters "+", "-", "X". Executing a statement means applying the operation it contains.A programme in Bit++ is a sequence of statements, each of them needs to be executed. Executing a programme means executing all the statements it contains.You're given a programme in language Bit++. The initial value of x is 0. Execute the programme and find its final value (the value of the variable when this programme is executed).
256 megabytes
import java.util.*; public class MyClass { public static void main(String args[]) { Scanner f=new Scanner(System.in); int x=0; int g=f.nextInt(); char k='+'; char t='-'; char b; String s[]=new String[g]; for(int i=0; i<g; i++) { s[i]=f.next(); } for(int i=0; i<g; i++) { b= s[i].charAt(1); if(b==k) { x++; } else { x--; } } System.out.println(x); } }
Java
["1\n++X", "2\nX++\n--X"]
1 second
["1", "0"]
null
Java 8
standard input
[ "implementation" ]
f3cf7726739290b280230b562cac7a74
The first line contains a single integer n (1 ≀ n ≀ 150) β€” the number of statements in the programme. Next n lines contain a statement each. Each statement contains exactly one operation (++ or --) and exactly one variable x (denoted as letter Β«XΒ»). Thus, there are no empty statements. The operation and the variable can be written in any order.
800
Print a single integer β€” the final value of x.
standard output
PASSED
96cd7bfd89ff64c01a5cef764e88635c
train_002.jsonl
1363188600
The classic programming language of Bitland is Bit++. This language is so peculiar and complicated.The language is that peculiar as it has exactly one variable, called x. Also, there are two operations: Operation ++ increases the value of variable x by 1. Operation -- decreases the value of variable x by 1. A statement in language Bit++ is a sequence, consisting of exactly one operation and one variable x. The statement is written without spaces, that is, it can only contain characters "+", "-", "X". Executing a statement means applying the operation it contains.A programme in Bit++ is a sequence of statements, each of them needs to be executed. Executing a programme means executing all the statements it contains.You're given a programme in language Bit++. The initial value of x is 0. Execute the programme and find its final value (the value of the variable when this programme is executed).
256 megabytes
import java.util.Scanner; public class A282BitPlusPlus { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = 0; n = in.nextInt(); int ans = 0; for (int i = 0; i < n; i++) { if (in.next().contains("+")) { ans++; } else { ans--; } } System.out.println(ans); } }
Java
["1\n++X", "2\nX++\n--X"]
1 second
["1", "0"]
null
Java 8
standard input
[ "implementation" ]
f3cf7726739290b280230b562cac7a74
The first line contains a single integer n (1 ≀ n ≀ 150) β€” the number of statements in the programme. Next n lines contain a statement each. Each statement contains exactly one operation (++ or --) and exactly one variable x (denoted as letter Β«XΒ»). Thus, there are no empty statements. The operation and the variable can be written in any order.
800
Print a single integer β€” the final value of x.
standard output
PASSED
589563a9ab0556c44b24701ac313d1cd
train_002.jsonl
1363188600
The classic programming language of Bitland is Bit++. This language is so peculiar and complicated.The language is that peculiar as it has exactly one variable, called x. Also, there are two operations: Operation ++ increases the value of variable x by 1. Operation -- decreases the value of variable x by 1. A statement in language Bit++ is a sequence, consisting of exactly one operation and one variable x. The statement is written without spaces, that is, it can only contain characters "+", "-", "X". Executing a statement means applying the operation it contains.A programme in Bit++ is a sequence of statements, each of them needs to be executed. Executing a programme means executing all the statements it contains.You're given a programme in language Bit++. The initial value of x is 0. Execute the programme and find its final value (the value of the variable when this programme is executed).
256 megabytes
import java.util.*; public class help { public static void main(String[] args) { Scanner in = new Scanner(System.in); int num = in.nextInt(); String stm = "",test=""; int var = 0; for (int i = 0; i < num; i++) { stm += in.next(); } for (int i = 0; i < stm.length(); i+=3) { test = stm.substring(i,i+3); if(test.endsWith("++X")) var++; else if(test.endsWith("X++")) var++; else if(test.endsWith("--X")) var--; else var--; }System.out.println(var); }}
Java
["1\n++X", "2\nX++\n--X"]
1 second
["1", "0"]
null
Java 8
standard input
[ "implementation" ]
f3cf7726739290b280230b562cac7a74
The first line contains a single integer n (1 ≀ n ≀ 150) β€” the number of statements in the programme. Next n lines contain a statement each. Each statement contains exactly one operation (++ or --) and exactly one variable x (denoted as letter Β«XΒ»). Thus, there are no empty statements. The operation and the variable can be written in any order.
800
Print a single integer β€” the final value of x.
standard output
PASSED
8bdfd8c9bba84ed78bb42f813de7862c
train_002.jsonl
1363188600
The classic programming language of Bitland is Bit++. This language is so peculiar and complicated.The language is that peculiar as it has exactly one variable, called x. Also, there are two operations: Operation ++ increases the value of variable x by 1. Operation -- decreases the value of variable x by 1. A statement in language Bit++ is a sequence, consisting of exactly one operation and one variable x. The statement is written without spaces, that is, it can only contain characters "+", "-", "X". Executing a statement means applying the operation it contains.A programme in Bit++ is a sequence of statements, each of them needs to be executed. Executing a programme means executing all the statements it contains.You're given a programme in language Bit++. The initial value of x is 0. Execute the programme and find its final value (the value of the variable when this programme is executed).
256 megabytes
import java.util.Scanner; public class Test{ public static void main(String args[]){ Scanner in =new Scanner(System.in); int T=in.nextInt(); String s; int x=0; while(T-->0){ s=in.next(); if((s.charAt(1))=='+') x++; else x--; } System.out.println(x); } }
Java
["1\n++X", "2\nX++\n--X"]
1 second
["1", "0"]
null
Java 8
standard input
[ "implementation" ]
f3cf7726739290b280230b562cac7a74
The first line contains a single integer n (1 ≀ n ≀ 150) β€” the number of statements in the programme. Next n lines contain a statement each. Each statement contains exactly one operation (++ or --) and exactly one variable x (denoted as letter Β«XΒ»). Thus, there are no empty statements. The operation and the variable can be written in any order.
800
Print a single integer β€” the final value of x.
standard output
PASSED
cb2491c1fb4b89c68c5d8def14a15f29
train_002.jsonl
1363188600
The classic programming language of Bitland is Bit++. This language is so peculiar and complicated.The language is that peculiar as it has exactly one variable, called x. Also, there are two operations: Operation ++ increases the value of variable x by 1. Operation -- decreases the value of variable x by 1. A statement in language Bit++ is a sequence, consisting of exactly one operation and one variable x. The statement is written without spaces, that is, it can only contain characters "+", "-", "X". Executing a statement means applying the operation it contains.A programme in Bit++ is a sequence of statements, each of them needs to be executed. Executing a programme means executing all the statements it contains.You're given a programme in language Bit++. The initial value of x is 0. Execute the programme and find its final value (the value of the variable when this programme is executed).
256 megabytes
import java.util.*; public class Bit_pp { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); sc.nextLine(); int x = 0; while(t-- > 0) { String str = sc.nextLine(); if(str.charAt(1) == '+') x++; else x--; } System.out.println(x); sc.close(); } }
Java
["1\n++X", "2\nX++\n--X"]
1 second
["1", "0"]
null
Java 8
standard input
[ "implementation" ]
f3cf7726739290b280230b562cac7a74
The first line contains a single integer n (1 ≀ n ≀ 150) β€” the number of statements in the programme. Next n lines contain a statement each. Each statement contains exactly one operation (++ or --) and exactly one variable x (denoted as letter Β«XΒ»). Thus, there are no empty statements. The operation and the variable can be written in any order.
800
Print a single integer β€” the final value of x.
standard output
PASSED
89646efe397409ce8616b016c901667b
train_002.jsonl
1363188600
The classic programming language of Bitland is Bit++. This language is so peculiar and complicated.The language is that peculiar as it has exactly one variable, called x. Also, there are two operations: Operation ++ increases the value of variable x by 1. Operation -- decreases the value of variable x by 1. A statement in language Bit++ is a sequence, consisting of exactly one operation and one variable x. The statement is written without spaces, that is, it can only contain characters "+", "-", "X". Executing a statement means applying the operation it contains.A programme in Bit++ is a sequence of statements, each of them needs to be executed. Executing a programme means executing all the statements it contains.You're given a programme in language Bit++. The initial value of x is 0. Execute the programme and find its final value (the value of the variable when this programme is executed).
256 megabytes
import java.util.Scanner; public class bit { public static void main(String [ ] args){ Scanner input = new Scanner(System.in); int n = input.nextInt(); String x[] =new String[n] ; int sum =0; for (int i= 0; i <=n-1;i++){ x[i]= input.next(); x[i] = x[i].toLowerCase(); } for (int j = 0; j<=x.length -1 ;j++){ if (x[j].equals("++x") || x[j].equals("x++")){ sum = sum + 1; }else if(x[j].equals("--x")|| x[j].equals("x--")){ sum= sum - 1; } } System.out.println(sum); } }
Java
["1\n++X", "2\nX++\n--X"]
1 second
["1", "0"]
null
Java 8
standard input
[ "implementation" ]
f3cf7726739290b280230b562cac7a74
The first line contains a single integer n (1 ≀ n ≀ 150) β€” the number of statements in the programme. Next n lines contain a statement each. Each statement contains exactly one operation (++ or --) and exactly one variable x (denoted as letter Β«XΒ»). Thus, there are no empty statements. The operation and the variable can be written in any order.
800
Print a single integer β€” the final value of x.
standard output
PASSED
876d664079ec9a017524bc1823b31b48
train_002.jsonl
1363188600
The classic programming language of Bitland is Bit++. This language is so peculiar and complicated.The language is that peculiar as it has exactly one variable, called x. Also, there are two operations: Operation ++ increases the value of variable x by 1. Operation -- decreases the value of variable x by 1. A statement in language Bit++ is a sequence, consisting of exactly one operation and one variable x. The statement is written without spaces, that is, it can only contain characters "+", "-", "X". Executing a statement means applying the operation it contains.A programme in Bit++ is a sequence of statements, each of them needs to be executed. Executing a programme means executing all the statements it contains.You're given a programme in language Bit++. The initial value of x is 0. Execute the programme and find its final value (the value of the variable when this programme is executed).
256 megabytes
import java.util.Scanner; public class Bit{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int lines = sc.nextInt(); int num = 0; sc.nextLine(); for(int i = 0; i < lines; i++){ String k = sc.nextLine(); if(k.charAt(0) == 'X'){ if(k.substring(1).equals("--")){ num--; } else{ num++; } } else{ if(k.substring(0,2).equals("--")){ num--; } else num++; } } System.out.print(num); } }
Java
["1\n++X", "2\nX++\n--X"]
1 second
["1", "0"]
null
Java 8
standard input
[ "implementation" ]
f3cf7726739290b280230b562cac7a74
The first line contains a single integer n (1 ≀ n ≀ 150) β€” the number of statements in the programme. Next n lines contain a statement each. Each statement contains exactly one operation (++ or --) and exactly one variable x (denoted as letter Β«XΒ»). Thus, there are no empty statements. The operation and the variable can be written in any order.
800
Print a single integer β€” the final value of x.
standard output
PASSED
455652937a1ba8b359bd55dbe83eaa6c
train_002.jsonl
1363188600
The classic programming language of Bitland is Bit++. This language is so peculiar and complicated.The language is that peculiar as it has exactly one variable, called x. Also, there are two operations: Operation ++ increases the value of variable x by 1. Operation -- decreases the value of variable x by 1. A statement in language Bit++ is a sequence, consisting of exactly one operation and one variable x. The statement is written without spaces, that is, it can only contain characters "+", "-", "X". Executing a statement means applying the operation it contains.A programme in Bit++ is a sequence of statements, each of them needs to be executed. Executing a programme means executing all the statements it contains.You're given a programme in language Bit++. The initial value of x is 0. Execute the programme and find its final value (the value of the variable when this programme is executed).
256 megabytes
import java.util.Scanner; public class first{ public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int s = 0; String a = "++X"; String b = "X++"; String str[] = new String[n]; for(int i = 0; i < n; i++) { String j = sc.next(); str[i] = j; } for(int i = 0; i < n; i++) { if(str[i].equals(a) || str[i].equals(b)) s++; else s--; } System.out.println(s); } }
Java
["1\n++X", "2\nX++\n--X"]
1 second
["1", "0"]
null
Java 8
standard input
[ "implementation" ]
f3cf7726739290b280230b562cac7a74
The first line contains a single integer n (1 ≀ n ≀ 150) β€” the number of statements in the programme. Next n lines contain a statement each. Each statement contains exactly one operation (++ or --) and exactly one variable x (denoted as letter Β«XΒ»). Thus, there are no empty statements. The operation and the variable can be written in any order.
800
Print a single integer β€” the final value of x.
standard output
PASSED
be0ee8fd8241f7746c2619271f128f46
train_002.jsonl
1363188600
The classic programming language of Bitland is Bit++. This language is so peculiar and complicated.The language is that peculiar as it has exactly one variable, called x. Also, there are two operations: Operation ++ increases the value of variable x by 1. Operation -- decreases the value of variable x by 1. A statement in language Bit++ is a sequence, consisting of exactly one operation and one variable x. The statement is written without spaces, that is, it can only contain characters "+", "-", "X". Executing a statement means applying the operation it contains.A programme in Bit++ is a sequence of statements, each of them needs to be executed. Executing a programme means executing all the statements it contains.You're given a programme in language Bit++. The initial value of x is 0. Execute the programme and find its final value (the value of the variable when this programme is executed).
256 megabytes
/*import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner milan = new Scanner(System.in); int n=milan.nextInt(); int y=0; for(int i=0;i<n;i++){ String s=milan.nextLine(); if(s.charAt(1)=='+'){ ++y; } else{ --y; } } System.out.println(y); } }*/ import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); scanner.nextLine(); int y=0; for(int i=0; i<n ; i++){ String s; s= scanner.nextLine(); if(s.charAt(1)=='+') y++; else y--; } System.out.println(y); } }
Java
["1\n++X", "2\nX++\n--X"]
1 second
["1", "0"]
null
Java 8
standard input
[ "implementation" ]
f3cf7726739290b280230b562cac7a74
The first line contains a single integer n (1 ≀ n ≀ 150) β€” the number of statements in the programme. Next n lines contain a statement each. Each statement contains exactly one operation (++ or --) and exactly one variable x (denoted as letter Β«XΒ»). Thus, there are no empty statements. The operation and the variable can be written in any order.
800
Print a single integer β€” the final value of x.
standard output
PASSED
21689e11d96387601a4608978559887b
train_002.jsonl
1363188600
The classic programming language of Bitland is Bit++. This language is so peculiar and complicated.The language is that peculiar as it has exactly one variable, called x. Also, there are two operations: Operation ++ increases the value of variable x by 1. Operation -- decreases the value of variable x by 1. A statement in language Bit++ is a sequence, consisting of exactly one operation and one variable x. The statement is written without spaces, that is, it can only contain characters "+", "-", "X". Executing a statement means applying the operation it contains.A programme in Bit++ is a sequence of statements, each of them needs to be executed. Executing a programme means executing all the statements it contains.You're given a programme in language Bit++. The initial value of x is 0. Execute the programme and find its final value (the value of the variable when this programme is executed).
256 megabytes
import java.util.Scanner; public class milann{ public static void main(String[] args){ Scanner milan = new Scanner(System.in); int n=milan.nextInt(); milan.nextLine(); int ans=0; for(int i=0;i<n;i++){ String s; s=milan.nextLine(); if(s.charAt(1)!='+'){ --ans; } else{ ++ans; } } System.out.println(ans); } }
Java
["1\n++X", "2\nX++\n--X"]
1 second
["1", "0"]
null
Java 8
standard input
[ "implementation" ]
f3cf7726739290b280230b562cac7a74
The first line contains a single integer n (1 ≀ n ≀ 150) β€” the number of statements in the programme. Next n lines contain a statement each. Each statement contains exactly one operation (++ or --) and exactly one variable x (denoted as letter Β«XΒ»). Thus, there are no empty statements. The operation and the variable can be written in any order.
800
Print a single integer β€” the final value of x.
standard output
PASSED
afad856e92fd38ac25e70e5892aa7950
train_002.jsonl
1363188600
The classic programming language of Bitland is Bit++. This language is so peculiar and complicated.The language is that peculiar as it has exactly one variable, called x. Also, there are two operations: Operation ++ increases the value of variable x by 1. Operation -- decreases the value of variable x by 1. A statement in language Bit++ is a sequence, consisting of exactly one operation and one variable x. The statement is written without spaces, that is, it can only contain characters "+", "-", "X". Executing a statement means applying the operation it contains.A programme in Bit++ is a sequence of statements, each of them needs to be executed. Executing a programme means executing all the statements it contains.You're given a programme in language Bit++. The initial value of x is 0. Execute the programme and find its final value (the value of the variable when this programme is executed).
256 megabytes
import java.util.*; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int x = 0; int input = sc.nextInt(); String s = ""; for (int i = 0; i < input; i ++) { s = sc.next(); for (int j = 0; j < input; j++) { if (s.charAt(0) == '+') { x++; break; } else if (s.charAt(0) == '-') { x--; break; } else if (s.charAt(1) == '+') { x++; break; } else if (s.charAt(1) == '-') { x--; break; } } } System.out.println(x); } }
Java
["1\n++X", "2\nX++\n--X"]
1 second
["1", "0"]
null
Java 8
standard input
[ "implementation" ]
f3cf7726739290b280230b562cac7a74
The first line contains a single integer n (1 ≀ n ≀ 150) β€” the number of statements in the programme. Next n lines contain a statement each. Each statement contains exactly one operation (++ or --) and exactly one variable x (denoted as letter Β«XΒ»). Thus, there are no empty statements. The operation and the variable can be written in any order.
800
Print a single integer β€” the final value of x.
standard output
PASSED
7dc8fe9f05a586938bb5487439b95fdb
train_002.jsonl
1363188600
The classic programming language of Bitland is Bit++. This language is so peculiar and complicated.The language is that peculiar as it has exactly one variable, called x. Also, there are two operations: Operation ++ increases the value of variable x by 1. Operation -- decreases the value of variable x by 1. A statement in language Bit++ is a sequence, consisting of exactly one operation and one variable x. The statement is written without spaces, that is, it can only contain characters "+", "-", "X". Executing a statement means applying the operation it contains.A programme in Bit++ is a sequence of statements, each of them needs to be executed. Executing a programme means executing all the statements it contains.You're given a programme in language Bit++. The initial value of x is 0. Execute the programme and find its final value (the value of the variable when this programme is executed).
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner input = new Scanner(System.in); int n,x =0; n = input.nextInt(); String[] str = new String [n]; input.nextLine (); for(int i = 0;i<n;i++){ str[i] = input.nextLine (); switch(str[i]){ case "X++": x = x + 1; break; case "++X": x = x + 1; break; case "X--": x = x - 1; break; case "--X": x = x -1; break; } } System.out.print(x); } }
Java
["1\n++X", "2\nX++\n--X"]
1 second
["1", "0"]
null
Java 8
standard input
[ "implementation" ]
f3cf7726739290b280230b562cac7a74
The first line contains a single integer n (1 ≀ n ≀ 150) β€” the number of statements in the programme. Next n lines contain a statement each. Each statement contains exactly one operation (++ or --) and exactly one variable x (denoted as letter Β«XΒ»). Thus, there are no empty statements. The operation and the variable can be written in any order.
800
Print a single integer β€” the final value of x.
standard output
PASSED
5536cab8bf6134d5329d92ae815b0be8
train_002.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≀ i ≀ n - 1) on every floor x, where a ≀ x ≀ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); long n, h, k, a, b, ta, fa, tb, fb, result=-1, min=0; n = in.nextLong(); h = in.nextLong(); a = in.nextLong(); b = in.nextLong(); k = in.nextLong(); for (int i = 0; i < k; i++) { ta = in.nextLong(); fa = in.nextLong(); tb = in.nextLong(); fb = in.nextLong(); if(ta == tb) { result = Math.abs(fa-fb); } else if (fa>=a && fa <=b) { result = Math.abs(ta-tb) + Math.abs(fa-fb); } else { if(fa == fb) { result = Math.abs(ta-tb) + 2*Math.min( Math.abs(fa-a),Math.abs(fa-b) ); } else { min = Math.min( Math.abs(fa-a),Math.abs(fa-b) ); if(min == Math.abs(fa-a)) { result = min + Math.abs(ta-tb) + Math.abs(a-fb); } else { result = min + Math.abs(ta-tb) + Math.abs(b-fb); } } } System.out.println(result); } } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≀ n ≀ 108), h: the number of floors in each tower (1 ≀ h ≀ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≀ a ≀ b ≀ h), k: total number of queries (1 ≀ k ≀ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≀ ta, tb ≀ n, 1 ≀ fa, fb ≀ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
0625caf6229827c784c7aebb6daa743c
train_002.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≀ i ≀ n - 1) on every floor x, where a ≀ x ≀ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int h = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); int k = sc.nextInt(); int[] result = new int[k]; for (int i = 0; i < k; i++) { int t1 = sc.nextInt(); int f1 = sc.nextInt(); int t2 = sc.nextInt(); int f2 = sc.nextInt(); if (t1 == t2) { result[i] = Math.abs(f2 - f1); continue; } int firstTowerMove = 0; if (f1 > b) { firstTowerMove = f1 - b; f1 = b; } else if (f1 < a) { firstTowerMove = a - f1; f1 = a; } int transition = Math.abs(t2 - t1); int secondTowerMove = Math.abs(f1 - f2); result[i] = firstTowerMove + transition + secondTowerMove; } for (int i = 0; i < k; i++) { System.out.println(result[i]); } } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≀ n ≀ 108), h: the number of floors in each tower (1 ≀ h ≀ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≀ a ≀ b ≀ h), k: total number of queries (1 ≀ k ≀ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≀ ta, tb ≀ n, 1 ≀ fa, fb ≀ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
f0c0ccdc16cefbf1a0dd42b826e81960
train_002.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≀ i ≀ n - 1) on every floor x, where a ≀ x ≀ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
import java.util.Scanner; public class main { public static void main(String args[]) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int h = in.nextInt(); int a = in.nextInt(); int b = in.nextInt(); int k = in.nextInt(); while(k-->0) { int ta = in.nextInt(); int fa = in.nextInt(); int tb = in.nextInt(); int fb = in.nextInt(); if(ta==tb) System.out.println(Math.abs(fa-fb)); else { int ans = Math.abs(ta-tb); int f1 = Math.min(fa,fb); int f2 = Math.max(fa,fb); if(f1<=a && f2>=b) ans+=f2-f1; else if(f1<=a && f2<=a) ans+=(a-f1)+(a-f2); else if(f1>=b && f2>=b) ans+=(f1-b)+(f2-b); else { ans += f2-f1; } System.out.println(ans); } } in.close(); } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≀ n ≀ 108), h: the number of floors in each tower (1 ≀ h ≀ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≀ a ≀ b ≀ h), k: total number of queries (1 ≀ k ≀ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≀ ta, tb ≀ n, 1 ≀ fa, fb ≀ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
bbf6ac62d3803e84a69411cc42c437e0
train_002.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≀ i ≀ n - 1) on every floor x, where a ≀ x ≀ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
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 prakhar897 */ 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(); } static class TaskB { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int h = in.nextInt(); int a = in.nextInt(); int b = in.nextInt(); int k = in.nextInt(); int i; long ans = 0; for (i = 0; i < k; i++) { int ta = in.nextInt(); int fa = in.nextInt(); int tb = in.nextInt(); int fb = in.nextInt(); ans = 0; if (ta == tb) { ans = Math.abs(fa - fb); out.println(ans); continue; } ans += Math.abs(tb - ta); if (fa > b) { ans += fa - b; fa = b; } else if (fa < a) { ans += a - fa; fa = a; } ans += Math.abs(fa - fb); out.println(ans); } } } 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(long i) { writer.println(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 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≀ n ≀ 108), h: the number of floors in each tower (1 ≀ h ≀ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≀ a ≀ b ≀ h), k: total number of queries (1 ≀ k ≀ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≀ ta, tb ≀ n, 1 ≀ fa, fb ≀ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
650c30f02a1832cc10e598d339fb9242
train_002.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≀ i ≀ n - 1) on every floor x, where a ≀ x ≀ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
import java.util.Scanner; public class New_Building_For_SIS { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int h = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); int k = sc.nextInt(); int ausgabe[] = new int[k]; for (int i = 0; i < k; i++) { boolean haus = false; int zΓ€hler = 0; int turm1 = sc.nextInt(); int floor1 = sc.nextInt(); int turm2 = sc.nextInt(); int floor2 = sc.nextInt(); if(turm1!=turm2) {zΓ€hler += Math.abs(turm1 - turm2); if (floor1 < a) { zΓ€hler += a - floor1; floor1 = a; } else if (floor1 > b) { zΓ€hler += floor1 - b; floor1 = b; } } if(floor1!=floor2)zΓ€hler += Math.abs(floor1 - floor2); ausgabe[i] = zΓ€hler; } for (int i = 0; i < k; i++) { System.out.println(ausgabe[i]); } } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≀ n ≀ 108), h: the number of floors in each tower (1 ≀ h ≀ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≀ a ≀ b ≀ h), k: total number of queries (1 ≀ k ≀ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≀ ta, tb ≀ n, 1 ≀ fa, fb ≀ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output