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
388f804995ec2a9563a2217d4ffdd498
train_001.jsonl
1327215600
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya has sequence a consisting of n integers.The subsequence of the sequence a is such subsequence that can be obtained from a by removing zero or more of its elements.Two sequences are considered different if index sets of numbers included in them are different. That is, the values ​of the elements ​do not matter in the comparison of subsequences. In particular, any sequence of length n has exactly 2n different subsequences (including an empty subsequence).A subsequence is considered lucky if it has a length exactly k and does not contain two identical lucky numbers (unlucky numbers can be repeated any number of times).Help Petya find the number of different lucky subsequences of the sequence a. As Petya's parents don't let him play with large numbers, you should print the result modulo prime number 1000000007 (109 + 7).
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.Arrays; import java.util.HashMap; import java.util.LinkedList; import java.util.Queue; public class E { static int[] L; static long[] Fac; static long[] InvMod; static long mod = 1000000007; static long[][] DP; static int c = 0; public static long get(int index, int left) { if (left == 0) return 1; if (index == c) return 0; if (DP[index][left] != -1) return DP[index][left]; return DP[index][left] = (get(index + 1, left) + L[index] * get(index + 1, left - 1)) % mod; } public static long C(int x, int y) { if (y > x) return 0; long ans = Fac[x]; ans = (ans * InvMod[y]) % mod; ans = (ans * InvMod[x - y]) % mod; return ans; } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String[] S = in.readLine().split(" "); int n = Integer.parseInt(S[0]); int k = Integer.parseInt(S[1]); Fac = new long[n + 1]; InvMod = new long[n + 1]; Fac[0] = 1; InvMod[0] = 1; for (int i = 1; i <= n; i++) { Fac[i] = (Fac[i - 1] * i) % mod; InvMod[i] = (new BigInteger("" + Fac[i])).modPow( BigInteger.valueOf(mod - 2), BigInteger.valueOf(mod)) .longValue(); } Queue<Long> Q = new LinkedList<Long>() { { add(4l); add(7l); } }; HashMap<Integer, Integer> H = new HashMap<Integer, Integer>(); while (true) { long temp = Q.poll(); if (temp > 1e9) break; H.put((int) temp, c++); Q.add(10 * temp + 4); Q.add(10 * temp + 7); } L = new int[c]; S = in.readLine().split(" "); int left = 0; for (int i = 0; i < n; i++) if (H.containsKey(Integer.parseInt(S[i]))) L[H.get(Integer.parseInt(S[i]))]++; else left++; DP = new long[c][c + 1]; for (int i = 0; i < c; i++) Arrays.fill(DP[i], -1); long ans = 0; for (int i = 0; i <= Math.min(c, k); i++) { ans = (ans + get(0, i) * C(left, k - i)) % mod; } System.out.println(ans); } }
Java
["3 2\n10 10 10", "4 2\n4 4 7 7"]
2 seconds
["3", "4"]
NoteIn the first sample all 3 subsequences of the needed length are considered lucky.In the second sample there are 4 lucky subsequences. For them the sets of indexes equal (the indexation starts from 1): {1, 3}, {1, 4}, {2, 3} and {2, 4}.
Java 6
standard input
[ "dp", "combinatorics" ]
c421f47149e70240a02903d9d47de429
The first line contains two integers n and k (1 ≀ k ≀ n ≀ 105). The next line contains n integers ai (1 ≀ ai ≀ 109) β€” the sequence a.
2,100
On the single line print the single number β€” the answer to the problem modulo prime number 1000000007 (109 + 7).
standard output
PASSED
8915cff129f0fd683249ca65c137a30f
train_001.jsonl
1327215600
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya has sequence a consisting of n integers.The subsequence of the sequence a is such subsequence that can be obtained from a by removing zero or more of its elements.Two sequences are considered different if index sets of numbers included in them are different. That is, the values ​of the elements ​do not matter in the comparison of subsequences. In particular, any sequence of length n has exactly 2n different subsequences (including an empty subsequence).A subsequence is considered lucky if it has a length exactly k and does not contain two identical lucky numbers (unlucky numbers can be repeated any number of times).Help Petya find the number of different lucky subsequences of the sequence a. As Petya's parents don't let him play with large numbers, you should print the result modulo prime number 1000000007 (109 + 7).
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.math.BigDecimal; import java.math.BigInteger; import java.util.HashMap; /** * Works good for CF * * @author cykeltillsalu */ public class C104 { // some local config static boolean test = false; static String testDataFile = "testdata.txt"; static String feedFile = "feed.txt"; CompetitionType type = CompetitionType.CF; private static String ENDL = "\n"; int mod = 1000000007; // solution private void solve() throws Throwable { int unlucky = 0; int lucky = 0; HashMap<String, Integer> luckyCnt = new HashMap<String, Integer>(); int n = iread(), k = iread(); out:for (int i = 0; i < n; i++) { String wread = wread(); for (int j = 0; j < wread.length(); j++) { char c = wread.charAt(j); if( c != '7' && c != '4'){ unlucky ++; continue out; } } lucky ++; Integer integer = luckyCnt.get(wread); if(integer == null){ integer = 0; } luckyCnt.put(wread, integer + 1); } int size = luckyCnt.keySet().size(); int[] cnt = new int[size+1]; int pos = 1; for (String s : luckyCnt.keySet()) { cnt[pos] = luckyCnt.get(s); pos ++; } long[][] dp = new long[size+1][size+1]; for (int i = 0; i < dp.length; i++) { dp[i][0] = 1; } for (int p = 1; p < dp.length; p++) { for (int c = 1; c < dp.length; c++) { dp[p][c] += dp[p-1][c]; dp[p][c] += dp[p-1][c-1] * cnt[p]; dp[p][c] %= mod; } } long[] factorial = new long[100001]; long[] factorialInverse = new long[100001]; long val = 1; for (int i = 1; i <= 100000; i++) { val *= i; val %= mod; int inverse = BigInteger.valueOf(val).modInverse(BigInteger.valueOf(mod)).intValue(); factorial[i] = (int) val; factorial[i] %= mod; factorialInverse[i] = inverse; factorialInverse[i] %= mod; } n -= lucky; lucky = luckyCnt.keySet().size(); long res = 0; for (int i = 0; i <= k; i++) { //get binom from both: int uk = k - i; long fromLucky =1 ; long fromUnlucky = 1; if((uk != 0 && unlucky < uk) || (i != 0 && lucky < i)){ continue; } if(i > 0){ fromLucky = (dp[size][i] % mod); } if(uk > 0 && uk < n){ fromUnlucky = ((((factorial[n] * factorialInverse[n-uk]) % mod) * factorialInverse[uk] )% mod); fromUnlucky %= mod; } res += (fromLucky * fromUnlucky) % mod; } res %= mod; out.write(res + ENDL); out.flush(); } public int iread() throws Exception { return Integer.parseInt(wread()); } public double dread() throws Exception { return Double.parseDouble(wread()); } public long lread() throws Exception { return Long.parseLong(wread()); } public String wread() throws IOException { StringBuilder b = new StringBuilder(); int c; c = in.read(); while (c >= 0 && c <= ' ') c = in.read(); if (c < 0) return ""; while (c > ' ') { b.append((char) c); c = in.read(); } return b.toString(); } public static void main(String[] args) throws Throwable { if (test) { // run all cases from testfile: BufferedReader testdataReader = new BufferedReader(new FileReader(testDataFile)); String readLine = testdataReader.readLine(); int casenr = 0; out: while (true) { BufferedWriter w = new BufferedWriter(new FileWriter(feedFile)); if (!readLine.equals("input")) { break; } while (true) { readLine = testdataReader.readLine(); if (readLine.equals("output")) { break; } w.write(readLine + "\n"); } w.close(); System.out.println("Answer on case " + (++casenr) + ": "); new C104().solve(); System.out.println("Expected answer: "); while (true) { readLine = testdataReader.readLine(); if (readLine == null) { break out; } if (readLine.equals("input")) { break; } System.out.println(readLine); } System.out.println("----------------"); } testdataReader.close(); } else { // run on server new C104().solve(); } out.close(); } public C104() throws Throwable { if (test) { in = new BufferedReader(new FileReader(new File(feedFile))); } } InputStreamReader inp = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(inp); static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); enum CompetitionType { CF, OTHER }; }
Java
["3 2\n10 10 10", "4 2\n4 4 7 7"]
2 seconds
["3", "4"]
NoteIn the first sample all 3 subsequences of the needed length are considered lucky.In the second sample there are 4 lucky subsequences. For them the sets of indexes equal (the indexation starts from 1): {1, 3}, {1, 4}, {2, 3} and {2, 4}.
Java 6
standard input
[ "dp", "combinatorics" ]
c421f47149e70240a02903d9d47de429
The first line contains two integers n and k (1 ≀ k ≀ n ≀ 105). The next line contains n integers ai (1 ≀ ai ≀ 109) β€” the sequence a.
2,100
On the single line print the single number β€” the answer to the problem modulo prime number 1000000007 (109 + 7).
standard output
PASSED
ebb490b49cf8173fc046cdb08ff23a26
train_001.jsonl
1327215600
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya has sequence a consisting of n integers.The subsequence of the sequence a is such subsequence that can be obtained from a by removing zero or more of its elements.Two sequences are considered different if index sets of numbers included in them are different. That is, the values ​of the elements ​do not matter in the comparison of subsequences. In particular, any sequence of length n has exactly 2n different subsequences (including an empty subsequence).A subsequence is considered lucky if it has a length exactly k and does not contain two identical lucky numbers (unlucky numbers can be repeated any number of times).Help Petya find the number of different lucky subsequences of the sequence a. As Petya's parents don't let him play with large numbers, you should print the result modulo prime number 1000000007 (109 + 7).
256 megabytes
import java.util.*; public class E { private static Scanner sc = new Scanner(System.in); private static final int P = 1000000007; /** * @param args */ public static void main(String[] args) { int n = sc.nextInt(); int k = sc.nextInt(); int[] xs = new int[n]; for (int i = 0; i < n; i++) { xs[i] = sc.nextInt(); } int unluckyCount = 0; int luckySetCount = 0; Map<Integer, Integer> luckyMap = new HashMap<Integer, Integer>(); for (int i = 0; i < n; i++) { if (isLucky(xs[i])) { if (!luckyMap.containsKey(xs[i])) { luckyMap.put(xs[i], 1); } else { luckyMap.put(xs[i], luckyMap.get(xs[i]) + 1); } } else { unluckyCount++; } } luckySetCount = luckyMap.size(); int[] luckySetSizes = toPrimitiveArray(luckyMap.values().toArray(new Integer[0])); long[][] dp = new long[luckySetCount + 1][luckySetCount + 1]; dp[0][0] = 1; for (int i = 0; i < luckySetCount; i++) { for (int j = 0; j <= luckySetCount; j++) { dp[i + 1][j] += dp[i][j]; dp[i + 1][j] = regularizeMod(dp[i + 1][j], P); if (j + 1 <= luckySetCount) { dp[i + 1][j + 1] += dp[i][j] * luckySetSizes[i]; dp[i + 1][j + 1] = regularizeMod(dp[i + 1][j + 1], P); } } } long ans = 0; long[] facts = new long[unluckyCount + 1]; facts[0] = 1; for (int i = 1; i <= unluckyCount; i++) { facts[i] = (facts[i - 1] * i) % P; } for (int i = 0; i <= luckySetCount; i++) { int left = k - i; if (left < 0 || unluckyCount < left) { continue; } ans += ((long) dp[luckySetCount][i]) * combinationMod(unluckyCount, left, P, facts) % P; ans = regularizeMod(ans, P); } System.out.println(ans); } private static long combinationMod(long n, long r, int m, long[] facts) { long x = facts[(int) n]; x = regularizeMod(x * inverseMod(facts[(int) r], m), m); x = regularizeMod(x * inverseMod(facts[(int) (n - r)], m), m); return x; } private static long factorialMod(long x, int m) { long ans = 1; while (x > 0) { ans *= x; ans %= m; x--; } return ans; } private static boolean isLucky(int x) { String string = String.valueOf(x); for (char c : string.toCharArray()) { if (c != '4' && c != '7') { return false; } } return true; } private static int[] toPrimitiveArray(Integer[] integers) { int length = integers.length; int[] primitiveIntegers = new int[length]; for (int i = 0; i < length; i++) { primitiveIntegers[i] = integers[i]; } return primitiveIntegers; } static long[] exgcd(long a, long b) { long x0 = 1, y0 = 0, z0 = a; long x1 = 0, y1 = 1, z1 = b; while (true) { if (z1 == 0) { return new long[] { x0, y0, z0 }; } long q = z0 / z1; long x2 = x0 - q * x1; long y2 = y0 - q * y1; long z2 = z0 - q * z1; x0 = x1; x1 = x2; y0 = y1; y1 = y2; z0 = z1; z1 = z2; } } static long inverseMod(long a, int m) { long[] xyr = exgcd(a, m); long x = xyr[0]; long r = xyr[2]; if (r == 1L) { return regularizeMod(x, m); } else { return 0L; } } static long regularizeMod(long a, int m) { a %= m; while (a < 0L) { a += m; } while (a >= m) { a -= m; } return a; } }
Java
["3 2\n10 10 10", "4 2\n4 4 7 7"]
2 seconds
["3", "4"]
NoteIn the first sample all 3 subsequences of the needed length are considered lucky.In the second sample there are 4 lucky subsequences. For them the sets of indexes equal (the indexation starts from 1): {1, 3}, {1, 4}, {2, 3} and {2, 4}.
Java 6
standard input
[ "dp", "combinatorics" ]
c421f47149e70240a02903d9d47de429
The first line contains two integers n and k (1 ≀ k ≀ n ≀ 105). The next line contains n integers ai (1 ≀ ai ≀ 109) β€” the sequence a.
2,100
On the single line print the single number β€” the answer to the problem modulo prime number 1000000007 (109 + 7).
standard output
PASSED
a7903373d47a01203dfbd7579f65d5f0
train_001.jsonl
1327215600
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya has sequence a consisting of n integers.The subsequence of the sequence a is such subsequence that can be obtained from a by removing zero or more of its elements.Two sequences are considered different if index sets of numbers included in them are different. That is, the values ​of the elements ​do not matter in the comparison of subsequences. In particular, any sequence of length n has exactly 2n different subsequences (including an empty subsequence).A subsequence is considered lucky if it has a length exactly k and does not contain two identical lucky numbers (unlucky numbers can be repeated any number of times).Help Petya find the number of different lucky subsequences of the sequence a. As Petya's parents don't let him play with large numbers, you should print the result modulo prime number 1000000007 (109 + 7).
256 megabytes
import java.util.*; public class E { private static Scanner sc = new Scanner(System.in); private static final int P = 1000000007; /** * @param args */ public static void main(String[] args) { int n = sc.nextInt(); int k = sc.nextInt(); int[] xs = new int[n]; for (int i = 0; i < n; i++) { xs[i] = sc.nextInt(); } int unluckyCount = 0; int luckySetCount = 0; Map<Integer, Integer> luckyMap = new HashMap<Integer, Integer>(); for (int i = 0; i < n; i++) { if (isLucky(xs[i])) { if (!luckyMap.containsKey(xs[i])) { luckyMap.put(xs[i], 1); } else { luckyMap.put(xs[i], luckyMap.get(xs[i]) + 1); } } else { unluckyCount++; } } luckySetCount = luckyMap.size(); int[] luckySetSizes = toPrimitiveArray(luckyMap.values().toArray(new Integer[0])); FiniteField[][] dp = new FiniteField[luckySetCount + 1][luckySetCount + 1]; for (int i = 0; i <= luckySetCount; i++) { for (int j = 0; j <= luckySetCount; j++) { dp[i][j] = new FiniteField(0, P); } } dp[0][0] = new FiniteField(1, P); for (int i = 0; i < luckySetCount; i++) { for (int j = 0; j <= luckySetCount; j++) { dp[i + 1][j] = dp[i + 1][j].add(dp[i][j]); if (j + 1 <= luckySetCount) { dp[i + 1][j + 1] = dp[i + 1][j + 1].add( dp[i][j].multiply(new FiniteField(luckySetSizes[i], P))); } } } FiniteField ans = new FiniteField(0, P); FiniteField[] facts = new FiniteField[unluckyCount + 1]; facts[0] = new FiniteField(1, P); for (int i = 1; i <= unluckyCount; i++) { facts[i] = facts[i - 1].multiply(new FiniteField(i, P)); } for (int i = 0; i <= luckySetCount; i++) { int left = k - i; if (left < 0 || unluckyCount - left < 0) { continue; } ans = ans.add( dp[luckySetCount][i] .multiply(facts[unluckyCount]) .divide(facts[left]) .divide(facts[unluckyCount - left])); } System.out.println(ans.getValue()); } private static boolean isLucky(int x) { String string = String.valueOf(x); for (char c : string.toCharArray()) { if (c != '4' && c != '7') { return false; } } return true; } private static int[] toPrimitiveArray(Integer[] integers) { int length = integers.length; int[] primitiveIntegers = new int[length]; for (int i = 0; i < length; i++) { primitiveIntegers[i] = integers[i]; } return primitiveIntegers; } } class FiniteField { private final long x; private final int p; public FiniteField(long x, int p) { x = x % p; if (x < 0) { x += p; } this.x = x; this.p = p; } public int getValue() { return (int)x; } private void check(FiniteField other) { if (p != other.p) { throw new RuntimeException(); } } public FiniteField add(FiniteField other) { check(other); return new FiniteField(x + other.x, p); } public FiniteField subtract(FiniteField other) { check(other); return new FiniteField(x - other.x, p); } public FiniteField multiply(FiniteField other) { check(other); return new FiniteField(x * other.x, p); } public FiniteField divide(FiniteField other) { check(other); return this.multiply(other.inverse()); } private static long[] exgcd(long a, long b) { long x0 = 1, y0 = 0, z0 = a; long x1 = 0, y1 = 1, z1 = b; while (true) { if (z1 == 0) { return new long[] { x0, y0, z0 }; } long q = z0 / z1; long x2 = x0 - q * x1; long y2 = y0 - q * y1; long z2 = z0 - q * z1; x0 = x1; x1 = x2; y0 = y1; y1 = y2; z0 = z1; z1 = z2; } } public FiniteField inverse() { long[] ixyr = exgcd(x, p); long ix = ixyr[0]; long ir = ixyr[2]; if (ir == 1L) { return new FiniteField(ix, p); } else { throw new RuntimeException(); } } } //class ModOperator { // private final int p; // // public ModOperator(int p) { // this.p = p; // } // // public long add(long x, long y) { // x = regularize(x); // y = regularize(y); // return (x + y) % p; // } // // public long subtract(long x, long y) { // x = regularize(x); // y = regularize(y); // return (x + p - y) % p; // } // // public long multiply(long x, long y) { // x = regularize(x); // y = regularize(y); // return (x * y) % p; // } // // public long divide(long x, long y) { // x = regularize(x); // y = regularize(y); // return (x * inverse(y)) % p; // } // // public long regularize(long x) { // x %= p; // if (x < 0) { // x += p; // } // return x; // } // // private static long[] exgcd(long a, long b) { // long x0 = 1, y0 = 0, z0 = a; // long x1 = 0, y1 = 1, z1 = b; // while (true) { // if (z1 == 0) { // return new long[] { x0, y0, z0 }; // } // long q = z0 / z1; // long x2 = x0 - q * x1; // long y2 = y0 - q * y1; // long z2 = z0 - q * z1; // x0 = x1; // x1 = x2; // y0 = y1; // y1 = y2; // z0 = z1; // z1 = z2; // } // } // // public long inverse(long a) { // a = regularize(a); // long[] xyr = exgcd(a, p); // long x = xyr[0]; // long r = xyr[2]; // if (r == 1L) { // return regularize(x); // } else { // throw new RuntimeException(); // } // } //}
Java
["3 2\n10 10 10", "4 2\n4 4 7 7"]
2 seconds
["3", "4"]
NoteIn the first sample all 3 subsequences of the needed length are considered lucky.In the second sample there are 4 lucky subsequences. For them the sets of indexes equal (the indexation starts from 1): {1, 3}, {1, 4}, {2, 3} and {2, 4}.
Java 6
standard input
[ "dp", "combinatorics" ]
c421f47149e70240a02903d9d47de429
The first line contains two integers n and k (1 ≀ k ≀ n ≀ 105). The next line contains n integers ai (1 ≀ ai ≀ 109) β€” the sequence a.
2,100
On the single line print the single number β€” the answer to the problem modulo prime number 1000000007 (109 + 7).
standard output
PASSED
fe41299e406d640317761d0b71db44a4
train_001.jsonl
1327215600
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya has sequence a consisting of n integers.The subsequence of the sequence a is such subsequence that can be obtained from a by removing zero or more of its elements.Two sequences are considered different if index sets of numbers included in them are different. That is, the values ​of the elements ​do not matter in the comparison of subsequences. In particular, any sequence of length n has exactly 2n different subsequences (including an empty subsequence).A subsequence is considered lucky if it has a length exactly k and does not contain two identical lucky numbers (unlucky numbers can be repeated any number of times).Help Petya find the number of different lucky subsequences of the sequence a. As Petya's parents don't let him play with large numbers, you should print the result modulo prime number 1000000007 (109 + 7).
256 megabytes
import java.util.*; public class E { private static Scanner sc = new Scanner(System.in); private static final int P = 1000000007; /** * @param args */ public static void main(String[] args) { int n = sc.nextInt(); int k = sc.nextInt(); int[] xs = new int[n]; for (int i = 0; i < n; i++) { xs[i] = sc.nextInt(); } int unluckyCount = 0; int luckySetCount = 0; Map<Integer, Integer> luckyMap = new HashMap<Integer, Integer>(); for (int i = 0; i < n; i++) { if (isLucky(xs[i])) { if (!luckyMap.containsKey(xs[i])) { luckyMap.put(xs[i], 1); } else { luckyMap.put(xs[i], luckyMap.get(xs[i]) + 1); } } else { unluckyCount++; } } luckySetCount = luckyMap.size(); int[] luckySetSizes = toPrimitiveArray(luckyMap.values().toArray(new Integer[0])); long[][] dp = new long[luckySetCount + 1][luckySetCount + 1]; dp[0][0] = 1; ModOperator modOp = new ModOperator(P); for (int i = 0; i < luckySetCount; i++) { for (int j = 0; j <= luckySetCount; j++) { dp[i + 1][j] = modOp.add(dp[i + 1][j], dp[i][j]); if (j + 1 <= luckySetCount) { dp[i + 1][j + 1] = modOp.add(dp[i + 1][j + 1], modOp.multiply(dp[i][j], luckySetSizes[i])); } } } long ans = 0; long[] facts = new long[unluckyCount + 1]; facts[0] = 1; for (int i = 1; i <= unluckyCount; i++) { facts[i] = modOp.multiply(facts[i - 1], i); } for (int i = 0; i <= luckySetCount; i++) { int left = k - i; if (left < 0 || unluckyCount - left < 0) { continue; } long add = dp[luckySetCount][i]; add = modOp.multiply(add, facts[unluckyCount]); add = modOp.divide(add, facts[left]); add = modOp.divide(add, facts[unluckyCount - left]); ans = modOp.add(ans, add); } System.out.println(ans); } private static boolean isLucky(int x) { String string = String.valueOf(x); for (char c : string.toCharArray()) { if (c != '4' && c != '7') { return false; } } return true; } private static int[] toPrimitiveArray(Integer[] integers) { int length = integers.length; int[] primitiveIntegers = new int[length]; for (int i = 0; i < length; i++) { primitiveIntegers[i] = integers[i]; } return primitiveIntegers; } } class ModOperator { private final int p; public ModOperator(int p) { this.p = p; } public long add(long x, long y) { x = regularize(x); y = regularize(y); return (x + y) % p; } public long subtract(long x, long y) { x = regularize(x); y = regularize(y); return (x + p - y) % p; } public long multiply(long x, long y) { x = regularize(x); y = regularize(y); return (x * y) % p; } public long divide(long x, long y) { x = regularize(x); y = regularize(y); return (x * inverse(y)) % p; } public long regularize(long x) { x %= p; if (x < 0) { x += p; } return x; } private static long[] exgcd(long a, long b) { long x0 = 1, y0 = 0, z0 = a; long x1 = 0, y1 = 1, z1 = b; while (true) { if (z1 == 0) { return new long[] { x0, y0, z0 }; } long q = z0 / z1; long x2 = x0 - q * x1; long y2 = y0 - q * y1; long z2 = z0 - q * z1; x0 = x1; x1 = x2; y0 = y1; y1 = y2; z0 = z1; z1 = z2; } } public long inverse(long a) { a = regularize(a); long[] xyr = exgcd(a, p); long x = xyr[0]; long r = xyr[2]; if (r == 1L) { return regularize(x); } else { throw new RuntimeException(); } } }
Java
["3 2\n10 10 10", "4 2\n4 4 7 7"]
2 seconds
["3", "4"]
NoteIn the first sample all 3 subsequences of the needed length are considered lucky.In the second sample there are 4 lucky subsequences. For them the sets of indexes equal (the indexation starts from 1): {1, 3}, {1, 4}, {2, 3} and {2, 4}.
Java 6
standard input
[ "dp", "combinatorics" ]
c421f47149e70240a02903d9d47de429
The first line contains two integers n and k (1 ≀ k ≀ n ≀ 105). The next line contains n integers ai (1 ≀ ai ≀ 109) β€” the sequence a.
2,100
On the single line print the single number β€” the answer to the problem modulo prime number 1000000007 (109 + 7).
standard output
PASSED
5f3e6c0436b8a15af7d32f33502b7b92
train_001.jsonl
1448382900
A function is called Lipschitz continuous if there is a real constant K such that the inequality |f(x) - f(y)| ≀ KΒ·|x - y| holds for all . We'll deal with a more... discrete version of this term.For an array , we define it's Lipschitz constant as follows: if n &lt; 2, if n β‰₯ 2, over all 1 ≀ i &lt; j ≀ n In other words, is the smallest non-negative integer such that |h[i] - h[j]| ≀ LΒ·|i - j| holds for all 1 ≀ i, j ≀ n.You are given an array of size n and q queries of the form [l, r]. For each query, consider the subarray ; determine the sum of Lipschitz constants of all subarrays of .
256 megabytes
//package DIV_333; import java.util.Scanner; public class D { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt() ,m=sc.nextInt(); int []a=new int[n+1]; int q[]=new int[100009]; int stk[]=new int[100009]; int pos[]=new int[100009]; long sum[]=new long[100009]; for (int j = 1; j <=n; j++) { a[j]=sc.nextInt(); } for (int j = 1; j <=m; j++) { int l, r; l=sc.nextInt(); r=sc.nextInt(); int top = 0; long ans = 0; pos[0] = l; for (int i = l+1; i <=r; i++) { int value = Math.abs(a[i] - a[i - 1]); while(top > 0 && stk[top] <= value)--top; sum[top + 1] = sum[top] + ( long) (i - pos[top]) * value; ans += sum[top + 1]; stk[++top] = value; pos[top] = i; } System.out.println(ans); } } }
Java
["10 4\n1 5 2 9 1 3 4 2 1 7\n2 4\n3 8\n7 10\n1 9", "7 6\n5 7 7 4 6 6 2\n1 2\n2 3\n2 6\n1 7\n4 7\n3 5"]
1 second
["17\n82\n23\n210", "2\n0\n22\n59\n16\n8"]
NoteIn the first query of the first sample, the Lipschitz constants of subarrays of with length at least 2 are: The answer to the query is their sum.
Java 7
standard input
[ "math" ]
e02938670cb1d586d065ca9750688404
The first line of the input contains two space-separated integers n and q (2 ≀ n ≀ 100 000 and 1 ≀ q ≀ 100)Β β€” the number of elements in array and the number of queries respectively. The second line contains n space-separated integers (). The following q lines describe queries. The i-th of those lines contains two space-separated integers li and ri (1 ≀ li &lt; ri ≀ n).
2,100
Print the answers to all queries in the order in which they are given in the input. For the i-th query, print one line containing a single integerΒ β€” the sum of Lipschitz constants of all subarrays of .
standard output
PASSED
876729f65fe7d6e10084ef30797a345b
train_001.jsonl
1448382900
A function is called Lipschitz continuous if there is a real constant K such that the inequality |f(x) - f(y)| ≀ KΒ·|x - y| holds for all . We'll deal with a more... discrete version of this term.For an array , we define it's Lipschitz constant as follows: if n &lt; 2, if n β‰₯ 2, over all 1 ≀ i &lt; j ≀ n In other words, is the smallest non-negative integer such that |h[i] - h[j]| ≀ LΒ·|i - j| holds for all 1 ≀ i, j ≀ n.You are given an array of size n and q queries of the form [l, r]. For each query, consider the subarray ; determine the sum of Lipschitz constants of all subarrays of .
256 megabytes
import java.io.BufferedInputStream; import java.io.IOException; import java.io.PrintWriter; import java.text.DecimalFormat; 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.InputMismatchException; import java.util.LinkedList; import java.util.Map.Entry; import java.util.Queue; import java.util.Random; import java.util.Set; import java.util.SortedSet; import java.util.Stack; import java.util.TreeMap; import java.util.TreeSet; public class Main { /********************************************** a list of common variables **********************************************/ private MyScanner scan = new MyScanner(); private PrintWriter out = new PrintWriter(System.out); private final double PI = Math.acos(-1.0); private final int SIZEN = (int)(1e5); private final int MOD = (int)(1e9 + 7); private final int[] DX = {0, 1, 0, -1}, DY = {-1, 0, 1, 0}; private ArrayList<Integer>[] edge; public void foo() { int n = scan.nextInt(); int q = scan.nextInt(); int[] a = new int[n + 1]; for(int i = 1;i <= n;++i) { a[i] = scan.nextInt(); } int[] b = new int[n + 1]; for(int i = 1;i < n;++i) { b[i] = Math.abs(a[i + 1] - a[i]); } int[] left = new int[n + 1]; int[] right = new int[n + 1]; Stack<int[]> st = new Stack<int[]>(); st.push(new int[]{Integer.MAX_VALUE, 0}); for(int i = 1;i < n;++i) { while(!st.isEmpty()) { int[] p = st.peek(); if(b[i] <= p[0]) { left[i] = p[1] + 1; st.push(new int[]{b[i], i}); break; } else { right[p[1]] = i - 1; st.pop(); } } } while(!st.isEmpty()) { int[] p = st.pop(); right[p[1]] = n - 1; } while(q-- > 0) { int x = scan.nextInt(); int y = scan.nextInt(); long ans = 0; for(int i = x;i < y;++i) { int down = i - Math.max(left[i], x) + 1; int up = Math.min(right[i], y - 1) - i + 1; ans += (long)down * up * b[i]; } out.println(ans); } } public static void main(String[] args) { Main m = new Main(); m.foo(); m.out.close(); } /********************************************** a list of common algorithms **********************************************/ /** * 1---Get greatest common divisor * @param a : first number * @param b : second number * @return greatest common divisor */ public long gcd(long a, long b) { return 0 == b ? a : gcd(b, a % b); } /** * 2---Get the distance from a point to a line * @param x1 the x coordinate of one endpoint of the line * @param y1 the y coordinate of one endpoint of the line * @param x2 the x coordinate of the other endpoint of the line * @param y2 the y coordinate of the other endpoint of the line * @param x the x coordinate of the point * @param y the x coordinate of the point * @return the distance from a point to a line */ public double getDist(long x1, long y1, long x2, long y2, long x, long y) { long a = y2 - y1; long b = x1 - x2; long c = y1 * (x2 - x1) - x1 * (y2 - y1); return Math.abs(a * x + b * y + c) / Math.sqrt(a * a + b * b); } /** * 3---Get the distance from one point to a segment (not a line) * @param x1 the x coordinate of one endpoint of the segment * @param y1 the y coordinate of one endpoint of the segment * @param x2 the x coordinate of the other endpoint of the segment * @param y2 the y coordinate of the other endpoint of the segment * @param x the x coordinate of the point * @param y the y coordinate of the point * @return the distance from one point to a segment (not a line) */ public double ptToSeg(long x1, long y1, long x2, long y2, long x, long y) { double cross = (x2 - x1) * (x - x1) + (y2 - y1) * (y - y1); if(cross <= 0) { return (x - x1) * (x - x1) + (y - y1) * (y - y1); } double d = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1); if(cross >= d) { return (x - x2) * (x - x2) + (y - y2) * (y - y2); } double r = cross / d; double px = x1 + (x2 - x1) * r; double py = y1 + (y2 - y1) * r; return (x - px) * (x - px) + (y - py) * (y - py); } /** * 4---KMP match, i.e. kmpMatch("abcd", "bcd") = 1, kmpMatch("abcd", "bfcd") = -1. * @param t: String to match. * @param p: String to be matched. * @return if can match, first index; otherwise -1. */ public int kmpMatch(char[] t, char[] p) { int n = t.length; int m = p.length; int[] next = new int[m + 1]; next[0] = -1; int j = -1; for(int i = 1;i < m;++i) { while(j >= 0 && p[i] != p[j + 1]) { j = next[j]; } if(p[i] == p[j + 1]) { ++j; } next[i] = j; } j = -1; for(int i = 0;i < n;++i) { while(j >= 0 && t[i] != p[j + 1]) { j = next[j]; } if(t[i] == p[j + 1]) { ++j; } if(j == m - 1) { return i - m + 1; } } return -1; } class MyScanner { private byte[] buf = new byte[1024]; private int curChar; private int numChars; BufferedInputStream bis = new BufferedInputStream(System.in); public int read() { if (-1 == numChars) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = bis.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c & 15; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } m /= 10; res += (c & 15) * m; c = read(); } } return res * sgn; } public String next() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return ' ' == c || '\n' == c || '\r' == c || '\t' == c || -1 == c; } } }
Java
["10 4\n1 5 2 9 1 3 4 2 1 7\n2 4\n3 8\n7 10\n1 9", "7 6\n5 7 7 4 6 6 2\n1 2\n2 3\n2 6\n1 7\n4 7\n3 5"]
1 second
["17\n82\n23\n210", "2\n0\n22\n59\n16\n8"]
NoteIn the first query of the first sample, the Lipschitz constants of subarrays of with length at least 2 are: The answer to the query is their sum.
Java 7
standard input
[ "math" ]
e02938670cb1d586d065ca9750688404
The first line of the input contains two space-separated integers n and q (2 ≀ n ≀ 100 000 and 1 ≀ q ≀ 100)Β β€” the number of elements in array and the number of queries respectively. The second line contains n space-separated integers (). The following q lines describe queries. The i-th of those lines contains two space-separated integers li and ri (1 ≀ li &lt; ri ≀ n).
2,100
Print the answers to all queries in the order in which they are given in the input. For the i-th query, print one line containing a single integerΒ β€” the sum of Lipschitz constants of all subarrays of .
standard output
PASSED
81c1cc27ba2da651c2eb5bf4c58d4242
train_001.jsonl
1348069500
A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left). Alice and Bob play dice. Alice has built a tower from n dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees).Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not.
256 megabytes
import java.util.Scanner; public class Main { private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int n = scanner.nextInt(), t = scanner.nextInt(); int x = 0, y = 0; String s = "YES"; for (int i = 0; i < n; i++) { x = scanner.nextInt(); y = scanner.nextInt(); if (x == t || x == 7 - t || y == t || y == 7 - t) { s = "NO"; break; } } System.out.println(s); } }
Java
["3\n6\n3 2\n5 4\n2 4", "3\n3\n2 6\n4 1\n5 3"]
2 seconds
["YES", "NO"]
null
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2d6a1202139e1f77f32377224e1fe752
The first line contains a single integer n (1 ≀ n ≀ 100) β€” the number of dice in the tower. The second line contains an integer x (1 ≀ x ≀ 6) β€” the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 ≀ ai, bi ≀ 6;Β ai ≠ bi) β€” the numbers Bob sees on the two sidelong faces of the i-th dice in the tower. Consider the dice in the tower indexed from top to bottom from 1 to n. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input.
1,100
Print "YES" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print "NO" (without the quotes).
standard output
PASSED
7b121a38c37b4005c8a9928e35aeb907
train_001.jsonl
1348069500
A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left). Alice and Bob play dice. Alice has built a tower from n dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees).Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not.
256 megabytes
import java.util.Scanner; public class Lol2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int x = sc.nextInt(); int x2 = Math.abs(7 - x); int test = 0; for(int i =0; i < n; i++) { int input =sc.nextInt(); int input2 = sc.nextInt(); if(Math.abs(7 - input) == x || Math.abs(7 - input) == x2) { test++; } if(Math.abs(7 - input2) == x || Math.abs(7 - input2) == x2) { test++; } } if(test == 0) { System.out.println("YES"); } else { System.out.println("NO"); } } }
Java
["3\n6\n3 2\n5 4\n2 4", "3\n3\n2 6\n4 1\n5 3"]
2 seconds
["YES", "NO"]
null
Java 11
standard input
[ "constructive algorithms", "greedy" ]
2d6a1202139e1f77f32377224e1fe752
The first line contains a single integer n (1 ≀ n ≀ 100) β€” the number of dice in the tower. The second line contains an integer x (1 ≀ x ≀ 6) β€” the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 ≀ ai, bi ≀ 6;Β ai ≠ bi) β€” the numbers Bob sees on the two sidelong faces of the i-th dice in the tower. Consider the dice in the tower indexed from top to bottom from 1 to n. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input.
1,100
Print "YES" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print "NO" (without the quotes).
standard output
PASSED
ab8533842022d78ddeb3a60b41a2daf7
train_001.jsonl
1551022500
Consider the following problem: given an array $$$a$$$ containing $$$n$$$ integers (indexed from $$$0$$$ to $$$n-1$$$), find $$$\max\limits_{0 \leq l \leq r \leq n-1} \sum\limits_{l \leq i \leq r} (r-l+1) \cdot a_i$$$. In this problem, $$$1 \leq n \leq 2\,000$$$ and $$$|a_i| \leq 10^6$$$.In an attempt to solve the problem described, Alice quickly came up with a blazing-fast greedy algorithm and coded it. Her implementation in pseudocode is as follows:function find_answer(n, a) # Assumes n is an integer between 1 and 2000, inclusive # Assumes a is a list containing n integers: a[0], a[1], ..., a[n-1] res = 0 cur = 0 k = -1 for i = 0 to i = n-1 cur = cur + a[i] if cur &lt; 0 cur = 0 k = i res = max(res, (i-k)*cur) return resAlso, as you can see, Alice's idea is not entirely correct. For example, suppose $$$n = 4$$$ and $$$a = [6, -8, 7, -42]$$$. Then, find_answer(n, a) would return $$$7$$$, but the correct answer is $$$3 \cdot (6-8+7) = 15$$$.You told Alice that her solution is incorrect, but she did not believe what you said.Given an integer $$$k$$$, you are to find any sequence $$$a$$$ of $$$n$$$ integers such that the correct answer and the answer produced by Alice's algorithm differ by exactly $$$k$$$. Note that although the choice of $$$n$$$ and the content of the sequence is yours, you must still follow the constraints earlier given: that $$$1 \leq n \leq 2\,000$$$ and that the absolute value of each element does not exceed $$$10^6$$$. If there is no such sequence, determine so.
256 megabytes
import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.PriorityQueue; import java.util.Random; import java.util.TreeSet; public final class CF_542_D1_C { static boolean verb=true; static void log(Object X){if (verb) System.err.println(X);} static void log(Object[] X){if (verb) {for (Object U:X) System.err.print(U+" ");System.err.println("");}} static void log(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");System.err.println("");}} static void log(int[] X,int L){if (verb) {for (int i=0;i<L;i++) System.err.print(X[i]+" ");System.err.println("");}} static void log(long[] X){if (verb) {for (long U:X) System.err.print(U+" ");System.err.println("");}} static void logWln(Object X){if (verb) System.err.print(X);} static void info(Object o){ System.out.println(o);} static void output(Object o){outputWln(""+o+"\n"); } static void outputWln(Object o){try {out.write(""+ o);} catch (Exception e) {}} static long pgcd(long a,long b){ if (a<b) return pgcd(b,a); while (b!=0){ long c=b; b=a%b; a=c; } return a; } static int time; static int[] visited; // inclusive static void test(){ log("testing"); int NTESTS=1000; Random r=new Random(); for (int t=0;t<NTESTS;t++){ } log("testing done"); } static int ln2max(int x){ int res=1; for (;(1 << res) <=x;res++); res--; return res; } // Global vars static BufferedWriter out; static InputReader reader; static int MX=Integer.MAX_VALUE; static void process() throws Exception { //test(); out = new BufferedWriter(new OutputStreamWriter(System.out)); reader = new InputReader(System.in); int k=reader.readInt(); long VX=1000001; long NX=2001; if (k<VX/2){ output("3"); output(0+" "+(-k)+" "+(2*k)); } else { log("case 2"); loop:for (long A=Math.max(100, k/(VX-1));A<NX;A++){ { long w=k/A; long c=k%A; //log("b:"+b+" c:"+c); for (int e=1;e<w;e++){ long b=w+e; long rem=b*A-k; //log("rem:"+rem+" A:"+A); for (long B=2;B<A;B++){ //log("B:"+B+" rem%B:"+rem%B); if (rem%B==0){ //log("step 2"); long a=rem/B; if (a>b){ //log("step 3"); // total on B elements must be a // total on A elements must be b log(k+" "+(A*b-B*a)); long diff=a-b; long delta=A-B; if (delta*(VX-1)>=diff){ if (a<=(VX-1)*B){ long[] ar=new long[(int)A]; int st=0; long item=diff/delta; if (item==0) item=1; for (int i=0;i<delta;i++){ long bob=Math.min(diff,item); ar[(int)delta-1-st]=-bob; st++; diff-=bob; } for (int i=0;i<B;i++){ long bob=Math.min(a,VX-1); a-=bob; ar[st++]=bob; } // check boolean bad=false; for (int i=0;i<A;i++){ if (Math.abs(ar[i])>=VX){ bad=true; break; } } if (!bad){ output(A); for (int i=0;i<A;i++){ outputWln(ar[i]+" "); } output(""); long sum=0; for (long x:ar) sum+=x; long ssum=0; for (int i=0;i<B;i++){ ssum+=ar[(int)A-1-i]; } if (A*sum-B*ssum!=k) log("Error"); else log("ok"); break loop; } } } } } } } } } } try { out.close(); } catch (Exception e) { } } public static void main(String[] args) throws Exception { process(); } static final class InputReader { private final InputStream stream; private final byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } private int read() throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read(buf); if (numChars <= 0) { return -1; } } return buf[curChar++]; } public final String readString() throws IOException { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.append((char) c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public final int readInt() throws IOException { int c = read(); boolean neg = false; while (isSpaceChar(c)) { c = read(); } char d = (char) c; // log("d:"+d); if (d == '-') { neg = true; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); // log("res:"+res); if (neg) return -res; return res; } public final long readLong() throws IOException { int c = read(); boolean neg = false; while (isSpaceChar(c)) { c = read(); } char d = (char) c; // log("d:"+d); if (d == '-') { neg = true; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); // log("res:"+res); if (neg) return -res; return res; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
Java
["8", "612"]
1 second
["4\n6 -8 7 -42", "7\n30 -12 -99 123 -2 245 -300"]
NoteThe first sample corresponds to the example given in the problem statement.In the second sample, one answer is $$$n = 7$$$ with $$$a = [30, -12, -99, 123, -2, 245, -300]$$$, in which case find_answer(n, a) returns $$$1098$$$, while the correct answer is $$$1710$$$.
Java 8
standard input
[ "constructive algorithms" ]
df7e4272ea2b48a5ad5f35a05c79044c
The first and only line contains one integer $$$k$$$ ($$$1 \leq k \leq 10^9$$$).
2,000
If there is no sought sequence, print "-1". Otherwise, in the first line, print one integer $$$n$$$ ($$$1 \leq n \leq 2\,000$$$), denoting the number of elements in the sequence. Then, in the second line, print $$$n$$$ space-separated integers: $$$a_0, a_1, \ldots, a_{n-1}$$$ ($$$|a_i| \leq 10^6$$$).
standard output
PASSED
c8070b50f0bab15086704943336c78e4
train_001.jsonl
1551022500
Consider the following problem: given an array $$$a$$$ containing $$$n$$$ integers (indexed from $$$0$$$ to $$$n-1$$$), find $$$\max\limits_{0 \leq l \leq r \leq n-1} \sum\limits_{l \leq i \leq r} (r-l+1) \cdot a_i$$$. In this problem, $$$1 \leq n \leq 2\,000$$$ and $$$|a_i| \leq 10^6$$$.In an attempt to solve the problem described, Alice quickly came up with a blazing-fast greedy algorithm and coded it. Her implementation in pseudocode is as follows:function find_answer(n, a) # Assumes n is an integer between 1 and 2000, inclusive # Assumes a is a list containing n integers: a[0], a[1], ..., a[n-1] res = 0 cur = 0 k = -1 for i = 0 to i = n-1 cur = cur + a[i] if cur &lt; 0 cur = 0 k = i res = max(res, (i-k)*cur) return resAlso, as you can see, Alice's idea is not entirely correct. For example, suppose $$$n = 4$$$ and $$$a = [6, -8, 7, -42]$$$. Then, find_answer(n, a) would return $$$7$$$, but the correct answer is $$$3 \cdot (6-8+7) = 15$$$.You told Alice that her solution is incorrect, but she did not believe what you said.Given an integer $$$k$$$, you are to find any sequence $$$a$$$ of $$$n$$$ integers such that the correct answer and the answer produced by Alice's algorithm differ by exactly $$$k$$$. Note that although the choice of $$$n$$$ and the content of the sequence is yours, you must still follow the constraints earlier given: that $$$1 \leq n \leq 2\,000$$$ and that the absolute value of each element does not exceed $$$10^6$$$. If there is no such sequence, determine so.
256 megabytes
//package com.company; // Always comment out package when submitting. import java.io.*; import java.util.*; public class Main { // Actual Code public static class Task { public void solve(Scanner sc, PrintWriter pw) throws IOException { int k = sc.nextInt(); int n = 2000; for (int i = 2; i < 2000; i++) { int[] a = new int[n]; int V = n - i - (k % (n - i)) + (n - i) * 300; a[0] = V; int get = V + k ; if (get % (n - i) != 0) { throw new RuntimeException(); } for (int j = 1; j < i; j++) { a[j] = -1000000; } a[1998] = -1; a[1999] = get / (n - i) + 1; long ts = 0; for (int j = 0; j < 2000; j++) { ts += a[j]; } ts = ts * 2000; if (ts <= get) { pw.println(2000); for (int y: a) { pw.print(y + " "); } pw.println(); return; } } pw.println(-1); } } // template, actual code is in class Task. static long TIME_START, TIME_END; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); // Scanner sc = new Scanner(new FileInputStream("Test.in")); PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out)); // PrintWriter pw = new PrintWriter(new FileOutputStream("File.out")); Runtime runtime = Runtime.getRuntime(); long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory(); TIME_START = System.currentTimeMillis(); Task t = new Task(); t.solve(sc, pw); TIME_END = System.currentTimeMillis(); long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory(); pw.close(); System.err.println("Memory increased:" + (usedMemoryAfter-usedMemoryBefore) / 1000000); System.err.println("Time used: " + (TIME_END - TIME_START) + "."); } // Faster IO with BufferedReader wrapped with Scanner static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){br = new BufferedReader(new InputStreamReader(s));} public Scanner(FileReader s) throws FileNotFoundException {br = new BufferedReader(s);} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public double nextDouble() throws IOException {return Double.parseDouble(next()); } public boolean ready() throws IOException {return br.ready();} } }
Java
["8", "612"]
1 second
["4\n6 -8 7 -42", "7\n30 -12 -99 123 -2 245 -300"]
NoteThe first sample corresponds to the example given in the problem statement.In the second sample, one answer is $$$n = 7$$$ with $$$a = [30, -12, -99, 123, -2, 245, -300]$$$, in which case find_answer(n, a) returns $$$1098$$$, while the correct answer is $$$1710$$$.
Java 8
standard input
[ "constructive algorithms" ]
df7e4272ea2b48a5ad5f35a05c79044c
The first and only line contains one integer $$$k$$$ ($$$1 \leq k \leq 10^9$$$).
2,000
If there is no sought sequence, print "-1". Otherwise, in the first line, print one integer $$$n$$$ ($$$1 \leq n \leq 2\,000$$$), denoting the number of elements in the sequence. Then, in the second line, print $$$n$$$ space-separated integers: $$$a_0, a_1, \ldots, a_{n-1}$$$ ($$$|a_i| \leq 10^6$$$).
standard output
PASSED
c922e21af470016b9fcec9bd51935711
train_001.jsonl
1551022500
Consider the following problem: given an array $$$a$$$ containing $$$n$$$ integers (indexed from $$$0$$$ to $$$n-1$$$), find $$$\max\limits_{0 \leq l \leq r \leq n-1} \sum\limits_{l \leq i \leq r} (r-l+1) \cdot a_i$$$. In this problem, $$$1 \leq n \leq 2\,000$$$ and $$$|a_i| \leq 10^6$$$.In an attempt to solve the problem described, Alice quickly came up with a blazing-fast greedy algorithm and coded it. Her implementation in pseudocode is as follows:function find_answer(n, a) # Assumes n is an integer between 1 and 2000, inclusive # Assumes a is a list containing n integers: a[0], a[1], ..., a[n-1] res = 0 cur = 0 k = -1 for i = 0 to i = n-1 cur = cur + a[i] if cur &lt; 0 cur = 0 k = i res = max(res, (i-k)*cur) return resAlso, as you can see, Alice's idea is not entirely correct. For example, suppose $$$n = 4$$$ and $$$a = [6, -8, 7, -42]$$$. Then, find_answer(n, a) would return $$$7$$$, but the correct answer is $$$3 \cdot (6-8+7) = 15$$$.You told Alice that her solution is incorrect, but she did not believe what you said.Given an integer $$$k$$$, you are to find any sequence $$$a$$$ of $$$n$$$ integers such that the correct answer and the answer produced by Alice's algorithm differ by exactly $$$k$$$. Note that although the choice of $$$n$$$ and the content of the sequence is yours, you must still follow the constraints earlier given: that $$$1 \leq n \leq 2\,000$$$ and that the absolute value of each element does not exceed $$$10^6$$$. If there is no such sequence, determine so.
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.util.ArrayDeque; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author prakharjain */ 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); BWrongAnswer solver = new BWrongAnswer(); solver.solve(1, in, out); out.close(); } static class BWrongAnswer { int max = 1_000_000; public void solve(int testNumber, InputReader in, OutputWriter out) { int k = in.nextInt(); ExtendedEuclid extendedEuclid = new ExtendedEuclid(); //ExtendedEuclid.res res = extendedEuclid.gcd(1000, -999, extendedEuclid.new res(-1, -1, -1)); long z = k; long x = k; long g = 1; // x *= k; // y *= k; if (z > 499500000) { long t = (z - 499500000 + 999 - 1) / 999; z -= t * 999; x -= t * 1000; } // if (x < 0) { // long d = -x; // long t = (d + 999 - 1) / 999; // // x += t * 1000; // y += t * 999; // } // if (y < -1_000_000_000) { // long d = -1_000_000_000 - y; // long t = (d + 1000 - 1) / 1000; // // x += t * 999; // y += t * 1000; // } long y = x + z; ArrayDeque<Integer> dq = new ArrayDeque<>(); for (int i = 0; i < 1000; i++) { if (x > max) { dq.addFirst(-max); x -= max; } else { dq.addFirst(-(int) x); x = 0; } } for (int i = 0; i < 999; i++) { if (y > max) { dq.addLast(max); y -= max; } else { dq.addLast((int) y); y = 0; } } out.println(dq.size()); for (int val : dq) { out.print(val + " "); } } class ExtendedEuclid { } } 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 static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } 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 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 close() { writer.close(); } public void println(int i) { writer.println(i); } } }
Java
["8", "612"]
1 second
["4\n6 -8 7 -42", "7\n30 -12 -99 123 -2 245 -300"]
NoteThe first sample corresponds to the example given in the problem statement.In the second sample, one answer is $$$n = 7$$$ with $$$a = [30, -12, -99, 123, -2, 245, -300]$$$, in which case find_answer(n, a) returns $$$1098$$$, while the correct answer is $$$1710$$$.
Java 8
standard input
[ "constructive algorithms" ]
df7e4272ea2b48a5ad5f35a05c79044c
The first and only line contains one integer $$$k$$$ ($$$1 \leq k \leq 10^9$$$).
2,000
If there is no sought sequence, print "-1". Otherwise, in the first line, print one integer $$$n$$$ ($$$1 \leq n \leq 2\,000$$$), denoting the number of elements in the sequence. Then, in the second line, print $$$n$$$ space-separated integers: $$$a_0, a_1, \ldots, a_{n-1}$$$ ($$$|a_i| \leq 10^6$$$).
standard output
PASSED
49751566f51f7f5fe6044039714793d1
train_001.jsonl
1551022500
Consider the following problem: given an array $$$a$$$ containing $$$n$$$ integers (indexed from $$$0$$$ to $$$n-1$$$), find $$$\max\limits_{0 \leq l \leq r \leq n-1} \sum\limits_{l \leq i \leq r} (r-l+1) \cdot a_i$$$. In this problem, $$$1 \leq n \leq 2\,000$$$ and $$$|a_i| \leq 10^6$$$.In an attempt to solve the problem described, Alice quickly came up with a blazing-fast greedy algorithm and coded it. Her implementation in pseudocode is as follows:function find_answer(n, a) # Assumes n is an integer between 1 and 2000, inclusive # Assumes a is a list containing n integers: a[0], a[1], ..., a[n-1] res = 0 cur = 0 k = -1 for i = 0 to i = n-1 cur = cur + a[i] if cur &lt; 0 cur = 0 k = i res = max(res, (i-k)*cur) return resAlso, as you can see, Alice's idea is not entirely correct. For example, suppose $$$n = 4$$$ and $$$a = [6, -8, 7, -42]$$$. Then, find_answer(n, a) would return $$$7$$$, but the correct answer is $$$3 \cdot (6-8+7) = 15$$$.You told Alice that her solution is incorrect, but she did not believe what you said.Given an integer $$$k$$$, you are to find any sequence $$$a$$$ of $$$n$$$ integers such that the correct answer and the answer produced by Alice's algorithm differ by exactly $$$k$$$. Note that although the choice of $$$n$$$ and the content of the sequence is yours, you must still follow the constraints earlier given: that $$$1 \leq n \leq 2\,000$$$ and that the absolute value of each element does not exceed $$$10^6$$$. If there is no such sequence, determine so.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author beginner1010 */ 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); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { final int maxTerms = 2000; final int maxElement = 1000 * 1000; public void solve(int testNumber, InputReader in, PrintWriter out) { int k = in.nextInt(); for (int t = 3; t <= maxTerms; t++) { for (int r = 2; r <= t; r++) { if ((k + t) % r == 0) { int x = (k + t) / r - 1; int req = ((x + 1) + maxElement - 1) / maxElement; if (req <= t - r) { out.println(t); for (int i = 0; i < r - 2; i++) { out.print("0 "); } out.print("1 -2"); int sum = 0; for (int i = 0; i < t - r; i++) { int element = Math.min(maxElement, x + 1 - sum); out.print(" " + element); sum += element; } out.println(); return; } } } } out.println("-1"); } } static class InputReader { private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputStream stream; public InputReader(InputStream stream) { this.stream = stream; } private boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isWhitespace(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 (!isWhitespace(c)); return res * sgn; } } }
Java
["8", "612"]
1 second
["4\n6 -8 7 -42", "7\n30 -12 -99 123 -2 245 -300"]
NoteThe first sample corresponds to the example given in the problem statement.In the second sample, one answer is $$$n = 7$$$ with $$$a = [30, -12, -99, 123, -2, 245, -300]$$$, in which case find_answer(n, a) returns $$$1098$$$, while the correct answer is $$$1710$$$.
Java 8
standard input
[ "constructive algorithms" ]
df7e4272ea2b48a5ad5f35a05c79044c
The first and only line contains one integer $$$k$$$ ($$$1 \leq k \leq 10^9$$$).
2,000
If there is no sought sequence, print "-1". Otherwise, in the first line, print one integer $$$n$$$ ($$$1 \leq n \leq 2\,000$$$), denoting the number of elements in the sequence. Then, in the second line, print $$$n$$$ space-separated integers: $$$a_0, a_1, \ldots, a_{n-1}$$$ ($$$|a_i| \leq 10^6$$$).
standard output
PASSED
4d3701a72c94853431de2e78d59dedd5
train_001.jsonl
1551022500
Consider the following problem: given an array $$$a$$$ containing $$$n$$$ integers (indexed from $$$0$$$ to $$$n-1$$$), find $$$\max\limits_{0 \leq l \leq r \leq n-1} \sum\limits_{l \leq i \leq r} (r-l+1) \cdot a_i$$$. In this problem, $$$1 \leq n \leq 2\,000$$$ and $$$|a_i| \leq 10^6$$$.In an attempt to solve the problem described, Alice quickly came up with a blazing-fast greedy algorithm and coded it. Her implementation in pseudocode is as follows:function find_answer(n, a) # Assumes n is an integer between 1 and 2000, inclusive # Assumes a is a list containing n integers: a[0], a[1], ..., a[n-1] res = 0 cur = 0 k = -1 for i = 0 to i = n-1 cur = cur + a[i] if cur &lt; 0 cur = 0 k = i res = max(res, (i-k)*cur) return resAlso, as you can see, Alice's idea is not entirely correct. For example, suppose $$$n = 4$$$ and $$$a = [6, -8, 7, -42]$$$. Then, find_answer(n, a) would return $$$7$$$, but the correct answer is $$$3 \cdot (6-8+7) = 15$$$.You told Alice that her solution is incorrect, but she did not believe what you said.Given an integer $$$k$$$, you are to find any sequence $$$a$$$ of $$$n$$$ integers such that the correct answer and the answer produced by Alice's algorithm differ by exactly $$$k$$$. Note that although the choice of $$$n$$$ and the content of the sequence is yours, you must still follow the constraints earlier given: that $$$1 \leq n \leq 2\,000$$$ and that the absolute value of each element does not exceed $$$10^6$$$. If there is no such sequence, determine so.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author beginner1010 */ 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); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { final int maxTerms = 2000; final int maxElement = 1000 * 1000; ArrayList<Integer> factors(int k) { ArrayList<Integer> res = new ArrayList<>(); for (long div1 = 1; div1 * div1 <= k; div1++) { if (k % div1 == 0) { if (div1 >= 3 && div1 <= maxTerms) res.add((int) div1); int div2 = (int) (k / div1); if (div2 >= 3 && div2 <= maxTerms) { res.add(div2); } } } return res; } public void solve(int testNumber, InputReader in, PrintWriter out) { int k = in.nextInt(); ArrayList<Integer> factors = factors(k); for (int t = 3; t <= maxTerms; t++) { for (int r = 2; r <= t; r++) { if ((k + t) % r == 0) { int x = (k + t) / r - 1; int req = ((x + 1) + maxElement - 1) / maxElement; if (req <= t - r) { out.println(t); for (int i = 0; i < r - 2; i++) { out.print("0 "); } out.print("1 -2"); int sum = 0; for (int i = 0; i < t - r; i++) { int element = Math.min(maxElement, x + 1 - sum); out.print(" " + element); sum += element; } out.println(); return; } } } } out.println("-1"); } } static class InputReader { private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputStream stream; public InputReader(InputStream stream) { this.stream = stream; } private boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isWhitespace(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 (!isWhitespace(c)); return res * sgn; } } }
Java
["8", "612"]
1 second
["4\n6 -8 7 -42", "7\n30 -12 -99 123 -2 245 -300"]
NoteThe first sample corresponds to the example given in the problem statement.In the second sample, one answer is $$$n = 7$$$ with $$$a = [30, -12, -99, 123, -2, 245, -300]$$$, in which case find_answer(n, a) returns $$$1098$$$, while the correct answer is $$$1710$$$.
Java 8
standard input
[ "constructive algorithms" ]
df7e4272ea2b48a5ad5f35a05c79044c
The first and only line contains one integer $$$k$$$ ($$$1 \leq k \leq 10^9$$$).
2,000
If there is no sought sequence, print "-1". Otherwise, in the first line, print one integer $$$n$$$ ($$$1 \leq n \leq 2\,000$$$), denoting the number of elements in the sequence. Then, in the second line, print $$$n$$$ space-separated integers: $$$a_0, a_1, \ldots, a_{n-1}$$$ ($$$|a_i| \leq 10^6$$$).
standard output
PASSED
f0ef0da6849cf481a9332a2a4d586c03
train_001.jsonl
1551022500
Consider the following problem: given an array $$$a$$$ containing $$$n$$$ integers (indexed from $$$0$$$ to $$$n-1$$$), find $$$\max\limits_{0 \leq l \leq r \leq n-1} \sum\limits_{l \leq i \leq r} (r-l+1) \cdot a_i$$$. In this problem, $$$1 \leq n \leq 2\,000$$$ and $$$|a_i| \leq 10^6$$$.In an attempt to solve the problem described, Alice quickly came up with a blazing-fast greedy algorithm and coded it. Her implementation in pseudocode is as follows:function find_answer(n, a) # Assumes n is an integer between 1 and 2000, inclusive # Assumes a is a list containing n integers: a[0], a[1], ..., a[n-1] res = 0 cur = 0 k = -1 for i = 0 to i = n-1 cur = cur + a[i] if cur &lt; 0 cur = 0 k = i res = max(res, (i-k)*cur) return resAlso, as you can see, Alice's idea is not entirely correct. For example, suppose $$$n = 4$$$ and $$$a = [6, -8, 7, -42]$$$. Then, find_answer(n, a) would return $$$7$$$, but the correct answer is $$$3 \cdot (6-8+7) = 15$$$.You told Alice that her solution is incorrect, but she did not believe what you said.Given an integer $$$k$$$, you are to find any sequence $$$a$$$ of $$$n$$$ integers such that the correct answer and the answer produced by Alice's algorithm differ by exactly $$$k$$$. Note that although the choice of $$$n$$$ and the content of the sequence is yours, you must still follow the constraints earlier given: that $$$1 \leq n \leq 2\,000$$$ and that the absolute value of each element does not exceed $$$10^6$$$. If there is no such sequence, determine so.
256 megabytes
import java.io.*; import java.util.*; public class E { public static void main(String[] args) { new E(); } public E() { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int T = 1; while(T-->0) { int K = fs.nextInt(); int[] arr = new int[1999]; arr[0] = -1; for(int i = 1; i < arr.length-1; i++) arr[i] = 1; arr[arr.length-1] = 2; for(int elem = arr.length-1; elem > 0; elem--) { int canTake = 1000000 - arr[elem]; canTake = Math.min(canTake, K); arr[elem] += canTake; K -= canTake; } out.println(arr.length); for(int i = 0; i < arr.length; i++) { if(i > 0) out.print(" "); out.print(arr[i]); } out.println(); } out.close(); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } } }
Java
["8", "612"]
1 second
["4\n6 -8 7 -42", "7\n30 -12 -99 123 -2 245 -300"]
NoteThe first sample corresponds to the example given in the problem statement.In the second sample, one answer is $$$n = 7$$$ with $$$a = [30, -12, -99, 123, -2, 245, -300]$$$, in which case find_answer(n, a) returns $$$1098$$$, while the correct answer is $$$1710$$$.
Java 8
standard input
[ "constructive algorithms" ]
df7e4272ea2b48a5ad5f35a05c79044c
The first and only line contains one integer $$$k$$$ ($$$1 \leq k \leq 10^9$$$).
2,000
If there is no sought sequence, print "-1". Otherwise, in the first line, print one integer $$$n$$$ ($$$1 \leq n \leq 2\,000$$$), denoting the number of elements in the sequence. Then, in the second line, print $$$n$$$ space-separated integers: $$$a_0, a_1, \ldots, a_{n-1}$$$ ($$$|a_i| \leq 10^6$$$).
standard output
PASSED
ebc05ce628d19d55ec39825fa1291b00
train_001.jsonl
1551022500
Consider the following problem: given an array $$$a$$$ containing $$$n$$$ integers (indexed from $$$0$$$ to $$$n-1$$$), find $$$\max\limits_{0 \leq l \leq r \leq n-1} \sum\limits_{l \leq i \leq r} (r-l+1) \cdot a_i$$$. In this problem, $$$1 \leq n \leq 2\,000$$$ and $$$|a_i| \leq 10^6$$$.In an attempt to solve the problem described, Alice quickly came up with a blazing-fast greedy algorithm and coded it. Her implementation in pseudocode is as follows:function find_answer(n, a) # Assumes n is an integer between 1 and 2000, inclusive # Assumes a is a list containing n integers: a[0], a[1], ..., a[n-1] res = 0 cur = 0 k = -1 for i = 0 to i = n-1 cur = cur + a[i] if cur &lt; 0 cur = 0 k = i res = max(res, (i-k)*cur) return resAlso, as you can see, Alice's idea is not entirely correct. For example, suppose $$$n = 4$$$ and $$$a = [6, -8, 7, -42]$$$. Then, find_answer(n, a) would return $$$7$$$, but the correct answer is $$$3 \cdot (6-8+7) = 15$$$.You told Alice that her solution is incorrect, but she did not believe what you said.Given an integer $$$k$$$, you are to find any sequence $$$a$$$ of $$$n$$$ integers such that the correct answer and the answer produced by Alice's algorithm differ by exactly $$$k$$$. Note that although the choice of $$$n$$$ and the content of the sequence is yours, you must still follow the constraints earlier given: that $$$1 \leq n \leq 2\,000$$$ and that the absolute value of each element does not exceed $$$10^6$$$. If there is no such sequence, determine so.
256 megabytes
import java.io.*; import java.util.*; public class B implements Runnable { public static void main (String[] args) {new Thread(null, new B(), "_cf", 1 << 28).start();} public void run() { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); System.err.println(""); int k = fs.nextInt(); int targetSum = -1; int n = -1; for(int m = 2; m <= 2000; m++) { int ts = k-1+m; int best = (m-1)*1000000-1; if(best >= ts) { n = m; targetSum = ts; break; } } if(n == -1) { System.out.println(n); return; } int[] res = new int[n]; res[0] = -1; targetSum++; for(int i = 1; i < n; i++) { res[i] = Math.min(1000000, targetSum); targetSum -= res[i]; } out.println(n); for(int i = 0; i < n; i++) { if(i > 0) out.print(" "); out.print(res[i]); } out.println(); out.close(); } class FastScanner { public int BS = 1<<16; public char NC = (char)0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) throws FileNotFoundException { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } public char nextChar(){ while(bId==size) { try { size = in.read(buf); }catch(Exception e) { return NC; } if(size==-1)return NC; bId=0; } return (char)buf[bId++]; } public int nextInt() { return (int)nextLong(); } public long nextLong() { num=1; boolean neg = false; if(c==NC)c=nextChar(); for(;(c<'0' || c>'9'); c = nextChar()) { if(c=='-')neg=true; } long res = 0; for(; c>='0' && c <='9'; c=nextChar()) { res = (res<<3)+(res<<1)+c-'0'; num*=10; } return neg?-res:res; } public double nextDouble() { double cur = nextLong(); return c!='.' ? cur:cur+nextLong()/num; } public String next() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c>32) { res.append(c); c=nextChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c!='\n') { res.append(c); c=nextChar(); } return res.toString(); } public boolean hasNext() { if(c>32)return true; while(true) { c=nextChar(); if(c==NC)return false; else if(c>32)return true; } } public int[] nextIntArray(int n) { int[] res = new int[n]; for(int i = 0; i < n; i++) res[i] = nextInt(); return res; } } }
Java
["8", "612"]
1 second
["4\n6 -8 7 -42", "7\n30 -12 -99 123 -2 245 -300"]
NoteThe first sample corresponds to the example given in the problem statement.In the second sample, one answer is $$$n = 7$$$ with $$$a = [30, -12, -99, 123, -2, 245, -300]$$$, in which case find_answer(n, a) returns $$$1098$$$, while the correct answer is $$$1710$$$.
Java 8
standard input
[ "constructive algorithms" ]
df7e4272ea2b48a5ad5f35a05c79044c
The first and only line contains one integer $$$k$$$ ($$$1 \leq k \leq 10^9$$$).
2,000
If there is no sought sequence, print "-1". Otherwise, in the first line, print one integer $$$n$$$ ($$$1 \leq n \leq 2\,000$$$), denoting the number of elements in the sequence. Then, in the second line, print $$$n$$$ space-separated integers: $$$a_0, a_1, \ldots, a_{n-1}$$$ ($$$|a_i| \leq 10^6$$$).
standard output
PASSED
c6a00d8d02209b1330566b647b8eed35
train_001.jsonl
1551022500
Consider the following problem: given an array $$$a$$$ containing $$$n$$$ integers (indexed from $$$0$$$ to $$$n-1$$$), find $$$\max\limits_{0 \leq l \leq r \leq n-1} \sum\limits_{l \leq i \leq r} (r-l+1) \cdot a_i$$$. In this problem, $$$1 \leq n \leq 2\,000$$$ and $$$|a_i| \leq 10^6$$$.In an attempt to solve the problem described, Alice quickly came up with a blazing-fast greedy algorithm and coded it. Her implementation in pseudocode is as follows:function find_answer(n, a) # Assumes n is an integer between 1 and 2000, inclusive # Assumes a is a list containing n integers: a[0], a[1], ..., a[n-1] res = 0 cur = 0 k = -1 for i = 0 to i = n-1 cur = cur + a[i] if cur &lt; 0 cur = 0 k = i res = max(res, (i-k)*cur) return resAlso, as you can see, Alice's idea is not entirely correct. For example, suppose $$$n = 4$$$ and $$$a = [6, -8, 7, -42]$$$. Then, find_answer(n, a) would return $$$7$$$, but the correct answer is $$$3 \cdot (6-8+7) = 15$$$.You told Alice that her solution is incorrect, but she did not believe what you said.Given an integer $$$k$$$, you are to find any sequence $$$a$$$ of $$$n$$$ integers such that the correct answer and the answer produced by Alice's algorithm differ by exactly $$$k$$$. Note that although the choice of $$$n$$$ and the content of the sequence is yours, you must still follow the constraints earlier given: that $$$1 \leq n \leq 2\,000$$$ and that the absolute value of each element does not exceed $$$10^6$$$. If there is no such sequence, determine so.
256 megabytes
import java.io.*; import java.util.*; public class CFB { BufferedReader br; PrintWriter out; StringTokenizer st; boolean eof; private static final long MOD = 1000L * 1000L * 1000L + 7; private static final int[] dx = {0, -1, 0, 1}; private static final int[] dy = {1, 0, -1, 0}; private static final String yes = "Yes"; private static final String no = "No"; long[] res; int n = 2000; void solve() throws IOException { res = new long[n]; long k = nextInt(); long sum = n + k; res[0] = -1; long ave = sum / (n - 1); long rem = sum % (n - 1); for (int i = 1; i < n; i++) { res[i] = ave; } for (int i = 1; i < rem + 1; i++) { res[i]++; } outln(n); for (int i = 0; i < n; i++) { out(res[i] + " "); } } void shuffle(long[] a) { int n = a.length; for(int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); long tmp = a[i]; a[i] = a[r]; a[r] = tmp; } } long gcd(long a, long b) { while(a != 0 && b != 0) { long c = b; b = a % b; a = c; } return a + b; } private void outln(Object o) { out.println(o); } private void out(Object o) { out.print(o); } private void formatPrint(double val) { outln(String.format("%.9f%n", val)); } public CFB() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } public static void main(String[] args) throws IOException { new CFB(); } public long[] nextLongArr(int n) throws IOException{ long[] res = new long[n]; for(int i = 0; i < n; i++) res[i] = nextLong(); return res; } public int[] nextIntArr(int n) throws IOException { int[] res = new int[n]; for(int i = 0; i < n; i++) res[i] = nextInt(); return res; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; return null; } } return st.nextToken(); } public String nextString() { try { return br.readLine(); } catch (IOException e) { eof = true; return null; } } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }
Java
["8", "612"]
1 second
["4\n6 -8 7 -42", "7\n30 -12 -99 123 -2 245 -300"]
NoteThe first sample corresponds to the example given in the problem statement.In the second sample, one answer is $$$n = 7$$$ with $$$a = [30, -12, -99, 123, -2, 245, -300]$$$, in which case find_answer(n, a) returns $$$1098$$$, while the correct answer is $$$1710$$$.
Java 8
standard input
[ "constructive algorithms" ]
df7e4272ea2b48a5ad5f35a05c79044c
The first and only line contains one integer $$$k$$$ ($$$1 \leq k \leq 10^9$$$).
2,000
If there is no sought sequence, print "-1". Otherwise, in the first line, print one integer $$$n$$$ ($$$1 \leq n \leq 2\,000$$$), denoting the number of elements in the sequence. Then, in the second line, print $$$n$$$ space-separated integers: $$$a_0, a_1, \ldots, a_{n-1}$$$ ($$$|a_i| \leq 10^6$$$).
standard output
PASSED
32571960d84c0592706f67009185e3ff
train_001.jsonl
1551022500
Consider the following problem: given an array $$$a$$$ containing $$$n$$$ integers (indexed from $$$0$$$ to $$$n-1$$$), find $$$\max\limits_{0 \leq l \leq r \leq n-1} \sum\limits_{l \leq i \leq r} (r-l+1) \cdot a_i$$$. In this problem, $$$1 \leq n \leq 2\,000$$$ and $$$|a_i| \leq 10^6$$$.In an attempt to solve the problem described, Alice quickly came up with a blazing-fast greedy algorithm and coded it. Her implementation in pseudocode is as follows:function find_answer(n, a) # Assumes n is an integer between 1 and 2000, inclusive # Assumes a is a list containing n integers: a[0], a[1], ..., a[n-1] res = 0 cur = 0 k = -1 for i = 0 to i = n-1 cur = cur + a[i] if cur &lt; 0 cur = 0 k = i res = max(res, (i-k)*cur) return resAlso, as you can see, Alice's idea is not entirely correct. For example, suppose $$$n = 4$$$ and $$$a = [6, -8, 7, -42]$$$. Then, find_answer(n, a) would return $$$7$$$, but the correct answer is $$$3 \cdot (6-8+7) = 15$$$.You told Alice that her solution is incorrect, but she did not believe what you said.Given an integer $$$k$$$, you are to find any sequence $$$a$$$ of $$$n$$$ integers such that the correct answer and the answer produced by Alice's algorithm differ by exactly $$$k$$$. Note that although the choice of $$$n$$$ and the content of the sequence is yours, you must still follow the constraints earlier given: that $$$1 \leq n \leq 2\,000$$$ and that the absolute value of each element does not exceed $$$10^6$$$. If there is no such sequence, determine so.
256 megabytes
import java.util.*; import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; public class B { FastScanner in; PrintWriter out; boolean systemIO = true; public static void quickSort(int[] a, int from, int to) { if (to - from <= 1) { return; } int i = from; int j = to - 1; int x = a[from + (new Random()).nextInt(to - from)]; while (i <= j) { while (a[i] < x) { i++; } while (a[j] > x) { j--; } if (i <= j) { int t = a[i]; a[i] = a[j]; a[j] = t; i++; j--; } } quickSort(a, from, j + 1); quickSort(a, j + 1, to); } public long gcd(long x, long y) { if (y == 0) { return x; } if (x == 0) { return y; } return gcd(y, x % y); } public boolean prime(long x) { for (int i = 2; i * i <= x; i++) { if (x % i == 0) { return false; } } return true; } public long pow(long x, long p) { if (p == 0) { return 1; } long t = pow(x, p / 2); t *= t; t %= mod; if (p % 2 == 1) { t *= x; t %= mod; } return t; } public class Pair { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } } long mod = 0; public void solve() { int len = 1500; int k = in.nextInt() + len + 1; ArrayList<Integer> ans = new ArrayList<>(); ans.add(-1); int max = 1000000; while (k >= max) { k -= max; ans.add(max); } while (ans.size() < len + 1) { ans.add(k); k = 0; } out.println(ans.size()); for (int i : ans) { out.print(i + " "); } } public void run() { try { if (systemIO) { in = new FastScanner(System.in); out = new PrintWriter(System.out); } else { in = new FastScanner(new File("input.txt")); out = new PrintWriter(new File("output.txt")); } solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA public static void main(String[] arg) { new B().run(); } }
Java
["8", "612"]
1 second
["4\n6 -8 7 -42", "7\n30 -12 -99 123 -2 245 -300"]
NoteThe first sample corresponds to the example given in the problem statement.In the second sample, one answer is $$$n = 7$$$ with $$$a = [30, -12, -99, 123, -2, 245, -300]$$$, in which case find_answer(n, a) returns $$$1098$$$, while the correct answer is $$$1710$$$.
Java 8
standard input
[ "constructive algorithms" ]
df7e4272ea2b48a5ad5f35a05c79044c
The first and only line contains one integer $$$k$$$ ($$$1 \leq k \leq 10^9$$$).
2,000
If there is no sought sequence, print "-1". Otherwise, in the first line, print one integer $$$n$$$ ($$$1 \leq n \leq 2\,000$$$), denoting the number of elements in the sequence. Then, in the second line, print $$$n$$$ space-separated integers: $$$a_0, a_1, \ldots, a_{n-1}$$$ ($$$|a_i| \leq 10^6$$$).
standard output
PASSED
81c11ff08679eb6a751399bf4854389d
train_001.jsonl
1539269400
Consider some set of distinct characters $$$A$$$ and some string $$$S$$$, consisting of exactly $$$n$$$ characters, where each character is present in $$$A$$$.You are given an array of $$$m$$$ integers $$$b$$$ ($$$b_1 &lt; b_2 &lt; \dots &lt; b_m$$$). You are allowed to perform the following move on the string $$$S$$$: Choose some valid $$$i$$$ and set $$$k = b_i$$$; Take the first $$$k$$$ characters of $$$S = Pr_k$$$; Take the last $$$k$$$ characters of $$$S = Su_k$$$; Substitute the first $$$k$$$ characters of $$$S$$$ with the reversed $$$Su_k$$$; Substitute the last $$$k$$$ characters of $$$S$$$ with the reversed $$$Pr_k$$$. For example, let's take a look at $$$S =$$$ "abcdefghi" and $$$k = 2$$$. $$$Pr_2 =$$$ "ab", $$$Su_2 =$$$ "hi". Reversed $$$Pr_2 =$$$ "ba", $$$Su_2 =$$$ "ih". Thus, the resulting $$$S$$$ is "ihcdefgba".The move can be performed arbitrary number of times (possibly zero). Any $$$i$$$ can be selected multiple times over these moves.Let's call some strings $$$S$$$ and $$$T$$$ equal if and only if there exists such a sequence of moves to transmute string $$$S$$$ to string $$$T$$$. For the above example strings "abcdefghi" and "ihcdefgba" are equal. Also note that this implies $$$S = S$$$.The task is simple. Count the number of distinct strings.The answer can be huge enough, so calculate it modulo $$$998244353$$$.
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); SideTransmutations solver = new SideTransmutations(); solver.solve(1, in, out); out.close(); } static class SideTransmutations { long MOD = 998244353; long inv2 = MathUtils.inverse(2, MOD); long A; long[] pref; long[] dp; public void solve(int testNumber, InputReader in, PrintWriter out) { long N = in.nextInt(); long M = in.nextInt(); A = in.nextInt(); int[] arr = new int[(int) (M + 1)]; for (int i = 1; i <= M; i++) { arr[i] = in.nextInt(); } pref = new long[(int) (M)]; for (int i = 0; i < M; i++) { pref[i] = arr[i + 1] - arr[i]; } dp = new long[(int) (M + 1)]; dp[0] = 1; for (int i = 1; i <= M; i++) { dp[i] = dp[i - 1] + (dp[i - 1] * (MathUtils.binPow(A, pref[i - 1], MOD) - 1) % MOD) * inv2; dp[i] %= MOD; } out.println((dp[(int) (M)] * MathUtils.binPow(A, N - arr[(int) (M)], MOD)) % MOD); } } static class MathUtils { public static long inverse(long a, long mod) { long[] inv = extended_gcd(a, mod); if (inv[0] != 1) { return 0; } else { return (inv[1] + 2 * mod) % mod; } } public static long[] extended_gcd(long a, long b) { //three numbers, first is gcd, second is x, third is y if (a == 0) { return new long[]{b, 0, 1}; } long[] next = extended_gcd(b % a, a); long tempX = next[1]; next[1] = next[2] - (b / a) * next[1]; next[2] = tempX; return next; } public static long binPow(long b, long p, long mod) { long res = 1; while (p > 0) { if ((p & 1) == 1) { res *= b; res %= mod; } b = b * b; p >>= 1; b %= mod; } return res; } } 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 1 2\n1", "9 2 26\n2 3", "12 3 1\n2 5 6"]
2 seconds
["6", "150352234", "1"]
NoteHere are all the distinct strings for the first example. The chosen letters 'a' and 'b' are there just to show that the characters in $$$A$$$ are different. "aaa" "aab" = "baa" "aba" "abb" = "bba" "bab" "bbb"
Java 8
standard input
[ "combinatorics", "strings" ]
385ac4db5b0e7613b03fb4f1044367dd
The first line contains three integers $$$n$$$, $$$m$$$ and $$$|A|$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le m \le min(\frac n 2, 2 \cdot 10^5)$$$, $$$1 \le |A| \le 10^9$$$) β€” the length of the strings, the size of the array $$$b$$$ and the size of the set $$$A$$$, respectively. The second line contains $$$m$$$ integers $$$b_1, b_2, \dots, b_m$$$ ($$$1 \le b_i \le \frac n 2$$$, $$$b_1 &lt; b_2 &lt; \dots &lt; b_m$$$).
2,300
Print a single integer β€” the number of distinct strings of length $$$n$$$ with characters from set $$$A$$$ modulo $$$998244353$$$.
standard output
PASSED
f175646c5a668e5026746ab2b05030aa
train_001.jsonl
1539269400
Consider some set of distinct characters $$$A$$$ and some string $$$S$$$, consisting of exactly $$$n$$$ characters, where each character is present in $$$A$$$.You are given an array of $$$m$$$ integers $$$b$$$ ($$$b_1 &lt; b_2 &lt; \dots &lt; b_m$$$). You are allowed to perform the following move on the string $$$S$$$: Choose some valid $$$i$$$ and set $$$k = b_i$$$; Take the first $$$k$$$ characters of $$$S = Pr_k$$$; Take the last $$$k$$$ characters of $$$S = Su_k$$$; Substitute the first $$$k$$$ characters of $$$S$$$ with the reversed $$$Su_k$$$; Substitute the last $$$k$$$ characters of $$$S$$$ with the reversed $$$Pr_k$$$. For example, let's take a look at $$$S =$$$ "abcdefghi" and $$$k = 2$$$. $$$Pr_2 =$$$ "ab", $$$Su_2 =$$$ "hi". Reversed $$$Pr_2 =$$$ "ba", $$$Su_2 =$$$ "ih". Thus, the resulting $$$S$$$ is "ihcdefgba".The move can be performed arbitrary number of times (possibly zero). Any $$$i$$$ can be selected multiple times over these moves.Let's call some strings $$$S$$$ and $$$T$$$ equal if and only if there exists such a sequence of moves to transmute string $$$S$$$ to string $$$T$$$. For the above example strings "abcdefghi" and "ihcdefgba" are equal. Also note that this implies $$$S = S$$$.The task is simple. Count the number of distinct strings.The answer can be huge enough, so calculate it modulo $$$998244353$$$.
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; import java.math.*; public class D { // Exponentation public static int exp(int base, int e, int mod) { if(e == 0) return 1; if(e == 1) return base; int val = exp(base, e/2, mod); int ans = (int)(1L*val*val % mod); if(e % 2 == 1) ans = (int)(1L*ans*base % mod); return ans; } public static void main(String[] args) throws Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); // Scanner scan = new Scanner(System.in); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); // int n = Integer.parseInt(bf.readLine()); StringTokenizer st = new StringTokenizer(bf.readLine()); long mod = 998244353; int mod_i = 998244353; int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); st = new StringTokenizer(bf.readLine()); int[] a = new int[m]; for(int i=0; i<m; i++) a[i] = Integer.parseInt(st.nextToken()); int middle = n - 2*a[m-1]; long ans = exp(b, middle, mod_i); for(int i=0; i<m; i++) { int diff = a[i]; if(i != 0) diff -= a[i-1]; long val = exp(b, diff, mod_i); long val2 = (val*(val-1)/2 % mod + val) % mod; ans = (ans * val2) % mod; } out.println(ans); // int n = scan.nextInt(); out.close(); System.exit(0); } }
Java
["3 1 2\n1", "9 2 26\n2 3", "12 3 1\n2 5 6"]
2 seconds
["6", "150352234", "1"]
NoteHere are all the distinct strings for the first example. The chosen letters 'a' and 'b' are there just to show that the characters in $$$A$$$ are different. "aaa" "aab" = "baa" "aba" "abb" = "bba" "bab" "bbb"
Java 8
standard input
[ "combinatorics", "strings" ]
385ac4db5b0e7613b03fb4f1044367dd
The first line contains three integers $$$n$$$, $$$m$$$ and $$$|A|$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le m \le min(\frac n 2, 2 \cdot 10^5)$$$, $$$1 \le |A| \le 10^9$$$) β€” the length of the strings, the size of the array $$$b$$$ and the size of the set $$$A$$$, respectively. The second line contains $$$m$$$ integers $$$b_1, b_2, \dots, b_m$$$ ($$$1 \le b_i \le \frac n 2$$$, $$$b_1 &lt; b_2 &lt; \dots &lt; b_m$$$).
2,300
Print a single integer β€” the number of distinct strings of length $$$n$$$ with characters from set $$$A$$$ modulo $$$998244353$$$.
standard output
PASSED
a7e3ec94778de13191f1781510ee2088
train_001.jsonl
1539269400
Consider some set of distinct characters $$$A$$$ and some string $$$S$$$, consisting of exactly $$$n$$$ characters, where each character is present in $$$A$$$.You are given an array of $$$m$$$ integers $$$b$$$ ($$$b_1 &lt; b_2 &lt; \dots &lt; b_m$$$). You are allowed to perform the following move on the string $$$S$$$: Choose some valid $$$i$$$ and set $$$k = b_i$$$; Take the first $$$k$$$ characters of $$$S = Pr_k$$$; Take the last $$$k$$$ characters of $$$S = Su_k$$$; Substitute the first $$$k$$$ characters of $$$S$$$ with the reversed $$$Su_k$$$; Substitute the last $$$k$$$ characters of $$$S$$$ with the reversed $$$Pr_k$$$. For example, let's take a look at $$$S =$$$ "abcdefghi" and $$$k = 2$$$. $$$Pr_2 =$$$ "ab", $$$Su_2 =$$$ "hi". Reversed $$$Pr_2 =$$$ "ba", $$$Su_2 =$$$ "ih". Thus, the resulting $$$S$$$ is "ihcdefgba".The move can be performed arbitrary number of times (possibly zero). Any $$$i$$$ can be selected multiple times over these moves.Let's call some strings $$$S$$$ and $$$T$$$ equal if and only if there exists such a sequence of moves to transmute string $$$S$$$ to string $$$T$$$. For the above example strings "abcdefghi" and "ihcdefgba" are equal. Also note that this implies $$$S = S$$$.The task is simple. Count the number of distinct strings.The answer can be huge enough, so calculate it modulo $$$998244353$$$.
256 megabytes
import java.io.*; import java.util.*; public class CF1065E { static final int MD = 998244353; static long power(int a, int k) { if (k == 0) return 1; long p = power(a, k / 2); p = p * p % MD; if (k % 2 == 1) p = p * a % MD; return p; } 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()); int a = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); int[] bb = new int[m]; for (int j = 0; j < m; j++) bb[j] = Integer.parseInt(st.nextToken()); long ans = power(a, n - bb[m - 1] * 2); for (int j = 0, b = 0; j < m; b = bb[j++]) { long p = power(a, bb[j] - b); ans = ans * (p * (p + 1) / 2 % MD) % MD; } System.out.println(ans); } }
Java
["3 1 2\n1", "9 2 26\n2 3", "12 3 1\n2 5 6"]
2 seconds
["6", "150352234", "1"]
NoteHere are all the distinct strings for the first example. The chosen letters 'a' and 'b' are there just to show that the characters in $$$A$$$ are different. "aaa" "aab" = "baa" "aba" "abb" = "bba" "bab" "bbb"
Java 8
standard input
[ "combinatorics", "strings" ]
385ac4db5b0e7613b03fb4f1044367dd
The first line contains three integers $$$n$$$, $$$m$$$ and $$$|A|$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le m \le min(\frac n 2, 2 \cdot 10^5)$$$, $$$1 \le |A| \le 10^9$$$) β€” the length of the strings, the size of the array $$$b$$$ and the size of the set $$$A$$$, respectively. The second line contains $$$m$$$ integers $$$b_1, b_2, \dots, b_m$$$ ($$$1 \le b_i \le \frac n 2$$$, $$$b_1 &lt; b_2 &lt; \dots &lt; b_m$$$).
2,300
Print a single integer β€” the number of distinct strings of length $$$n$$$ with characters from set $$$A$$$ modulo $$$998244353$$$.
standard output
PASSED
3eaeb00ccb2406fe973d4886e91e9012
train_001.jsonl
1581518100
Your company was appointed to lay new asphalt on the highway of length $$$n$$$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $$$g$$$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $$$b$$$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $$$g$$$ good days, $$$b$$$ bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days $$$1, 2, \dots, g$$$ are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $$$n = 5$$$ then at least $$$3$$$ units of the highway should have high quality; if $$$n = 4$$$ then at least $$$2$$$ units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.FileReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); BNationalProject solver = new BNationalProject(); solver.solve(1, in, out); out.close(); } static class BNationalProject { public void solve(int testNumber, Scanner sc, PrintWriter pw) { int q = sc.nextInt(); while (q-- > 0) { int n = sc.nextInt(); int good = sc.nextInt(); int bad = sc.nextInt(); if (good >= n) pw.println(n); else { long req = (n + 1) / 2; long sum = 1l * good + bad; long cur = 1l * (n / sum) * good + Math.min(n % sum, good); if (cur >= req) pw.println(n); else { long temp = ((req + good - 1) / good) - 1; temp *= bad; long rem = Math.max(n - ((req + good - 1) / good) * good - temp, 0); long min=req%good==0?0:good-req%good; pw.println((sum * ((req + good - 1) / good)) - bad + rem-min); } } } } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["3\n5 1 1\n8 10 10\n1000000 1 1000000"]
2 seconds
["5\n8\n499999500000"]
NoteIn the first test case, you can just lay new asphalt each day, since days $$$1, 3, 5$$$ are good.In the second test case, you can also lay new asphalt each day, since days $$$1$$$-$$$8$$$ are good.
Java 8
standard input
[ "math" ]
be9138aca8e1b8a5d722f99fcd70b685
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains three integers $$$n$$$, $$$g$$$ and $$$b$$$ ($$$1 \le n, g, b \le 10^9$$$) β€” the length of the highway and the number of good and bad days respectively.
1,400
Print $$$T$$$ integers β€” one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.
standard output
PASSED
1ac8537e8e9bba75f4e635b143165c3f
train_001.jsonl
1581518100
Your company was appointed to lay new asphalt on the highway of length $$$n$$$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $$$g$$$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $$$b$$$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $$$g$$$ good days, $$$b$$$ bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days $$$1, 2, \dots, g$$$ are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $$$n = 5$$$ then at least $$$3$$$ units of the highway should have high quality; if $$$n = 4$$$ then at least $$$2$$$ units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?
256 megabytes
import java.io.*; import java.text.DecimalFormat; import java.util.*; import java.util.function.BinaryOperator; public class Main { private final static long mod = 1000000007; private final static int MAXN = 1000001; private static long power(long x, long y, long m) { long temp; if (y == 0) return 1; temp = power(x, y / 2, m); temp = (temp * temp) % m; if (y % 2 == 0) return temp; else return ((x % m) * temp) % m; } private static long power(long x, long y) { return power(x, y, mod); } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static int nextPowerOf2(int a) { return 1 << nextLog2(a); } static int nextLog2(int a) { return (a == 0 ? 0 : 32 - Integer.numberOfLeadingZeros(a - 1)); } private static long modInverse(long a, long m) { long m0 = m; long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { long q = a / m; long t = m; m = a % m; a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } private static int[] getLogArr(int n) { int arr[] = new int[n + 1]; for (int i = 1; i < n + 1; i++) { arr[i] = (int) (Math.log(i) / Math.log(2) + 1e-10); } return arr; } private static int log[] = getLogArr(MAXN); private static int getLRSpt(int st[][], int L, int R, BinaryOperator<Integer> binaryOperator) { int j = log[R - L + 1]; return binaryOperator.apply(st[L][j], st[R - (1 << j) + 1][j]); } private static int[][] getSparseTable(int array[], BinaryOperator<Integer> binaryOperator) { int k = log[array.length+1] + 1; int st[][] = new int[array.length+1][k + 1]; for (int i = 0; i < array.length; i++) st[i][0] = array[i]; for (int j = 1; j <= k; j++) { for (int i = 0; i + (1 << j) <= array.length; i++) { st[i][j] = binaryOperator.apply(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); } } return st; } static class Subset { int parent; int rank; @Override public String toString() { return "" + parent; } } static int find(Subset[] Subsets, int i) { if (Subsets[i].parent != i) Subsets[i].parent = find(Subsets, Subsets[i].parent); return Subsets[i].parent; } static void union(Subset[] Subsets, int x, int y) { int xroot = find(Subsets, x); int yroot = find(Subsets, y); if (Subsets[xroot].rank < Subsets[yroot].rank) Subsets[xroot].parent = yroot; else if (Subsets[yroot].rank < Subsets[xroot].rank) Subsets[yroot].parent = xroot; else { Subsets[xroot].parent = yroot; Subsets[yroot].rank++; } } private static int maxx(Integer... a) { return Collections.max(Arrays.asList(a)); } private static int minn(Integer... a) { return Collections.min(Arrays.asList(a)); } private static long maxx(Long... a) { return Collections.max(Arrays.asList(a)); } private static long minn(Long... a) { return Collections.min(Arrays.asList(a)); } private static class Pair<T, U> { T a; U b; public Pair(T a, U b) { this.a = a; this.b = b; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; return a.equals(pair.a) && b.equals(pair.b); } @Override public int hashCode() { return Objects.hash(a, b); } @Override public String toString() { return "(" + a + "," + b + ')'; } } private static boolean isPrimeAKS(long n) { if(n%mod==0||n<2) return false; if(n>2&&n%2==0)return false; return (power((mod-1),n, n)==(power(mod,n,n)-1+n)%n); } public static int upperBound(List<Integer> list, int value) { int low = 0; int high = list.size(); while (low < high) { final int mid = (low + high) / 2; if (value >= list.get(mid)) { low = mid + 1; } else { high = mid; } } return low; } private static int[] getLPSArray(String pattern) { int i,j,n=pattern.length(); int[] lps = new int[pattern.length()]; lps[0]=0; for(i=1,j=0;i<n;){ if(pattern.charAt(i)==pattern.charAt(j)) { lps[i++]=++j; } else if(j>0){ j=lps[j-1]; } else { lps[i++]=0; } } return lps; } private static List<Integer> findPattern(String text, String pattern) { List<Integer> matchedIndexes = new ArrayList<>(); if(pattern.length()==0) { return matchedIndexes; } int[] lps = getLPSArray(pattern); int i=0,j=0,n=text.length(),m=pattern.length(); while(i<n) { if(text.charAt(i)==pattern.charAt(j)) { i++; j++; } if (j == m) { matchedIndexes.add(i - j); j = lps[j - 1]; } if(i<n&&text.charAt(i)!=pattern.charAt(j)) { if (j > 0) { j = lps[j - 1]; } else { i++; } } } return matchedIndexes; } private static Set<Long> getDivisors(long n) { Set<Long> divisors = new HashSet<>(); divisors.add(1L); divisors.add(n); for (long i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) { divisors.add(i); divisors.add(n / i); } } return divisors; } private static long getLCM(long a, long b) { return (Math.max(a,b)/gcd(a,b))*Math.min(a,b); } private static int getState(int grid[][], int i) { if(i==0||i>=grid[0].length)return 0; int x= grid[0][i-1]+ 2*(grid[1][i-1]+2*(grid[0][i]+ 2*(grid[1][i]))); return x; } static long fac[]=new long[2000005]; static long ifac[]=new long[2000005]; private static void preCompute(){ fac[0]=ifac[0]=fac[1]=ifac[1]=1; int i; for(i=2;i<2000005;i++){ fac[i]=(i*fac[i-1])%mod; ifac[i]=(power(i,mod-2)*ifac[i-1])%mod; } } private static long C(int n,int r){ return (fac[n]*((ifac[r]*ifac[n-r])%mod))%mod; } public static void main(String[] args) throws Exception { long START_TIME = System.currentTimeMillis(); try (FastReader in = new FastReader(); FastWriter out = new FastWriter()) { int t, i, j, n, k, l, r, c,m, ti, tidx,gm,mx,mx1,mn,p,q,g,b; for (t = in.nextInt(), tidx = 1; tidx <= t; tidx++) { long w,x=0, y=0, z=0,sum=0,bhc=0,ans=0; //out.print(String.format("Case #%d: ", tidx)); n=in.nextInt(); g=in.nextInt(); b=in.nextInt(); y=(n+1)/2; ans=Math.max(n,((y-1)/g)*(g+b)+(y%g>0?y%g:g)); out.println(ans); } if(args.length>0 && "ex_time".equals(args[0])) { out.print("\nTime taken: "); out.println(System.currentTimeMillis()-START_TIME); } out.commit(); } catch (Exception e){ e.printStackTrace(); } } public static class FastReader implements Closeable { private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer st; String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception 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; } int[] nextIntArr(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } double[] nextDoubleArr(int n) { double[] arr = new double[n]; for (int i = 0; i < n; i++) { arr[i] = nextDouble(); } return arr; } long[] nextLongArr(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } String[] nextStrArr(int n) { String[] arr = new String[n]; for (int i = 0; i < n; i++) { arr[i] = next(); } return arr; } long[][] nextLongArr2(int n, int m) { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) { arr[i] = nextLongArr(m); } return arr; } @Override public void close() throws IOException { br.close(); } } public static class FastWriter implements Closeable { BufferedWriter bw; StringBuilder sb = new StringBuilder(); List<String> list = new ArrayList<>(); Set<String> set = new HashSet<>(); public FastWriter() { bw = new BufferedWriter(new OutputStreamWriter(System.out)); } <T> void commit() throws IOException { bw.write(sb.toString()); bw.flush(); sb = new StringBuilder(); } public <T> void print(T obj) throws IOException { sb.append(obj.toString()); commit(); } public void println() throws IOException { print("\n"); } public <T> void println(T obj) throws IOException { print(obj.toString() + "\n"); } <T> void printArrLn(T[] arr) throws IOException { for (int i = 0; i < arr.length - 1; i++) { print(arr[i] + " "); } println(arr[arr.length - 1]); } <T> void printArr2(T[][] arr) throws IOException { for (int j = 0; j < arr.length; j++) { for (int i = 0; i < arr[j].length - 1; i++) { print(arr[j][i] + " "); } println(arr[j][arr.length - 1]); } } <T> void printColl(Collection<T> coll) throws IOException { for (T e : coll) { print(e + " "); } println(); } void printCharN(char c, int n) throws IOException { for (int i = 0; i < n; i++) { print(c); } } void printIntArr2(int[][] arr) throws IOException { for (int j = 0; j < arr.length; j++) { for (int i = 0; i < arr[j].length - 1; i++) { print(arr[j][i] + " "); } println(arr[j][arr.length - 1]); } } @Override public void close() throws IOException { bw.close(); } } }
Java
["3\n5 1 1\n8 10 10\n1000000 1 1000000"]
2 seconds
["5\n8\n499999500000"]
NoteIn the first test case, you can just lay new asphalt each day, since days $$$1, 3, 5$$$ are good.In the second test case, you can also lay new asphalt each day, since days $$$1$$$-$$$8$$$ are good.
Java 8
standard input
[ "math" ]
be9138aca8e1b8a5d722f99fcd70b685
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains three integers $$$n$$$, $$$g$$$ and $$$b$$$ ($$$1 \le n, g, b \le 10^9$$$) β€” the length of the highway and the number of good and bad days respectively.
1,400
Print $$$T$$$ integers β€” one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.
standard output
PASSED
3683fd225b976f4c200c469a2c995221
train_001.jsonl
1581518100
Your company was appointed to lay new asphalt on the highway of length $$$n$$$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $$$g$$$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $$$b$$$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $$$g$$$ good days, $$$b$$$ bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days $$$1, 2, \dots, g$$$ are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $$$n = 5$$$ then at least $$$3$$$ units of the highway should have high quality; if $$$n = 4$$$ then at least $$$2$$$ units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class CodeForce{ public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while(t-->0){ String[] str=br.readLine().split(" "); long n=Integer.parseInt(str[0]); long g=Integer.parseInt(str[1]); long b=Integer.parseInt(str[2]); long ss; if(n%2==0) ss=n/2; else ss=(n/2)+1; if(n<=g){ System.out.println(n); } else if(g>=ss){ long a,c; a=n-g; System.out.println(g+a); } else{ long good=0,f,m; long days=0; f=ss/g; m=ss%g; days=(f*g); if(m==0) f--; days=days+(f*b)+m; if(days<n) days+=(n-days); System.out.println(days); } } } }
Java
["3\n5 1 1\n8 10 10\n1000000 1 1000000"]
2 seconds
["5\n8\n499999500000"]
NoteIn the first test case, you can just lay new asphalt each day, since days $$$1, 3, 5$$$ are good.In the second test case, you can also lay new asphalt each day, since days $$$1$$$-$$$8$$$ are good.
Java 8
standard input
[ "math" ]
be9138aca8e1b8a5d722f99fcd70b685
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains three integers $$$n$$$, $$$g$$$ and $$$b$$$ ($$$1 \le n, g, b \le 10^9$$$) β€” the length of the highway and the number of good and bad days respectively.
1,400
Print $$$T$$$ integers β€” one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.
standard output
PASSED
b5608aa8d371505203c4d0f9229ea253
train_001.jsonl
1581518100
Your company was appointed to lay new asphalt on the highway of length $$$n$$$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $$$g$$$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $$$b$$$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $$$g$$$ good days, $$$b$$$ bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days $$$1, 2, \dots, g$$$ are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $$$n = 5$$$ then at least $$$3$$$ units of the highway should have high quality; if $$$n = 4$$$ then at least $$$2$$$ units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?
256 megabytes
import java.util.Scanner; public class Main { public static long count(int len, int g, int b){ int gd = (int) Math.ceil((double) len / 2); int period = g + b; if (gd < g) return (long) len; else{ int n = (int) Math.floor(gd / g); if (gd % g == 0) return Math.max(len, (long) n * period - b); else return Math.max((long) n * period + gd % g, len); } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int k = Integer.parseInt(scanner.nextLine()); int[] input = new int[k*3]; for (int i = 0; i < 3 * k; i++){ input[i] = scanner.nextInt(); } for (int i = 0; i < k; i++){ System.out.println(count(input[3*i], input[3*i+1], input[3*i+2])); } } }
Java
["3\n5 1 1\n8 10 10\n1000000 1 1000000"]
2 seconds
["5\n8\n499999500000"]
NoteIn the first test case, you can just lay new asphalt each day, since days $$$1, 3, 5$$$ are good.In the second test case, you can also lay new asphalt each day, since days $$$1$$$-$$$8$$$ are good.
Java 8
standard input
[ "math" ]
be9138aca8e1b8a5d722f99fcd70b685
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains three integers $$$n$$$, $$$g$$$ and $$$b$$$ ($$$1 \le n, g, b \le 10^9$$$) β€” the length of the highway and the number of good and bad days respectively.
1,400
Print $$$T$$$ integers β€” one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.
standard output
PASSED
dd4d4857e53749aa9cdb7ec4e98fe83e
train_001.jsonl
1581518100
Your company was appointed to lay new asphalt on the highway of length $$$n$$$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $$$g$$$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $$$b$$$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $$$g$$$ good days, $$$b$$$ bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days $$$1, 2, \dots, g$$$ are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $$$n = 5$$$ then at least $$$3$$$ units of the highway should have high quality; if $$$n = 4$$$ then at least $$$2$$$ units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; public class B { static final int MOD = 1000000007; // 1e9 + 7 static final boolean AUTO_FLUSH = false; // slow if true // int = num(); // long = ll(); // string = next(); // a string line = line(); // ---------------------------------- \ void main() { int t = num(); while (t-- > 0) { long n = ll(), g = ll(), b = ll(); long need = n / 2 + (n % 2 == 1 ? 1 : 0) - g; if (need < 0) { outln(n); continue; } long ans = need / g * (g + b); if (need % g > 0) { ans += b + need % g; } outln(Math.max(ans + g, n)); } } // ---------------------------------- \ // #region public static void main(String[] args) { startTime(); new B().main(); boolean endsWithEnter = TO_BE_PRINTED.length() == 0 || TO_BE_PRINTED.charAt(TO_BE_PRINTED.length() - 1) == '\n'; flush(); if (!endsWithEnter) log('\n'); logTime(); logMem(); } static Random RAND = new Random(); static int random(int from, int to) { return RAND.nextInt(to - from + 1) + from; } static void logTime() { log("Time: " + getTime() + "ms"); } static void logMem() { log("Memory (End): " + (int) (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 + " KB"); } static long ll() { return Long.parseLong(next()); } static int num() { return Integer.parseInt(next()); } static void generateInputMode() throws Exception { System.setOut(new PrintStream(new FileOutputStream("~/kode/input.txt"))); } static String line() { if (!tokenize()) return null; INPUT_STREAM.setLength(0); if (STRING_TOKENIZER.hasMoreTokens()) INPUT_STREAM.append(STRING_TOKENIZER.nextToken()); while (STRING_TOKENIZER.hasMoreTokens()) INPUT_STREAM.append(' ').append(STRING_TOKENIZER.nextToken()); return INPUT_STREAM.length() == 0 ? null : INPUT_STREAM.toString(); } static void startTime() { TIME_COMPLEXITY = System.currentTimeMillis(); } static long getTime() { return System.currentTimeMillis() - TIME_COMPLEXITY; } static void flush() { System.out.print(TO_BE_PRINTED.toString()); TO_BE_PRINTED.setLength(0); } static void out(Object o) { TO_BE_PRINTED.append(o); if (AUTO_FLUSH) flush(); } static void _log(String s) { System.err.print(s); } static void _logln(String s) { System.err.println(s); } static void _logln() { System.err.println(); } static void outln(Object o) { out(o); outln(); } static void outln() { out("\n"); } static class Pair implements Comparable<Pair> { public int a, b; public Pair(int a, int b) { this.a = a; this.b = b; } public static Pair of(int a, int b) { return new Pair(a, b); } @Override public int compareTo(Pair p) { if (a == p.a) return Integer.compare(b, p.b); return Integer.compare(a, p.a); } @Override public String toString() { return "[" + a + "," + b + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + a; result = prime * result + b; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Pair other = (Pair) obj; if (a != other.a) return false; if (b != other.b) return false; return true; } } static void logArr(Object val) { Class<?> valKlass = val.getClass(); Object[] outputArray = null; for (Class<?> arrKlass : new Class<?>[] { int[].class, float[].class, double[].class, boolean[].class, byte[].class, short[].class, long[].class, char[].class }) { if (valKlass.isAssignableFrom(arrKlass)) { int arrlength = Array.getLength(val); outputArray = new Object[arrlength]; for (int i = 0; i < arrlength; ++i) outputArray[i] = Array.get(val, i); break; } } if (outputArray == null) outputArray = (Object[]) val; logArr0(outputArray); } static void logArr0(Object[] objs) { if (objs.length == 0) { _log("* \n"); return; } _log("* " + objs[0]); for (int i = 1; i < objs.length; i++) _log(objs[i].toString().equals("\n") ? "\n>" : (" " + objs[i])); _logln(); } static void log(Object... objs) { logArr0(objs); } static String next() { return tokenize() ? STRING_TOKENIZER.nextToken() : null; } static boolean tokenize() { if (STRING_TOKENIZER == null || !STRING_TOKENIZER.hasMoreTokens()) { try { STRING_TOKENIZER = new StringTokenizer(STREAM_READER.readLine()); } catch (Exception _e) { return false; } } return true; } static long TIME_COMPLEXITY; static BufferedReader STREAM_READER = new BufferedReader(new InputStreamReader(System.in), 32768); static StringTokenizer STRING_TOKENIZER; static StringBuilder INPUT_STREAM = new StringBuilder(), TO_BE_PRINTED = new StringBuilder(); // #endregion }
Java
["3\n5 1 1\n8 10 10\n1000000 1 1000000"]
2 seconds
["5\n8\n499999500000"]
NoteIn the first test case, you can just lay new asphalt each day, since days $$$1, 3, 5$$$ are good.In the second test case, you can also lay new asphalt each day, since days $$$1$$$-$$$8$$$ are good.
Java 8
standard input
[ "math" ]
be9138aca8e1b8a5d722f99fcd70b685
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains three integers $$$n$$$, $$$g$$$ and $$$b$$$ ($$$1 \le n, g, b \le 10^9$$$) β€” the length of the highway and the number of good and bad days respectively.
1,400
Print $$$T$$$ integers β€” one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.
standard output
PASSED
032ea8f0dd4176332a9484cb9b122fb7
train_001.jsonl
1581518100
Your company was appointed to lay new asphalt on the highway of length $$$n$$$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $$$g$$$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $$$b$$$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $$$g$$$ good days, $$$b$$$ bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days $$$1, 2, \dots, g$$$ are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $$$n = 5$$$ then at least $$$3$$$ units of the highway should have high quality; if $$$n = 4$$$ then at least $$$2$$$ units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?
256 megabytes
import java.util.*; public class Main { private static final int INF = 2_000_000_000; public static void main(String[] args) { Scanner sc = new Scanner(System.in); StringBuilder ans = new StringBuilder(); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int g = sc.nextInt(); int b = sc.nextInt(); long cycle = (long)n/(g+b); long remain = n-cycle*(g+b); long allG = cycle*g + Math.min(remain, g); if ((n+1)/2 <= allG) { ans.append(n).append('\n'); } else { // skip cycle = ((long)n+1)/2/g; remain = (n+1)/2 - cycle*g; if (remain == 0) { ans.append((cycle-1)*(g+b)+g).append('\n'); } else { ans.append(cycle*(g+b)+remain).append('\n'); } } } System.out.print(ans); } }
Java
["3\n5 1 1\n8 10 10\n1000000 1 1000000"]
2 seconds
["5\n8\n499999500000"]
NoteIn the first test case, you can just lay new asphalt each day, since days $$$1, 3, 5$$$ are good.In the second test case, you can also lay new asphalt each day, since days $$$1$$$-$$$8$$$ are good.
Java 8
standard input
[ "math" ]
be9138aca8e1b8a5d722f99fcd70b685
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains three integers $$$n$$$, $$$g$$$ and $$$b$$$ ($$$1 \le n, g, b \le 10^9$$$) β€” the length of the highway and the number of good and bad days respectively.
1,400
Print $$$T$$$ integers β€” one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.
standard output
PASSED
f0b390514a1b3a8619779eae934d8584
train_001.jsonl
1581518100
Your company was appointed to lay new asphalt on the highway of length $$$n$$$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $$$g$$$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $$$b$$$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $$$g$$$ good days, $$$b$$$ bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days $$$1, 2, \dots, g$$$ are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $$$n = 5$$$ then at least $$$3$$$ units of the highway should have high quality; if $$$n = 4$$$ then at least $$$2$$$ units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; import java.math.*; public class A { public void run() throws Exception { FastScanner sc = new FastScanner(); int test = sc.nextInt(); outer: for (int i = 0; i<test; i++) { long n = sc.nextLong(); long g = sc.nextLong(); long b = sc.nextLong(); long high = (long) Math.ceil(n/2.0); long low = n-high; long remh = high%g; long divh = high/g; if (divh == 0) { System.out.println(n); continue outer; } long best = (divh-1)*(g+b)+g; long extra = 0; if (remh != 0) { extra=b; best+=remh; } // System.out.println(best + " " + extra); if ((divh-1)*b+extra<low) { extra+=low-(divh-1)*b-extra; } System.out.println(Math.max(best+extra, n)); } } //SSR <3 2020 static class FastScanner { public BufferedReader reader; public StringTokenizer tokenizer; public FastScanner() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } } public static void main (String[] args) throws Exception { new A().run(); } public void shuffleArray(long[] arr) { int n = arr.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = arr[i]; int randomPos = i + rnd.nextInt(n-i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } }
Java
["3\n5 1 1\n8 10 10\n1000000 1 1000000"]
2 seconds
["5\n8\n499999500000"]
NoteIn the first test case, you can just lay new asphalt each day, since days $$$1, 3, 5$$$ are good.In the second test case, you can also lay new asphalt each day, since days $$$1$$$-$$$8$$$ are good.
Java 8
standard input
[ "math" ]
be9138aca8e1b8a5d722f99fcd70b685
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains three integers $$$n$$$, $$$g$$$ and $$$b$$$ ($$$1 \le n, g, b \le 10^9$$$) β€” the length of the highway and the number of good and bad days respectively.
1,400
Print $$$T$$$ integers β€” one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.
standard output
PASSED
16eb65bc8da3ef91da16e2b12a1761f6
train_001.jsonl
1581518100
Your company was appointed to lay new asphalt on the highway of length $$$n$$$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $$$g$$$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $$$b$$$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $$$g$$$ good days, $$$b$$$ bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days $$$1, 2, \dots, g$$$ are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $$$n = 5$$$ then at least $$$3$$$ units of the highway should have high quality; if $$$n = 4$$$ then at least $$$2$$$ units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?
256 megabytes
import java.util.*; import java.io.*; public class NationalProject { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(br.readLine()); while(T-->0) { String[] s= br.readLine().split(" "); long n = Long.parseLong(s[0]); long g = Long.parseLong(s[1]); long b = Long.parseLong(s[2]); long needG = (n+1)/2; long total = needG/ g*(b+g); total += (needG%g==0)?-b:needG%g; System.out.println(Math.max(n,total)); } } }
Java
["3\n5 1 1\n8 10 10\n1000000 1 1000000"]
2 seconds
["5\n8\n499999500000"]
NoteIn the first test case, you can just lay new asphalt each day, since days $$$1, 3, 5$$$ are good.In the second test case, you can also lay new asphalt each day, since days $$$1$$$-$$$8$$$ are good.
Java 8
standard input
[ "math" ]
be9138aca8e1b8a5d722f99fcd70b685
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains three integers $$$n$$$, $$$g$$$ and $$$b$$$ ($$$1 \le n, g, b \le 10^9$$$) β€” the length of the highway and the number of good and bad days respectively.
1,400
Print $$$T$$$ integers β€” one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.
standard output
PASSED
2cc083605518fafc9c4526081f0510d1
train_001.jsonl
1581518100
Your company was appointed to lay new asphalt on the highway of length $$$n$$$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $$$g$$$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $$$b$$$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $$$g$$$ good days, $$$b$$$ bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days $$$1, 2, \dots, g$$$ are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $$$n = 5$$$ then at least $$$3$$$ units of the highway should have high quality; if $$$n = 4$$$ then at least $$$2$$$ units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?
256 megabytes
import java.util.Scanner; public class B1303 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); for (int i = 0; i < t; i++) { int n = scanner.nextInt(); int g = scanner.nextInt(); int b = scanner.nextInt(); process(n, g, b); } } private static void process(int n, int g, int b) { long half = (n + 1) / 2; if (g >= b || g >= half) { System.out.println(n); return; } // long good_length = n; // good_length -= g; long days = half / g * (g + b); if (half % g == 0) days -= b; else days += half % g; System.out.println(Math.max(n, days)); } }
Java
["3\n5 1 1\n8 10 10\n1000000 1 1000000"]
2 seconds
["5\n8\n499999500000"]
NoteIn the first test case, you can just lay new asphalt each day, since days $$$1, 3, 5$$$ are good.In the second test case, you can also lay new asphalt each day, since days $$$1$$$-$$$8$$$ are good.
Java 8
standard input
[ "math" ]
be9138aca8e1b8a5d722f99fcd70b685
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains three integers $$$n$$$, $$$g$$$ and $$$b$$$ ($$$1 \le n, g, b \le 10^9$$$) β€” the length of the highway and the number of good and bad days respectively.
1,400
Print $$$T$$$ integers β€” one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.
standard output
PASSED
335cdf57250cece72eef40bea4a7ff57
train_001.jsonl
1581518100
Your company was appointed to lay new asphalt on the highway of length $$$n$$$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $$$g$$$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $$$b$$$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $$$g$$$ good days, $$$b$$$ bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days $$$1, 2, \dots, g$$$ are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $$$n = 5$$$ then at least $$$3$$$ units of the highway should have high quality; if $$$n = 4$$$ then at least $$$2$$$ units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?
256 megabytes
import java.util.*; public class NationalProject { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while(t -->0) { long n = scanner.nextInt(); long g = scanner.nextInt(); long b = scanner.nextInt(); long res; long div = (n+1)/2; if(div%g!=0) { res = div/g * (b+g)+ div%g; } else { res = div/g * (b+g)- b; } System.out.println(Math.max(n, res)); } } }
Java
["3\n5 1 1\n8 10 10\n1000000 1 1000000"]
2 seconds
["5\n8\n499999500000"]
NoteIn the first test case, you can just lay new asphalt each day, since days $$$1, 3, 5$$$ are good.In the second test case, you can also lay new asphalt each day, since days $$$1$$$-$$$8$$$ are good.
Java 8
standard input
[ "math" ]
be9138aca8e1b8a5d722f99fcd70b685
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains three integers $$$n$$$, $$$g$$$ and $$$b$$$ ($$$1 \le n, g, b \le 10^9$$$) β€” the length of the highway and the number of good and bad days respectively.
1,400
Print $$$T$$$ integers β€” one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.
standard output
PASSED
5e83ebea3d54a88213390cb3f9f372a4
train_001.jsonl
1581518100
Your company was appointed to lay new asphalt on the highway of length $$$n$$$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $$$g$$$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $$$b$$$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $$$g$$$ good days, $$$b$$$ bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days $$$1, 2, \dots, g$$$ are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $$$n = 5$$$ then at least $$$3$$$ units of the highway should have high quality; if $$$n = 4$$$ then at least $$$2$$$ units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?
256 megabytes
import java.util.Scanner; public class fix { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int r = sc.nextInt(); for(int i = 0;i<r;i++) { long n = sc.nextLong(); long g = sc.nextLong(); long b = sc.nextLong(); days(n,g,b); } } public static void days(long n,long g,long b) { long d = n/2; if(n%2==1) d++; long sum = 0; boolean j = true; if(g>=d) { j = false; System.out.println(n); }else { if(d%g!=0) { sum = (g+b)*(d/g) + d%g; }else { sum = (g+b)*(d/g-1) + g; } } if(j) { if(sum<n) { System.out.println(n); }else { System.out.println(sum); } } } }
Java
["3\n5 1 1\n8 10 10\n1000000 1 1000000"]
2 seconds
["5\n8\n499999500000"]
NoteIn the first test case, you can just lay new asphalt each day, since days $$$1, 3, 5$$$ are good.In the second test case, you can also lay new asphalt each day, since days $$$1$$$-$$$8$$$ are good.
Java 8
standard input
[ "math" ]
be9138aca8e1b8a5d722f99fcd70b685
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains three integers $$$n$$$, $$$g$$$ and $$$b$$$ ($$$1 \le n, g, b \le 10^9$$$) β€” the length of the highway and the number of good and bad days respectively.
1,400
Print $$$T$$$ integers β€” one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.
standard output
PASSED
8d459d004d252ac2699ffd0d303ba849
train_001.jsonl
1581518100
Your company was appointed to lay new asphalt on the highway of length $$$n$$$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $$$g$$$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $$$b$$$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $$$g$$$ good days, $$$b$$$ bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days $$$1, 2, \dots, g$$$ are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $$$n = 5$$$ then at least $$$3$$$ units of the highway should have high quality; if $$$n = 4$$$ then at least $$$2$$$ units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?
256 megabytes
import java.util.*; public class CodeForces{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { long n=sc.nextLong(); long g=sc.nextLong(); long b=sc.nextLong(); long gr=(n+1)/2; long ans=(gr/g)*(g+b); if(gr%g==0){ ans-=b; }else{ ans+=gr%g; } ans=Math.max(ans,n); System.out.println(ans); } } }
Java
["3\n5 1 1\n8 10 10\n1000000 1 1000000"]
2 seconds
["5\n8\n499999500000"]
NoteIn the first test case, you can just lay new asphalt each day, since days $$$1, 3, 5$$$ are good.In the second test case, you can also lay new asphalt each day, since days $$$1$$$-$$$8$$$ are good.
Java 8
standard input
[ "math" ]
be9138aca8e1b8a5d722f99fcd70b685
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains three integers $$$n$$$, $$$g$$$ and $$$b$$$ ($$$1 \le n, g, b \le 10^9$$$) β€” the length of the highway and the number of good and bad days respectively.
1,400
Print $$$T$$$ integers β€” one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.
standard output
PASSED
1a39eec8f7b584eea82c0906edbdafdb
train_001.jsonl
1581518100
Your company was appointed to lay new asphalt on the highway of length $$$n$$$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $$$g$$$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $$$b$$$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $$$g$$$ good days, $$$b$$$ bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days $$$1, 2, \dots, g$$$ are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $$$n = 5$$$ then at least $$$3$$$ units of the highway should have high quality; if $$$n = 4$$$ then at least $$$2$$$ units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scn= new Scanner(System.in); int t = scn.nextInt(); while(t-->0){ int n= scn.nextInt(); int g =scn.nextInt(); int b= scn.nextInt(); System.out.println(res(n,g,b)); } } public static long res(int n ,int g ,int b){ int val = (n+1)/2; long ans =0; ans+=(long)(val); ans+=(long)(Math.ceil(val*1.0/g)-1)*(long)b; return Math.max(ans, n); } }
Java
["3\n5 1 1\n8 10 10\n1000000 1 1000000"]
2 seconds
["5\n8\n499999500000"]
NoteIn the first test case, you can just lay new asphalt each day, since days $$$1, 3, 5$$$ are good.In the second test case, you can also lay new asphalt each day, since days $$$1$$$-$$$8$$$ are good.
Java 8
standard input
[ "math" ]
be9138aca8e1b8a5d722f99fcd70b685
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains three integers $$$n$$$, $$$g$$$ and $$$b$$$ ($$$1 \le n, g, b \le 10^9$$$) β€” the length of the highway and the number of good and bad days respectively.
1,400
Print $$$T$$$ integers β€” one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.
standard output
PASSED
da8ff939b88994c0377a327bec3b4f02
train_001.jsonl
1581518100
Your company was appointed to lay new asphalt on the highway of length $$$n$$$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $$$g$$$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $$$b$$$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $$$g$$$ good days, $$$b$$$ bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days $$$1, 2, \dots, g$$$ are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $$$n = 5$$$ then at least $$$3$$$ units of the highway should have high quality; if $$$n = 4$$$ then at least $$$2$$$ units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author bhavy seth */ 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); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, InputReader sc, PrintWriter out) { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int g = sc.nextInt(); int b = sc.nextInt(); long hq = 0; if (n % 2 == 0) hq = n / 2; else hq = (n + 1) / 2; long temp = 0; long remofg = 0; if (hq % g == 0) temp = hq / g; else { remofg = g - hq % g; temp = hq / g; temp++; } long sum = temp * (g + b); if (temp != 0) sum -= b; long rem = n - hq; if (temp > 0) { rem = rem - ((temp - 1) * b); if (rem > 0) { if (remofg > rem) { sum -= remofg - rem; rem = 0; } else rem = rem - remofg; } else sum -= remofg; } sum += Math.max(rem, 0); if (g >= n) { sum = n; } out.println(sum); } } } static class InputReader { BufferedReader br; StringTokenizer st; public InputReader(InputStream inputStream) { br = new BufferedReader(new InputStreamReader(inputStream)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["3\n5 1 1\n8 10 10\n1000000 1 1000000"]
2 seconds
["5\n8\n499999500000"]
NoteIn the first test case, you can just lay new asphalt each day, since days $$$1, 3, 5$$$ are good.In the second test case, you can also lay new asphalt each day, since days $$$1$$$-$$$8$$$ are good.
Java 8
standard input
[ "math" ]
be9138aca8e1b8a5d722f99fcd70b685
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains three integers $$$n$$$, $$$g$$$ and $$$b$$$ ($$$1 \le n, g, b \le 10^9$$$) β€” the length of the highway and the number of good and bad days respectively.
1,400
Print $$$T$$$ integers β€” one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.
standard output
PASSED
b8b527783b9329dad1289e0dd2063e00
train_001.jsonl
1581518100
Your company was appointed to lay new asphalt on the highway of length $$$n$$$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $$$g$$$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $$$b$$$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $$$g$$$ good days, $$$b$$$ bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days $$$1, 2, \dots, g$$$ are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $$$n = 5$$$ then at least $$$3$$$ units of the highway should have high quality; if $$$n = 4$$$ then at least $$$2$$$ units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; import java.util.*; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Pranay2516 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); int t=in.nextInt(); for(int i=0;i<t;++i) solver.solve(i,in, out); out.close(); } static class TaskA { public void solve(int testNumeber,FastReader in, PrintWriter out) { long n=in.nextInt(); long g=in.nextInt(); long b=in.nextInt(); long count=0; long dev=0; long t=(long)Math.ceil((double)n/2); if(g>=b) count=n; else{ dev=(long)Math.ceil((double)t/g)-1; count=dev*(g+b)+(t-dev*g); if(count<n) count=n; } System.out.println(count); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public 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\n5 1 1\n8 10 10\n1000000 1 1000000"]
2 seconds
["5\n8\n499999500000"]
NoteIn the first test case, you can just lay new asphalt each day, since days $$$1, 3, 5$$$ are good.In the second test case, you can also lay new asphalt each day, since days $$$1$$$-$$$8$$$ are good.
Java 8
standard input
[ "math" ]
be9138aca8e1b8a5d722f99fcd70b685
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains three integers $$$n$$$, $$$g$$$ and $$$b$$$ ($$$1 \le n, g, b \le 10^9$$$) β€” the length of the highway and the number of good and bad days respectively.
1,400
Print $$$T$$$ integers β€” one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.
standard output
PASSED
40f95d96bc60a180f1754794f8774a63
train_001.jsonl
1581518100
Your company was appointed to lay new asphalt on the highway of length $$$n$$$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $$$g$$$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $$$b$$$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $$$g$$$ good days, $$$b$$$ bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days $$$1, 2, \dots, g$$$ are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $$$n = 5$$$ then at least $$$3$$$ units of the highway should have high quality; if $$$n = 4$$$ then at least $$$2$$$ units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?
256 megabytes
import java.util.Scanner; public class NationalProjectEduRound82B { public static void main(String[] args) { Scanner s =new Scanner(System.in); int t = s.nextInt(); while( t-- > 0) { long n = s.nextLong(); long g = s.nextLong(); long b = s.nextLong(); long half = n / 2; long ans = half; if(n % 2 != 0) { ans ++; half ++; } long count = half / g; if(half % g !=0) { count ++; } ans += (count - 1) * b; if(n > ans ) { ans += (n - ans); } System.out.println(ans); } } }
Java
["3\n5 1 1\n8 10 10\n1000000 1 1000000"]
2 seconds
["5\n8\n499999500000"]
NoteIn the first test case, you can just lay new asphalt each day, since days $$$1, 3, 5$$$ are good.In the second test case, you can also lay new asphalt each day, since days $$$1$$$-$$$8$$$ are good.
Java 8
standard input
[ "math" ]
be9138aca8e1b8a5d722f99fcd70b685
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains three integers $$$n$$$, $$$g$$$ and $$$b$$$ ($$$1 \le n, g, b \le 10^9$$$) β€” the length of the highway and the number of good and bad days respectively.
1,400
Print $$$T$$$ integers β€” one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.
standard output
PASSED
46de5bf4a997130e2ed74bfd605f1e92
train_001.jsonl
1581518100
Your company was appointed to lay new asphalt on the highway of length $$$n$$$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $$$g$$$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $$$b$$$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $$$g$$$ good days, $$$b$$$ bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days $$$1, 2, \dots, g$$$ are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $$$n = 5$$$ then at least $$$3$$$ units of the highway should have high quality; if $$$n = 4$$$ then at least $$$2$$$ units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void solve(InputReader in, OutputWriter out) { int n = in.readInt(), g = in.readInt(), b = in.readInt(); long res = (n+1)/2; long total = 0; if(res % g == 0) total += res + (res/g - 1)*b; else total += res + (res/g)*b; if(total < n) total += n-total; out.println(total); } public static void main(String[] args) { InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); int t = in.readInt(); while (t-- > 0) solve(in , out); out.flush(); out.close(); } } 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 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); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } }
Java
["3\n5 1 1\n8 10 10\n1000000 1 1000000"]
2 seconds
["5\n8\n499999500000"]
NoteIn the first test case, you can just lay new asphalt each day, since days $$$1, 3, 5$$$ are good.In the second test case, you can also lay new asphalt each day, since days $$$1$$$-$$$8$$$ are good.
Java 8
standard input
[ "math" ]
be9138aca8e1b8a5d722f99fcd70b685
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains three integers $$$n$$$, $$$g$$$ and $$$b$$$ ($$$1 \le n, g, b \le 10^9$$$) β€” the length of the highway and the number of good and bad days respectively.
1,400
Print $$$T$$$ integers β€” one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.
standard output
PASSED
6030e0eb6a48ef2de361dc4656782b7b
train_001.jsonl
1581518100
Your company was appointed to lay new asphalt on the highway of length $$$n$$$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $$$g$$$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $$$b$$$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $$$g$$$ good days, $$$b$$$ bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days $$$1, 2, \dots, g$$$ are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $$$n = 5$$$ then at least $$$3$$$ units of the highway should have high quality; if $$$n = 4$$$ then at least $$$2$$$ units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?
256 megabytes
import java.util.*; import java.io.*; import java.text.*; public class io2 { static long mod = (long) 1e9 + 7; static boolean multipleTC = true; void solve(int TC) throws Exception { long n = in.nl(), g = in.nl(), b = in.nl(); long hgdjf = n / 2 + n % 2; long tm = hgdjf / g, v = hgdjf % g == 0 ? 0 : 1,d = (tm + v - 1) * (b); long val = n - hgdjf, ans = 0; if (d < val) { ans = (tm) * g + d + (val - d) + hgdjf % g; } else ans = tm * g + d + hgdjf % g; pl(ans); } void hold(boolean b) throws Exception { if (!b) throw new Exception("@author : ashraf101"); } DecimalFormat df = new DecimalFormat("0.00000000000"); FastReader in; PrintWriter out; void run() throws Exception { in = new FastReader(); out = new PrintWriter(System.out); int T = (multipleTC) ? in.ni() : 1; pre(); for (int t = 1; t <= T; t++) solve(t); out.flush(); out.close(); } public static void main(String[] args) throws Exception { new io2().run(); } void pre() throws Exception { } void p(Object o) { out.print(o); } void pl(Object o) { out.println(o); } void pni(Object o) { out.println(o); out.flush(); } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception { br = new BufferedReader(new FileReader(s)); } String next() throws Exception { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception { String str = ""; try { str = br.readLine(); } catch (IOException e) { throw new Exception(e.toString()); } return str; } int bit(long n) { return (n == 0) ? 0 : (1 + bit(n & (n - 1))); } String n() throws Exception { return in.next(); } String nln() throws Exception { return in.nextLine(); } int ni() throws Exception { return Integer.parseInt(in.next()); } long nl() throws Exception { return Long.parseLong(in.next()); } double nd() throws Exception { return Double.parseDouble(in.next()); } } }
Java
["3\n5 1 1\n8 10 10\n1000000 1 1000000"]
2 seconds
["5\n8\n499999500000"]
NoteIn the first test case, you can just lay new asphalt each day, since days $$$1, 3, 5$$$ are good.In the second test case, you can also lay new asphalt each day, since days $$$1$$$-$$$8$$$ are good.
Java 8
standard input
[ "math" ]
be9138aca8e1b8a5d722f99fcd70b685
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains three integers $$$n$$$, $$$g$$$ and $$$b$$$ ($$$1 \le n, g, b \le 10^9$$$) β€” the length of the highway and the number of good and bad days respectively.
1,400
Print $$$T$$$ integers β€” one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.
standard output
PASSED
8150ad48ea7e0b0f0910f5aa81c82dc9
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.io.*; import java.text.DecimalFormat; import java.util.*; public class main2 { static ArrayList<pair> factors; static boolean isComposite[]; static ArrayList<Integer> primes; public static boolean isprime(long n){ if(n<=10001){ return !isComposite[(int)n]; } for(int i=0;i<primes.size();i++){ if(primes.get(i)*primes.get(i)*1l>n){ break; } if(n%primes.get(i)==0){ return false; } } return true; } static int[] res; public static void sieve(int n){ isComposite=new boolean[n+1]; primes =new ArrayList<Integer>(); isComposite[0]=true; isComposite[1]=true; for(int i=2;i<=n;i++){ if(!isComposite[i]){ for(int j=2*i;j<=n;j=j+i){ isComposite[j]=true; } } primes.add(i); } } public static void primefactors(long n){ factors=new ArrayList<pair>(); for(int x:primes){ if(x*x*1l>n){ break; } if(n%x==0){ pair p=new pair(x,0); factors.add(p); while(n%x==0){ n/=x; p.y++;} } } if(n!=1){ factors.add(new pair((int)n,1)); } } public static long powfact(long n,long p){ int res=0; for(long i=p;i*1l<=n;i=i*p*1l){ res+=(n/i); } return res; } public static long GCD(long i, long j){ if (i==0){ return j; } return GCD(j%i,i); } public static long LCM(long i, long j){ return(i*j)/GCD(i,j); } public static void main(String[] args) throws Exception { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int [] [] freq=new int [n+1][101]; int [] a=new int[n]; int [] b=new int[n]; int [] c=new int[n]; for(int i=0;i<n;i++){ a[i]=sc.nextInt(); freq[i][a[i]]++;} for(int i=0;i<n;i++){ b[i]=sc.nextInt(); freq[i][b[i]]++;} for(int i=0;i<n;i++){ c[i]=sc.nextInt(); freq[i][c[i]]++;} int [] ans=new int[n]; if(freq[1][a[0]]<3){ ans[0]=a[0]; } else if(freq[1][b[0]]<3){ ans[0]=b[0]; } else { ans[0]=c[0]; } for(int i=1;i<n-1;i++){ if(freq[i+1][a[i]]<3&&a[i]!=ans[i-1]){ ans[i]=a[i]; } else if(freq[i+1][b[i]]<3&&b[i]!=ans[i-1]){ ans[i]=b[i]; } else { ans[i]=c[i]; } } if(a[n-1]!=ans[0]&&a[n-1]!=ans[n-2]){ ans[n-1]=a[n-1]; } else if(b[n-1]!=ans[0]&&b[n-1]!=ans[n-2]){ ans[n-1]=b[n-1]; } else { ans[n-1]=c[n-1]; } for(int i=0;i<n;i++){ System.out.print(ans[i]+" "); } System.out.println(); } } static class matrix{ int a, b,c, d; public matrix(int x,int y, int z,int w){ a=x; b=y; c=z; d=w; } public boolean issSem(){ return b==c ; } } public static int p(long x){ int t=0; while(x>1){ t++; x=x/2; } return t; } static long[] fact; public static long fact(int x){ long y=(int)Math.pow(2,x)-1; return((y+1)*(y/2)+(y/2+1)); } static ArrayList<Integer > [] adjlist; static int [] leaves; static PriorityQueue<pair> centroid; public static void dfs1(int u,int p){ leaves[u]=1; if(adjlist[u].size()==1&&u!=0){ return; } for( int x:adjlist[u]){ if(x!=p){ dfs1(x,u); leaves[u]+=leaves[x]; } } } static class pair implements Comparable<pair>{ int x; int y; public pair(int x, int y){ this.x=x; this.y=y; } @Override public int compareTo(pair o) { // TODO Auto-generated method stub return x-o.x; } } static class SegmentTree{ int[] arr , sTree; static int N; public SegmentTree(int[] in){ arr=in; N=arr.length-1; sTree=new int[2*N]; build(1,1,N); } public void build(int node, int l, int r){ if(l==r){ sTree[node]=arr[l]; } else{ int leftchild=node*2; int rightchild=node*2+1; int mid =(l+r)/2; build(leftchild,l,mid); build(rightchild,mid+1,r); sTree[node]=Math.min(sTree[leftchild],sTree[rightchild]); } } public void update(int i, int val){ int node =i+N-1; arr[i]=val; sTree[node]=val; node=node/2; while(node>0){ int leftchild=node *2; int rightchild=node*2+1; sTree[node]=sTree[leftchild]&sTree[rightchild]; node=node/2; } return; } public int query(int i, int j){ return query(1,1, N, i, j); } public int query(int node,int l, int r, int i , int j){ if(i>r||j<l){ return Integer.MAX_VALUE; } if(i<=l&&r<=j){ return sTree[node]; } int leftchild=node*2; int rightchild=node*2+1; int mid =(l+r)/2; return Math.min(query(leftchild,l,mid,i,j),query(rightchild,mid+1,r,i,j)); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public boolean hasNext() { // TODO Auto-generated method stub return false; } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } }}
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
7abeac4d10913b549ec7b44fb9bbb7c8
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
// import java.io.BufferedInputStream; // import java.util.*; // public class Main { // public static void main(String[] args) { // Scanner scan = new Scanner(new BufferedInputStream(System.in)); // // int t = scan.nextInt(); // // while(t-->0) { // int n = scan.nextInt(); // int colourNumber = 1; // int[] result = new int[n]; // for (int i = 2 ; i * i <= n + 1; i++) { // if (result[i - 2] != 0) // continue; // for (int j = 2 * i; j <= n + 1; j += i) { // if (result[j - 2] == 0) { // result[j - 2] = result[i]+1; // colourNumber = Math.max(result[i]+1,colourNumber); // } // } // } // for (int i = 2; i <= n + 1; i++) { // if (result[i - 2] == 0) { // result[i - 2] = 1; // } // } // System.out.println(colourNumber); // for (int res : result) { // System.out.print(res + " "); // } // // } // } // } import java.util.*; import java.io.BufferedInputStream; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(new BufferedInputStream(System.in)); int t = scan.nextInt(); while(t-->0) { int n = scan.nextInt(); int[] a = new int[n]; int[] b = new int[n]; int[] c = new int[n]; int prev = 0; int next = 0; for(int i = 0 ; i < n ; i++) { a[i] = scan.nextInt(); } for(int i = 0 ; i < n ; i++) { b[i] = scan.nextInt(); } for(int i = 0 ; i < n ; i++) { c[i] = scan.nextInt(); } int[] result = new int[n]; for(int i = 0 ; i < n ; i++) { int[] arr = {a[i],b[i],c[i]}; if(i == n-1) { next = result[0]; } for(int val : arr) { if(val != prev && val != next) { result[i] = val; prev = val; break; } } System.out.print(result[i] + " "); } System.out.println(); } } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
b6798763a418a60e92f4c4d3ce0af457
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.io.*; import java.util.*; public class CircleColor { static class InputReader { public BufferedReader reader; public StringTokenizer tok; public InputReader(InputStream inputStream) { reader = new BufferedReader(new InputStreamReader(inputStream)); tok = null; } public InputReader(String inputFile) throws FileNotFoundException { reader = new BufferedReader(new FileReader(inputFile)); tok = null; } public String nextLine() { String c = ""; try { c = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return c; } public String next() { while (tok == null || !tok.hasMoreTokens()) { try { tok = new StringTokenizer(nextLine()); } catch (Exception e) { throw new RuntimeException(e); } } return tok.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } public static void main(String args[]) { InputStream inputstream = System.in; OutputStream outputstream = System.out; InputReader in = new InputReader(inputstream); PrintWriter out = new PrintWriter(outputstream); Task solver = new Task(); solver.solve(in, out); out.flush(); } static class Task { public void solve(InputReader in, PrintWriter out) { int t = in.nextInt(); while (t > 0) { t--; int n = in.nextInt(); int[] a = new int[n]; int[] b = new int[n]; int[] c = new int[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); for (int i = 0; i < n; i++) b[i] = in.nextInt(); for (int i = 0; i < n; i++) c[i] = in.nextInt(); int[] ans = new int[n]; ans[0] = a[0]; for (int i = 1; i < n - 1; i++) { int previous = ans[i - 1]; if (previous != a[i] ) ans[i] = a[i]; else if (previous != b[i] ) ans[i] = b[i]; else ans[i] = c[i]; } int i = n - 1; if(i>0) { if (ans[i - 1] != a[i] && ans[0] != a[i]) ans[i] = a[i]; else if (ans[i - 1] != b[i] && ans[0] != b[i]) ans[i] = b[i]; else ans[i] = c[i]; } for (int x : ans) System.out.print(x + " "); System.out.println(); } } } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
08670649c5c188af0acd7a47168e5fe2
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Comparator; import java.util.StringTokenizer; public class A { public static void main(String[] args) { int T = 0; FastReader reader = new FastReader(); BufferedWriter write = new BufferedWriter(new OutputStreamWriter(System.out)); T = reader.nextInt(); while (T-- > 0) { try { circleColoring(reader, write); write.flush(); } catch (Exception ex) { System.out.println("invalid"); } } } private static void circleColoring(FastReader reader, BufferedWriter writer) throws IOException { int ai = 0, bi = 0, ci = 0, n = 0, turn = 1, oi = 1; int[] a, b, c, out; n = reader.nextInt(); a = new int[n]; b = new int[n]; c = new int[n]; out = new int[n]; StringBuilder output = new StringBuilder(); for (int i = 0; i < n; i++) { a[i] = reader.nextInt(); } for (int i = 0; i < n; i++) { b[i] = reader.nextInt(); } for (int i = 0; i < n; i++) { c[i] = reader.nextInt(); } out[0] = a[ai++]; bi++; ci++; while (oi < n - 1) { switch (turn) { case 0: if (out[oi - 1] == a[ai]) { out[oi] = b[bi]; } else { out[oi] = a[ai]; } turn = 1; break; case 1: if (out[oi - 1] == b[bi]) { out[oi] = c[ci]; } else { out[oi] = b[bi]; } turn = 2; break; case 2: if (out[oi - 1] == c[ci]) { out[oi] = a[ai]; } else { out[oi] = c[ci]; } turn = 0; break; } ai++; bi++; ci++; oi++; } if (a[n - 1] != out[0] && a[n - 1] != out[n - 2]) { out[oi] = a[n - 1]; } else if (b[n - 1] != out[0] && b[n - 1] != out[n - 2]) { out[oi] = b[n - 1]; } else { out[oi] = c[n - 1]; } for (int num : out) { output.append(num + " "); } output.deleteCharAt(output.length() - 1).append("\n"); writer.write(output.toString()); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
1c26b2a5ed90da2bb5b1a4e968a3b96e
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.io.*; import java.util.*; public class Problem1 { public static void getResult(int list1[],int list2[],int list3[]) { int list4[]=new int[list1.length]; list4[0]=list1[0]; int size=list1.length; for(int i=1;i<size;i++) { if(list1[i]==list4[i-1]) { list4[i]=list2[i]; } else list4[i]=list1[i]; } if(list4[0]==list4[size-1]) { if(list3[size-1]!=list4[size-2]) list4[size-1]=list3[size-1]; else list4[size-1]=list2[size-1]; } //System.out.println("-------"); for(int i:list4) System.out.print(i+" "); //System.out.println(); } public static void main(String[] args) throws IOException { //System.out.println("Hello World!"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine().trim()); for(int z=0;z<t;z++) { int size=Integer.parseInt(br.readLine().trim()); String ip1[]=br.readLine().trim().split(" "); String ip2[]=br.readLine().trim().split(" "); String ip3[]=br.readLine().trim().split(" "); int list1[]=new int[size]; int list2[]=new int[size]; int list3[]=new int[size]; for(int j=0;j<size;j++) { list1[j]=Integer.parseInt(ip1[j]); list2[j]=Integer.parseInt(ip2[j]); list3[j]=Integer.parseInt(ip3[j]); } //int list4[]=new int[size]; getResult(list1,list2,list3); System.out.println(); //System.out.println(Arrays.toString(list1)+" "+Arrays.toString(list2)+" "+Arrays.toString(list3)); } } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
5793c4a66f9f44cef68a3d2743893dd8
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.util.*; import java.io.*; public class CircleColoring { // https://codeforces.com/contest/1408/problem/A public static void main(String[] args) throws IOException, FileNotFoundException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); //BufferedReader in = new BufferedReader(new FileReader("CircleColoring")); int t = Integer.parseInt(in.readLine()); while (t-->0) { int n = Integer.parseInt(in.readLine()); StringTokenizer st = new StringTokenizer(in.readLine()); int[] a = new int[n]; for (int i=0; i<n; i++) { a[i] = Integer.parseInt(st.nextToken()); } st = new StringTokenizer(in.readLine()); int[] b = new int[n]; for (int i=0; i<n; i++) { b[i] = Integer.parseInt(st.nextToken()); } st = new StringTokenizer(in.readLine()); int[] c = new int[n]; for (int i=0; i<n; i++) { c[i] = Integer.parseInt(st.nextToken()); } int[] p = new int[n]; p[0] = a[0]; for (int i=1; i<n; ++i) { p[i] = a[i]; if (p[i]==p[i-1]) { p[i] = b[i]; } } if (p[n-1] == p[0] || p[n-1] == p[n-2]) { p[n-1] = a[n-1]; } if (p[n-1] == p[0] || p[n-1] == p[n-2]) { p[n-1] = b[n-1]; } if (p[n-1] == p[0] || p[n-1] == p[n-2]) { p[n-1] = c[n-1]; } StringBuilder s = new StringBuilder(); for (int i=0; i<n; i++) { s.append(p[i] + " "); } System.out.println(s); } } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
523ff3663e423b2d41d2b4ea667d58f8
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.io.*; import java.util.*; public class D { public static void main(String[] args) { PrintWriter out = new PrintWriter(System.out); FastScanner in = new FastScanner(); int t = in.nextInt(); while(t-- >0){ int n = in.nextInt(); int[] a = in.readArray(n); int[] b = in.readArray(n); int[] c = in.readArray(n); int[] p = new int[n]; p[0] = a[0]; int last = p[0]; for (int i = 1; i < n; i++) { boolean isfilled = false; while(a[i] == last || a[i]==p[0]) { while(b[i] == last || b[i] == p[0]) { last = c[i]; p[i] = last; isfilled = true; break; } if(!isfilled) { last = b[i]; p[i] = last; isfilled = true; } break; } if(!isfilled) { last = a[i]; p[i] = last; isfilled = true; } } for (int i : p) { out.print(i); out.print(" "); } out.println(); } out.close(); } static boolean isLastAndMatched(int i, int[] p, int[] arr) { boolean isok = false; if(arr[i] == p[0]) { isok = true; } return isok; } static boolean isPrime(int x) { for (int d = 2; d * d <= x; d++) { if (x % d == 0) return false; } return true; } static long gcd (long a, long b) { if (b == 0) return a; else return gcd (b, a % b); } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readLongArray(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
da8108de74a020ecb3263fe91081502b
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.Arrays; public class Codeforces { static boolean ab(String a[],String b[],String c[],ArrayList<Integer> ans,int n2) { //System.out.println(ans); if(ans.size()==a.length) return true; if(ans.size()==0) { ans.add(Integer.parseInt(a[0])); if(ab(a,b,c,ans,n2)) return true; else ans.remove(0); ans.add(Integer.parseInt(a[0])); if(ab(a,b,c,ans,n2)) return true; else ans.remove(0); ans.add(Integer.parseInt(a[0])); if(ab(a,b,c,ans,n2)) return true; else ans.remove(0); return false; } else if(ans.size()==n2-1) { int n=ans.get(ans.size()-1); int i=ans.size(); if(Integer.parseInt(a[i])!=n&&Integer.parseInt(a[i])!=ans.get(0)) { //System.out.println(Integer.parseInt(a[i])+","+n); ans.add(Integer.parseInt(a[i])); if(ab(a,b,c,ans,n2)) return true; else ans.remove(i); } if(Integer.parseInt(b[i])!=n&&Integer.parseInt(b[i])!=ans.get(0)) { ans.add(Integer.parseInt(b[i])); if(ab(a,b,c,ans,n2)) return true; else ans.remove(i); } if(Integer.parseInt(c[i])!=n&&Integer.parseInt(c[i])!=ans.get(0)) { ans.add(Integer.parseInt(c[i])); if(ab(a,b,c,ans,n2)) return true; else ans.remove(i); } return false; } else { int n=ans.get(ans.size()-1); int i=ans.size(); if(i!=1&&Integer.parseInt(a[i])!=n) { //System.out.println(Integer.parseInt(a[i])+","+n); ans.add(Integer.parseInt(a[i])); if(ab(a,b,c,ans,n2)) return true; else ans.remove(i); } if(Integer.parseInt(b[i])!=n) { ans.add(Integer.parseInt(b[i])); if(ab(a,b,c,ans,n2)) return true; else ans.remove(i); } if(Integer.parseInt(c[i])!=n) { ans.add(Integer.parseInt(c[i])); if(ab(a,b,c,ans,n2)) return true; else ans.remove(i); } return false; } } public static void main(String[] args)throws IOException { BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in)); int T=Integer.parseInt(bReader.readLine()); while(T-->0) { int n=Integer.parseInt(bReader.readLine()); String a[]=bReader.readLine().split(" "); String b[]=bReader.readLine().split(" "); String c[]=bReader.readLine().split(" "); ArrayList<Integer> ans=new ArrayList<>(); ab(a, b, c, ans,n); for(int i=0;i<n;i++) System.out.print(ans.get(i)+" "); System.out.println(); } } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
2240f82b344ea46af5266eabc2bbd74d
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; public class Main{ public static void main(String[] args) { FastScanner sc =new FastScanner(); PrintWriter out=new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int a[]=new int[n],b[]=new int[n],c[]=new int[n]; int p[]=new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); for(int i=0;i<n;i++) b[i]=sc.nextInt(); for(int i=0;i<n;i++) c[i]=sc.nextInt(); p[0]=a[0]; for(int i=1;i<n;i++){ if(p[i-1]==a[i]){ if(p[i-1]==b[i]){ p[i]=c[i]; } else{ p[i]=b[i]; } } else{ p[i]=a[i]; } } if(p[n-1]==p[0]){ if(p[n-2]==c[n-1]){ p[n-1]=b[n-1]; } else{ p[n-1]=c[n-1]; } } for(int i:p){ out.print(i+" "); } out.println(); } out.close(); } } class FastScanner{ BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st =new StringTokenizer(""); String next(){ if(!st.hasMoreTokens()){ try{ st=new StringTokenizer(br.readLine()); } catch(Exception e){ } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
045d33c53431d23aeb76f2a7b83e5924
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; public class Main{ public static void main(String[] args) { FastScanner sc =new FastScanner(); PrintWriter out=new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int a[]=new int[n],b[]=new int[n],c[]=new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); for(int i=0;i<n;i++) b[i]=sc.nextInt(); for(int i=0;i<n;i++) c[i]=sc.nextInt(); for(int i=1;i<n;i++){ if(a[i]==a[i-1] ){ if(a[i]!=b[i-1]){ a[i]=b[i]; } else{ a[i]=c[i]; } } if(i==n-1 && a[i]==a[0]){ if(b[i]!=a[0] && b[i]!=a[i-1]){ a[i]=b[i]; } else{ a[i]=c[i]; } } } for(int i:a){ out.print(i+" "); } out.println(); } out.close(); } } class FastScanner{ BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st =new StringTokenizer(""); String next(){ if(!st.hasMoreTokens()){ try{ st=new StringTokenizer(br.readLine()); } catch(Exception e){ } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
736b9b13756227d5856d0d964cf11d97
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, Scanner in, PrintWriter out) { int T = in.nextInt(); while (T-- > 0) { solveOne(in, out); } } private void solveOne(Scanner in, PrintWriter out) { int N = in.nextInt(); int A[] = CPUtils.readIntArray(N, in); int B[] = CPUtils.readIntArray(N, in); int C[] = CPUtils.readIntArray(N, in); int ans[] = new int[N]; ans[0] = A[0]; for (int i = 1; i < N; i++) { if (i < N - 1) { if (ans[i - 1] != A[i]) ans[i] = A[i]; else if (ans[i - 1] != B[i]) ans[i] = B[i]; else ans[i] = C[i]; } else { if (ans[i - 1] != A[i] && A[0] != A[i]) ans[i] = A[i]; else if (ans[i - 1] != B[i] && A[0] != B[i]) ans[i] = B[i]; else ans[i] = C[i]; } } CPUtils.printIntArrayNoLastSpace(ans, out); out.println(); } } static class CPUtils { public static int[] readIntArray(int size, Scanner in) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = in.nextInt(); } return array; } public static void printIntArrayNoLastSpace(int[] array, PrintWriter out) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < array.length; i++) { if (i == array.length - 1) stringBuilder.append(array[i]); else stringBuilder.append(array[i] + " "); } out.print(stringBuilder.toString()); } } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
1fbaaf77eb60b54b720c19ab8137f64a
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { // write your code here FastScanner f = new FastScanner(); int t = f.nextInt(); for(int tt=0;tt<t;tt++) { int n = f.nextInt(); int[] a = f.readArray(n); int[] b= f.readArray(n); int[] c= f.readArray(n); int[] p = new int[n]; p[0]=a[0]; for(int i=1;i<n-1;i++) { if(a[i]!=p[i-1]) p[i] = a[i]; else if(b[i]!= p[i-1]) p[i] = b[i]; else c[i] = p[i]; } { if (a[n - 1] != p[n - 1 - 1] && p[0] != a[n - 1]) p[n - 1] = a[n - 1]; else if (b[n - 1] != p[n - 1 - 1] && p[0] != b[n - 1]) p[n - 1] = b[n - 1]; else p[n - 1] = c[n - 1]; } for(int val : p) System.out.print(val + " "); System.out.println(); } } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
23b543cb7cdb76ca44638c01c5099634
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.Duration; import java.time.Instant; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.*; import java.util.StringJoiner; import java.util.StringTokenizer; public class A { public static void main(String[] args) throws ParseException { FastScanner sc = new FastScanner(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int a[]=sc.readArray(n),b[]=sc.readArray(n),c[]=sc.readArray(n); int ans[]=new int[n]; StringJoiner sj=new StringJoiner(""); sj.add(a[0]+" "); int prev=a[0]; for(int i=1;i<n;i++) { if(i!=n-1) { if(a[i]!=prev) { sj.add(a[i]+" "); prev=a[i]; } else if(b[i]!=prev) { sj.add(b[i]+" "); prev=b[i]; } else if(c[i]!=prev) { sj.add(c[i]+" "); prev=c[i]; } } else { if(a[i]!=prev&&a[n-1]!=a[0]) { sj.add(a[i]+" "); prev=a[i]; } else if(b[i]!=prev&&b[n-1]!=a[0]) { sj.add(b[i]+" "); prev=b[i]; } else if(c[i]!=prev&&c[n-1]!=a[0]) { sj.add(c[i]+" "); prev=c[i]; } } //System.out.println(prev); } System.out.println(sj); } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
c405b6cda76773b4601b96a5ab33f4e2
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.util.*; public class Codef { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); for (int i = 0; i < t; i++) { int n=sc.nextInt(); int a[]=new int[n]; int c[]=new int[n]; int b[]=new int[n]; for(int j=0;j<n;j++) { a[j]=sc.nextInt(); } for(int j=0;j<n;j++) { b[j]=sc.nextInt(); } for(int j=0;j<n;j++) { c[j]=sc.nextInt(); } //input taken int prev=a[0]; System.out.print(a[0]+" "); for (int j=1;j<n;j++) { if(j==n-1) { if(b[j]!=prev && b[j]!=a[0]) System.out.print(b[j]+" "); else if(c[j]!=prev && c[j]!=a[0]) System.out.print(c[j]+" "); else System.out.print(a[j]+" "); } else if(b[j]!=prev) { prev=b[j]; System.out.print(b[j]+" "); } else if(c[j]!=prev) { prev=c[j]; System.out.print(c[j]+" "); } else if(c[j]!=prev) { prev=a[j]; System.out.print(a[j]+" "); } } }//for ends // System.out.println("hekk"); } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
a7845fc29a4803d989e51a20bce4679b
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.io.*; import java.util.*; public class Rough { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(System.out); StringTokenizer st; int t = Integer.parseInt(br.readLine()); while(t-- > 0) { int n = Integer.parseInt(br.readLine()); int[] a = new int[n]; int[] b = new int[n]; int[] c = new int[n]; st = new StringTokenizer(br.readLine()); for(int i = 0 ; i < n ; i++) a[i] = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); for(int i = 0 ; i < n ; i++) b[i] = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); for(int i = 0 ; i < n ; i++) c[i] = Integer.parseInt(st.nextToken()); writer.write(a[0]+" "); int prev = a[0]; for(int i = 1 ; i < n-1 ; i++) { if(a[i] != prev) { writer.write(a[i]+" "); prev = a[i]; } else { writer.write(b[i]+" "); prev = b[i]; } } if(!(a[n-1]==prev || a[n-1]==a[0])) writer.write(a[n-1]+""); else if(!(b[n-1]==prev || b[n-1]==a[0])) writer.write(b[n-1]+""); else writer.write(c[n-1]+""); writer.write("\n"); } writer.flush(); writer.close(); } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
381f10fac94472052665a6972a8a0105
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; import static java.lang.Integer.min; import static java.lang.Long.max; import static java.lang.Long.min; public final class Main { static FastScanner in=new FastScanner(); static PrintWriter out=new PrintWriter(System.out); /* 1. Never use sorting on 1-indexed array! or be very careful about negative values and stuff 2. read the statement carefully! 3. What about corner cases a=0? 4. Remember about Dynamic Programming */ public static void main(String[]args) { int t=in.nextInt(); while(t-->0){ int n=in.nextInt(); int[]a=in.nextArray(n,1); int[]b=in.nextArray(n,1); int[]c=in.nextArray(n,1); int l=0; for(int i=1;i<=n-1;i++){ if(a[i]==l){ l=b[i]; out.print(b[i]+" "); }else{ l=a[i]; out.print(a[i]+" "); } } for(int i:new int[]{a[n],b[n],c[n]}){ if(i!=a[1]&&i!=l){ out.println(i); break; } } } out.close(); } static int min(int...a){ int b = Integer.MAX_VALUE; for(int x:a)if(x<b)b=x; return b; } static long lcm(long a,long b, long c){ return lcm(a,lcm(b,c)); } static long lcm(long a,long b){ return a*b/gcd(a,b); } static long gcd(long a,long b) { if (a == 0) return b; return gcd(b%a,a); } static void printArray(long[]a,int m){ for (int i=m;i<a.length+m;i++)out.print(a[i]+" "); out.println(); } static void printArray(int[]a,int m){ for (int i=m;i<a.length+m;i++)out.print(a[i]+" "); out.println(); } static void printArray(String[]a,int m){ for (int i=m;i<a.length+m;i++)out.print(a[i]+" "); out.println(); } static void printArray(double[]a,int m){ for (int i=m;i<a.length+m;i++)out.print(a[i]+" "); out.println(); } static void printArray(boolean[]a,int m){ for (int i=m;i<a.length+m;i++)out.print(a[i]+" "); out.println(); } static class Pair implements Comparable<Pair>{ int first,second; public Pair(int a,int b) { first=a; second=b; } @Override public int compareTo(Pair o) { return first-o.first; } } static class FastScanner{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next(){ while(!st.hasMoreTokens()) try{ st=new StringTokenizer(br.readLine()); }catch(IOException e){ e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] nextArray(int n,int m) { int[] a=new int[n+m]; for (int i=m;i<n+m;i++)a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
c2f3e56ab44a03393c36b63722ca2c5f
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int T = sc.nextInt(); for(int t = 0; t<T; t++){ int n = sc.nextInt(); int arr[][] = new int[3][n]; for(int i = 0; i<n; i++)arr[0][i] = sc.nextInt(); for(int i = 0; i<n; i++)arr[1][i] = sc.nextInt(); for(int i = 0; i<n; i++)arr[2][i] = sc.nextInt(); List<Integer> list = new ArrayList(); int prev = arr[0][0]; list.add(prev); for(int i = 1; i<n; i++){ for(int j = 0; j<3; j++){ if(i == n - 1){ if(arr[j][i] != prev && arr[j][i] != list.get(0)){ list.add(arr[j][i]); break; } } else if(arr[j][i] != prev){ list.add(arr[j][i]); prev = arr[j][i]; break; } } } for(Integer i: list)out.print(i + " "); out.println(""); } out.flush(); } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
c830867cc2b6165d0b61334ee5e922e6
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.awt.*; import java.io.*; import java.util.*; import java.util.List; public class Coding { static long MOD = 998244353; static long[] fact = new long[302]; static long[] invFact = new long[302]; static long[][][] segmentTree = new long[4*300000 + 11][2][2]; static long MINVALUE = Integer.MIN_VALUE; private static class Operation { public int i,j,x; public Operation() { } } public static void main(String[] args) { BufferedReader bi = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out)); //precompute(); try { int t = Integer.parseInt(bi.readLine()); while (t > 0) { String[] inputs = bi.readLine().split(" "); int n = Integer.parseInt(inputs[0].trim()); int[] a = new int[n]; int[] b = new int[n]; int[] c = new int[n]; inputs = bi.readLine().split(" "); for(int i=0;i<n;++i) { a[i] = Integer.parseInt(inputs[i].trim()); } inputs = bi.readLine().split(" "); for(int i=0;i<n;++i) { b[i] = Integer.parseInt(inputs[i].trim()); } inputs = bi.readLine().split(" "); for(int i=0;i<n;++i) { c[i] = Integer.parseInt(inputs[i].trim()); } int prev = a[0]; writer.append(a[0] + " "); for(int i=1;i<n;++i) { if (i<n-1) { if (a[i]!=prev) { writer.append(a[i] + " "); prev = a[i]; } else { writer.append(b[i] + " "); prev = b[i]; } } else if (i==n-1) { if (a[i]!=prev && a[i]!=a[0]) { writer.append(a[i] + " "); prev = a[i]; } else if (b[i]!=prev && b[i]!=a[0]){ writer.append(b[i] + " "); prev = b[i]; } else { writer.append(c[i] + " "); prev = c[i]; } } } writer.append("\n"); writer.flush(); t--; } writer.flush(); } catch (Exception e) { e.printStackTrace(); } } private static int gcd(int a, int b) { if (a<b) { return gcd(b, a); } if (a%b==0) { return b; } return gcd(b, a%b); } private static void dfs(int cur, List<Integer>[] adj, int[] nodeCount, int[] maxChild, int parent, int[] par) { par[cur] = parent; for(int i=0;i<adj[cur].size();++i) { int nxt = adj[cur].get(i); if (nxt!=parent) { dfs(nxt, adj, nodeCount, maxChild, cur, par); nodeCount[cur]+=nodeCount[nxt]; maxChild[cur] = Math.max(maxChild[cur], nodeCount[nxt]); } } } private static int findDist(int a, int b, int[] par) { Map<Integer, Integer> dist = new HashMap<>(); int count = 0; dist.put(a, 0); while (par[a]!=-1) { a = par[a]; count++; dist.put(a, count); } count=0; while(!dist.containsKey(b)) { b = par[b]; count++; } return count+dist.get(b); } private static int findLongestPath(int cur, int prev, List<Integer>[] adj, int[] dep) { int diameter = 0; int highest = -1, second=-1; for(int i=0;i<adj[cur].size();++i) { int nxt = adj[cur].get(i); if (nxt!=prev) { diameter = Math.max(diameter, findLongestPath(nxt, cur, adj, dep)); if (dep[nxt]>highest) { second = highest; highest = dep[nxt]; } else if (dep[nxt]>second) { second = dep[nxt]; } } } diameter = Math.max(diameter, highest + second + 2); return diameter; } static long ncr(int n, int r) { long num = fact[n]; long denom = (invFact[r]*invFact[n-r])%MOD; //System.out.println("num is " + num + " denom is" + denom); return (num*denom)%MOD; } static long modExp(long a, long b, long mod) { //System.out.println("a is " + a + " and b is " + b); if (a==1) return 1; long ans = 1; while (b!=0) { if (b%2==1) { ans = (ans*a)%mod; } a = (a*a)%mod; b/=2; } return ans; } private static void precompute() { fact[0] = 1; invFact[0] = 1; for(int i=1;i<=300000;++i) { fact[i] = (fact[i-1]*i)%MOD; invFact[i] = modExp(fact[i], MOD-2, MOD); } } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
9adc7f11cb8f757cb4d56dcbf236c5b8
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; public class A { public static void main(String[] args) { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int n = in.nextInt(); int[][] a = new int[3][n]; for (int j = 0; j < 3; j++) { for (int k = 0; k < n; k++) { a[j][k] = in.nextInt(); } } int[] p = new int[n]; p[0] = a[0][0]; for (int j = 1; j < n; j++) { for (int k = 0; k < 3; k++) { //debug(k, j); if(p[j-1] != a[k][j] && p[j%(n-1)] != a[k][j]){ p[j] = a[k][j]; break; } } } for (int j = 0; j < n; j++) { out.print(p[j] + " "); } out.println(); } out.flush(); } 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; } private int next() { 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 = next(); while (isSpaceChar(c)) c = next(); int sgn = 1; if (c == '-') { sgn = -1; c = next(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = next(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = next(); while (isSpaceChar(c)) c = next(); long sgn = 1; if (c == '-') { sgn = -1; c = next(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = next(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static void debug(Object... o) { System.err.println(Arrays.deepToString(o)); } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
c1cf43a84e754d0922760701cba0a2d0
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); int z=1; while(t-->0){ int n=sc.nextInt(); int a[]=new int[n]; int b[]=new int[n]; int c[]=new int[n]; for(int i=0;i<n;i++){ a[i]=sc.nextInt(); } for(int i=0;i<n;i++){ b[i]=sc.nextInt(); } for(int i=0;i<n;i++){ c[i]=sc.nextInt(); } System.out.print(a[0]+" "); int prev=a[0]; int start=a[0]; for(int i=1;i<n;i++){ if(i!=n-1){ if(a[i]!=prev){ prev=a[i]; } else if(b[i]!=prev){ prev=b[i]; } else{ prev=c[i]; } System.out.print(prev+" "); } else{ if(a[i]!=prev&&a[i]!=start){ System.out.print(a[i]+" "); } else if(b[i]!=prev&&b[i]!=start){ System.out.print(b[i]+" "); } else if(c[i]!=start&&c[i]!=prev){ System.out.print(c[i]+" "); } } } System.out.println(); } } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
3e99f5de77b7b3b025904d1df05fcd2c
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args){ Scanner in=new Scanner(System.in); int t=in.nextInt(); while(t-->0){ int n=in.nextInt(); int a[]=new int[n]; int b[]=new int[n]; int c[]=new int[n]; for(int i=0;i<n;i++){ a[i]=in.nextInt(); } for(int i=0;i<n;i++){ b[i]=in.nextInt(); } for(int i=0;i<n;i++){ c[i]=in.nextInt(); } System.out.print(a[0]+" "); int prev=a[0]; for(int i=1;i<n-1;i++){ if(a[i]!=prev){ System.out.print(a[i]+" "); prev=a[i]; continue; } else if (b[i]!=prev){ System.out.print(b[i]+" "); prev=b[i]; continue; } else { System.out.print(c[i]+" "); prev=c[i]; continue; } } if(a[n-1]!=a[0] && a[n-1]!=prev) System.out.print(a[n-1]); else if(b[n-1]!=a[0] && b[n-1]!=prev) System.out.print(b[n-1]); else System.out.print(c[n-1]); System.out.println(); } } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
f781b2ab0d77118b1f2ade7e2b5f97ba
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.util.Scanner; /** * * @author Shivam Patel */ public class CircleColoring { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int test = sc.nextInt(); for (int i = 0; i < test; i++) { int seq = sc.nextInt(); int[] a = new int[seq]; int[] b = new int[seq]; int[] c = new int[seq]; for (int j = 0; j < seq; j++) { a[j] = sc.nextInt(); } for (int j = 0; j < seq; j++) { b[j] = sc.nextInt(); } for (int j = 0; j < seq; j++) { c[j] = sc.nextInt(); } printSequence(a, b, c); } } private static void printSequence(int[] a, int[] b, int[] c) { for (int i = 1; i < a.length; i++) { if (a[i] == a[i - 1]) { if (a[i] != b[i - 1]) { a[i] = b[i]; } else { a[i] = c[i]; } } if (i == a.length - 1 && a[i] == a[0]) { if (b[i] != a[0] && b[i] != a[i - 1]) { a[i] = b[i]; } else { a[i] = c[i]; } } } for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
05662f71a5b8d394d899ea4803e5b1bf
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
/* If you want to aim high, aim high Don't let that studying and grades consume you Just live life young ****************************** What do you think? What do you think? 1st on Billboard, what do you think of it Next is a Grammy, what do you think of it However you think, I’m sorry, but shit, I have no fcking interest ******************************* I'm standing on top of my Monopoly board That means I'm on top of my game and it don't stop til my hip don't hop anymore https://www.a2oj.com/Ladder16.html ******************************* Shining through the city with a little funk and soul ******************************* 300iq as writer = Sad! */ import java.util.*; import java.io.*; import java.math.*; public class A { public static void main(String hi[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int T = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); while(T-->0) { int N = Integer.parseInt(infile.readLine()); int[] arr = readArr(N, infile, st); int[] brr = readArr(N, infile, st); int[] crr = readArr(N, infile, st); int[] res = new int[N]; res[0] = arr[0]; for(int i=1; i < N; i++) { if(res[i-1] != arr[i]) res[i] = arr[i]; else if(res[i-1] != brr[i]) res[i] = brr[i]; else res[i] = crr[i]; } HashSet<Integer> set = new HashSet<Integer>(); set.add(arr[N-1]); set.add(brr[N-1]); set.add(crr[N-1]); if(set.contains(res[N-2])) set.remove(res[N-2]); if(set.contains(res[0])) set.remove(res[0]); for(int x: set) res[N-1] = x; for(int x: res) sb.append(x+" "); sb.append("\n"); } System.out.print(sb); } public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception { int[] arr = new int[N]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
ceeced11735d7f985ba8b2731010a7c2
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.util.*; import java.io.*; public class _1408A { static int[] MODS = {1000000007, 998244353, 1000000009}; static int MOD = MODS[0]; public static void main(String[] args) { sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[][] arr = new int[3][n]; scan(arr[0]); scan(arr[1]); scan(arr[2]); int[] ajit = new int[n]; int last = -1; for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { if (arr[j][i] != last && i < n-1) { ajit[i] = arr[j][i]; last = ajit[i]; break; } if (i == n-1 && arr[j][i] != last && arr[j][i] != ajit[0]) { ajit[i] = arr[j][i]; break; } } } for (int i = 0; i < n; i++) { out.print(ajit[i]+" "); } out.println(); } out.close(); } public static int[] sort(int[] arr) { ArrayList<Integer> list = new ArrayList<>(); for (int i = 0; i < arr.length; i++) { list.add(arr[i]); } Collections.sort(list); for (int i = 0; i < arr.length; i++) { arr[i] = list.get(i); } return arr; } public static void scan(int[] arr) { for (int i = 0; i < arr.length; i++) { arr[i] = sc.nextInt(); } } public static MyScanner sc; public static PrintWriter out; public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
f92e7dcf5257a8ec60c8145295a7e4db
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Scanner; import java.util.StringTokenizer; import java.io.BufferedReader; import java.util.*; import java.math.BigInteger; public class A{ public static FastReader fs = new FastReader(); static PrintWriter out = new PrintWriter(System.out); static void solve() { int n = fs.nextInt(); ArrayList<Integer> ans = new ArrayList<>(); int[]a = fs.readintarray(n),b = fs.readintarray(n),c = fs.readintarray(n); ans.add(a[0]); int i = 1; for(; i<n-1; i++){ if(ans.get(ans.size()-1)!=a[i]) ans.add(a[i]); else if(ans.get(ans.size()-1)!=b[i]) ans.add(b[i]); else ans.add(c[i]); } if(ans.get(ans.size()-1) != a[i] && ans.get(0) != a[i]) ans.add(a[i]); else if(ans.get(ans.size()-1)!=b[i] && ans.get(0) != b[i]) ans.add(b[i]); else ans.add(c[i]); for(int x : ans) out.print(x + " "); out.println(); } public static void main(String[] args) { Thread t = new Thread(null,null,"",1 << 28) { public void run() { int test_case = 1; test_case = fs.nextInt(); for(int cs = 1; cs <= test_case; cs++) { solve(); } out.close(); } }; t.start(); try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); } } 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; } int [] readintarray(int n) { int res [] = new int [n]; for(int i = 0; i<n; i++)res[i] = nextInt(); return res; } long [] readlongarray(int n) { long res [] = new long [n]; for(int i = 0; i<n; i++)res[i] = nextLong(); return res; } } static int ceil(int x,int y) { return (x % y == 0 ? x / y : (x / y +1)); } static long ceil(long x,long y) { return (x % y == 0 ? x / y : (x / y +1)); } static int max(int x,int y) { return Math.max(x, y); } static int min(int x,int y) { return Math.min(x, y); } static long max(long x,long y) { return Math.max(x, y); } static long min(long x,long y) { return Math.min(x, y); } static int min(int a []) { int x = 1_000_000_00_9; for(int i = 0; i<a.length; i++)x = min(x,a[i]); return x; } static int max(int a []) { int x = -1_000_000_00_9; for(int i = 0; i<a.length; i++)x = max(x,a[i]); return x; } static long min(long a []) { long x = (long)3e18; for(int i = 0; i<a.length; i++)x = min(x,a[i]); return x; } static long max(long a []) { long x = -(long)3e18; for(int i = 0; i<a.length; i++)x = max(x,a[i]); return x; } static int power(int x,int y) { int res = 1; while(y > 0) { if( y % 2 == 1)res = (res * x); y >>= 1; x = (x * x); } return res; } static long power(long x,long y) { long res = 1; while(y > 0) { if( y % 2 == 1)res = (res * x); y >>= 1; x = (x * x); } return res; } static long power(long x,long y,long mod) { long res = 1; x %= mod; while(y > 0) { if( y % 2 == 1)res = (res * x) % mod; y >>= 1; x = (x * x) % mod; } return res; } static void intsort(int [] a) { List<Integer> temp = new ArrayList<Integer>(); for(int i = 0; i<a.length; i++)temp.add(a[i]); Collections.sort(temp); for(int i = 0; i<a.length; i++)a[i] = temp.get(i); } static void longsort(long [] a) { List<Long> temp = new ArrayList<Long>(); for(int i = 0; i<a.length; i++)temp.add(a[i]); Collections.sort(temp); for(int i = 0; i<a.length; i++)a[i] = temp.get(i); } static void reverseintsort(int [] a) { List<Integer> temp = new ArrayList<Integer>(); for(int i = 0; i<a.length; i++)temp.add(a[i]); Collections.sort(temp); Collections.reverse(temp); for(int i = 0; i<a.length; i++)a[i] = temp.get(i); } static void reverselongsort(long [] a) { List<Long> temp = new ArrayList<Long>(); for(int i = 0; i<a.length; i++)temp.add(a[i]); Collections.sort(temp); Collections.reverse(temp); for(int i = 0; i<a.length; i++)a[i] = temp.get(i); } static void intervalsort(int a [],int start,int end) { ArrayList<Integer> temp = new ArrayList<>(); for(int i = start; i<end; i++) temp.add(a[i]); Collections.sort(temp); for(int i = 0; i<temp.size(); i++) a[i+start] = temp.get(i); } static void intervalsort(long a [],int start,int end) { ArrayList<Long> temp = new ArrayList<>(); for(int i = start; i<end; i++) temp.add(a[i]); Collections.sort(temp); for(int i = 0; i<temp.size(); i++) a[i+start] = temp.get(i); } static void reverseintervalsort(int a [],int start,int end) { ArrayList<Integer> temp = new ArrayList<>(); for(int i = start; i<end; i++) temp.add(a[i]); Collections.sort(temp); Collections.reverse(temp); for(int i = 0; i<temp.size(); i++) a[i+start] = temp.get(i); } static void reverseintervalsort(long a [],int start,int end) { ArrayList<Long> temp = new ArrayList<>(); for(int i = start; i<end; i++) temp.add(a[i]); Collections.sort(temp); Collections.reverse(temp); for(int i = 0; i<temp.size(); i++) a[i+start] = temp.get(i); } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
f45b8dd291bc721526a959bb0880012e
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.io.*; import java.util.*; import java.util.concurrent.TimeUnit; public class aGrakn implements Runnable{ public static void main(String[] args) { try{ new Thread(null, new aGrakn(), "process", 1<<26).start(); } catch(Exception e){ System.out.println(e); } } public void run() { FastReader scan = new FastReader(); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); //PrintWriter out = new PrintWriter("file.out"); Task solver = new Task(); int t = scan.nextInt(); //int t = 1; for(int i = 1; i <= t; i++) solver.solve(i, scan, out); out.close(); } static class Task { static final int oo = Integer.MAX_VALUE; static final long OO = Long.MAX_VALUE; public void solve(int testNumber, FastReader sc, PrintWriter out) { int N = sc.nextInt(); int[] a = sc.readArray(N); int[] b = sc.readArray(N); int[] c = sc.readArray(N); int last = -1; int[] ans = new int[N]; for(int i = 0; i < N; i++) { if(a[i] == last) { ans[i] = b[i]; last = b[i]; } else { ans[i] = a[i]; last = a[i]; } } if(ans[0] == ans[N-1]) { ans[0] = c[0]; } if(ans[0] == ans[1]) { ans[0] = b[0]; } for(int each: ans) out.print(each + " "); out.println(); } } static long modDivide(long a, long b, long MOD) { a %= MOD; return (binpow(b, MOD-2, MOD) * a) % MOD; } static long binpow(long a, long b, long m) { a %= m; long res = 1; while (b > 0) { if ((b & 1) == 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } static int[] reverse(int a[]) { int[] b = new int[a.length]; for (int i = 0, j = a.length; i < a.length; i++, j--) { b[j - 1] = a[i]; } return b; } static long[] reverse(long a[]) { long[] b = new long[a.length]; for (int i = 0, j = a.length; i < a.length; i++, j--) { b[j - 1] = a[i]; } return b; } static void sort(int[] x){ shuffle(x); Arrays.sort(x); } static void sort(long[] x){ shuffle(x); Arrays.sort(x); } static class tup implements Comparable<tup>, Comparator<tup>{ int a, b; tup(int a,int b){ this.a=a; this.b=b; } public tup() { } @Override public int compareTo(tup o){ return Integer.compare(b,o.b); } @Override public int compare(tup o1, tup o2) { return Integer.compare(o1.b, o2.b); } @Override public int hashCode() { return Objects.hash(a, b); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; tup other = (tup) obj; return a==other.a && b==other.b; } @Override public String toString() { return a + " " + b; } } static void shuffle(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } } static void shuffle(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int size) { int[] a = new int[size]; for(int i = 0; i < size; i++) { a[i] = nextInt(); } return a; } long[] readLongArray(int size) { long[] a = new long[size]; for(int i = 0; i < size; i++) { a[i] = nextLong(); } return a; } } static void dbg(int[] arr) { System.out.println(Arrays.toString(arr)); } static void dbg(long[] arr) { System.out.println(Arrays.toString(arr)); } static void dbg(boolean[] arr) { System.out.println(Arrays.toString(arr)); } static void dbg(Object... args) { for (Object arg : args) System.out.print(arg + " "); System.out.println(); } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
2ee8274553c9fddf228dc9aa651e489c
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; import java.math.*; public class Prac{ static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int ni() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nl() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nia(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public int[] nia1(int n) { int a[] = new int[n+1]; for (int i = 1; i <=n; i++) { a[i] = ni(); } return a; } public long[] nla(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nl(); } return a; } public long[] nla1(int n) { long a[] = new long[n+1]; for (int i = 1; i <= n; i++) { a[i] = nl(); } return a; } public Long[] nLa(int n) { Long a[] = new Long[n]; for (int i = 0; i < n; i++) { a[i] = nl(); } return a; } public Long[] nLa1(int n) { Long a[] = new Long[n+1]; for (int i = 1; i <= n; i++) { a[i] = nl(); } return a; } public Integer[] nIa(int n) { Integer a[] = new Integer[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public Integer[] nIa1(int n) { Integer a[] = new Integer[n+1]; for (int i = 1; i <= n; i++) { a[i] = ni(); } return a; } public String rs() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } public static class Key { private final int x, y; public Key(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Key)) return false; Key key = (Key) o; return x == key.x && y == key.y; } @Override public int hashCode() { int result = x; result = 31 * result + y; return result; } } static class Pair{ int x,y; Pair(int a,int b){ x=a;y=b; } // @Override // public int compareTo(Pair p) { // if(x==p.x) return y-p.y; // return x-p.x; // } } static void shuffleArray(int temp[]){ int n = temp.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ int tmp = temp[i]; int randomPos = i + rnd.nextInt(n-i); temp[i] = temp[randomPos]; temp[randomPos] = tmp; } } static long gcd(long a,long b){ return b==0?a:gcd(b,a%b);} static long lcm(long a,long b){return (a/gcd(a,b))*b;} static PrintWriter w = new PrintWriter(System.out); static long mod=998244353L,mod1=1000000007; //static int r[]={0,1,0,-1}, c[]={1,0,-1,0}; static long modInverse(long a, long m){ long num=m; long x=1; long power=a%mod; while(num>0){ if(num%2==1){ x=(x*power)%mod; } num>>=1; power=(power*power)%mod; } return x; } static long fact[]=new long[1000005]; public static void findFact(){ fact[0]=1; for(int i=1;i<=1000000;i++){ fact[i]=(i*fact[i-1])%mod; } } static long ncrWithMod(int n,int r){ if(n<r)return 0L; return (fact[n]*modInverse((fact[r]*fact[n-r])%mod,mod-2))%mod; } public static void main(String [] args){ InputReader sc=new InputReader(System.in); int t = sc.ni(); while(t -- > 0 ){ int n = sc.ni(); int a[][] = new int[n][3]; for(int i =0 ; i < n ; i ++){ a[i][0] = sc.ni(); } for(int i =0 ; i < n ; i ++){ a[i][1] = sc.ni(); } for(int i =0 ; i < n ; i ++){ a[i][2] = sc.ni(); } int ans[] = new int[n]; for(int i = 0 ; i < n ; i ++){ for(int j = 0 ; j < 3 ; j ++){ int l = (i-1+n)%n; int r = (i+1)%n; if(a[i][j] != ans[l] && a[i][j]!=ans[r]){ ans[i] = a[i][j]; break; } } } for(int i1 : ans)w.print(i1+" "); w.println(); } w.close(); } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
4e77ddfae818dd55adeb9c62acb29b61
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.math.BigInteger; public final class codeForces { public static void main(String args[]) { StringBuilder ans=new StringBuilder();; FastReader in=new FastReader(); int T=in.nextInt(); while(T-->0) { int N=in.nextInt(); int A[]=new int[N]; int B[]=new int[N]; int C[]=new int[N]; for(int i=0; i<N; i++)A[i]=in.nextInt(); for(int i=0; i<N; i++)B[i]=in.nextInt(); for(int i=0; i<N; i++)C[i]=in.nextInt(); int a=A[0]; int b=B[0]; int c=C[0]; ans.append(a+" "); int f=a; for(int i=1; i<N-1; i++) { int x=A[i]; int y=B[i]; int z=C[i]; if(x!=a) { ans.append(x+" "); a=x; } else { ans.append(y+" "); a=y; } } int x=A[N-1]; int y=B[N-1]; int z=C[N-1]; if(x!=f && x!=a) { ans.append(x+"\n"); } else if(y!=a && y!=f) { ans.append(y+"\n"); } else { ans.append(z+"\n"); } }System.out.println(ans); } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } //fucntions //fucntions //fucntions //fucntions static int[] input(int A[]) //input of Int Array { FastReader in=new FastReader(); int N=A.length; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] input(long A[]) //Input of long Array { FastReader in=new FastReader(); for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } static int GCD(int a,int b) //wrong output if a ||b are intially zero { if(b==0) { return a; } else return GCD(b,a%b ); } static boolean isPrime(int N) { for(int i=2; i*i<N; i++) if(N%i==0)return false; return true; } } //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()) { try { st=new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
0a95264694d4ef700b13b6a9887027f9
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for (int i = 0; i < t; i++) { int n = in.nextInt(), prev = 0, first = 0; int[][] v = new int[n][3]; for (int j = 0; j < n; j++) { v[j][0] = in.nextInt(); } for (int j = 0; j < n; j++) { v[j][1] = in.nextInt(); } for (int j = 0; j < n; j++) { v[j][2] = in.nextInt(); } first = v[0][0]; prev = v[0][0]; System.out.format("%d ", prev); for (int j = 1; j < n; j++) { for (int k = 0; k < 3; k++) { if (v[j][k] != prev && v[j][k] != first) { prev = v[j][k]; break; } } System.out.format("%d ", prev); } System.out.println(""); } } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
3d1ae9615108eb819eef608b64f46c10
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s=new Scanner(System.in); int t=s.nextInt(); for(int i=0;i<t;i++) { int n=s.nextInt(); int[] arr=new int[n]; int[] brr=new int[n]; int[] crr=new int[n]; for(int j=0;j<n;j++) { arr[j]=s.nextInt(); } for(int j=0;j<n;j++) { brr[j]=s.nextInt(); } for(int j=0;j<n;j++) { crr[j]=s.nextInt(); } ArrayList<Integer> list=new ArrayList<>(); list.add(arr[0]); for(int j=1;j<n-1;j++) { if(arr[j]!=list.get(j-1)) { list.add(arr[j]); } else if(brr[j]!=list.get(j-1)) { list.add(brr[j]); } else { list.add(crr[j]); } } if(arr[n-1]!=list.get(n-2)&&arr[n-1]!=list.get(0)) { list.add(arr[n-1]); } else if(brr[n-1]!=list.get(n-2)&&brr[n-1]!=list.get(0)) { list.add(brr[n-1]); } else { list.add(crr[n-1]); } for(int j=0;j<n;j++) { System.out.print(list.get(j)+" "); } System.out.println(); } } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
180f5832b4d04de58098605732dcb326
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
//package CodeForces; import java.io.*; import java.util.*; import javafx.util.Pair; public class Main { public class Haha { /* _____ ___ /\ /\ | | | | /\ /\ /\ /\ /\ /\ | \ Ω…Ψ­Ω…Ψ― Ψ£Ψ¨ΩˆΨ­Ψ³Ω†* / \ / \ | | |___| /__\ / \ / \ / \ / \ /__\ | \ / \ / \ | | | | / \ / \ / \ / \ / \ / \ | / / \/ \ |_____| | | / \ / \/ \ / \/ \ / \ |___/ */ } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static class MyPair { int x; int y; public MyPair(int x, int y) { this.x = x; this.y = y; } } public static int Fibonacci(int n) { int[] a = new int[n + 1]; a[0] = 0; a[1] = 1; for (int i = 2; i < a.length; i++) { a[i] = a[i - 1] + a[i - 2]; } return a[n]; } public static boolean isprime(int n) { for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return true; } } return false; } public static int[] addarr(int[] a, int n) { for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } return a; } public static int fact(int n) { if(n == 0) return 1; return n * fact(n-1); } public static long sumDigit(long n) { int a = 0; while (n != 0) { a += n % 10; n /= 10; } return a; } public static long fastpow(long v, long p) { if (p == 0) { return 1; } if (p == 1) { return v; } long ans = fastpow(v, p / 2); if (p % 2 == 1) { return ans * ans * v; } else { return ans * ans; } } public static int LCS(String s1, String s2, int i, int j){ // String s1 = sc.next(); // String s2 = sc.next(); // dp = new int[s1.length()][s2.length()]; // System.out.println(LCS(s1, s2, s1.length()-1, s2.length()-1)); if(i == -1 || j == -1) return 0; else if(dp[i][j] != 0) return dp[i][j]; else if(s1.charAt(i) == s2.charAt(j)) return dp[i][j] = 1 + LCS(s1, s2, i-1, j-1); else return dp[i][j] = Math.max(LCS(s1, s2, i-1, j), LCS(s1, s2, i, j-1)); } public static MyScanner sc = new MyScanner(); public static int MOD = 1000000007; // __builtin_popcountll(n) <-- c++ // binary count(1) ex. 8 --> 0100 --> 1 HashSet<Integer> set = new HashSet<>(); HashMap<Integer, Integer> map = new HashMap<>(); Random r = new Random(); ArrayList<Integer> l = new ArrayList<>(); public static int[][] dp; public static void main(String[] args) { int t = 1; t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); ArrayList<Integer> l = new ArrayList<>(); int[][] a = new int[3][n]; for (int i = 0; i < 3; i++) for (int j = 0; j < n; j++) a[i][j] = sc.nextInt(); l.add(a[2][0]); for (int i = 1; i < n; i++) { if(i == n-1){ if(l.get(0) != a[0][i]){ if(l.get(i-1) != a[0][i]){ l.add(a[0][i]); continue; } } if(l.get(0) != a[1][i]){ if(l.get(i-1) != a[1][i]){ l.add(a[1][i]); continue; } } l.add(a[2][i]); } else if(l.get(i-1) != a[0][i]) l.add(a[0][i]); else if(l.get(i-1) != a[1][i]) l.add(a[1][i]); else l.add(a[2][i]); } // System.out.println(l); for (int i = 0; i < n; i++) System.out.print(l.get(i) + " "); System.out.println(""); } } } /* 1 7 1 3 3 1 1 1 1 2 4 4 3 2 2 4 4 2 2 2 4 4 2 */
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
7b9cdc07196d368f1117b59b94d994d1
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.util.Scanner; public class Solution1 { public static void main(String[] args) { Solution1 s = new Solution1(); Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while(T-- > 0) { s.solve(sc); System.out.println(); } sc.close(); } public void solve(Scanner sc) { int N = sc.nextInt(); if(N == 0) { System.out.print(0); return; } int[] a = new int[N+1]; int[] b = new int[N+1]; int[] c = new int[N+1]; for(int i=0; i< N; i++) { a[i] = sc.nextInt(); } for(int i=0; i< N; i++) { b[i] = sc.nextInt(); } for(int i=0; i< N; i++) { c[i] = sc.nextInt(); } int p[] = new int[N]; p[0] = a[0]; for(int i=1;i<N;i++) { if(a[i] == p[i-1]) { if(b[i] == p[i-1]) { p[i] = c[i]; } else { p[i] = b[i]; } } else { p[i] = a[i]; } if(a[N-1] == p[0] || a[N-1] == p[N-2]) { if(b[N-1] == p[0] || b[N-1] == p[N-2]) { p[N-1] = c[i]; } else { p[N-1] = b[i]; } } else { p[N-1] = a[i]; } } for(int i=0;i<N;i++) { System.out.print(p[i] + " "); } } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
3109f3ba0d0c717e47111af368547e12
train_001.jsonl
1601476500
You are given three sequences: $$$a_1, a_2, \ldots, a_n$$$; $$$b_1, b_2, \ldots, b_n$$$; $$$c_1, c_2, \ldots, c_n$$$.For each $$$i$$$, $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$.Find a sequence $$$p_1, p_2, \ldots, p_n$$$, that satisfy the following conditions: $$$p_i \in \{a_i, b_i, c_i\}$$$ $$$p_i \neq p_{(i \mod n) + 1}$$$.In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i&lt;n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value.It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class CF1408A { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); for (int p = 0; p < t; p++) { int n = Integer.parseInt(br.readLine()); int[] a = new int[n]; int[] b = new int[n]; int[] c = new int[n]; String[] strings; strings = br.readLine().split(" "); for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(strings[i]); } strings = br.readLine().split(" "); for (int i = 0; i < n; i++) { b[i] = Integer.parseInt(strings[i]); } strings = br.readLine().split(" "); for (int i = 0; i < n; i++) { c[i] = Integer.parseInt(strings[i]); } int[] ans = new int[n]; ans[0] = a[0]; for (int i = 1; i < n-1; i++) { if (a[i] != ans[i-1]) ans[i] = a[i]; else if (b[i] != ans[i-1]) ans[i] = b[i]; else ans[i] = c[i]; } if (a[n-1] != ans[0] && a[n-1] != ans[n-2]) ans[n-1] = a[n-1]; else if (b[n-1] != ans[0] && b[n-1] != ans[n-2]) ans[n-1] = b[n-1]; else ans[n-1] = c[n-1]; for(int a_: ans) { System.out.print(a_ + " "); } System.out.println(); } } }
Java
["5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3"]
1 second
["1 2 3\n1 2 1 2\n1 3 4 3 2 4 2\n1 3 2\n1 2 3 1 2 3 1 2 3 2"]
NoteIn the first test case $$$p = [1, 2, 3]$$$.It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 \neq p_2 $$$, $$$p_2 \neq p_3 $$$, $$$p_3 \neq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$.In the second test case $$$p = [1, 2, 1, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal.In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$.In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
Java 8
standard input
[ "constructive algorithms" ]
7647528166b72c780d332ef4ff28cb86
The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 100$$$). It is guaranteed that $$$a_i \neq b_i$$$, $$$a_i \neq c_i$$$, $$$b_i \neq c_i$$$ for all $$$i$$$.
800
For each test case, print $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$p_i \in \{a_i, b_i, c_i\}$$$, $$$p_i \neq p_{i \mod n + 1}$$$). If there are several solutions, you can print any.
standard output
PASSED
0cc242cd47f21e6e1d41847d8c17ed74
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.util.*; import java.io.*; public class Round135 { public static void main(String[] args) throws IOException { BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int k = sc.nextInt(); String s = sc.next(); int[] a = new int[26]; for(int i=0; i<s.length(); i++){ int ch = (int) (s.charAt(i)-'a'); a[ch]++; } for(int i=0; i<26; i++){ if(a[i]%k!=0){ System.out.println(-1); return; } } String res = ""; for(int i=0; i<26; i++){ int num = a[i]/k; for(int x=0; x<num; x++) res += (char) ('a'+i); } for(int x=0; x<k; x++) pw.print(res); pw.println(); pw.flush(); } static StringTokenizer st; }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
b957f775ba6896e066331f3bc978cb14
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.util.Scanner; public class Kstring { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int k = sc.nextInt(); String cad = sc.next(); int[] ar = new int[26]; boolean correcto = true; String cadFin = ""; for(int i=0; i<cad.length(); i++) { ar[(int)cad.charAt(i) - 97]++; } for(int i=0; i<ar.length; i++) { if(ar[i] > 0 && ar[i] % k != 0) { correcto = false; } } if(correcto) { for(int i=0; i<k; i++) { for(int j=0; j<ar.length; j++) { if(ar[j] != 0) { for(int f=0; f<ar[j] / k; f++) { cadFin += (char)(j + 97); } } } } System.out.println(cadFin); } else { System.out.println(-1); } } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
ed66f11bbd663b0161429f550e45c4bc
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import static java.util.Arrays.copyOfRange; import static java.util.Arrays.sort; import java.io.*; import java.util.*; public class A { static void solve() throws IOException { int k = nextInt(); char[] c = nextToken().toCharArray(); String res = get(c, k); if (res == null) { out.println("-1"); } else { out.println(res); } } static String get(char[] c, int k) { if (c.length % k != 0) { return null; } sort(c); char[] res = new char[c.length]; int cnt = 0; for (int i = 0; i < c.length / k; i++) { for (int j = 0; j < k; j++) { res[j * c.length / k + i] = c[cnt++]; } } for (int i = 0; i < k; i++) { if (!Arrays.equals( copyOfRange(res, i * c.length / k, (i + 1) * c.length / k), (copyOfRange(res, 0, c.length / k)))) { return null; } } return new String(res); } static BufferedReader br; static StringTokenizer st; static PrintWriter out; static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } static long nextLong() throws IOException { return Long.parseLong(nextToken()); } static double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } static String nextToken() throws IOException { return hasNextToken() ? st.nextToken() : null; } static boolean hasNextToken() throws IOException { while (st == null || !st.hasMoreTokens()) { String line = br.readLine(); if (line == null) { return false; } st = new StringTokenizer(line); } return true; } public static void main(String[] args) throws IOException { InputStream input = System.in; File inputFile = new File("fatrat.in"); if (inputFile.canRead()) { input = new FileInputStream(inputFile); } br = new BufferedReader(new InputStreamReader(input)); out = new PrintWriter(System.out); solve(); br.close(); out.close(); } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
7e42a86880bfac12b72a7da9b3cfe210
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.util.Scanner; public class k_String { public static void main(String[] args) { Scanner input = new Scanner(System.in); int k = input.nextInt(); input.nextLine(); String s = input.nextLine(); final int NUMOFCHARS = 26; int[] num = new int[NUMOFCHARS]; for (int i = 0; i < NUMOFCHARS; ++i) { num[i] = 0; } for (int i = 0; i < s.length(); ++i) { ++num[s.charAt(i) - 'a']; } for (int i = 0; i < NUMOFCHARS; ++i) { if (num[i] % k != 0) { System.out.println(-1); return; } else { num[i] /= k; } } StringBuilder sb = new StringBuilder(); for (int i = 0; i < NUMOFCHARS; ++i) { for (int j = 0; j < num[i]; ++j) { sb.append((char)('a' + i)); } } for (int i = 0; i < k; ++i) { System.out.print(sb.toString()); } System.out.println(); } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
6ec302b3d149fc7503867e74b1b70cd3
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.util.*; public class A219 { public static void main(String[] args){ Scanner br = new Scanner(System.in); int k = br.nextInt(); String line = br.next(); int[] freq = new int[26]; for(int i = 0;i<line.length();i++){ freq[line.charAt(i)-'a']++; } boolean good = true; for(int i = 0;i<26;i++){ if(freq[i]%k != 0){ good = false; } } if(!good){ System.out.println(-1); } else{ StringBuilder res = new StringBuilder(""); for(int i = 0;i<26;i++){ int times = freq[i]/k; for(int j = 0;j<times;j++){ res.append((char)(i+'a')); } } for(int i = 0;i<k;i++){ System.out.print(res); } System.out.println(); } } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
3b037898eb1117944ae7ca55241f7003
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.util.Scanner; public class A_219_k_String { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int k = sc.nextInt(); char s[] = sc.next().toCharArray(); int a[] = new int[255]; for (int i = 0; i < s.length; i++) { a[s[i]]++; } for (int i = 'a'; i <= 'z'; i++) { if (a[i] % k != 0) { System.out.print(-1); return; } } String answer = ""; for (int i = 0; i < a.length; i++) { if (a[i] > 0) { int n = a[i] / k; for (int j = 0; j < n; j++) { answer += (char) i; } } } for (int i = 0; i < k; i++) { System.out.print(answer); } } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
49c913f64c6e0efe0d2553cd58a4f569
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; import java.lang.*; public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here try { Parserdoubt pd=new Parserdoubt(System.in); int k=pd.nextInt(); String str=pd.nextString(); int tmp[]=new int[200]; for(int i=0;i<str.length();i++) tmp[str.charAt(i)]++; boolean can=true; for(int i=0;i<tmp.length;i++) if(tmp[i]%k!=0) can=false; if(can) { StringBuffer sb=new StringBuffer(); for(int i=0;i<200;i++) { for(int j=0;j<tmp[i]/k;j++) sb.append((char)i); } String s=sb.toString(); sb=new StringBuffer(); for(int i=0;i<k;i++)sb.append(s); System.out.println(sb); } else System.out.println("-1"); } catch(Exception e) { e.printStackTrace(); } } } class Parserdoubt { final private int BUFFER_SIZE = 1 << 17; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Parserdoubt(InputStream in) { din = new DataInputStream(in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String nextString() throws Exception { StringBuffer sb = new StringBuffer(""); byte c = read(); while (c <= ' ') { c = read(); } do { sb.append((char) c); c = read(); } while (c > ' '); return sb.toString(); } public char nextChar() throws Exception { byte c = read(); while (c <= ' ') { c = read(); } return (char) c; } public int nextInt() throws Exception { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) { return -ret; } return ret; } public long nextLong() throws Exception { long ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) { return -ret; } return ret; } private void fillBuffer() throws Exception { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) { buffer[0] = -1; } } private byte read() throws Exception { if (bufferPointer == bytesRead) { fillBuffer(); } return buffer[bufferPointer++]; } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
41f134db6cb3f7ac0d1c47b4f393bc0a
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.io.*; import java.util.*; public class test1 { public static void main(String[] args) throws Exception { new test1().run(); } PrintWriter out = null; void run() throws Exception { Scanner in = new Scanner(System.in); out = new PrintWriter(System.out); int k = in.nextInt(); String s=in.next(); int[] count=new int[26]; for(int i=0;i<s.length();i++) { count[s.charAt(i)-'a']++; } boolean f=true; for(int i=0;i<26;i++) { if(count[i]%k!=0) { f=false; break; } } if(f) { StringBuffer sb=new StringBuffer(); for(int i=0;i<26;i++) { for(int j=0;j<count[i]/k;j++) sb.append((char)('a'+i)); } for(int i=0;i<k;i++) out.print(sb.toString()); } else out.println(-1); out.close(); } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
7e6278741a86c7a54d68e66ef76cf2da
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int k = s.nextInt(); s.nextLine(); String w = s.nextLine(); int[] cc = new int[26]; for (int j = 0; j < w.length(); ++j) cc[(w.charAt(j) - 'a')]++; StringBuffer out = new StringBuffer(); for (int j = 0; j < 26; j++) { if (cc[j] == 0) continue; if (cc[j] % k != 0) { out.setLength(0); out.append("-1"); break; } for (int l = 0; l < cc[j]/k; l++) out.append((char) (j + 'a')); } if (!out.toString().equals("-1")) { String f = out.toString(); for (int j = 1; j < k; j++) out.append(f); } System.out.println(out.toString()); } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
89fda8e44edd83c28c8efdcafc5056f5
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.util.Scanner; public class A { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); String line = in.next(); int letters[] = new int[26]; for(int i=0;i<line.length();i++) letters[line.charAt(i)-'a']++; for(int i=0;i<26;i++) if(letters[i]%n!=0) { System.out.println("-1"); return; } String ans=""; for(int i=0;i<26;i++) for(int j=0;j<letters[i]/n;j++) ans+=(char)(i+'a'); String res=""; for(int i=0;i<n;i++) res+=ans; System.out.println(res); } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
585ebdd2fef2fabfcb18c8b6e7518d1c
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.io.*; import java.util.*; public class KString{ BufferedReader in; PrintWriter out; StringTokenizer st; public void run() throws Exception { //in = new BufferedReader(new FileReader("D.IN")); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int k=nextInt(); String s=next(); char[] map=new char[26]; for (int i = 0; i < s.length(); i++) { map[s.charAt(i)-'a']++; } for (int i = 0; i < 26; i++) { if(map[i]%k!=0) {System.out.println(-1); return;} } String res=""; for (int i = 0; i < 26; i++) { int times=map[i]/k; for (int j = 0; j < times; j++) { res+=(char)('a'+i); } } String toPrint=""; for (int i = 0; i < k; i++) { toPrint+=res; } System.out.println(toPrint); out.flush(); out.close(); in.close(); } public static void main(String[] args) throws Exception { new KString().run(); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (Exception e) { } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
18970ca99557bfc0eb3b96156137d7ab
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Locale; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeMap; public class CFA { BufferedReader in; StringTokenizer st; PrintWriter out; public static void main(String[] args) { CFA t = new CFA(); t.init(); } void run() throws Exception { int k = Int(); String s = String(); TreeCounter<Character> counter = new TreeCounter<Character>(); for (char c : s.toCharArray()) { counter.increment(c); } StringBuilder sb = new StringBuilder(); int div = 0; for (char c : counter.getKeySet()) { if (counter.count(c) % k != 0) { PrintLn("-1"); return; } else { div = (int) counter.count(c) / k; for (int i = 0; i < div; i++) { sb.append(c); } } } String copy = sb.toString(); for (int i = 0; i < k - 1; i++) { sb.append(copy); } PrintLn(sb); } static class TreeCounter<K> { private final TreeMap<K, long[]> map = new TreeMap<K, long[]>(); public void increment(K element) { long[] el = map.get(element); if (el == null) { map.put(element, new long[] { 1 }); } else { el[0]++; } } public long count(K element) { long[] el = map.get(element); return el == null ? 0 : map.get(element)[0]; } public Set<K> getKeySet() { return map.keySet(); } } void init() { try { Locale.setDefault(Locale.US); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); run(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } String String() throws Exception { return next(); } int Int() throws Exception { return Integer.parseInt(next()); } int[] IntArray(int n) throws Exception { int[] array = new int[n]; for (int i = 0; i < array.length; i++) { array[i] = Int(); } return array; } long Long() throws Exception { return Long.parseLong(next()); } double Double() throws Exception { return Double.parseDouble(next()); } void PrintLn(Object line) { out.println(line); } void PrintArray(int[] array) { for (int i = 0; i < array.length; i++) { out.print(array[i] + " "); } out.println(); } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
6ddb479513b7c0368944f3ca059dd5a4
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Problem219A { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); String s = in.readLine(); int[] arr = new int[26]; for (int i = 0; i < s.length(); i++) { byte b = (byte) s.charAt(i); arr[b - 97]++; } System.out.println(checkAva(arr,n)); } public static String checkAva(int[] arr, int n) { String s=""; for (int a : arr) { if (a != 0 && a % n != 0) return "-1"; } for (int i = 0; i < n; i++) { for (int j=0;j<arr.length;j++) { for (int k=0;k<arr[j]/n;k++) { s+= (char) (byte) (j+97); } } } return s; } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
3c255c7124b14a883e79f5354ed6ea92
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Scanner; public class main { public static void main(String[] args) { Scanner s= new Scanner(new BufferedReader(new InputStreamReader(System.in))); //BufferedReader n = new BufferedReader(new InputStreamReader(System.in)); int k=s.nextInt(); String aux=s.next(); String a="a"; if(k==1){ System.out.println(aux); } else{ int [] arr=new int[27]; for (int i=0;i<27;i++){ arr[i]=0; } for (int i=0;i<aux.length();i++){ arr[aux.codePointAt(i)-97]++; } String resp=""; for (int i=0;i<arr.length;i++){ if (arr[i]!=0){ if (arr[i]%k==0){ for(int j=0;j<arr[i]/k;j++) resp+=(char)(i+97); } else{ resp="-1"; break; } } } String answer=resp; if (!resp.equals("-1")){ for (int i=0;i<k-1;i++){ answer+=resp; } } System.out.println(answer); } } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
0df4ab35fa9ce03dab1ae56a9d994764
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { static int INF = Integer.MAX_VALUE; public static void main(String args[]){ Scanner s = new Scanner(System.in); int k = s.nextInt(); int count[] = new int[150]; String str = s.next(); for(char c : str.toCharArray()){ count[c]++; } int ok = 1; for (int i = 0; i < count.length; i++) { int val = count[i]; if(val > 0 && val%k != 0) ok = 0; } if(ok == 0){ System.out.println("-1"); return ; } String rez = ""; for (int i = 0; i < count.length; i++) { int val = count[i]; while(val > 0){ val -= k; rez += (char)i; } } // System.out.println(rez); String ans = ""; for (int i = 0; i < k; i++) { ans += rez; } System.out.println(ans); } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
f3e17e8551fb1575c65f5e82ac40cb66
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.io.*; import java.util.*; public class CodeForce { private void solve() throws IOException { final int K = nextInt(); int []C = new int[26]; final String line = nextLine(); Set<Character> ccs = new HashSet<Character>(); for(char c : line.toCharArray()) { ccs.add(c); C[c - 'a']++; } for(char c : ccs) { if(C[c - 'a'] % K != 0) { System.out.println(-1); return; } } StringBuffer buf = new StringBuffer(); for(char c : ccs) { int n = C[c - 'a'] / K; for(int i = 0; i < n; i++) buf.append(c); } StringBuffer ret = new StringBuffer(); for(int i = 0; i < K; i++) { ret.append(buf); } System.out.println(ret.toString()); } public static void main(String[] args) { new CodeForce().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() { try { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; writer = new PrintWriter(new FileOutputStream(new File("output.txt"))); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String nextLine() throws IOException { return reader.readLine(); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
a7c9d5889458fdf07e88d1454b39ff03
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.util.*; /************************************************************************* * 219D Codeforce * *************************************************************************/ public class Sourcecode { public static void main (String[] args) { Scanner scan=new Scanner(System.in); int N=scan.nextInt(); String inputstring=scan.next(); if(inputstring.length()%N!=0){System.out.println("-1");return;} Map<Character,Integer> mymap=new HashMap<Character,Integer>(); for(int i=0;i<inputstring.length();i++){ char ati=inputstring.charAt(i); if(!mymap.containsKey(ati)){ mymap.put(inputstring.charAt(i), 1); } else{ int a=mymap.get(ati); a++; mymap.put(ati, a); } } char[] repeatpiece=new char[inputstring.length()/N]; int index=0; for(char entry : mymap.keySet()){ int times=(int)mymap.get(entry)%N; if(times!=0){ System.out.println("-1"); return; } int piecetimes=(int)mymap.get(entry)/N; for(int i=0;i<piecetimes;i++){ repeatpiece[index]=(char)entry; index++; } } String piece=String.valueOf(repeatpiece); for(int i=0;i<N;i++){ System.out.print(piece);} } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
0fdf78ffec96c562dbc840c4bbbfbb3d
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.util.*; import java.io.*; public class A { public void solve() throws IOException { int n = nextInt(); String str = nextToken(); int[] letter = new int[26]; for(int i = 0; i < str.length(); i++){ char x = str.charAt(i); letter[(int)(x-'a')]++; } StringBuilder temp = new StringBuilder(); for(int i = 0; i < 26; i++){ int add = letter[i]/n; while( add-- > 0 ){ temp.append((char)('a'+i)); } } if( temp.length()*n == str.length() ){ StringBuilder ret = new StringBuilder(); while( n-- > 0 ){ ret.append(temp); } writer.println(ret); } else { writer.println(-1); } } public static void main(String[] args) throws IOException { new A().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() throws IOException { try { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
857c7fb7a1031bd1eb8389a19dca2bb1
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.util.*; import java.io.*; public class A { public void solve() throws IOException { int n = nextInt(); String str = nextToken(); int[] letter = new int[26]; for(int i = 0; i < str.length(); i++){ char x = str.charAt(i); letter[(int)(x-'a')]++; } StringBuilder temp = new StringBuilder(); for(int i = 0; i < 26; i++){ int add = letter[i]/n; while( add-- > 0 ){ temp.append((char)('a'+i)); } } if( temp.length()*n == str.length() ){ StringBuilder ret = new StringBuilder(); while( n-- > 0 ){ ret.append(temp); } writer.println(ret); } else { writer.println(-1); } } public static void main(String[] args) throws IOException { new A().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() throws IOException { try { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
ec59765bb10f6c88bf2d2cc4d6d56e23
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
//package arbuz; import java.util.Scanner; public class Arbuz { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int k = sc.nextInt(); String s = sc.next(); int[] num = new int[26]; int i; for (i = 0; i < s.length(); i++) { num[s.charAt(i) - 97]++; } boolean possible = true; for (i = 0; i < 26; i++) { if (num[i] % k != 0) { possible = false; } else { num[i] = num[i] / k; } } if (possible) { for (i = 0; i < k; i++) { for (int j = 0; j < 26; j++) { for (int n = 0; n < num[j]; n++) { System.out.print((char) (j + 97)); } } } } else { System.out.println(-1); } } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
708b9146000a9ce26cf47700e84d1697
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.util.Scanner; public class A_219 { public static void main(String[] args){ Scanner sc = new Scanner(System.in); final int k = sc.nextInt(); int[] alpha = new int[26]; for(char c : sc.next().toCharArray()){ alpha[c - 'a']++; } boolean flag = true; for(int i = 0; i < 26; i++){ if(alpha[i] % k != 0){ flag = false; break; } } if(!flag){ System.out.println(-1); }else{ //System.out.println(min + " " + k); for(int i = 0; i < 26; i++){ alpha[i] /= k; } StringBuilder sb = new StringBuilder(); for(int i = 0; i < 26; i++){ final int times = alpha[i]; for(int j = 0; j < times; j++){ sb.append((char)(i + 'a')); } } String str = sb.toString(); for(int i = 1; i < k; i++){ sb.append(str); } System.out.println(sb); } sc.close(); } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
dbc3ff98ef717dbf08919d954bd5edd6
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.io.*; import java.util.*; public class KString{ public static void main(String args[])throws IOException{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); String s = in.readLine(); int arr[] = new int[26]; boolean cek = true; String res = ""; for(int i=0;i<s.length();i++){ int temp = (int)s.charAt(i)-'a'; arr[temp]++; } for(int x : arr) if(x%n!=0)cek=false; if(cek){ for(int i=0;i<26;i++){ if(arr[i]!=0){ int temp = arr[i]/n; for(int j=0;j<temp;j++){ res += (char)(i+'a'); } } } String res2= ""; for(int i=0;i<n;i++){ res2+=res; } System.out.println(res2); }else{ System.out.println(-1); } } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
09205b0b98295f51882847aed0c97664
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.awt.Point; import java.io.*; import java.math.BigInteger; import java.util.*; import static java.lang.Math.*; public class A implements Runnable{ final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws FileNotFoundException{ if (ONLINE_JUDGE){ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); }else{ in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } } String readString() throws IOException{ while(!tok.hasMoreTokens()){ try{ tok = new StringTokenizer(in.readLine()); }catch (Exception e){ return null; } } return tok.nextToken(); } int readInt() throws IOException{ return Integer.parseInt(readString()); } long readLong() throws IOException{ return Long.parseLong(readString()); } double readDouble() throws IOException{ return Double.parseDouble(readString()); } public static void main(String[] args){ new Thread(null, new A(), "", 64* (1L << 20)).start(); } long timeBegin, timeEnd; void time(){ timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } long memoryTotal, memoryFree; void memory(){ memoryFree = Runtime.getRuntime().freeMemory(); System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10) + " KB"); } void debug(Object... objects){ if (DEBUG){ for (Object o: objects){ System.err.println(o.toString()); } } } public void run(){ try{ timeBegin = System.currentTimeMillis(); memoryTotal = Runtime.getRuntime().freeMemory(); init(); solve(); out.close(); time(); memory(); }catch (Exception e){ e.printStackTrace(System.err); System.exit(-1); } } boolean DEBUG = false; void solve() throws IOException{ int[] count = new int[26]; int k = readInt(); char[] c = in.readLine().toCharArray(); for (char ch: c){ count[ch-'a']++; } for (int i = 0; i < 26; ++i){ if (count[i] % k != 0){ out.print(-1); return; } } StringBuilder str = new StringBuilder(); for (int i = 0; i < 26; ++i){ for (int j = 0; j < count[i] / k; ++j){ str = str.append((char) (i + 'a')); } } for (int i = 0; i < k; ++i){ out.print(str); } } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
f528d1b0b805abfce8b10f461faf381b
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.util.*; import java.io.*; public class C135A { /** * @param args */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String s = sc.next(); int[] w = new int[26]; if (n == 1) { System.out.println(s); return; } if (n > s.length() || s.length() % n != 0) { System.out.println(-1); return; } for (int i = 0; i < s.length(); i++) { int val = s.charAt(i) - 'a'; w[val]++; } boolean bad = false; for (int i = 0; i < w.length; i++) { if (w[i] % n != 0) { bad = true; break; } w[i] /= n; } if (bad) { System.out.println(-1); } else { String p = ""; for (int i = 0; i < w.length; i++) { for (int j = 0; j < w[i]; j++) { p += (char)('a' + i); } } for (int i = 0; i < n; i++) { System.out.print(p); } System.out.println(); } } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
26e36501861b2dee1766db7b58f76f83
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.util.Scanner; public class A_k_String { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n =sc.nextInt(); String s=sc.next(); int a[]=new int[30]; for (int i = 0; i < s.length(); i++) { a[(int)s.charAt(i)-96]++; } for (int i = 1; i <=26; i++) { if(a[i]!=0 && a[i]%n!=0){ System.out.println(-1); return; } } for (int i = 1; i <=n; i++) { for (int j = 1; j <=26; j++) { for (int j2 =1; j2 <=a[j]/n; j2++) { System.out.print((char)(j+96)); } } } } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
37677f618173e959928fe71d27b2e096
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.util.Scanner; public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int k = sc.nextInt(); String s = sc.next(); if (s.length()%k != 0){ System.out.println(-1); return; } int []a = new int [26]; for (int i = 0; i < s.length(); i++) { a[s.codePointAt(i)-97]++; } for (int i = 0; i < 26; i++) { if (a[i] % k != 0) { System.out.println(-1); return; } a[i] /= k; } for (int i = 1; i <= k; i++) { for (int j = 0; j < 26; j++) { for (int j2 = 1; j2 <= a[j]; j2++) { System.out.print((char)(j+97)); } } } } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
30c847a5fb3f8e6ff434eb03e6e6fef7
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class Main{ public static String res(String s ,int x){ if (x==1) return s; if ((s.length()+1)%x==0) return "-1"; char [] temp = new char [s.length()+1]; for (int i=0; i<s.length();i++) temp[i]=s.charAt(i); temp[s.length()]='|'; Arrays.sort(temp); String ans=""; for (int i=0;i<s.length();i++){ if (temp[i]!=temp[i+1]&&(i+1)%x!=0) return "-1"; else if ((i+1)%x==0) ans+=temp[i]+""; } String answer=""; while(x-->0) answer+=ans; return answer; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int x = Integer.parseInt(br.readLine()); String s =br.readLine(); System.out.println(res(s,x)); } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output
PASSED
72e220e5810fa7a40b0e502a299cf82c
train_001.jsonl
1346081400
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
256 megabytes
import java.util.Arrays; import java.util.Locale; import java.util.Scanner; public class Codeforces2 { public static void main(String[] args){ Scanner sc=new Scanner(System.in); String k=""; int n=sc.nextInt(); String S=sc.next(); int []a=new int[255]; for(int i=0;i<255;i++) a[i]=0; for(int i=0;i<S.length();i++){ a[S.codePointAt(i)]++; } int l=0; for(int i=0;i<255;i++){ l+=a[i]%n; } if(l==0){ for(int i=0;i<S.length();i++){ for(int j=0;j<a[S.codePointAt(i)]/n;j++){ k+=S.charAt(i); } a[S.codePointAt(i)]=0; } for(int i=0;i<n;i++){ System.out.print(k); } }else{ System.out.println("-1"); } } }
Java
["2\naazz", "3\nabcabcabz"]
2 seconds
["azaz", "-1"]
null
Java 6
standard input
[ "implementation", "strings" ]
f5451b19cf835b1cb154253fbe4ea6df
The first input line contains integer k (1 ≀ k ≀ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≀ |s| ≀ 1000, where |s| is the length of string s.
1,000
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes).
standard output