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
bb0f74d42a400b966c933739a0d173cc
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i > a_{i - 1}$$$ and $$$a_i > a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { // write your code here Scanner sc=new Scanner(System.in); int T=sc.nextInt(); for(int t=0;t<T;t++) { int n = sc.nextInt(); //int x=sc.nextInt(); long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextLong(); } int count = 0; if(n==2){ System.out.println(0); for (int i = 0; i < n; i++) { System.out.print(a[i]+" "); } } else { for (int i = 1; i < n - 2; i++) { if (a[i] > a[i - 1] && a[i] > a[i + 1]) { count++; if (a[i + 2] > a[i]) { a[i + 1] = a[i + 2]; } else { a[i + 1] = a[i]; } } } if (a[n - 2] > a[n - 3] && a[n - 2] > a[n - 1]) { count++; a[n - 1] = a[n - 2]; } System.out.println(count); for (int i = 0; i < n; i++) { System.out.print(a[i] + " "); } } System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
b2c01db08c90ee1923ad021b91c67c3a
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
/* "Everything in the universe is balanced. Every disappointment you face in life will be balanced by something good for you! Keep going, never give up." Just have Patience + 1... */ import java.util.*; import java.lang.*; import java.io.*; public class Solution { public static void main(String[] args) throws java.lang.Exception { out = new PrintWriter(new BufferedOutputStream(System.out)); sc = new FastReader(); int test = sc.nextInt(); for (int t = 1; t <= test; t++) { solve(); } out.close(); } private static void solve() { int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } int operations = 0; for (int i = 1; i < n - 1; i++) { if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) { operations++; arr[i + 1] = Math.max(arr[i + 1], arr[i]); if (i + 2 < n) { arr[i + 1] = Math.max(arr[i + 1], arr[i + 2]); } } } out.println(operations); for (int i = 0; i < n; i++) { out.print(arr[i] + " "); } out.println(); } public static FastReader sc; public static PrintWriter out; static class FastReader { BufferedReader br; StringTokenizer str; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (str == null || !str.hasMoreElements()) { try { str = new StringTokenizer(br.readLine()); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } } return str.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 lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } return str; } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
20f89e62fd8ae9411ff19fadb725ae44
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.*; import java.util.*; public class Main { static PrintWriter out; static Kioken sc; public static void main(String[] args) throws FileNotFoundException { boolean t = true; boolean f = false; if (f) { out = new PrintWriter("output.txt"); sc = new Kioken("input.txt"); } else { out = new PrintWriter((System.out)); sc = new Kioken(); } int tt = 1; tt = sc.nextInt(); while (tt-- > 0) { solve(); } out.flush(); out.close(); } public static void solve() { int n = sc.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = sc.nextInt(); } boolean[] isMax = new boolean[n]; int cnt = 0; for(int i = 1; i < n - 1; i++){ if(arr[i] > arr[i-1] && arr[i] > arr[i+1]){ isMax[i] = true; } } // finding min. for(int i = 1; i < n-1; i++){ if(arr[i] < arr[i - 1] && arr[i] < arr[i+1]){ if(isMax[i-1] && isMax[i+1]){ arr[i] = Math.max(arr[i-1], arr[i+1]); cnt++; isMax[i-1] = false; isMax[i+1] = false; } } } for(int i = 1; i < n-1; i++){ if(isMax[i]){ arr[i] = Math.max(arr[i-1], arr[i+1]); cnt++; } } out.println(cnt); for(int i: arr){ out.print(i + " "); } out.println(); } public static long gcd(long a, long b) { while (b != 0) { long rem = a % b; a = b; b = rem; } return a; } public static long leftShift(long a) { return (long) Math.pow(2, a); } public static boolean[] sieve(int n) { boolean isPrime[] = new boolean[n + 1]; Arrays.fill(isPrime, true); isPrime[0] = false; isPrime[1] = false; for (int i = 2; i * i <= n; i++) { if (!isPrime[i]) continue; for (int j = i * 2; j <= n; j = j + i) { isPrime[j] = false; } } return isPrime; } public static void reverse(int[] arr) { Arrays.sort(arr); int n = arr.length; for (int i = 0; i < arr.length / 2; i++) { int temp = arr[i]; arr[i] = arr[n - 1 - i]; arr[n - 1 - i] = temp; } return; } public static int lower_bound(ArrayList<Integer> ar, int k) { int s = 0, e = ar.size(); while (s != e) { int mid = s + e >> 1; if (ar.get(mid) <= k) { s = mid + 1; } else { e = mid; } } return Math.abs(s) - 1; } public static int upper_bound(ArrayList<Integer> ar, int k) { int s = 0; int e = ar.size(); while (s != e) { int mid = s + e >> 1; if (ar.get(mid) < k) { s = mid + 1; } else { e = mid; } } if (s == ar.size()) { return -1; } return s; } static class Kioken { // FileInputStream br = new FileInputStream("input.txt"); BufferedReader br; StringTokenizer st; Kioken(String filename) { try { FileReader fr = new FileReader(filename); br = new BufferedReader(fr); st = new StringTokenizer(""); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } Kioken() { try { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } public String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); } return null; } public boolean hasNext() { String next = null; try { next = br.readLine(); } catch (Exception e) { } if (next == null || next.length() == 0) { return false; } st = new StringTokenizer(next); return true; } } class DSU { int[] parent, size; DSU(int n) { parent = new int[n]; size = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; size[i] = 1; } } int findParent(int i) { if (parent[i] == i) { return i; } return parent[i] = findParent(parent[i]); } void Union(int u, int v) { int parent_u = findParent(u); int parent_v = findParent(v); if (parent_u == parent_v) return; // small attached to big, since we want to reduce overall size if (size[parent_u] < size[parent_v]) { parent[parent_u] = parent_v; size[parent_v]++; } else { parent[parent_v] = parent_u; size[parent_u]++; } } } // SEGMENT-TREE static class SegmentTree { int[] arr = new int[4 * 100000]; int[] givenArr; // HINT: This can be updated with ques. int build(int index, int l, int r) { if (l == r) { return arr[index] = givenArr[l]; } int mid = (l + r) / 2; return arr[index] = build(2 * index + 1, l, mid) + build(2 * index + 2, mid + 1, r); } SegmentTree(int[] nums) { givenArr = nums; build(0, 0, nums.length - 1); } // HINT: This can be updated with ques. void update(int index, int l, int r, int diff, int i) { if (i >= arr.length) { return; } if (index >= l && index <= r) { arr[i] = arr[i] + diff; } if (index < l || index > r) { return; } int mid = (l + r) / 2; update(index, l, mid, diff, 2 * i + 1); update(index, mid + 1, r, diff, 2 * i + 2); return; } void update(int index, int val) { int diff = val - givenArr[index]; givenArr[index] = val; update(index, 0, givenArr.length - 1, diff, 0); } int query(int left, int right, int l, int r, int i) { // not overlapping if (r < left || l > right) { return 0; } // total - overlapping if (l >= left && r <= right) { return arr[i]; } // partial overlapping int mid = (l + r) / 2; int le = query(left, right, l, mid, 2 * i + 1); int ri = query(left, right, mid + 1, r, 2 * i + 2); return le + ri; } // HINT: for max sum, can be changed according to ques. int query(int l, int r) { return query(l, r, 0, givenArr.length - 1, 0); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
718b10e3a45c63eb1bd1d3a5b1e2c54a
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class Q2 { 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){ int n = Integer.parseInt(br.readLine()); String[] str = br.readLine().split(" "); long[] arr = new long[n]; for(int i=0; i<n; i++){ arr[i] = Long.parseLong(str[i]); } int changes = 0; boolean[] b = new boolean[n]; for(int i=1; i<n-1; i++){ if(arr[i]>arr[i-1] && arr[i]>arr[i+1]){ b[i] = true; } } for(int i=1; i<n; i++){ if(i < n-2 && b[i]==true && b[i+2]==true){ changes++; arr[i+1] = Math.max(arr[i], arr[i+2]); b[i] = false; b[i+2] = false; }else if(b[i]==true){ if(i+1 < n) arr[i] = Math.max(arr[i-1], arr[i+1]); else arr[i] = arr[i-1]; changes++; } } System.out.println(changes); for (int i = 0; i < n; i++) { if (i == n - 1) System.out.print(arr[i]); else System.out.print(arr[i] + " "); } if(t>1) System.out.println(); t--; } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
aaa5f9f2b14d83b1952d3a338cd50fd2
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; import java.io.*; public class Main { static BufferedReader br; static StringBuilder sb = new StringBuilder(); static int index; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); sb = new StringBuilder(); int t = toi(br.readLine()); while(t-- > 0) { int n = toi(br.readLine()); int[] arr = getArr(); int cnt = 0; for(int i = 1; i < n - 2; i++) { if(arr[i - 1] < arr[i] && arr[i] > arr[i + 1]) { cnt++; arr[i + 1] = Math.max(arr[i], arr[i + 2]); } } if(n > 2) { if(arr[n - 3] < arr[n-2] && arr[n-2] > arr[n-1] ) { arr[n-1] = arr[n-2]; cnt++; } } sb.append(cnt).append("\n"); for(int e : arr) sb.append(e).append(" "); sb.append("\n"); } print(sb); } static int toi(String s) { return Integer.parseInt(s); } static long tol(String s) { return Long.parseLong(s); } static String[] getLine() throws IOException { return br.readLine().split(" "); } static int[] getArr() throws IOException { return Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } static <T> void print(T s) { System.out.print(s); } static <T> void println(T s) { System.out.println(s); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
e305976487f524bc025be3959abc985a
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; import java.io.*; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static FastReader obj = new FastReader(); public static PrintWriter out = new PrintWriter(System.out); public static void sort(long[] a) { ArrayList<Long> arr = new ArrayList<>(); for (int i = 0; i < a.length; i++) arr.add(a[i]); Collections.sort(arr); for (int i = 0; i < arr.size(); i++) a[i] = arr.get(i); } public static void revsort(long[] a) { ArrayList<Long> arr = new ArrayList<>(); for (int i = 0; i < a.length; i++) arr.add(a[i]); Collections.sort(arr, Collections.reverseOrder()); for (int i = 0; i < arr.size(); i++) a[i] = arr.get(i); } //Cover the small test cases like for n=1 . public static class pair { long a; long b; pair(long x, long y) { a = x; b = y; } } public static long l() { return obj.nextLong(); } public static int i() { return obj.nextInt(); } public static String s() { return obj.next(); } public static long[] l(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = l(); return arr; } public static int[] i(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = i(); return arr; } public static long ceil(long a, long b) { return (a + b - 1) / b; } public static void p(long val) { out.println(val); } public static void p(String s) { out.println(s); } public static void pl(long[] arr) { for (int i = 0; i < arr.length; i++) { out.print(arr[i] + " "); } out.println(); } public static void p(int[] arr) { for (int i = 0; i < arr.length; i++) { out.print(arr[i] + " "); } out.println(); } public static void sortpair(Vector<pair> arr) { //ascending just change return 1 to return -1 and vice versa to get descending. //compare based on value of pair.a arr.sort(new Comparator<pair>() { public int compare(pair o1, pair o2) { long val = o1.a - o2.a; if (val == 0) return 0; else if (val > 0) return 1; else return -1; } }); } // Take of the small test cases such as when n=1,2 etc. // remember in case of fenwick tree ft is 1 based but our array should be 0 based. // in fenwick tree when we update some index it doesn't change the value to val but it // adds the val value in it so remember to add val-a[i] instead of just adding val. //in case of finding the inverse mod do it (biexpo(a,mod-2)%mod + mod )%mod public static void main(String[] args) { int len = i(); while (len-- != 0) { int n = i(); long[] a=l(n); int[] b=new int[n]; int c=0; for(int i=1;i<n-1;i++) { if(a[i]>a[i-1] && a[i]>a[i+1]) { if(i+2<n) a[i+1]=Math.max(a[i], a[i+2]); else a[i+1]=a[i]; c++; } } out.println(c); for(int i=0;i<n;i++)out.print(a[i]+" "); out.println(); } out.flush(); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
f644cc581db5ac510f9375978eafc3b5
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; import java.lang.*; import java.math.BigInteger; import java.io.*; public class Main implements Runnable { public static void main(String[] args) { new Thread(null, new Main(), "whatever", 1 << 26).start(); } // private FastScanner sc; private PrintWriter pw; public void run() { try { boolean isSumitting = true; // isSumitting = false; if (isSumitting) { pw = new PrintWriter(System.out); sc = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); } else { pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); sc = new FastScanner(new BufferedReader(new FileReader("input.txt"))); } } catch (Exception e) { throw new RuntimeException(); } int t = sc.nextInt(); // int t = 1; while (t-- > 0) { // sc.nextLine(); // System.out.println("for t=" + t); solve(); } pw.close(); } public long mod = 1_000_000_007; private class Pair { int first, second; Pair(int first, int second) { this.first = first; this.second = second; } } public void solve() { int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } ArrayList<Integer> list = new ArrayList<Integer>(); for (int i = 1; i < n - 1; i++) { if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) { list.add(i); } } int ans = 0; for (int i = 0; i < list.size(); i++) { if (i + 1 < list.size() && list.get(i + 1) == list.get(i) + 2) { arr[list.get(i) + 1] = Math.max(arr[list.get(i)], arr[list.get(i) + 2]); i++; ans++; } else { arr[list.get(i)] = Math.max(arr[list.get(i) - 1], arr[list.get(i) + 1]); ans++; } } pw.println(ans); for (int i = 0; i < n; i++) { pw.print(arr[i] + " "); } pw.println(); } class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(BufferedReader bf) { reader = bf; tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public float nextFloat() { return Float.parseFloat(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String[] nextStringArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) { a[i] = next(); } return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } } private static class Sorter { public static <T extends Comparable<? super T>> void sort(T[] arr) { Arrays.sort(arr); } public static <T> void sort(T[] arr, Comparator<T> c) { Arrays.sort(arr, c); } public static <T> void sort(T[][] arr, Comparator<T[]> c) { Arrays.sort(arr, c); } public static <T extends Comparable<? super T>> void sort(ArrayList<T> arr) { Collections.sort(arr); } public static <T> void sort(ArrayList<T> arr, Comparator<T> c) { Collections.sort(arr, c); } public static void normalSort(int[] arr) { Arrays.sort(arr); } public static void normalSort(long[] arr) { Arrays.sort(arr); } public static void sort(int[] arr) { timSort(arr); } public static void sort(int[] arr, Comparator<Integer> c) { timSort(arr, c); } public static void sort(int[][] arr, Comparator<Integer[]> c) { timSort(arr, c); } public static void sort(long[] arr) { timSort(arr); } public static void sort(long[] arr, Comparator<Long> c) { timSort(arr, c); } public static void sort(long[][] arr, Comparator<Long[]> c) { timSort(arr, c); } private static void timSort(int[] arr) { Integer[] temp = new Integer[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(int[] arr, Comparator<Integer> c) { Integer[] temp = new Integer[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(int[][] arr, Comparator<Integer[]> c) { Integer[][] temp = new Integer[arr.length][arr[0].length]; for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; } private static void timSort(long[] arr) { Long[] temp = new Long[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(long[] arr, Comparator<Long> c) { Long[] temp = new Long[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(long[][] arr, Comparator<Long[]> c) { Long[][] temp = new Long[arr.length][arr[0].length]; for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; } } public long fastPow(long x, long y, long mod) { if (y == 0) return 1; if (y == 1) return x % mod; long temp = fastPow(x, y / 2, mod); long ans = (temp * temp) % mod; return (y % 2 == 1) ? (ans * (x % mod)) % mod : ans; } public long fastPow(long x, long y) { if (y == 0) return 1; if (y == 1) return x; long temp = fastPow(x, y / 2); long ans = (temp * temp); return (y % 2 == 1) ? (ans * x) : ans; } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
5c42a87d9e11c9c999812d2898b2eccf
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.StringTokenizer; import java.util.function.IntFunction; import java.util.stream.Collectors; public class codeforces { //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; public static int cnt; //-----------MyScanner class for faster input---------- 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; } } static boolean isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static long fact(long n) { long fact=1; long i=1; while(i<=n) { fact=fact*i; i++; } return fact; } public class ArrayListComparator<T extends Comparable<T>> implements Comparator<ArrayList<T>> { @Override public int compare(ArrayList<T> o1, ArrayList<T> o2) { for (int i = 0; i < Math.min(o1.size(), o2.size()); i++) { int c = o1.get(i).compareTo(o2.get(i)); if (c != 0) { return c; } } return Integer.compare(o1.size(), o2.size()); } } public static int reverseBits(int n) { int rev = 0; // traversing bits of 'n' // from the right while (n > 0) { // bitwise left shift // 'rev' by 1 rev <<= 1; // if current bit is '1' if ((int)(n & 1) == 1) rev ^= 1; // bitwise right shift //'n' by 1 n >>= 1; } // required number return rev; } public static void main(String[] args) { // TODO Auto-generated method stub MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); StringBuilder sb = new StringBuilder(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long arr[]=new long[n]; long brr[]=new long[n]; long ans1=0,ans2=0; for(int i=0;i<n;i++) { arr[i]=sc.nextLong(); brr[i]=arr[i]; } for(int i=1;i<n-1;i++) { if(arr[i]>arr[i-1] && arr[i]>arr[i+1]) { if(i<n-2) { arr[i+1]=Math.max(arr[i], arr[i+2]); ans1++; } else { long p=Math.max(arr[i],arr[i+1]); arr[i]=p; arr[i+1]=p; ans1++; } } } for(int i=n-2;i>=1;i--) { if(brr[i]>brr[i-1] && brr[i]>brr[i+1]) { if(i>=2) { brr[i-1]=Math.max(brr[i],brr[i-2]); ans2++; } else { long p=Math.max(brr[i-1],brr[i]); brr[i]=p; brr[i-1]=p; ans2++; } } } out.println(Math.min(ans1, ans2)); for(int i=0;i<n;i++) { out.print(arr[i]+" "); } out.println(); } out.close(); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
ae03b6733fe431b4f6b3bd31b136153c
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.security.cert.X509CRL; import java.util.*; import java.lang.*; import java.util.stream.Collector; import java.util.stream.Collectors; @SuppressWarnings("unused") public class Main { static InputStream is; static PrintWriter out; static String INPUT = ""; static String OUTPUT = ""; //global private final static long BASE = 998244353L; private final static int ALPHABET = (int)('z') - (int)('a') + 1; //private final static int BASE = 1000000007; private final static int INF_I = (1<<31)-1; private final static long INF_L = (1l<<63)-1; private final static int MAXN = 200100; private final static int MAXK = 31; private final static int[] DX = {-1,0,1,0}; private final static int[] DY = {0,1,0,-1}; private static long pw(int a,int n) { if (n==0) return 1l; if (n==1) return 1l*a; long tmp = pw(a, n/2); tmp = tmp * tmp %BASE; if (n%2==0) return tmp; return tmp*a%BASE; } static void solve() { int ntest = readInt(); for (int test=0;test<ntest;test++) { int N = readInt(); int[] A = readIntArray(N); List<Integer> localMax = new ArrayList<>(); for (int i=1;i+1<N;i++) if (A[i-1] < A[i] && A[i] > A[i+1]) localMax.add(i); int extra=0; for (int i=0; i<localMax.size();) { int pos = localMax.get(i); extra++; if (i+1<localMax.size() && pos+2 == localMax.get(i+1)) { A[pos+1] = Math.max(A[pos],A[pos+2]); i+=2; } else { A[pos] = Math.max(A[pos-1],A[pos+1]); i++; } } out.println(extra); for (int i=0;i<N;i++) out.print(A[i] + " "); out.println(); } } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); if (INPUT=="") { is = System.in; } else { File file = new File(INPUT); is = new FileInputStream(file); } if (OUTPUT == "") out = new PrintWriter(System.out); else out = new PrintWriter(OUTPUT); solve(); out.flush(); long G = System.currentTimeMillis(); } private static class Point<T extends Number & Comparable<T>> implements Comparable<Point<T>> { private T x; private T y; public Point(T x, T y) { this.x = x; this.y = y; } public T getX() {return x;} public T getY() {return y;} @Override public int compareTo(Point<T> o) { int cmp = x.compareTo(o.getX()); if (cmp==0) return y.compareTo(o.getY()); return cmp; } } private static class ClassComparator<T extends Comparable<T>> implements Comparator<T> { public ClassComparator() {} @Override public int compare(T a, T b) { return a.compareTo(b); } } private static class ListComparator<T extends Comparable<T>> implements Comparator<List<T>> { public ListComparator() {} @Override public int compare(List<T> o1, List<T> o2) { for (int i = 0; i < Math.min(o1.size(), o2.size()); i++) { int c = o1.get(i).compareTo(o2.get(i)); if (c != 0) { return c; } } return Integer.compare(o1.size(), o2.size()); } } private static boolean eof() { if(lenbuf == -1)return true; int lptr = ptrbuf; while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false; try { is.mark(1000); while(true){ int b = is.read(); if(b == -1){ is.reset(); return true; }else if(!isSpaceChar(b)){ is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } // private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); } private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private static double readDouble() { return Double.parseDouble(readString()); } private static char readChar() { return (char)skip(); } private static String readString() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static char[] readChar(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private static char[][] readTable(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = readChar(m); return map; } private static int[] readIntArray(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = readInt(); return a; } private static long[] readLongArray(int n) { long[] a = new long[n]; for (int i=0;i<n;i++) a[i] = readLong(); return a; } private static int readInt() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static long readLong() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
9bd573757a38098ec0d355b6bc9b3b53
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class MaximalAnd { static int[] memo; public static void main(String[] args) throws Exception { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int arr[] = sc.nextIntArray(n); int ans = 0; for (int i = 1; i < n - 1; i++) { boolean f = false; if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) { ans++; f = true; } if (i + 2 < n && f && arr[i + 2] >= arr[i]) arr[i + 1] = arr[i + 2]; else if (f) arr[i + 1] = arr[i]; } pw.println(ans); for (int x : arr) pw.print(x + " "); pw.println(); } pw.flush(); } // isPrime public static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long i = 5; i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) return false; } return true; } // get the nth fibonacci number using BigInteger // public static BigInteger fib(int n) { // BigInteger a = BigInteger.valueOf(0); // BigInteger b = BigInteger.valueOf(1); // for (int i = 0; i < n; i++) { // BigInteger c = a.add(b); // a = b; // b = c; // } // return b; // } // // isPrime using BigInteger // public static boolean isPrime(BigInteger n) { // if (n.compareTo(BigInteger.valueOf(2)) < 0) // return false; // if (n.compareTo(BigInteger.valueOf(2)) == 0) // return true; // if (n.mod(BigInteger.valueOf(2)).compareTo(BigInteger.valueOf(0)) == 0) // return false; // BigInteger d = n.subtract(BigInteger.valueOf(1)); // BigInteger s = d.divide(BigInteger.valueOf(2)); // while (s.mod(BigInteger.valueOf(2)).compareTo(BigInteger.valueOf(0)) == 0) { // s = s.divide(BigInteger.valueOf(2)); // } // for (BigInteger i = BigInteger.valueOf(2); i.compareTo(s) <= 0; i = // i.add(BigInteger.valueOf(1))) { // if (n.mod(i).compareTo(BigInteger.valueOf(0)) == 0) // return false; // } // return true; // } static class fenwickTree { static long[] tree; static int[] arr; static int n; public fenwickTree(int[] arr) { this.arr = arr; n = arr.length; tree = new long[n + 1]; long[] prefixSum = new long[n]; prefixSum[0] = arr[0]; for (int i = 1; i < n; i++) { prefixSum[i] = prefixSum[i - 1] + arr[i]; } for (int i = 1; i <= n; i++) { int lsb = LSB(i); tree[i] = prefixSum[i - 1] - (i == lsb ? 0 : prefixSum[i - 1 - lsb]); } } public static long getSum(int l, int r) { if (l > r) return 0; return getPrefixSum(r) - getPrefixSum(l - 1); } public static long getPrefixSum(int idx) { long ans = 0; while (idx > 0) { ans += tree[idx]; idx -= LSB(idx); } return ans; } public static void updatePoint(int idx, int newVal) { int change = newVal - arr[idx - 1]; arr[idx - 1] = newVal; while (idx <= n) { tree[idx] += change; idx += LSB(idx); } } public static int LSB(int x) { return x & -x; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } 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 long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } static class pair implements Comparable<pair> { long x; long y; public pair(long x, long y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) { return this.z - other.z; } return this.y - other.y; } else { return this.x - other.x; } } } static long mod = 1000000007; static Random rn = new Random(); static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
b678237d817a64855ad1b80c4861073a
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; import java.util.stream.*; public class Sequence { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); int j, max; scan.nextLine(); StringBuilder result = new StringBuilder(); for(int i = 0; i < t; i++) { int n = scan.nextInt(); int[] res = new int[n]; int k = 0; List<Integer> maxInd = new ArrayList<>(); for(j = 0; j < n; j++) { int next = scan.nextInt(); res[j] = next; if(j > 1) { if(res[j-1] > res[j-2] && res[j-1] > res[j]) { maxInd.add(j-1); } } } j = 0; while(j < maxInd.size()) { boolean found = false; if(j < maxInd.size()-1) { if(maxInd.get(j+1) == maxInd.get(j) + 2) { //System.out.println("Case 1 : " + maxInd.get(j) + ", " + (maxInd.get(j) + 2)); max = Math.max(res[maxInd.get(j)], res[maxInd.get(j)+ 2]); res[maxInd.get(j)+1] = max; k++; found = true; j += 2; } } if(!found) { //System.out.println("Case 2 : " + maxInd.get(j)); max = Math.max(res[maxInd.get(j)-1], res[maxInd.get(j)+ 1]); res[maxInd.get(j)] = max; k++; j++; } } result.append(k + "\n"); for(j = 0; j < n; j++) { result.append(res[j] + " "); } result.append("\n"); } System.out.println(result); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
1ef86f5e65a36ced0d6883c0c7ad4b19
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
//package javaapplication1; import java.util.Scanner; public class JavaApplication1 { public static void main(String[] args) { Scanner in=new Scanner(System.in); int Test; Test=in.nextInt(); while(Test-->0){ int Number; Number=in.nextInt(); long [] Array=new long[Number+5]; for(int i=0;i<Number;++i){ Array[i]=in.nextLong(); } int Ans=0; for(int i=1;i<Number-1;i++) { if(Array[i]>Array[i+1]&&Array[i]>Array[i-1]){ Ans++; if(i<Number-2){ if(Array[i]>=Array[i+2])Array[i+1]=Array[i]; else Array[i+1]=Array[i+2]; } else{ Array[i+1]=Array[i]; } } } System.out.println(Ans); for(int i=0;i<Number;i++){ System.out.print(Array[i]); System.out.print(" "); } System.out.print("\n"); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
2450e3376d97dc5bfea78247ffe71409
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.util.*; import java.io.*; import java.math.*; public class B_Avoid_Local_Maximums { public static void main(String[] args) { OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); FastReader f = new FastReader(); int t = f.nextInt(); while(t-- > 0){ int n = f.nextInt(); int arr[] = new int[n]; for(int i = 0; i < n; i++) { arr[i] = f.nextInt(); } int count = 0; for(int i = 1; i < n-1; i++) { if(arr[i] > arr[i-1] && arr[i] > arr[i+1]) { if(i+2 < n) { arr[i+1] = max(arr[i], arr[i+2]); } else { arr[i+1] = arr[i]; } count++; } } out.println(count); for(int i = 0; i < n; i++) { out.print(arr[i] + " "); } out.println(); } out.close(); } public static void allDivisors(int n) { for(int i = 1; i*i <= n; i++) { if(n%i == 0) { System.out.println(i + " "); if(i != n/i) { System.out.println(n/i + " "); } } } } public static boolean isPrime(int n) { if(n < 1) return false; if(n == 2 || n == 3) return true; if(n % 2 == 0 || n % 3 == 0) return false; for(int i = 5; i*i <= n; i += 6) { if(n % i == 0 || n % (i+2) == 0) { return false; } } return true; } public static int Gcd(int a, int b) { int dividend = a > b ? a : b; int divisor = a < b ? a : b; while(divisor > 0) { int reminder = dividend % divisor; dividend = divisor; divisor = reminder; } return dividend; } public static int lcm1(int a, int b) { int lcm = Gcd(a, b); int hcf = (a * b) / lcm; return hcf; } 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()); } float nextFloat() { return Float.parseFloat(next()); } boolean nextBoolean() { return Boolean.parseBoolean(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } /** Dec Char Dec Char Dec Char Dec Char --------- --------- --------- ---------- 0 NUL (null) 32 SPACE 64 @ 96 ` 1 SOH (start of heading) 33 ! 65 A 97 a 2 STX (start of text) 34 " 66 B 98 b 3 ETX (end of text) 35 # 67 C 99 c 4 EOT (end of transmission) 36 $ 68 D 100 d 5 ENQ (enquiry) 37 % 69 E 101 e 6 ACK (acknowledge) 38 & 70 F 102 f 7 BEL (bell) 39 ' 71 G 103 g 8 BS (backspace) 40 ( 72 H 104 h 9 TAB (horizontal tab) 41 ) 73 I 105 i 10 LF (NL line feed, new line) 42 * 74 J 106 j 11 VT (vertical tab) 43 + 75 K 107 k 12 FF (NP form feed, new page) 44 , 76 L 108 l 13 CR (carriage return) 45 - 77 M 109 m 14 SO (shift out) 46 . 78 N 110 n 15 SI (shift in) 47 / 79 O 111 o 16 DLE (data link escape) 48 0 80 P 112 p 17 DC1 (device control 1) 49 1 81 Q 113 q 18 DC2 (device control 2) 50 2 82 R 114 r 19 DC3 (device control 3) 51 3 83 S 115 s 20 DC4 (device control 4) 52 4 84 T 116 t 21 NAK (negative acknowledge) 53 5 85 U 117 u 22 SYN (synchronous idle) 54 6 86 V 118 v 23 ETB (end of trans. block) 55 7 87 W 119 w 24 CAN (cancel) 56 8 88 X 120 x 25 EM (end of medium) 57 9 89 Y 121 y 26 SUB (substitute) 58 : 90 Z 122 z 27 ESC (escape) 59 ; 91 [ 123 { 28 FS (file separator) 60 < 92 \ 124 | 29 GS (group separator) 61 = 93 ] 125 } 30 RS (record separator) 62 > 94 ^ 126 ~ 31 US (unit separator) 63 ? 95 _ 127 DEL */
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
bb6758345fd2fa07eb17f2daa72b83e4
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.util.*; import java.io.*; import java.math.*; public class B_Avoid_Local_Maximums { public static void main(String[] args) { OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); FastReader f = new FastReader(); int t = f.nextInt(); while(t-- > 0){ int n = f.nextInt(); int arr[] = new int[n]; for(int i = 0; i < n; i++) { arr[i] = f.nextInt(); } int count = 0; for(int i = 1; i < n-1; i++) { if(arr[i] > arr[i-1] && arr[i] > arr[i+1]) { if(i+3 < n && arr[i+2] > arr[i+1] && arr[i+2] > arr[i+3]) { arr[i+1] = max(arr[i], arr[i+2]); } else { arr[i] = max(arr[i-1], arr[i+1]); } count++; } } out.println(count); for(int i = 0; i < n; i++) { out.print(arr[i] + " "); } out.println(); } out.close(); } public static void allDivisors(int n) { for(int i = 1; i*i <= n; i++) { if(n%i == 0) { System.out.println(i + " "); if(i != n/i) { System.out.println(n/i + " "); } } } } public static boolean isPrime(int n) { if(n < 1) return false; if(n == 2 || n == 3) return true; if(n % 2 == 0 || n % 3 == 0) return false; for(int i = 5; i*i <= n; i += 6) { if(n % i == 0 || n % (i+2) == 0) { return false; } } return true; } public static int Gcd(int a, int b) { int dividend = a > b ? a : b; int divisor = a < b ? a : b; while(divisor > 0) { int reminder = dividend % divisor; dividend = divisor; divisor = reminder; } return dividend; } public static int lcm1(int a, int b) { int lcm = Gcd(a, b); int hcf = (a * b) / lcm; return hcf; } 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()); } float nextFloat() { return Float.parseFloat(next()); } boolean nextBoolean() { return Boolean.parseBoolean(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } /** Dec Char Dec Char Dec Char Dec Char --------- --------- --------- ---------- 0 NUL (null) 32 SPACE 64 @ 96 ` 1 SOH (start of heading) 33 ! 65 A 97 a 2 STX (start of text) 34 " 66 B 98 b 3 ETX (end of text) 35 # 67 C 99 c 4 EOT (end of transmission) 36 $ 68 D 100 d 5 ENQ (enquiry) 37 % 69 E 101 e 6 ACK (acknowledge) 38 & 70 F 102 f 7 BEL (bell) 39 ' 71 G 103 g 8 BS (backspace) 40 ( 72 H 104 h 9 TAB (horizontal tab) 41 ) 73 I 105 i 10 LF (NL line feed, new line) 42 * 74 J 106 j 11 VT (vertical tab) 43 + 75 K 107 k 12 FF (NP form feed, new page) 44 , 76 L 108 l 13 CR (carriage return) 45 - 77 M 109 m 14 SO (shift out) 46 . 78 N 110 n 15 SI (shift in) 47 / 79 O 111 o 16 DLE (data link escape) 48 0 80 P 112 p 17 DC1 (device control 1) 49 1 81 Q 113 q 18 DC2 (device control 2) 50 2 82 R 114 r 19 DC3 (device control 3) 51 3 83 S 115 s 20 DC4 (device control 4) 52 4 84 T 116 t 21 NAK (negative acknowledge) 53 5 85 U 117 u 22 SYN (synchronous idle) 54 6 86 V 118 v 23 ETB (end of trans. block) 55 7 87 W 119 w 24 CAN (cancel) 56 8 88 X 120 x 25 EM (end of medium) 57 9 89 Y 121 y 26 SUB (substitute) 58 : 90 Z 122 z 27 ESC (escape) 59 ; 91 [ 123 { 28 FS (file separator) 60 < 92 \ 124 | 29 GS (group separator) 61 = 93 ] 125 } 30 RS (record separator) 62 > 94 ^ 126 ~ 31 US (unit separator) 63 ? 95 _ 127 DEL */
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
aebf6889e40241b86d28f9b1c6057687
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; import java.io.*; public class Main { // when can't think of anything -->> // 1. In sorting questions try to think about all possibilities like sorting from start, end, middle. // 2. Two pointers, brute force. // 3. In graph query questions try to solve it reversely or try to process all the queries in a single parse. // 4. If order does not matter then just sort the data if constraints allow. It'll never harm. // 5. In greedy problems if you are just overwhelmed by the possibilities and stuck, try to code whatever you come up with. // 6. Think like a robot, just consider all the possibilities not probabilities if you still can't solve. public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); PrintWriter writer = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int arr[] = new int[n]; int ans = 0; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } for (int i = 1; i < n-1; i++) { if(arr[i] > arr[i-1] && arr[i] > arr[i+1]) { if(i+2 >= n) arr[i+1] = arr[i]; else { arr[i+1] = Math.max(arr[i], arr[i+2]); } ans++; } } writer.println(ans); for (int i : arr) { writer.print(i); writer.print(" "); } writer.println(); } writer.flush(); writer.close(); } private static boolean isPrime(int c) { for (int i = 2; i*i <= c; i++) { if(c%i==0)return false; } return true; } private static int find(int a , int arr[]) { if(arr[a] != a) return arr[a] = find(arr[a], arr); return arr[a]; } private static void union(int a, int b, int arr[]) { int aa = find(a,arr); int bb = find(b, arr); arr[aa] = bb; } private static int gcd(int a, int b) { if(a==0)return b; return gcd(b%a, a); } public static int[] readIntArray(int size, FastReader s) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = s.nextInt(); } return array; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } class Pair implements Comparable<Pair>{ int a; int b; Pair(int a, int b){ this.a = a; this.b = b; } @Override public boolean equals(Object obj) { if(this == obj) return true; if(obj == null || obj.getClass()!= this.getClass()) return false; Pair pair = (Pair) obj; return (pair.a == this.a && pair.b == this.b); } @Override public int hashCode() { return Objects.hash(a,b); } @Override public int compareTo(Pair o) { if(this.a == o.a) { return Integer.compare(this.b, o.b); }else { return Integer.compare(this.a, o.a); } } @Override public String toString() { return this.a + " " + this.b; } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
714c7b2751b4307d55c9eae01e6bcdb6
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main{ static final int MOD = (int) 1e9 + 7; static FastScanner fs = new FastScanner(); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { int t = fs.nextInt(); while(t-- >0) solve(); out.close(); } private static void solve(){ int n = fs.nextInt(); int []a = fs.readArray(n); int ans=0; for(int i=1;i<n-1;i++){ if(a[i]>a[i-1] && a[i]>a[i+1]){ if(i<n-2){ if(a[i+2]>=a[i] || a[i]>=a[i+2]) { a[i + 1] = Math.max(a[i + 2], a[i]); } } else a[i]= Math.max(a[i+1],a[i-1]); ans++; } } out.println(ans); for(int x:a) out.print(x+" "); 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[] 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; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
424159e8db46a4e8e13ccc201d41f296
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.*; import java.util.*; public class Main { static final int INF = Integer.MAX_VALUE; static final int NINF = Integer.MIN_VALUE; static final long LINF = Long.MAX_VALUE; static final long LNINF = Long.MIN_VALUE; static Reader reader; static Writer writer; static PrintWriter out; static FastScanner fs; static void solve() { int n = fs.nextInt(); int[] a = fs.readArrayInt(n); boolean[] lmax = new boolean[n]; for (int i = 1; i < n - 1; i++) { lmax[i] = a[i] > a[i-1] && a[i] > a[i+1]; } int c=0; for (int i = 1; i < n - 1; i++) { if (lmax[i-1] && lmax[i+1]) { a[i] = Math.max(a[i-1], a[i+1]); c++; lmax[i-1] = lmax[i+1] = false; } } for (int i = 0; i <= n - 1; i++) { if (i > 0 && lmax[i-1]) { a[i] = a[i-1]; c++; lmax[i-1] = false; } if (i < n-1 && lmax[i+1]) { a[i] =a[i+1]; c++; lmax[i+1] = false; } } out.println(c); for (int i : a) out.print(i + " "); out.print("\n"); } public static void main(String[] args) { setReaderWriter(); fs = new FastScanner(reader); testForTestcases(); // solve(); out.close(); } static void setReaderWriter() { reader = new InputStreamReader(System.in); writer = new OutputStreamWriter(System.out); out=new PrintWriter(writer); } static void testForTestcases() { int T = fs.nextInt(); while (T-- > 0) { solve(); } } static boolean isInteger(double val) { return !(val - (long)val > 0); } static void swap(int[] a , int i , int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } static int opposite(int n, int x) { return (n-1)^x; } static Pair print(int a, int b) { out.println(a+" "+b); return new Pair(a, b); } static class Pair { int a, b; public Pair(int a, int b) { this.a=a; this.b=b; } } static final Random random=new Random(); static final int mod=1_000_000_007; 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 long add(long a, long b) { return (a+b)%mod; } static long sub(long a, long b) { return ((a-b)%mod+mod)%mod; } static long mul(long a, long b) { return (a*b)%mod; } static long exp(long base, long exp) { if (exp==0) return 1; long half=exp(base, exp/2); if (exp%2==0) return mul(half, half); return mul(half, mul(half, base)); } static long[] factorials=new long[2_000_001]; static long[] invFactorials=new long[2_000_001]; static void precompFacts() { factorials[0]=invFactorials[0]=1; for (int i=1; i<factorials.length; i++) factorials[i]=mul(factorials[i-1], i); invFactorials[factorials.length-1]=exp(factorials[factorials.length-1], mod-2); for (int i=invFactorials.length-2; i>=0; i--) invFactorials[i]=mul(invFactorials[i+1], i+1); } static long nCk(int n, int k) { return mul(factorials[n], mul(invFactorials[k], invFactorials[n-k])); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader reader) { br =new BufferedReader(reader); st =new StringTokenizer(""); } String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int nextInt() { return Integer.parseInt(next()); } int[] readArrayInt(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } char[] readCharArray() { return next().toCharArray(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
c724350b48c53d3437822ca2f786efc7
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; //import java.lang.*; import java.io.*; public class Main { public static void main (String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(),n,i=0,j,ans; int[] a; while(t-- >0){ n=sc.nextInt(); a=new int[n]; for(i=0;i<n;i++) a[i]=sc.nextInt(); ans=0; for(i=1;i<n-1;i++){ if(a[i-1]<a[i] && a[i+1]<a[i]){ ans++; if(i+2<n && a[i+2]>=a[i]){ a[i+1]=a[i+2]; } else if(i+2<n && a[i+2]<a[i]) a[i+1]=a[i]; else a[i]=Math.max(a[i+1],a[i-1]); } } System.out.println(ans); for(int x:a) System.out.print(x+" "); System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
c4835cd612afc8aec05eb8473d6ae955
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main (String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(),n,i=0,j,ans; int[] a; while(t-- >0){ n=sc.nextInt(); a=new int[n]; for(i=0;i<n;i++) a[i]=sc.nextInt(); ans=0; for(i=1;i<n-1;i++){ if(a[i-1]<a[i] && a[i+1]<a[i]){ ans++; if(i+2<n && a[i+2]>=a[i]){ a[i+1]=a[i+2]; } else if(i+2<n && a[i+2]<a[i]) a[i+1]=a[i]; else a[i]=Math.max(a[i+1],a[i-1]); } } System.out.println(ans); for(int x:a) System.out.print(x+" "); System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
dc4bf970ee88fddb4b0b77276198557f
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; import java.io.*; public class HelloWorld{ public static void main(String []args){ Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int temp=0; int n=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } int count=0; for(int i=1;i<n-1;i++) { if(a[i]>a[i-1] && a[i]>a[i+1]) { if(i==n-2) { count++; a[i+1]=a[i]; } else { count++; if(a[i+2]>=a[i]) a[i+1]=a[i+2]; else a[i+1]=a[i]; } } } System.out.println(count); for(int i=0;i<n;i++) { System.out.print(a[i]+" "); } System.out.println(); } }}
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
af25c12c3416c1aefde2d0f9ea307335
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.Arrays; import java.util.Scanner; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int tc = 0; tc < t; ++tc) { int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < a.length; ++i) { a[i] = sc.nextInt(); } System.out.println(solve(a)); } sc.close(); } static String solve(int[] a) { int operationCount = 0; for (int i = 1; i < a.length - 1; ++i) { if (a[i] > a[i - 1] && a[i] > a[i + 1]) { if (i + 1 == a.length - 1) { a[i + 1] = a[i]; } else { a[i + 1] = Math.max(a[i], a[i + 2]); } ++operationCount; } } return String.format( "%d\n%s", operationCount, Arrays.stream(a).mapToObj(String::valueOf).collect(Collectors.joining(" "))); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
2c809b2f9f5791d272cfe0e76caaf974
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { PrintWriter out = new PrintWriter(System.out); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(""); String next() throws IOException { if (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int ni() throws IOException { return Integer.parseInt(next()); } long nl() throws IOException { return Long.parseLong(next()); } int[] na(int n) throws IOException { int[]A=new int[n]; for (int i=0;i<n;i++) A[i]=ni(); return A; } long mod=1000000007; void solve() throws IOException { for (int tc=ni();tc>0;tc--) { int n=ni(); int[]A=na(n); int ans=0; int prev=-10; for (int i=1;i<n-1;i++) { if (A[i]>A[i-1] && A[i]>A[i+1]) { if (prev+2==i) { A[i-1]=A[i]; prev=-10; } else { A[i+1]=A[i]; prev=i; ans++; } } } out.println(ans); for (int v:A) out.print(v+" "); out.println(); } out.flush(); } int gcd(int a,int b) { return(b==0?a:gcd(b,a%b)); } long gcd(long a,long b) { return(b==0?a:gcd(b,a%b)); } long mp(long a,long p) { long r=1; while(p>0) { if ((p&1)==1) r=(r*a)%mod; p>>=1; a=(a*a)%mod; } return r; } public static void main(String[] args) throws IOException { new Main().solve(); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
e35e828fabdc8bb734c647d09d9ea65e
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.*; import java.util.*; public class AvoidingLocalMaximums { //io static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(System.out); static boolean debug = false; public static void main(String[] args) throws IOException { int T = Integer.parseInt(br.readLine()); for (int i=0;i<T;i++) solve(); out.close(); } public static void solve() throws IOException { int N = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); int[] arr = new int[N]; for (int i=0;i<N;i++){ arr[i] = Integer.parseInt(st.nextToken()); } ArrayList<Integer> locMax = new ArrayList<>(); for (int i=1;i<arr.length-1;i++){ if (arr[i]>arr[i-1]&&arr[i]>arr[i+1]) locMax.add(i); } if (debug) System.out.println(locMax); int ops = 0; for (int i=0;i<locMax.size();i++){ if (i<locMax.size()-1&&locMax.get(i+1)-locMax.get(i)==2){ arr[locMax.get(i)+1]=Math.max(arr[locMax.get(i)],arr[locMax.get(i+1)]); i++; ops++; } else { arr[locMax.get(i)]=Math.max(arr[locMax.get(i)-1],arr[locMax.get(i)+1]); ops++; } } out.println(ops); for (int i=0;i<arr.length;i++) out.print(arr[i]+" "); out.println(); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
e82ed7ce16e5aa629a4d90b3c820c242
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.Scanner; public class MainClass { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int T = scanner.nextInt(); while (T > 0) { T--; int n = scanner.nextInt(); int ans = 0; int[] a = new int[n + 10]; for (int i = 1; i <= n; i++) { a[i] = scanner.nextInt(); } a[n + 1] = 0; for (int i = 2; i < n; i++) { if (a[i] > a[i - 1] && a[i] > a[i + 1]) { ans++; a[i + 1] = Math.max(a[i], a[i + 2]); } } System.out.println(ans); for (int i = 1; i <= n; i++) { System.out.print(a[i] + " "); } System.out.println(""); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
f822c3c42a7b1fd1ca5888ed36660db0
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
// import java.lang.reflect.Array; // import java.math.BigInteger; // import java.nio.channels.AcceptPendingException; // import java.nio.charset.IllegalCharsetNameException; // import java.util.Collections; // import java.util.logging.SimpleFormatter; // import java.util.regex.Matcher; // import java.util.regex.Pattern; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.*; /* docstring*/ public class Codeforces { static Templates.FastScanner sc = new Templates.FastScanner(); static PrintWriter fop = new PrintWriter(System.out); public static void main(String[] args) { try { // A(); B(); // C(); // D(); // E(); } catch (Exception e) { System.out.println("error "+e); return; } } /* docstring */ static void A() throws IOException { int T = Integer.parseInt(sc.next()); while (T-- > 0) { int n = Integer.parseInt(sc.next()); int ans = 0; for (int i = 0; i < n; i++) { int num1 = Integer.parseInt(sc.next()); ans = ans | num1; } System.out.println(ans); } fop.flush(); fop.close(); } /* docstring */ static void B() throws IOException { int T = Integer.parseInt(sc.next()); while (T-- > 0) { int n = Integer.parseInt(sc.next()); int arr[] = sc.readArray(n); int ans = 0; for (int i = 1; i < n-1; i++) { if( arr[i] > arr[i-1] && arr[i] > arr[i+1]) { if (i + 2 < n){ arr[i+1] = Math.max(arr[i],arr[i+2]); } else{ arr[i+1] = arr[i]; } ans++; } } System.out.println(ans); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(""); } fop.flush(); fop.close(); } /* docstring */ static void C() throws IOException { int T = Integer.parseInt(sc.next()); while (T-- > 0) { // Write Your Code... } fop.flush(); fop.close(); } /* docstring */ static void D() throws IOException { int T = Integer.parseInt(sc.next()); while (T-- > 0) { // Write Your Code... } fop.flush(); fop.close(); } /* docstring */ static void E() throws IOException { int T = Integer.parseInt(sc.next()); while (T-- > 0) { // Write Your Code... } fop.flush(); fop.close(); } } class Templates { // int tree[] = new int[4*n+1] ; // BuiltTree(A , 0 , n-1 , tree , 1); // int lazy[] = new int[4*n + 1] ; // // updateRangeLazy(tree , lazy , 0 , n-1 , 0 , 2 , 10 , 1); // updateRangeLazy(tree , lazy , 0 , n-1 , 0 , 4 , 10 , 1); // // fop.println(querylazy(tree , lazy , 0 , n-1 , 1 ,1 ,1)); // // updateRangeLazy(tree, lazy , 0 , n-1 , 10 , 4 , 3 ,1); // fop.println(querylazy(tree , lazy , 0 , n-1 , 3 , 5 , 1 )); 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()); } } // segment tree // BuiltTree(A , 0 , n-1 , tree , 1); static void BuiltTree(int A[], int s, int e, int tree[], int index) { if (s == e) { tree[index] = A[s]; return; } int mid = (s + e) / 2; BuiltTree(A, s, mid, tree, 2 * index); BuiltTree(A, mid + 1, e, tree, 2 * index + 1); tree[index] = Math.min(tree[2 * index], tree[2 * index + 1]); return; } static int query(int tree[], int ss, int se, int qs, int qe, int index) { // complete overlap if (ss >= qs && se <= qe) return tree[index]; // no overlap if (qe < ss || qs > se) return Integer.MAX_VALUE; // partial overlap int mid = (ss + se) / 2; int left = query(tree, ss, mid, qs, qe, 2 * index); int right = query(tree, mid + 1, se, qs, qe, 2 * index + 1); return Math.min(left, right); } static void update(int tree[], int ss, int se, int i, int increment, int index) { // i is the index of which we want to update the value if (i > se || i < ss) return; if (ss == se) { tree[index] += increment; return; } int mid = (ss + se) / 2; update(tree, ss, mid, i, increment, 2 * index); update(tree, mid + 1, se, i, increment, 2 * index + 1); tree[index] = Math.min(tree[2 * index], tree[2 * index + 1]); } static void updateRange(int tree[], int ss, int se, int l, int r, int inc, int index) { if (l > se || r < ss) return; if (ss == se) { tree[index] += inc; return; } int mid = (ss + se) / 2; updateRange(tree, ss, mid, l, r, inc, 2 * index); updateRange(tree, mid + 1, se, l, r, inc, 2 * index + 1); tree[index] = Math.min(tree[2 * index], tree[2 * index + 1]); return; } // Lazy rnage Update static void updateRangeLazy(int tree[], int lazy[], int ss, int se, int l, int r, int increment, int index) { if (lazy[index] != 0) { tree[index] += lazy[index]; if (ss != se) { lazy[2 * index] += lazy[index]; lazy[2 * index + 1] += lazy[index]; } lazy[index] = 0; } if (ss > r && se < l) return; if (ss >= l && se <= r) { tree[index] += increment; if (ss != se) { lazy[2 * index] += increment; lazy[2 * index + 1] += increment; } return; } int mid = (ss + se) / 2; updateRange(tree, ss, mid, l, r, increment, 2 * index); updateRange(tree, mid + 1, se, l, r, increment, 2 * index + 1); tree[index] = Math.min(tree[2 * index], tree[2 * index + 1]); } // min lazy query static int querylazy(int tree[], int lazy[], int ss, int se, int qs, int qe, int index) { if (lazy[index] != 0) { tree[index] += lazy[index]; if (ss != se) { lazy[2 * index] += lazy[index]; lazy[2 * index + 1] += lazy[index]; } lazy[index] = 0; } if (ss > qe || se < qs) return Integer.MAX_VALUE; if (ss >= qs && se <= qe) return tree[index]; int mid = (ss + se) / 2; int left = querylazy(tree, lazy, ss, mid, qs, qe, 2 * index); int right = querylazy(tree, lazy, mid + 1, se, qs, qe, 2 * index + 1); return Math.min(left, right); } static void sieve(int n) { boolean[] flag = new boolean[n]; for (int i = 2; i * i < n; i++) { if (flag[i]) continue; else for (int j = i * i; j <= n; j += i) { flag[j] = true; } } } static int gcd(int a, int b) { if (b > a) { int tenp = b; b = a; a = tenp; } int temp = 0; while (b != 0) { a %= b; temp = b; b = a; a = temp; } return a; } static long gcdl(long a, long b) { if (b > a) { long tenp = b; b = a; a = tenp; } long temp = 0; while (b != 0) { a %= b; temp = b; b = a; a = temp; } return a; } 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); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
57586e3170a7e4a80d3e56f3ef0f54b7
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class A { static QuickReader fs = new QuickReader(); static PrintWriter out = new PrintWriter(System.out); static void output() { int n = fs.nextInt(); int[] a = fs.readIntArray(n); int c = 0; for(int i=1;i<n-1;i++) if(a[i] > a[i+1] && a[i] > a[i-1]){ int max = a[i]; if(i+2 < n)max = Math.max(max,a[i+2]); a[i+1] =max;//Max a[i] - a[i+2] c++; } out.println(c); printArray(a); } public static void main(String[] args) { int T = 1; T = fs.nextInt(); for (int tt = 0; tt < T; tt++) { output(); out.flush(); } } static void printArray(int[] a) { for (int i : a) out.print(i + " "); out.println(); } static void printlnArray(int[] a) { for (int i : a) out.println(i); } static void printMatrix(int[][] a) { for (int[] i : a) printArray(i); } static void sortA(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sortD(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l, (i, j) -> j - i); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class QuickReader { 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[] readIntArray(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()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
596c3b547d7cc0896598ca1aa80e39aa
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class B { static QuickReader fs = new QuickReader(); static PrintWriter out = new PrintWriter(System.out); static void output() { int n = fs.nextInt(); int[] a = fs.readIntArray(n); int c = 0; for(int i=1;i<n-1;i++) if(a[i] > a[i-1] && a[i] > a[i+1]){ int max =a[i]; if(i+2 < n)max = Math.max(max,a[i+2]); a[i+1] = max; c++; } out.println(c); printArray(a); } public static void main(String[] args) { int T = 1; T = fs.nextInt(); for (int tt = 0; tt < T; tt++) { output(); out.flush(); } } static void printArray(int[] a) { for (int i : a) out.print(i + " "); out.println(); } static void printlnArray(int[] a) { for (int i : a) out.println(i); } static void printMatrix(int[][] a) { for (int[] i : a) printArray(i); } static void sortA(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sortD(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l, (i, j) -> j - i); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class QuickReader { 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[] readIntArray(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()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
5d1a1d42990c2d3463ad8e7604c37317
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; import java.io.*; public class Practice { static boolean multipleTC = true; final static int mod = 1000000007; final static int mod2 = 998244353; final double E = 2.7182818284590452354; final double PI = 3.14159265358979323846; int MAX = 10000005; void pre() throws Exception { } // All the best void solve(int TC) throws Exception { int n = ni(), arr[] = readArr(n); int count = 0; ArrayList<Integer> indices = new ArrayList<>(); TreeSet<Integer> set = new TreeSet<>(); for (int i = 1; i + 1 < n; i++) { if (arr[i - 1] < arr[i] && arr[i] > arr[i + 1]) { indices.add(i); set.add(i); } } int sz = indices.size(); for (int i = 0; i + 1 < sz; i++) { if (indices.get(i) + 2 == indices.get(i + 1)) { arr[indices.get(i) + 1] = max(arr[indices.get(i)], arr[indices.get(i + 1)]); count++; i++; } } for (int i = 1; i + 1 < n; i++) { if (arr[i - 1] < arr[i] && arr[i] > arr[i + 1]) { arr[i + 1] = arr[i]; count++; } } pn(count); StringBuilder ans = new StringBuilder(); for (int i = 0; i < n; i++) { ans.append(arr[i] + " "); } pn(ans); } int[] readArr(int n) throws Exception { int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ni(); } return arr; } void sort(int arr[], int left, int right) { ArrayList<Integer> list = new ArrayList<>(); for (int i = left; i <= right; i++) list.add(arr[i]); Collections.sort(list); for (int i = left; i <= right; i++) arr[i] = list.get(i - left); } void 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); } public long max(long... arr) { long max = arr[0]; for (long itr : arr) max = Math.max(max, itr); return max; } public int max(int... arr) { int max = arr[0]; for (int itr : arr) max = Math.max(max, itr); return max; } public long min(long... arr) { long min = arr[0]; for (long itr : arr) min = Math.min(min, itr); return min; } public int min(int... arr) { int min = arr[0]; for (int itr : arr) min = Math.min(min, itr); return min; } public long sum(long... arr) { long sum = 0; for (long itr : arr) sum += itr; return sum; } public long sum(int... arr) { long sum = 0; for (int itr : arr) sum += itr; return sum; } String bin(long n) { return Long.toBinaryString(n); } String bin(int n) { return Integer.toBinaryString(n); } static int bitCount(int x) { return x == 0 ? 0 : (1 + bitCount(x & (x - 1))); } static void dbg(Object... o) { System.err.println(Arrays.deepToString(o)); } int bit(long n) { return (n == 0) ? 0 : (1 + bit(n & (n - 1))); } int abs(int a) { return (a < 0) ? -a : a; } long abs(long a) { return (a < 0) ? -a : a; } void p(Object o) { out.print(o); } void pn(Object o) { out.println(o); } void pni(Object o) { out.println(o); out.flush(); } 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()); } public static void main(String[] args) throws Exception { new Practice().run(); } FastReader in; PrintWriter out; void run() throws Exception { in = new FastReader(); out = new PrintWriter(System.out); int T = (multipleTC) ? ni() : 1; pre(); for (int t = 1; t <= T; t++) solve(t); out.flush(); out.close(); } 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; } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
8c6dac471eeee002e55f1fe0db97f402
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
// package div_2_772; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class B{ public static void main(String[] args){ FastReader sc = new FastReader(); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int a[]=sc.fastArray(n); int cnt=0; int pre=-1; for(int i=1;i<n-1;i++) { if(a[i]>a[i-1] && a[i]>a[i+1]) { if(pre==(i-1)) { a[i-1]=a[i]; } else if(a[i]>a[i+1]) { pre=i+1; a[i+1]=a[i]; cnt++; } } } System.out.println(cnt); print(a); } } static int pow(int a,int b) { if(b==0)return 1; if(b==1)return a; return a*pow(a,b-1); } static class pair { int x;int y; pair(int x,int y){ this.x=x; this.y=y; } } static ArrayList<Integer> primeFac(int n){ ArrayList<Integer>ans = new ArrayList<Integer>(); int lp[]=new int [n+1]; Arrays.fill(lp, 0); //0-prime for(int i=2;i<=n;i++) { if(lp[i]==0) { for(int j=i;j<=n;j+=i) { if(lp[j]==0) lp[j]=i; } } } int fac=n; while(fac>1) { ans.add(lp[fac]); fac=fac/lp[fac]; } print(ans); return ans; } static ArrayList<Long> prime_in_given_range(long l,long r){ ArrayList<Long> ans= new ArrayList<>(); int n=(int)Math.sqrt(r)+1; int prime[]=sieve_of_Eratosthenes(n); long res[]=new long [(int)(r-l)+1]; for(int i=0;i<=r-l;i++) { res[i]=i+l; } for(int i=0;i<prime.length;i++) { if(prime[i]==1) { System.out.println(2); for(int j=Math.max((int)i*i, (int)(l+i-1)/i*i);j<=r;j+=i) { res[j-(int)l]=0; } } } for(long i:res) if(i!=0)ans.add(i); return ans; } static int [] sieve_of_Eratosthenes(int n) { int prime[]=new int [n]; Arrays.fill(prime, 1); // 1-prime | 0-not prime prime[0]=prime[1]=0; for(int i=2;i<n;i++) { if(prime[i]==1) { for(int j=i*i;j<n;j+=i) { prime[j]=0; } } } return prime; } static long binpow(long a,long b) { long res=1; if(b==0)return 1; if(a==0)return 0; while(b>0) { if((b&1)==1) { res*=a; } a*=a; b>>=1; } return res; } static void print(int a[]) { // System.out.println(a.length); for(int i:a) { System.out.print(i+" "); } System.out.println(); } static void print(long a[]) { System.out.println(a.length); for(long i:a) { System.out.print(i+" "); } System.out.println(); } static long rolling_hashcode(String s ,int st,int end,long Hashcode,int n) { if(end>=s.length()) return -1; int mod=1000000007; Hashcode=Hashcode-(s.charAt(st-1)*(long)Math.pow(27,n-1)); Hashcode*=10; Hashcode=(Hashcode+(long)s.charAt(end))%mod; return Hashcode; } static long hashcode(String s,int n) { long code=0; for(int i=0;i<n;i++) { code+=((long)s.charAt(i)*(long)Math.pow(27, n-i-1)%1000000007); } return code; } static void print(ArrayList<Integer> a) { System.out.println(a.size()); for(long i:a) { System.out.print(i+" "); } System.out.println(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } int [] fastArray(int n) { int a[]=new int [n]; for(int i=0;i<n;i++) { a[i]=nextInt(); } return a; } 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\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
2274e8ea9a986d71ac7b593b0acebb69
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; import java.io.*; public class _772 { public static void main(String[] args) { MyScanner sc = new MyScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); while (t-- >0) { int n = sc.nextInt(); int [] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } ArrayList<Integer> b = new ArrayList<>(); for (int i = 1; i < n - 1; i++) { if (a[i] > a[i - 1] && a[i] > a[i + 1]) { b.add(i); } } if (b.size() == 0) { out.println(0); for (int i = 0; i < n; ++i) out.print(a[i] + " "); out.println(); continue; } else if (b.size() == 1) { out.println(1); a[b.get(0) + 1] = a[b.get(0)]; for (int i = 0; i < n; ++i) out.print(a[i] + " "); out.println(); continue; } int run = 1; int prev = b.get(0); int res = 0; for (int i = 1; i < b.size(); i++) { if (b.get(i) == prev + 2) { run++; } else { int x = (run + 1) / 2; for (int j = prev - 1; j >= prev - (4 * x - 3); j -= 4) { int max = 0; if (j + 1 < n) max = Math.max(max, a[j + 1]); if (j - 1 >= 0) max = Math.max(max, a[j - 1]); a[j] = max; ++res; } run = 1; } prev = b.get(i); } int x = (run + 1) / 2; for (int j = prev - 1; j >= prev - (4 * x - 3); j -= 4) { int max = 0; if (j + 1 < n) max = Math.max(max, a[j + 1]); if (j - 1 >= 0) max = Math.max(max, a[j - 1]); a[j] = max; ++res; } run = 1; out.println(res); for (int i = 0; i < n; i++) out.print(a[i] + " "); out.println(); } out.close(); } static void sort(int[] a) { ArrayList<Integer> q = new ArrayList<>(); for (int i : a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } static void sort(long[] a) { ArrayList<Long> q = new ArrayList<>(); for (long i : a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } //-----------MyScanner class for faster input---------- 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\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
47923dea5f1fff8ebc9cbc2c7a1e6c43
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.Scanner; public class B { public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for (int i = 1; i <= T; i++) { int n = in.nextInt(); long[] arr = new long[n]; for (int j = 0; j < n; j++) { long v = in.nextInt(); arr[j] = v; } int c = 0; for (int j = 1; j < n-2; j++) { if(arr[j] > arr[j-1] && arr[j] > arr[j+1]) { c++; arr[j+1] = Math.max(arr[j], arr[j+2]); } } if(n > 2) { if (arr[n - 2] > arr[n - 1] && arr[n - 2] > arr[n - 3]){ arr[n-2]= Math.max(arr[n-3],arr[n-1]); c++; } } System.out.println(c); for (int j = 0; j < n; j++) { System.out.print(arr[j] + " "); } System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
06771a369aa086c5ccb480b8b0b3dc6c
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; public class CF { public static void main(String[] args) { // write your code here 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 temp=0; for(int j=0;j<n;j++) { a[j]=sc.nextInt(); } for(int j=1;j<n-1;j++) { if(a[j]>a[j+1] && a[j]>a[j-1]) { if(j+2<n && a[j+2]>a[j]) { a[j+1]=a[j+2]; } else if(j+1<n) { a[j+1]=a[j]; } temp++; } } System.out.println(temp); for(int j=0;j<n;j++) { System.out.print(a[j] + " "); } System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
45dd631b88aa7b2d76f92d5c97d7690b
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class TaskB { static void solve(FastScanner fs) { int n = fs.nextInt(); int a[] = takeArrayInput(fs, n); int res = 0; for(int i = 1; i < n-1; i++) { if(a[i-1] < a[i] && a[i+1] < a[i]) { if(i+2 < n) { a[i+1] = Math.max(a[i], a[i+2]); } else { a[i+1] = a[i]; } res++; } } System.out.println(res); for (int i = 0; i < n; i++) { System.out.print(a[i] + " "); } System.out.print("\n"); } static void print(String s) { System.out.print(s); } static boolean isPowerOfTwo(int x) { return x != 0 && ((x & (x - 1)) == 0); } static int countBits(int number) { return (int) (Math.log(number) / Math.log(2) + 1); } static int countOdd(int L, int R) { int N = (R - L) / 2; if (R % 2 != 0 || L % 2 != 0) N++; return N; } static int[] takeArrayInput(FastScanner fs, int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = fs.nextInt(); } return a; } static void printArrayWithSpace(int a[]) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.print("\n"); } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } public static void main(String[] args) { FastScanner fs = new FastScanner(); int c = fs.nextInt(); while (c-- > 0) { solve(fs); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
292880d692babf07a646e3b1d4017903
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class b1635 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Scanner sc = new Scanner(System.in); int t = sc.nextInt();//Integer.parseInt(br.readLine()); l: while(t-- > 0) { int n = sc.nextInt();//Integer.parseInt(br.readLine()); // String[] sa = br.readLine().split(" "); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt();//Integer.parseInt(sa[i]); } int ans = 0; for (int i = 0; i < n; i++) { if (i == 0 || i == n-1) continue; if (a[i-1] < a[i] && a[i+1] < a[i]) { ans++; int j = i+2; if (j < n-1 && a[j-1] < a[j] && a[j+1]< a[j]) { a[i+1] = Math.max(a[i], a[j]); } else { a[i] = Math.max(a[i-1], a[i+1]); } } } System.out.println(ans); for (int i = 0; i < n; i++) { System.out.print(a[i]+" "); } System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
a07dea61f42b39b62344f18eaa6910aa
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; import java.io.*; public class Demo{ public static void main(String args[]) throws Exception{ BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer sc = new StringTokenizer(bf.readLine()); int t = Integer.parseInt(sc.nextToken()); while(t-->0){ sc = new StringTokenizer(bf.readLine()); int n = Integer.parseInt(sc.nextToken()); sc = new StringTokenizer(bf.readLine()); int[] a = new int[n]; for(int i=0;i<n;i++){ a[i] = Integer.parseInt(sc.nextToken()); } solve(a, a.length); } } public static void solve(int[] a, int n){ int[] maxi = new int[n]; for(int i=1;i<n-1;i++){ if(a[i]>a[i-1] && a[i]>a[i+1]){ maxi[i]=1; } } int ans=0; for(int i=1;i<n-2;i++){ if(maxi[i]==1 && maxi[i+2]==1){ //lift the valley a[i+1]=Math.max(a[i], a[i+2]); ans++; maxi[i+2]=0; }else if(maxi[i]==1){ ans++; a[i]=Math.max(a[i-1],a[i+1]); } } if(maxi[n-2]==1){ a[n-1] = a[n-2]; ans++; } System.out.println(ans); for(int i:a) System.out.print(i+" "); System.out.println(); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
5d0ffbb877adb6e4add77ef207bbd58e
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.*; import java.util.*; public class Main { static int MAX_SIZE = (int) 1e6; static boolean []prime = new boolean[MAX_SIZE + 1]; static FastReader scn = new FastReader(); static PrintWriter out= new PrintWriter(System.out); static class Pair{ int x; int y; Pair(int a, int b){ this.x = a; this.y = b; } } public static void main(String[] args) throws IOException { int t = scn.nextInt(); int caseNum = 1; while (t-- > 0){ int n = scn.nextInt(); int[] arr = new int[n]; int ans = 0; for(int i = 0; i<n; i++){ arr[i] = scn.nextInt(); } for(int i = 1; i<n-1; i++){ boolean flag = false; if(maxima(i, arr)){ if(i < n-3){ if(maxima(i+2, arr)){ arr[i+1] = Math.max(arr[i], arr[i+2]); ans++; flag = true; } } if(!flag){ arr[i+1] = arr[i]; ans++; } } } out.println(ans); print(arr); } out.close(); } static boolean maxima(int i, int[] arr){ return (arr[i-1] < arr[i] && arr[i] > arr[i+1]); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class Sortby implements Comparator<Pair> { public int compare(Pair a, Pair b) { return a.x - b.x; } } static boolean isPalindrome(String s){ int n = s.length(); for(int i = 0; i<n/2; i++){ if (s.charAt(i) != s.charAt(n-i-1)){ return false; } } return true; } static int getMex(int[] arr){ int mex = 0; HashMap<Integer, Boolean> map = new HashMap<>(); for (int j : arr) { map.put(j, true); } while(map.containsKey(mex)){ mex++; } return mex; } static void sieve() { Arrays.fill(prime, true); for (int p = 2; p * p <= MAX_SIZE; p++) { if (prime[p]) { for (int i = p * p;i <= MAX_SIZE; i += p){ prime[i] = false; } } } } static int kthPrimeGreaterThanN(int n) { int res = -1; for (int i = n; i < MAX_SIZE; i++) { if (prime[i]){ res = i; break; } } return res; } static int knapSack(int W, int wt[], int val[], int n) { int i, w; int K[][] = new int[n + 1][W + 1]; // Build table K[][] in bottom up manner for (i = 0; i <= n; i++) { for (w = 0; w <= W; w++) { if (i == 0 || w == 0) K[i][w] = 0; else if (wt[i - 1] <= w) K[i][w] = Math.max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]); else K[i][w] = K[i - 1][w]; } } return K[n][W]; } static int moves(int num){ int cur = num; int ans = 0; int val = 0; while (cur != 0){ val = (int)(Math.log(cur)/Math.log(2)); ans += val; cur -= (int)Math.pow(2, val); } if(val == 0 && num != 1){ ans++; } return ans; } static boolean isEven(long a){ return (a & 1) == 0; } static long pow2(long a){ return 1L <<a; } static boolean isPowerof2(long x){ return x!=0 && ((x&(x-1)) == 0); } static long highestSetBit(long n){ long k = 0; while(1L << (k+1) <= n-1){ k++; } return k; } static void accessSubsets(int[] arr, int index){ int n = arr.length; for(int i = index; i<n; i++){ accessSubsets(arr, i+1); } } public int arrayGCD(int[] nums) { int n = nums.length; Arrays.sort(nums); return gcd(nums[0], nums[n-1]); } public int subsetGCD(int[] nums, int s, int e) { int minIndex = 0; int maxIndex = 0; for(int i = s; i<e; i++){ if(nums[i] > nums[maxIndex]){ maxIndex = i; } else if(nums[i] < nums[minIndex]){ minIndex = i; } } return gcd(nums[maxIndex], nums[minIndex]); } public int gcd(int a, int b){ if(b == 0){ return a; } return gcd(b,a%b); } public static int maxArr(int[] arr){ int val = Integer.MIN_VALUE; int n = arr.length; for(int i = 0; i<n; i++){ if(arr[i] > val){ val = arr[i]; } } return val; } public static void reverseArr(int[] arr){ int n = arr.length; int temp; for(int i = 0; i<n/2; i++){ temp = arr[i]; arr[i] = arr[n-i-1]; arr[n-i-1] = temp; } } static void print(int[] arr){ for(int val : arr){ out.print(val + " "); } out.println(); } static void print(ArrayList<?> list){ for(Object val : list){ out.print(val + " "); } out.println(); } static void readArray(int[] arr){ int n = arr.length; for(int i = 0; i<n; i++){ arr[i] = scn.nextInt(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
aec5ead4d2c6b2315973787f34227e85
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.Scanner; public class B { public static void main(String[] args) { Scanner go =new Scanner(System.in); int t = go.nextInt(); while (t-->0){ int n = go.nextInt(); int[] a = new int[n]; int count = 0; int ch = 0; a[0] = go.nextInt(); int now = a[0]; for (int i =1; i<n; i++){ a[i] = go.nextInt(); } for (int i = 1; i<n-1; i++){ if (a[i] > a[i+1] && a[i] > a[i-1]) { if (i + 2 < n){ if (a[i] > a[i+2] && a[i] > a[i-1]){ a[i+1] = a[i]; }else { a[i+1] = a[i+2]; } }else { a[i+1] = a[i]; } count++; } } System.out.println(count); for (int i =0; i<n; i++){ System.out.print(a[i] + " "); } System.out.println(); } } } /* 1 2 3 4 */
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
c49635492eaf1d81afe2b51742373ae7
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; import java.io.*; /** * Problem: CodeForces 1635A * * @author atulanand */ public class Solution { public static String solve(int[] a) { int changes = 0; int[] x = new int[a.length]; for (int i = 1; i + 1 < a.length; i++) { if (a[i - 1] < a[i] && a[i + 1] < a[i]) { x[i] = 1; } } for (int i = 1; i + 1 < a.length; i++) { if (x[i - 1] == 1 && x[i] == 0 && x[i + 1] == 1) { a[i] = Math.max(a[i - 1], a[i + 1]); x[i + 1] = 0; x[i - 1] = 0; changes++; } } for (int i = 1; i + 1 < a.length; i++) { if (x[i - 1] == 0 && x[i] == 1 && x[i + 1] == 0) { a[i] = Math.max(a[i - 1], a[i + 1]); x[i] = 0; changes++; } } StringBuilder sb = new StringBuilder(changes + "\n"); for (int i : a) { sb.append(i).append(" "); } return sb.toString().trim(); } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = inputInt(br); StringBuilder sb = new StringBuilder(); while (t-- > 0) { int y = inputInt(br); int[] a = inputIntArray(br); sb.append(solve(a)).append("\n"); } System.out.println(sb); } private static char[] inputCharArray(BufferedReader br) throws IOException { return br.readLine().trim().toCharArray(); } public static int inputInt(BufferedReader br) throws IOException { return Integer.parseInt(br.readLine().trim()); } public static long inputLong(BufferedReader br) throws IOException { return Long.parseLong(br.readLine().trim()); } public static int[] inputIntArray(BufferedReader br) throws IOException { String[] spec = br.readLine().split(" "); int[] arr = new int[spec.length]; for (int i = 0; i < spec.length; i++) arr[i] = Integer.parseInt(spec[i]); return arr; } public static long[] inputLongArray(BufferedReader br) throws IOException { String[] spec = br.readLine().split(" "); long[] arr = new long[spec.length]; for (int i = 0; i < spec.length; i++) arr[i] = Long.parseLong(spec[i]); return arr; } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
dfb371d0b53c132f29279c890e109ac5
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; public class CF1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int test = 0;test<t;test++){ int n = sc.nextInt(); int[] arr = new int[n]; int[] lms = new int[n]; int ans = 0; for(int i=0;i<n;i++) { arr[i] = sc.nextInt(); } for(int i=1;i<n-1;i++){ if(arr[i]>arr[i+1] && arr[i]>arr[i-1]){ lms[i] = arr[i]; } } for(int i=1;i<n-1;i++){ if(lms[i]>0){ ans+=1; arr[i+1] = lms[i]; if(i<n-2 && lms[i+2]>0){ arr[i+1] = Math.max(lms[i],lms[i+2]); lms[i+2]=0; } } } System.out.println(ans); for(int i=0;i<n;i++){ System.out.print(arr[i]); if(i!=n-1){System.out.print(" ");} } System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
afeaaecb2af82dba45eda42102418f01
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.StringTokenizer; public class Main { static class FastReader { StringTokenizer st; BufferedReader br; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void solve(Set<Long>set,int n) { } public static void main(String[] args) { FastReader sc = new FastReader(); int t = sc.nextInt(); while (t-- > 0) { int n=sc.nextInt(); long[]arr=new long[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextLong(); } long max=Long.MIN_VALUE; for(int i=0;i<n;i++){ max=Math.max(arr[i],max); } int c=0; for(int i=1;i<n-1;i++){ if(arr[i]>arr[i-1]&&arr[i]>arr[i+1]){ c++; if(i+2<n&&arr[i+2]>=arr[i]) arr[i+1]=arr[i+2]; else arr[i+1]=arr[i]; } } System.out.println(c); for(int i=0;i<n;i++){ System.out.print(arr[i]+" "); } System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
3fad711907f5a97718a94d987daca3d1
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Solution { private final static FastReader scan = new FastReader(); static List<Integer> l1 = new ArrayList<>(); public static void main(String[] args) { int tc = 1; tc = scan.nextInt(); while (tc-- != 0) { solve(); } } private static void solve() { int n = scan.nextInt(); long[] a = new long[n]; for(int i =0;i<n;i++) { a[i] = scan.nextLong(); } int count =0; List<Integer> l1 = new ArrayList<>(); for(int i = 1;i<n-1;i++) { if(a[i]>a[i-1] && a[i]>a[i+1]) { l1.add(i); } } // System.out.print(l1); count = l1.size(); for(int i =0 ;i<l1.size();i++) { if(i!=l1.size()-1 && l1.get(i+1)-l1.get(i)==2) { int cur = l1.get(i)+1; a[cur] = Math.max(a[l1.get(i)],a[l1.get(i+1)]); count-=1; i++; }else { int cur = l1.get(i); a[cur] = Math.max(a[cur-1], a[cur+1]); } } System.out.println(count); for(int i =0;i<n;i++) { System.out.print(a[i]+" "); } System.out.println(); } private static void doxor(long[] k,int l2,int l1) { long a = k[l1],b = k[l2]; long ans = 1; for(int i = 0;i<30;i++) { if((ans&a)==ans && (ans&b)==ans) { a = a^ans; // System.out.println(a+" s"); } ans = ans<<1; } k[l1] =a ; k[l2]= b; } private static int findNum(int[] a,int k){ for(int i = 0;i<a.length;i++){ if(a[i]==k) return i; } return -1; } @SuppressWarnings("unused") private static void printArr(final int[] a) { for (int i : a) System.out.print(i + " "); System.out.println(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static class ArrayHash { int[] a; ArrayHash(int[] a) { this.a = Arrays.copyOf(a, a.length); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + Arrays.hashCode(a); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; ArrayHash other = (ArrayHash) obj; return Arrays.equals(a, other.a); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
9d64e2029b2964c23c53d3ed39a76612
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; public class avoid { public static void main(String[] args){ Scanner sc=new Scanner(System.in); int t; t=sc.nextInt(); while(t-->0){ int n; n=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=sc.nextInt(); int c=0; for(int i=1;i<n-1;i++){ double maxi=0; if(i<n-2){ if(arr[i]>arr[i-1]&&arr[i]>arr[i+1]){ c++; maxi=Math.max(arr[i],arr[i+2]); arr[i+1]=(int)maxi; } } else{ if(arr[i]>arr[i-1]&&arr[i]>arr[i+1]){ c++; arr[i+1]=arr[i]; } } } System.out.println(c); for(int i=0;i<n;i++){ System.out.print(arr[i]+" "); } System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
71116a4b41bd58cd505f5cc736547d50
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
// Java is like Alzheimer's, it starts off slow, but eventually, your memory is gone. import java.io.*; import java.util.*; public class Aqueous { static MyScanner sc = new MyScanner(); static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int a[] = new int[n]; int max= Integer.MIN_VALUE; for(int i = 0; i<n; i++) { a[i] = sc.nextInt(); max = Math.max(a[i],max); } int cnt = 0; for(int i =1; i<n-1; i++) { if(a[i]>a[i-1] && a[i]>a[i+1]) { if(i+2<n) { a[i+1] = Math.max(a[i],a[i+2]); } else { a[i+1] = a[i]; } cnt++; } } pw.println(cnt); for(int i = 0; i<n; i++) { pw.print(a[i]+" "); } pw.println(); } pw.close(); } static void ruffleSort(int a[]) { int n = a.length; Random r = new Random(); for (int i = 0; i < n; i++) { int oi = r.nextInt(n); int temp = a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } static void ruffleSort(long a[]) { int n = a.length; Random r = new Random(); for (int i = 0; i < n; i++) { int oi = r.nextInt(n); long temp = a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } 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\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
17f48d6c51cc39cbc71b0b1d70ce7c3c
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.BufferedReader; import java.lang.*; import java.io.FileReader; import java.io.IOException; import java.math.BigInteger; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigDecimal; import java.math.BigInteger; import java.math.MathContext; import java.util.*; import java.util.LinkedList; public class CEdu{ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void sort(long[] arr) { ArrayList<Long> a = new ArrayList<>(); for (long i : arr) { a.add(i); } Collections.sort(a); for (int i = 0; i < a.size(); i++) { arr[i] = a.get(i); } } static boolean get(char[] ch, int i, long k) { long ope = 0; for(int j = i; j >=0; j--) { long ele = ((ch[j]-'0') + ope)%10; long req = 10 - ele; if(req == 10) { req = 0; } if(ope+req <= k) { ope = ope + req; if(j == 0) { return true; } }else { return false; } } return false; } static void solve(long a, long b, long c) { System.out.println((a|b) & (b|c) & (c|a)); } static long get(long[] arr , int low, int high) { long len = high-low+1; for(int i = low; i <= high; i++) { if(arr[i] == 0) { len++; } } return len; } public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); int t = sc.nextInt(); test: while(t-- > 0) { int n = sc.nextInt(); long[] arr = new long[n]; long[] brr = new long[n]; for(int i = 0; i < n; i++){ arr[i] = sc.nextLong(); brr[i] = arr[i]; } boolean ope = false; boolean[] ch = new boolean[n]; for(int i = 1; i < n-1; i++){ if(arr[i-1] < arr[i] && arr[i] > arr[i+1]){ ch[i] = true; } } // System.out.println(Arrays.toString(ch)); for(int i = 0; i < n; i++){ if(ch[i] == true ) { ope = true; if(i+2 < n) { if(ch[i+2] == true){ long max = Math.max(arr[i], arr[i+2]); arr[i+1] = max; ch[i+2] = false; }else { arr[i+1] = arr[i]; } }else { arr[i+1] =arr[i]; } } } long s = 0; for(int i = 0; i < n; i++){ if(arr[i] != brr[i])s++; } System.out.println(s); for(int i = 0; i < n; i++){ System.out.print(arr[i]+" "); } System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
659b9104b732fa4346f952976098063b
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.BufferedReader; import java.io.*; import java.io.InputStreamReader; import java.util.Scanner; import java.util.*; public class Yoo { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader sc=new FastReader(); int i,j=0; int t =sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int a[]=new int[n]; int b[]=new int[n]; for(i=0;i<n;i++) { a[i]=sc.nextInt(); b[i]=0; } for(i=1;i<n-1;i++) { if(a[i-1]<a[i] && a[i+1]<a[i]) { b[i]=1; } } int ans=0; for(i=0;i<n-2;i++) { if(b[i]==1 && b[i+2]==1) { a[i+1]=Math.max(a[i],a[i+2]); b[i]=0; b[i+2]=0; i+=2; ans++; continue; } if(b[i]==1) { a[i+1]=a[i]; ans++; } } if(b[n-2]==1) { a[n-1]=a[n-2]; ans++; } System.out.println(ans); for(i=0;i<n;i++) { System.out.print(a[i]+" "); } System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
b67844a2c226bf88144f7877aba0b4fa
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.*; import java.lang.*; import java.util.*; public class GFG { public static void main (String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); long arr[]=new long[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextLong(); } int c=0; if(n==2){ System.out.println(c); for(int i=0;i<n;i++){ System.out.print(arr[i]+" "); } System.out.println(); }else{ for(int i=1;i<n-1;i++){ if(arr[i]>arr[i-1]&&arr[i]>arr[i+1]&&i!=n-2){ arr[i+1]=Math.max(arr[i],arr[i+2]); c++; }else if(arr[i]>arr[i-1]&&arr[i]>arr[i+1]&&i==n-2){ c++; arr[n-2]=Math.max(arr[n-3],arr[n-1]); } } System.out.println(c); for(int i=0;i<n;i++){ System.out.print(arr[i]+" "); } System.out.println(); } } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
3f6d45267e32e420632db34b080756d8
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { CP sc =new CP(); int tt = sc.nextInt(); while (tt-- > 0) { int n = sc.nextInt(); int[] A = new int[n]; for(int i=0;i<n;i++) { A[i] = sc.nextInt(); } ArrayList<Integer> ar = new ArrayList<>(); for(int i=1;i<n-1;i++) { if(A[i] > A[i-1] && A[i] > A[i+1]){ ar.add(i); } } int c = 0; //System.out.println(ar); if(ar.size()==0){ System.out.println(0); for(int i: A) System.out.print(i+" "); System.out.println(); } else if(ar.size() == 1){ c = 1; A[ar.get(0)] = Math.max(A[ar.get(0) - 1], A[ar.get(0) + 1]); System.out.println(1); for(int i: A) System.out.print(i+" "); System.out.println(); }else { boolean[] done = new boolean[ar.size()]; for (int i = 0; i < ar.size() - 1; i++) { if (ar.get(i + 1) - ar.get(i) == 2) { // System.out.println(Math.max(A[ar.get(i)], A[ar.get(i + 1)])); // System.out.println(A[ar.get(i) + 1]); A[ar.get(i) + 1] = Math.max(A[ar.get(i)], A[ar.get(i + 1)]); done[i] = true; done[i+1] =true; i++; c++; } } //System.out.println(Arrays.toString(done)); for(int i=0;i<done.length;i++){ if(!done[i]) { c++; A[ar.get(i)] = Math.max(A[ar.get(i) - 1], A[ar.get(i) + 1]); } } System.out.println(c); for(int i: A) System.out.print(i+" "); System.out.println(); } } } /*****************************************************************************/ static class CP { BufferedReader bufferedReader; StringTokenizer stringTokenizer; public CP() { bufferedReader = new BufferedReader(new InputStreamReader(System.in)); } int nextInt() { return Integer.parseInt(NNNN()); } long nextLong() { return Long.parseLong(NNNN()); } double nextDouble() { return Double.parseDouble(NNNN()); } String NNNN() { while (stringTokenizer == null || !stringTokenizer.hasMoreElements()) { try { stringTokenizer = new StringTokenizer(bufferedReader.readLine()); } catch (IOException e) { e.printStackTrace(); } } return stringTokenizer.nextToken(); } String nextLine() { String spl = ""; try { spl = bufferedReader.readLine(); } catch (IOException e) { e.printStackTrace(); } return spl; } } /*****************************************************************************/ }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
7b24808ab3aab1594252eb17a0bae25f
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
/* ROHAN SHARMA */ 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 A { public static final FastScanner sc = new FastScanner(); public static void main (String[] args) throws java.lang.Exception { FastWriter out = new FastWriter(); int T = sc.nextInt(); while (T-- > 0) { int n = sc.nextInt(); int max = 0; int[] arr = new int[n]; for(int i = 0; i< n ; i++){ arr[i] = sc.nextInt(); max = Math.max(max , arr[i]); } int c = 0; for(int i = 1 ; i< n-1; i++){ if(arr[i] > arr[i-1] && arr[i] > arr[i+1]){ c++; int x = arr[i]; int y = 0; if(i+2 < n){ y = arr[i+2]; } arr[i+1] = Math.max(x , y); } } out.println(c); for(int e : arr){ out.print(e + " "); } out.println(""); } out.close(); } 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(); } String nextLine(){ String str=""; try { str=br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } 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()); } double nextDouble(){ return Double.parseDouble(next()); } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
e0e4a35ef7d31facb9763704d1e7c376
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
/**Main Snippet */ import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; public class Main { static FastReader sc; static StringBuilder sb = new StringBuilder(); public static void main(String args[]) { sc=new FastReader(); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int[]a=readArray(n); List<Integer>list=new ArrayList<>(); for(int i=1;i<n-1;i++){ if(a[i]>a[i-1]&&a[i]>a[i+1]){ list.add(i); } } int cnt=0; int sz=list.size(); for(int i=0;i<sz;){ if(i<sz-1){ if(list.get(i+1)-list.get(i)==2){ int idx=list.get(i)+1; a[idx]=max(a[idx+1],a[idx-1]); i+=2; }else{ int idx=list.get(i); a[idx]=max(a[idx-1],a[idx+1]); i++; } cnt++; } else{ int idx=list.get(i); a[idx]=max(a[idx-1],a[idx+1]); i++; cnt++; } } sb.append(cnt+"\n"); for(int i=0;i<n;i++){ sb.append(a[i]+" "); } sb.append("\n"); } System.out.println(sb.toString()); } static void swap(int[]a,int i,int j){ int k=a[i]; a[i]=a[j]; a[j]=k; } static void printArray(int[]a){ int n=a.length; for(int i=0;i<n;i++){ System.out.print(a[i]+" "); } System.out.println(); } public static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static void sort(long[] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static boolean isVowel(char c) { return (c=='a' || c=='A' || c=='e' || c=='E' || c=='i' || c=='I' || c=='o' || c=='O' || c=='u' || c=='U'); } static int[] readArray(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) sc.nextInt(); } return res; } public static long gcd(long a, long b){ if(b == 0) return a; return gcd(b , a%b); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
83d44999dc22a43e0d005ba6ea2d8b41
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; /** __ __ ( _) ( _) / / \\ / /\_\_ / / \\ / / | \ \ / / \\ / / |\ \ \ / / , \ , / / /| \ \ / / |\_ /| / / / \ \_\ / / |\/ _ '_| \ / / / \ \\ | / |/ 0 \0\ / | | \ \\ | |\| \_\_ / / | \ \\ | | |/ \.\ o\o) / \ | \\ \ | /\\`v-v / | | \\ | \/ /_| \\_| / | | \ \\ | | /__/_ `-` / _____ | | \ \\ \| [__] \_/ |_________ \ | \ () / [___] ( \ \ |\ | | // | [___] |\| \| / |/ /| [____] \ |/\ / / || ( \ [____ / ) _\ \ \ \| | || \ \ [_____| / / __/ \ / / // | \ [_____/ / / \ | \/ // | / '----| /=\____ _/ | / // __ / / | / ___/ _/\ \ | || (/-(/-\) / \ (/\/\)/ | / | / (/\/\) / / // _________/ / / \____________/ ( */ public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-- >0) { int n=sc.nextInt(); long[] arr=new long[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextLong(); } int op=0; for(int i=2;i<n;i++) { if(arr[i-1]>arr[i-2] && arr[i-1]>arr[i]) { if(i+1<n && arr[i+1]>=arr[i-1]) { op++; arr[i]=arr[i+1]; } else { op++; arr[i]=arr[i-1]; } } } System.out.println(op); for(long val:arr) { System.out.print(val+" "); } System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
bbb9a7e95c7964ddec54e8acd8ab8242
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int [] nums = new int[2 * 100000 + 1]; for (int caseNum = 0; caseNum < n; caseNum++){ int arrayNum = scanner.nextInt(); int ans = 0; for (int i = 0; i < arrayNum; i++) { nums[i] = scanner.nextInt(); } for (int i = 1; i < arrayNum - 1; i++) { if(nums[i] > nums[i - 1] && nums[i] > nums[i + 1]){ ans++; if (i == arrayNum - 2){ nums[i + 1] = nums[i]; }else { nums[i + 1] = Math.max(nums[i], nums[i + 2]); } } } System.out.println(ans); for (int i = 0; i < arrayNum; i++) { System.out.print(nums[i]); if(i != arrayNum - 1){ System.out.print(" "); } } System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
d471f69e51a95c9344bf380e2df2e102
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Collections; public class AvoidLocalMaximums { 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) { int n = Integer.parseInt(br.readLine()); String s[] = br.readLine().split(" "); Long[] a = new Long[n]; for(int i =0; i < n; i++) { a[i] = Long.parseLong(s[i]); } int ans = 0; boolean prev = false; for(int i = 1; i < n-1; i++) { if(a[i] > a[i-1] && a[i] > a[i+1]) { prev = true; continue; } else if(prev == true){ a[i] = Math.max(a[i-1], a[i+1]); ans++; prev = false; } } if(prev == true) { a[n-1] = a[n-2]; ans++; } System.out.println(ans); for(long i : a) { System.out.print(i +" "); } System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
46e0373decc10b086a6810f0e5b25513
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; import java.lang.*; public class Main{ public static PrintWriter out; public static FastReader in; public static int mod = 1000003; static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } String next(){ while(st==null || !st.hasMoreTokens()){ try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str=""; try { str=br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { try { in = new FastReader(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t = in.nextInt(); while(t-->0){ int n = in.nextInt(); long[] a = new long[n]; input(a); if(n<=2){ out.println(0); print(a); continue; } int ct =0; for (int i=1;i<n-1;i++){ if(a[i]>a[i-1] && a[i]>a[i+1]){ if(i+2<=n-2 && a[i+2]>=a[i]){ a[i+1] = a[i+2]; }else{ a[i+1] = a[i]; } ct++; } } out.println(ct); print(a); } out.flush(); out.close(); } catch (Exception e) { System.out.println("Exception"); return; } } static boolean isPrime(int a){ if (a == 1) return false; if (a == 2 || a == 3) return true; if (a%2==0 || a%3==0) return false; for (int i=5;i*i<=a;i+=6){ if (a%i==0 || a%(i+2)==0) return false; } return true; } static void printAllPrime(int n){ // Sieve of Eratosthenes algorithm if(n <= 1) return; boolean[] arr = new boolean[n+1]; Arrays.fill(arr,true); for (int i=2;i*i<=n;i++){ if (arr[i]){ for (int j=i*i;j<=n;j+=i){ arr[j] = false; } } } for (int i=2;i<=n;i++){ if (arr[i]){ System.out.printf(i+" "); } } } static int highestPowerOf2(int x) { // check for the set bits x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; // Then we remove all but the top bit by xor'ing the // string of 1's with that string of 1's shifted one to // the left, and we end up with just the one top bit // followed by 0's. return x ^ (x >> 1); } static long pow(long a,long b){ long ans = b; long res =1; if(b<0){ ans = -1*b; } while(ans>0){ if((ans&1)==1){ res = (res*a)%mod; } a = (a*a)%mod; ans = ans>>1; } if(b<0){ res = 1/res; } return res; } static double pow(double a,long b){ long ans = b; double res =1; if(b<0){ ans = -1*b; } while(ans>0){ if((ans&1)==1){ res = (res*a)%mod; } a = (a*a)%mod; ans = ans>>1; } if(b<0){ res = 1/res; } return res; } static void sort(int[] arr) { ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } static void sort(long[] arr) { ArrayList<Long> ls = new ArrayList<Long>(); for(long x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } static HashMap<Integer, Integer> sortValue(HashMap<Integer,Integer> h){ List<Map.Entry<Integer, Integer> > list = new ArrayList<>(h.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2){ int fp = o2.getValue().compareTo(o1.getValue()); int sp = o2.getKey().compareTo(o1.getKey()); return fp==0 ? sp : fp; } }); //clear the hashmap // h.clear(); HashMap<Integer, Integer> hm = new LinkedHashMap<>(); for(Map.Entry<Integer, Integer> mp : list){ hm.put(mp.getKey(),mp.getValue()); } return hm; } static HashMap<Integer, Integer> sortKey(HashMap<Integer,Integer> h){ List<Map.Entry<Integer, Integer> > list = new ArrayList<>(h.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2){ int fp = o2.getValue().compareTo(o1.getValue()); int sp = o2.getKey().compareTo(o1.getKey()); return fp==0 ? fp : sp; } }); //clear the hashmap // h.clear(); HashMap<Integer, Integer> hm = new LinkedHashMap<>(); for(Map.Entry<Integer, Integer> mp : list){ hm.put(mp.getKey(),mp.getValue()); } return hm; } static long totient(long n) { long result = n; for (int p = 2; p*p <= n; ++p) if (n % p == 0) { while(n%p == 0) n /= p; result -= result/p; } if (n > 1) result -= result/n; return result; } static int pow(int x,int y){ return (int)Math.pow(x,y); } static void allDivisor(int a){ int i=0; for (i=1;i*i<=a;i++){ if (a%i==0){ System.out.printf(i+" "); } } for (;i>=1;i--){ if (a%i==0){ System.out.printf(a/i+" "); } } } static int binaryToInt(String s){ return Integer.parseInt(s,2); } static String toBinaryString(int s){ return Integer.toBinaryString(s); } static void primeFactors(int a){ if (a<=1) return; while (a%2==0) {System.out.printf(2+" "); a=a/2;} while (a%3==0) {System.out.printf(3+" "); a=a/3;} for (int i=5;i*i<=a;i+=6){ while (a%i==0){ System.out.printf(i+" "); a = a/i; } while (a%(i+2)==0){ System.out.printf((i+2)+" "); a = a / (i+2); } } if (a>3){ System.out.printf(a+" "); } System.out.println(); } static int lcm(int a,int b){ return a*b/gcd(a,b); } static int gcd(int a, int b){ if (a==0) return b; return gcd(b%a,a); } static int countZeros(int f){ int x = 0; for (int i=5;i<=f;i=i*5){ x += f/i; } return x; } static int ExtendedGcd(int a, int b,int x,int y){ if(b == 0){ x = 1; y = 0; return a; } int x1=1,y1=1; int ans = ExtendedGcd(b, a%b,x1,y1); x = y1; y = x1 - (a/b)*y1; System.out.println(x+" "+y); return ans; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////Lazy work///////////////////////////////////////////////////////////// static void input(int[] a){ for (int i=0;i<a.length;i++){ a[i] = in.nextInt(); } } static void input(long[] a){ for (int i=0;i<a.length;i++){ a[i] = in.nextLong(); } } static void input(String[] a){ for (int i=0;i<a.length;i++){ a[i] = in.next(); } } static void swap(char[] c,int i,int j){ char t = c[i]; c[i] = c[j]; c[j] = t; } static void swap(int[] c,int i,int j){ int t = c[i]; c[i] = c[j]; c[j] = t; } static void swap(long[] c,int i,int j){ long t = c[i]; c[i] = c[j]; c[j] = t; } static void print(int[] a){ for (int i=0;i<a.length;i++){ out.printf(a[i]+" "); } out.println(); } static void print(int[][] a){ for (int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.printf(a[i][j]+" "); } out.println(); } } static void print(long[] a){ for (int i=0;i<a.length;i++){ out.printf(a[i]+" "); } out.println(); } static void print(char[] a){ for (int i=0;i<a.length;i++){ out.printf(a[i]+" "); } out.println(); } static void print(String s){ for (int i=0;i<s.length();i++){ out.printf(s.charAt(i)+" "); } out.println(); } static void print(ArrayList<Integer> a){ a.forEach(e -> out.printf(e+" ")); out.println(); } static void print(LinkedList<Integer> a){ a.forEach(e -> out.printf(e+" ")); out.println(); } static void print(HashSet<Integer> a){ a.forEach(e -> out.printf(e+" ")); out.println(); } static void print(HashMap<Integer,Integer> a){ for(Map.Entry<Integer, Integer> mp : a.entrySet()){ out.println(mp.getKey() + " "+ mp.getValue()); } } static void reverse(int[] a){ int i=0,j=a.length-1; while(i<j){ int t = a[i]; a[i] = a[j]; a[j]= t; i++; j--; } } static String reverse(String s){ char[] a = s.toCharArray(); int i=0,j=a.length-1; while(i<j){ char t = a[i]; a[i] = a[j]; a[j]= t; i++; j--; } return String.valueOf(a); } } class CompareP implements Comparator<Pair> { public int compare(Pair a, Pair b){ long dif = a.v - b.v; if (dif > 0) return 1; if (dif < 0) return -1; return 0; } } class CompareT implements Comparator<Triplet> { public int compare(Triplet a, Triplet b){ long dif = a.z - b.z; if (dif > 0) return 1; if (dif < 0) return -1; return 0; } } class Triplet{ long x; long y; long z; public Triplet(long x,long y,long z){ this.x = x; this.y = y; this.z = z; } } class Pair { int k; int v; public Pair(int k, int v) { this.k = k; this.v = v; } } class ncr { public int mod = 1000003; public long[] fact = new long[mod+1]; public long[] ifact = new long[mod+1]; public int nCr(long n, long r) { preFactFermat(); long ans = 1; // while(n>0 && r>0){ // int a=(int) (n%mod),b= (int)(r%mod); // n = n/mod;r=r/mod; // if(a<b){ // return 0; // }else{ // ans = (ans* (fact[a]*((ifact[b]*ifact[a-b])%mod)%mod))%mod; // } // } ans = lucas(n,r,ans); return (int)ans; } public long lucas(long n,long r,long ans){ if(r==0)return 1; long ni=n%mod,ri=r%mod; return (lucas(n/mod,r/mod,ans)*(fermat(ni,ri,ans)%mod))%mod; } public long fermat(long n,long r,long ans){ if(n<r){ return 0; } ans = (ans* (fact[(int)n]*((ifact[(int)r]*ifact[(int)(n-r)])%mod)%mod))%mod; return ans; } public void preFactFermat(){ fact[1] = 1; fact[0] = 1; ifact[0] = expo(1,mod-2); ifact[1] = expo(1,mod-2); for(int i=2;i<=mod;i++){ fact[i] = (i*(fact[i-1]%mod))%mod; ifact[i] = expo(fact[i],mod-2); } } public long expo(long a,long b){ long ans = b; long res =1; if(b<0){ ans = -1*b; } while(ans>0){ if((ans&1)==1){ res = (res*a)%mod; } a = (a*a)%mod; ans = ans>>1; } if(b<0){ res = 1/res; } return res; } } class FenwickTree{ int[] ft; public void print(){ for (int i=0;i<ft.length;i++){ System.out.printf(ft[i]+" "); } } public FenwickTree(int[] a){ ft = new int[a.length+1]; for (int i=0;i<a.length;i++){ this.update(i,a[i]); } } public int getSum(int i){ int sum = 0; while(i>0){ sum += ft[i]; i = i - (i & (-i)); } return sum; } public void update(int i,int d){ i = i +1; while(i<ft.length){ ft[i] += d; i = i + (i &(-i)); } } } class SegmentTree{ int[] st; public SegmentTree(int[] a){ st = new int[a.length*4]; construct(a,0,a.length-1,0); } void print(){ for(int i=0;i<st.length;i++){ System.out.printf(st[i]+" "); } System.out.println(); } int construct(int[] a,int ss,int se,int si){ if(ss==se){ st[si] = a[ss]; return a[ss]; } int mid = (ss+se)/2; st[si] = construct(a,ss,mid,2*si+1) + construct(a,mid+1,se,2*si+2); return st[si]; } int getSum(int qs,int qe,int ss,int se,int si){ if(qe<ss || qs>se){ return 0; } if(ss>=qs && se <= qe){ return st[si]; } int mid = (ss+se)/2; return getSum(qs,qe,ss,mid,2*si+1) + getSum(qs,qe,mid+1,se,2*si+2); } void update(int ss,int se,int si,int i,int diff){ if(ss > i || i> se){ return; } this.st[si] += diff; if(ss< se){ int mid = (ss+se)/2; update(ss,mid,2*si+1,i,diff); update(mid+1,se,2*si+2,i,diff); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
4507d516a88bab9cb519c2410eada3ac
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.*; import java.util.*; public class codeforces { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String args[]) { if (System.getProperty("ONLINE_JUDGE") == null) { // Input is a file try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { System.err.println("Error"); } } else { // Input is System.in } FastReader sc = new FastReader(); // Scanner sc = new Scanner(System.in); //System.out.println(java.time.LocalTime.now()); StringBuilder sb = new StringBuilder(); int t = sc.nextInt(); while(t>0) { int n = sc.nextInt(); int[] arr = new int[n]; for(int i = 0; i<n; i++){ arr[i] = sc.nextInt(); } int count = 0; int prev = -1; for(int i =1; i<n-1; i++){ if(arr[i]>arr[i-1] && arr[i] >arr[i+1]){ if(prev == -1){ prev = i; count++; }else { if(prev +2 == i){ arr[prev+1] = Math.max(arr[i], arr[prev]); prev = -1; }else{ arr[prev] = Math.max(arr[prev+1], arr[prev-1]); prev = i; count++; } } } } if(prev != -1)arr[prev] = Math.max(arr[prev-1], arr[prev+1]); sb.append(count + "\n"); for(int i : arr)sb.append(i + " "); sb.append("\n"); t--; } System.out.println(sb); } //////////nCr//////////////////////////////////////// ///////// SUM OF EACH DIGIT OF A NUMBER /////////////// public static long digSum(long a) { long sum = 0; while(a>0) { sum += a%10; a /= 10; } return sum; } ///////// TO CHECK NUMBER IS PRIME OR NOT /////////////// public static boolean isPrime(int n) { if(n<=1)return false; if(n <= 3)return true; if(n%2==0 || n%3==0)return false; for(int i = 5; i*i<=n; i+=6) { if(n%i == 0 || n%(i+2) == 0)return false; } return true; } ///////// NEXT PRIME NUMBER BIGGER THAN GIVEN NUMBER /////////////// public static int nextPrime(int n) { while(true) { n++; if(isPrime(n)) break; } return n; } ///////// GCD /////////////// public static int gcd(int a, int b) { if(b == 0)return a; return gcd(b, a%b); } ///////// LCM /////////////// public static int lcm(int a, int b) { return (a*b)/gcd(a,b); } ///////// IS POWER OF 2 /////////////// public static boolean isPowerOfTwo (int x){ /* First x in the below expression is for the case when x is 0 */ return x!=0 && ((x&(x-1)) == 0); } } class Pair{ int x; int y; Pair(int x, int y){ this.x = x; this.y = y; } @Override public boolean equals(Object o) { if(this == o)return true; if(o == null || this.getClass() != o.getClass())return false; Pair p = (Pair)o; return x == p.x && y == p.y; } @Override public int hashCode(){ return Objects.hash(x , y); } // @Override // public int compareTo(Pair o) { // } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
ba486eedf6cddea4716dee8d1bdc8a4f
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.*; import java.util.*; public class AvoidLocalMaximums { public static void main(String args[])throws IOException { int i,j,k,t,n; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); t=Integer.parseInt(br.readLine()); while(t-->0) { n=Integer.parseInt(br.readLine()); int a[]=new int[n]; String s[]=br.readLine().split(" "); for(i=0;i<n;i++) { a[i]=Integer.parseInt(s[i]); } int count=0; for(i=1;i<n-1;i++) { if(i!=n-2) { if(a[i]>a[i-1]&&a[i]>a[i+1]) { if(a[i+2]>=a[i+1]&&a[i+2]>=a[i]) { a[i+1]=a[i+2]; } else { a[i+1]=a[i]; } count++; } } else { if(a[i]>a[i-1]&&a[i]>a[i+1]) { count++; a[i+1]=a[i]; } } } System.out.println(count); for(i=0;i<n;i++) { System.out.print(a[i]+" "); } System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
d3f90dd9551a02819d4380e3bd05fc93
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
/*LoudSilence*/ import java.io.*; import java.util.*; import static java.lang.Math.*; public class Solution { /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ static FastScanner s = new FastScanner(); static FastWriter out = new FastWriter(); final static int mod = (int)1e9 + 7; final static int INT_MAX = Integer.MAX_VALUE; final static int INT_MIN = Integer.MIN_VALUE; final static long LONG_MAX = Long.MAX_VALUE; final static long LONG_MIN = Long.MIN_VALUE; final static double DOUBLE_MAX = Double.MAX_VALUE; final static double DOUBLE_MIN = Double.MIN_VALUE; final static float FLOAT_MAX = Float.MAX_VALUE; final static float FLOAT_MIN = Float.MIN_VALUE; /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ static class FastScanner{BufferedReader br;StringTokenizer st; public FastScanner() {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());} List<Integer> readIntList(int n){List<Integer> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextInt()); return arr;} List<Long> readLongList(int n){List<Long> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextLong()); return arr;} int[] readIntArr(int n){int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = s.nextInt(); return arr;} long[] readLongArr(int n){long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = s.nextLong(); return arr;} String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;}} static class FastWriter{private final BufferedWriter bw;public FastWriter() {this.bw = new BufferedWriter(new OutputStreamWriter(System.out));} public void print(Object object) throws IOException{bw.append(""+ object);} public void println(Object object) throws IOException{print(object);bw.append("\n");} public void debug(int object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");} public void debug(long object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");} public void close() throws IOException{bw.close();}} /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ public static ArrayList<Integer> seive(int n){ArrayList<Integer> list = new ArrayList<>();int arr[] = new int[n+1];for(int i = 2; i <= n; i++) {if(arr[i] == 1) {continue;}else {list.add(i);for(int j = i*i; j <= n; j = j + i) {arr[j] = 1;}}}return list;} public static long gcd(long a, long b){if(a > b) {a = (a+b)-(b=a);}if(a == 0L){return b;}return gcd(b%a, a);} public static void swap(int[] arr, int i, int j) {arr[i] = arr[i] ^ arr[j]; arr[j] = arr[j] ^ arr[i]; arr[i] = arr[i] ^ arr[j];} public static boolean isPrime(long n){if(n < 2){return false;}if(n == 2 || n == 3){return true;}if(n%2 == 0 || n%3 == 0){return false;}long sqrtN = (long)Math.sqrt(n)+1;for(long i = 6L; i <= sqrtN; i += 6) {if(n%(i-1) == 0 || n%(i+1) == 0) return false;}return true;} public static long mod_add(long a, long b){ return (a%mod + b%mod)%mod;} public static long mod_sub(long a, long b){ return (a%mod - b%mod + mod)%mod;} public static long mod_mul(long a, long b){ return (a%mod * b%mod)%mod;} public static long modInv(long a, long b){ return expo(a, b-2)%b;} public static long mod_div(long a, long b){return mod_mul(a, modInv(b, mod));} public static long expo (long a, long n){if(n == 0){return 1;}long recAns = expo(mod_mul(a,a), n/2);if(n % 2 == 0){return recAns;}else{return mod_mul(a, recAns);}} /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ // Pair class public static class Pair<X extends Comparable<X>,Y extends Comparable<Y>> implements Comparable<Pair<X, Y>>{ X first; Y second; public Pair(X first, Y second){ this.first = first; this.second = second; } public String toString(){ return "( " + first+" , "+second+" )"; } @Override public int compareTo(Pair<X, Y> o) { int t = first.compareTo(o.first); if(t == 0) return second.compareTo(o.second); return t; } } /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ // Code begins public static void solve() throws IOException { int n = s.nextInt(); int a[] = new int[n]; int b[] = new int[n]; for(int i = 0; i < n; i++){ a[i] = s.nextInt(); b[i] = a[i]; } for(int i = 1; i < n-1; i++){ if(a[i] > a[i-1] && a[i] > a[i+1]){ if((i+2) < n && a[i+1] < a[i+2]){ a[i+1] = max(a[i], a[i+2]); }else{ a[i] = max(a[i-1], a[i+1]); } } } int ans = 0; for(int i = 0; i < n; i++){ if(a[i] != b[i]){ ans++; } } out.println(ans); for(int ele : a){ out.print(ele+" "); } out.println(""); } /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ public static void main(String[] args) throws IOException { int test = s.nextInt(); for(int tt = 1; tt <= test; tt++) { solve(); } out.close(); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 11
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
b3031d1063db4bba24b2632e442b6a89
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.io.*; import java.util.*; public class Main { static TreeSet<String> tSet; static PrintWriter pw; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int []arr=sc.nextIntArray(n); int res=arr[0]; for (int i = 1; i < arr.length; i++) { res|=arr[i]; } pw.println(res); } pw.close(); pw.flush(); } public static char flip(char x) { if(x=='0') return '1'; return'0'; } public static int dfsOnAdjacencyList(ArrayList<Integer>[] graph, int curr, boolean[] vis, int m, int conscats, int[] cats) { // same DFS code but a7la mn ellyfoo2 fel writing if (conscats > m) return 0; if (graph[curr].size() == 1 && curr != 1) { if (cats[curr] == 1) conscats++; else conscats = 0; if (conscats > m) return 0; return 1; } vis[curr] = true; if (cats[curr] == 1) conscats++; else conscats = 0; int res = 0; for (int x : graph[curr]) { if (!vis[x]) res += dfsOnAdjacencyList(graph, x, vis, m, conscats, cats); } return res; } public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public static int lcm(int a, int b) { return a * b / gcd(a, b); } public static long fac(long i) { long res = 1; while (i > 0) { res = res * i--; } return res; } public static long combination(long x, long y) { return 1l * (fac(x) / (fac(x - y) * fac(y))); } public static long permutation(long x, long y) { return combination(x, y) * fac(y); } static class Pair implements Comparable<Pair> { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public int compareTo(Pair p) { if(this.x==p.x) return this.y-p.y; return this.x-p.x; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public int[] nextIntArray(int n) throws IOException { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] array = new Integer[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } public long[] nextlongArray(int n) throws IOException { long[] array = new long[n]; for (int i = 0; i < n; i++) { array[i] = nextLong(); } return array; } public Long[] nextLongArray(int n) throws IOException { Long[] array = new Long[n]; for (int i = 0; i < n; i++) { array[i] = nextLong(); } return array; } public char[] nextCharArray(int n) throws IOException { char[] array = new char[n]; String string = next(); for (int i = 0; i < n; i++) { array[i] = string.charAt(i); } return array; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
89dc3cebdbca8a3590d5065299ef552f
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.util.Scanner; public class MinOrSum { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int cases = sc.nextInt(); int sum=0; while(cases-- > 0) { int N = sc.nextInt(); while(N-- >0) { sum|=sc.nextInt(); } System.out.println(sum); sum=0; } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
88953e81e6c0bd2a335f07d745d4b63f
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
//package com.company; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class ProblemSolving { static Scanner scanner = new Scanner(System.in); static BufferedReader reader =new BufferedReader(new InputStreamReader(System.in)); //start main public static void main(String[] args) throws Exception{ int t = 1; t = Integer.parseInt(reader.readLine()); while(t-- > 0){ solve(); } }//end main //start solve public static void solve() throws IOException { //int n = Integer.parseInt(reader.readLine()); //String str = reader.readLine(); //StringTokenizer st = new StringTokenizer("this is a test"); //st.hasMoreTokens(); //st.nextToken(); StringTokenizer st; int n = Integer.parseInt(reader.readLine()); String str = reader.readLine(); st = new StringTokenizer(str); List<Integer> numbers = new ArrayList<>(); while (st.hasMoreTokens()){ numbers.add(Integer.parseInt(st.nextToken())); } // in case in one column we have more than one number with value 1 then we will make them all 0 unless one value will keep it 1 boolean isDeleteOn; for(int column=0; column<=30; column++){ isDeleteOn = false; for(int number = 0; number<n; number++){ if(isDeleteOn){ //clear the bit of corresponding column (make it 0) numbers.set(number, numbers.get(number) & ~(1<<column)); }else if( (numbers.get(number) & (1<<column)) != 0){ //to check if the bit in the corresponding column equal to 1 isDeleteOn = true; } } } long ans = 0; for(int number = 0; number<n;number++){ ans += (long)numbers.get(number); } System.out.println(ans); }//end solve }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
99af5a5cba783d399a0370a54f19c09f
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
//package com.company; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class ProblemSolving { static Scanner scanner = new Scanner(System.in); static BufferedReader reader =new BufferedReader(new InputStreamReader(System.in)); //start main public static void main(String[] args) throws Exception{ int t = 1; t = Integer.parseInt(reader.readLine()); while(t-- > 0){ solve(); } }//end main //start solve public static void solve() throws IOException { //StringTokenizer st = new StringTokenizer("this is a test"); //st.hasMoreTokens(); //st.nextToken(); StringTokenizer st; int n = Integer.parseInt(reader.readLine()); String str = reader.readLine(); st = new StringTokenizer(str); List<String> binaryCodes = new ArrayList<>(); // put binary codes in the binaryCodes list while (st.hasMoreTokens()){ binaryCodes.add( int_to_binary( Integer.parseInt(st.nextToken()) ) ); } // in case in one colunm we have more than one number with value 1 then we will make them all 0 unless one value will keep it 1 boolean isDeleteOn; for(int chr=0; chr< 31; chr++){ isDeleteOn = false; for(int code = 0; code < n; code++){ if(isDeleteOn){ char[] charArr = binaryCodes.get(code).toCharArray(); charArr[chr] = '0'; binaryCodes.set(code, new String(charArr)); }else{ if(binaryCodes.get(code).charAt(chr) == '1') isDeleteOn = true; } } } long ans = 0; for(int code = 0; code<n;code++){ ans += Long.parseLong(binaryCodes.get(code), 2); } System.out.println(ans); }//end solve public static String int_to_binary(int number){ String binaryCode = ""; while (number!=0){ if(number%2==0) binaryCode += "0"; else binaryCode += "1"; number = number/2; } while (binaryCode.length()<31) binaryCode += "0"; StringBuilder sb=new StringBuilder(binaryCode); sb.reverse(); return sb.toString(); } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
8c9cdd7221c9e250651c91ce422d5b9e
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; public class Codechef { static int[] num_to_bits = new int[] { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 }; static int countSetBitsRec(int num) { int nibble = 0; if (0 == num) return num_to_bits[0]; // Find last nibble nibble = num & 0xf; // Use pre-stored values to find count // in last nibble plus recursively add // remaining nibbles. return num_to_bits[nibble] + countSetBitsRec(num >> 4); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try { br=new BufferedReader(new FileReader("input.txt")); PrintStream out= new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); } // Catch block to handle the exceptions catch (Exception e) { br=new BufferedReader(new InputStreamReader(System.in)); } } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int computeXOR(int n) { // If n is a multiple of 4 if (n % 4 == 0) return n; // If n%4 gives remainder 1 if (n % 4 == 1) return 1; // If n%4 gives remainder 2 if (n % 4 == 2) return n + 1; // If n%4 gives remainder 3 return 0; } public static String getString(String s1,String s2) { StringBuilder sb1 = new StringBuilder(s2); while(sb1.length()<s1.length()) { sb1.insert(0,"0"); } StringBuilder ans = new StringBuilder(); for(int i=0;i<s1.length();i++) { if(s1.charAt(i) == '1' && sb1.charAt(i)=='1') ans.append("0"); else if(s1.charAt(i) == '1' && sb1.charAt(i) == '0') ans.append("1"); else ans.append("0"); } return ans.toString(); } public static void main (String[] args) throws java.lang.Exception { try{ FastReader sc=new FastReader(); BufferedWriter out = new BufferedWriter( new OutputStreamWriter(System.out)); int t=sc.nextInt(); while(t-->0) { int n = sc.nextInt(); long arr[] = new long[n]; for(int i=0;i<n;i++) { arr[i] = sc.nextLong(); } Arrays.sort(arr); for(int i=0;i<n-1;i++) { //Arrays.sort(arr); for(int j=i+1;j<n;j++) { long h = arr[i] | arr[j]; long b = ~(arr[i]); long c = h & b; if(c<arr[j]) arr[j] = c; } } long sum = 0; for(int i=0;i<n;i++) sum+=arr[i]; System.out.println(sum); } }catch(Exception e){ return; } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
0ccec42df71fb1866afa8dceeddbef08
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.io.*; import java.util.*; public class A { public static void main(String args[]){ FScanner in = new FScanner(); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); while(t-->0) { int n=in.nextInt(); long arr[]=new long[n]; for(int i=0;i<n;i++) arr[i]=in.nextLong(); long sum=arr[0]; for(int i=1;i<n;i++) sum|=arr[i]; out.println(sum); } out.close(); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static boolean checkprime(int n1) { if(n1%2==0||n1%3==0) return false; else { for(int i=5;i*i<=n1;i+=6) { if(n1%i==0||n1%(i+2)==0) return false; } return true; } } static class FScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer sb = new StringTokenizer(""); String next(){ while(!sb.hasMoreTokens()){ try{ sb = new StringTokenizer(br.readLine()); } catch(IOException e){ } } return sb.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } int nextInt(){ return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } float nextFloat(){ return Float.parseFloat(next()); } double nextDouble(){ return Double.parseDouble(next()); } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
35df48285670e8e9a4d961c102dde29c
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main (String[] args) throws java.lang.Exception { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t= sc.nextInt(); while (t-- != 0) { int n = sc.nextInt(); int[] Y = new int[n]; long sum = 0; for (int i = 0; i < n; i++) { Y[i] = sc.nextInt(); } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int orVal = Y[i] | Y[j]; if (orVal < Y[i] + Y[j]) { Y[i] = 0; Y[j] = orVal; } } } for (int i = 0; i < n; i++) { sum += Y[i]; } out.println(sum); } out.close(); } 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
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
01d2f164209ef1d0d4ecc09e85d13c26
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class test { static int[] dx = {1,-1,0,0,1,1,-1,-1}; static int[] dy = {0,0,1,-1,1,-1,1,-1}; static long e97=1000000007; public static void main(String args[]) { Scanner in = new Scanner(System.in); int times=in.nextInt(); //Pattern p2=Pattern.compile("(\\W+)|(\\w+)"); while (times-->0) { int n=in.nextInt(),res=0; for (int i = 0; i < n; i++) { res|=in.nextInt(); } System.out.println(res); } } static class node{ int order,value; public node(int value, int index) { this.value=value; order=index; } } } class Reader { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer = new StringTokenizer(""); /** 获取下一段文本 */ String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } int nextInt() throws IOException { return Integer.parseInt( next() ); } double nextDouble() throws IOException { return Double.parseDouble( next() ); } } class segTree{ /* 疑似常数过大/写假了,之后需尝试用segment【】数组重写一遍,看看能不能降到可接受的时间 现在貌似比ac时间多了几倍 */ class segment{ segment left,right; int l,r,lazy=0,length; long value=0; public segment(int a,int b){l=a;r=b;length=r-l+1;} } segment root; int source[]; public segTree(int size){root=new segment(0,size);} public segTree(int[] source){ root=new segment(0,source.length-1); this.source=source; build(root); } private void build(segment a){ if(a.l==a.r) a.value=source[a.l]; else { int mid=(a.l+a.r)/2; a.left=new segment(a.l,mid); a.right=new segment(mid+1,a.r); build(a.left); build(a.right); pushUp(a); } } private void pushUp(segment a){ a.value=Math.max(a.left.value,a.right.value); //a.value=a.left.value+a.right.value; } private void pushDown(segment a){ if(a.lazy!=0){ a.left.value=a.lazy*a.left.length; a.right.value=a.lazy*a.right.length; /*处理a的两个子节点的value变化,视实现而定*/ a.left.lazy=a.right.lazy=a.lazy; a.lazy=0; } } public void rangeModify(segment a,int start,int end,int value){ int l=a.l,r=a.r,mid=(l+r)/2; if(l>=start&&r<=end) { //a.value += value * (r - l + 1); a.value=value; //a.lazy = value; //视实现而定 return; }else if(l>end||r<start||r==l) return; if(a.left==null) a.left=new segment(l,mid); if(a.right==null) a.right=new segment(mid+1,r); //pushDown(a); if(mid>=start) rangeModify(a.left,start,end,value); if(mid<=end) rangeModify(a.right,start,end,value); pushUp(a); } public long rangeQuery(segment a, int start, int end){ int l=a.l,r=a.r; if(l>=start&&r<=end) { return a.value; }else if(r<start||l>end) return 0; //pushDown(a); return Math.max(rangeQuery(a.left,start,end),rangeQuery(a.right,start,end)); } } class Complex { //参考:https://www.jianshu.com/p/a765679b1826 private final double re; // the real part private final double im; // the imaginary part // create a new object with the given real and imaginary parts public Complex(double real, double imag) { re = real; im = imag; } //返回单位圆分成n份后,k刻度(第k个)点的虚数系下的坐标 public static Complex omega(int n,int k){ return new Complex(Math.cos(2*Math.PI*k/n),Math.sin(2*Math.PI*k/n)); } // return a string representation of the invoking Complex object @Override public String toString() { if (im == 0) return re + ""; if (re == 0) return im + "i"; if (im < 0) return re + " - " + (-im) + "i"; return re + " + " + im + "i"; } // return abs/modulus/magnitude public double abs() { return Math.hypot(re, im); } // return angle/phase/argument, normalized to be between -pi and pi public double phase() { return Math.atan2(im, re); } // return a new Complex object whose value is (this + b) public Complex plus(Complex b) { Complex a = this; // invoking object double real = a.re + b.re; double imag = a.im + b.im; return new Complex(real, imag); } // return a new Complex object whose value is (this - b) public Complex minus(Complex b) { Complex a = this; double real = a.re - b.re; double imag = a.im - b.im; return new Complex(real, imag); } // return a new Complex object whose value is (this * b) public Complex multiple(Complex b) { Complex a = this; double real = a.re * b.re - a.im * b.im; double imag = a.re * b.im + a.im * b.re; return new Complex(real, imag); } // scalar multiplication // return a new object whose value is (this * alpha) public Complex multiple(double alpha) { return new Complex(alpha * re, alpha * im); } // return a new object whose value is (this * alpha) public Complex scale(double alpha) { return new Complex(alpha * re, alpha * im); } // return a new Complex object whose value is the conjugate of this public Complex conjugate() { return new Complex(re, -im); } // return a new Complex object whose value is the reciprocal of this public Complex reciprocal() { double scale = re * re + im * im; return new Complex(re / scale, -im / scale); } // return the real or imaginary part public double re() { return re; } public double im() { return im; } // return a / b public Complex divides(Complex b) { Complex a = this; return a.multiple(b.reciprocal()); } // return a new Complex object whose value is the complex exponential of // this public Complex exp() { return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im)); } // return a new Complex object whose value is the complex sine of this public Complex sin() { return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im)); } // return a new Complex object whose value is the complex cosine of this public Complex cos() { return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im)); } // return a new Complex object whose value is the complex tangent of this public Complex tan() { return sin().divides(cos()); } // a static version of plus public static Complex plus(Complex a, Complex b) { double real = a.re + b.re; double imag = a.im + b.im; Complex sum = new Complex(real, imag); return sum; } // See Section 3.3. @Override public boolean equals(Object x) { if (x == null) return false; if (this.getClass() != x.getClass()) return false; Complex that = (Complex) x; return (this.re == that.re) && (this.im == that.im); } // See Section 3.3. @Override public int hashCode() { return Objects.hash(re, im); } } class OtherTool{ public static long[][] MatrixTranspose(long[][] matrix){ long len=matrix.length,temp=0; if(len!=matrix[0].length) throw new IllegalArgumentException(); for (int i = 0; i < len; i++) { for (int j = i; j < len; j++) { temp=matrix[i][j]; matrix[i][j]=matrix[j][i]; matrix[j][i]=temp; } } return matrix; } public static long[][] matrixMul(long[][] matrix1,long[][] matrix2, long mod){ int row = matrix1.length,column=matrix2[0].length,max=Math.max(row,column),cnt1=0,cnt2=0; long[][] temp=new long[row][column]; for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { long res=0; for (int k = 0; k < max; k++) { //if(matrix1[i][k]==1) cnt1++; //if(matrix2[k][j]==1) cnt2++; res+=matrix1[i][k]* matrix2[k][j]; res%=mod; //if(cnt1>1&&cnt2>1) {cnt1=cnt2=0;break;} } temp[i][j]=res%mod; } } return temp; } public static long[][] matrixPow(long[][] matrix,long times, long mod){ if(times==1) return matrix; int l= matrix.length; long[][] temp=new long[l][l]; for (int i = 0; i < l; i++) { temp[i][i]=1; } if(times<1) return temp; while (times>0){ if(times%2==1) temp=matrixMul(temp,matrix,mod); matrix=matrixMul(matrix,matrix,mod); times/=2; } return temp; } public static long fastPower(int source, long times, int mod){ if(times==0||source==1) return 1; long i=1,cnt=1; while (times>0){ if(times%2==1) i=(source*i)%mod; source*=source; source%=mod; times/=2; } return i; } public static long fastPowerMerge(int source,int times,int mod){ if(times==1) return source; if(source==1||times==0) return 1; long temp=fastPowerMerge(source,times/2,mod)%mod; temp=temp*temp%mod; if(times%2==1){ return temp*source%mod; }else return temp; } public static void twoArraySort(int[] a, int[] b, int l, int r) { int mid = (l + r) / 2; if (l < r - 1) { twoArraySort(a, b, l, mid); twoArraySort(a, b, mid + 1, r); } if (l == r) return; int[] temp = new int[r - l + 1]; int[] tempb = new int[r - l + 1]; int i = l, j = mid + 1, t = 0; while (i < mid + 1 && j < r + 1) { if (a[i] <= a[j]) { temp[t] = a[i]; tempb[t++] = b[i++]; } else { temp[t] = a[j]; tempb[t++] = b[j++]; } } while (i < mid + 1) { temp[t] = a[i]; tempb[t++] = b[i++]; } while (j < r + 1) { temp[t] = a[j]; tempb[t++] = b[j++]; } for (int k = l; k < r + 1; k++) { a[k] = temp[k - l]; b[k] = tempb[k - l]; } } public static String nextPermutation(String s){ if(s.length()<=1) return null; StringBuilder sb=new StringBuilder(s); int i1=-1,i2=-1; for (int i = s.length()-1; i >=0 ; i--) if(s.charAt(i-1)<s.charAt(i)) {i1=i-1;break;} if(i1==-1) return null; for (int i = s.length()-1; i >=0 ; i--) if(s.charAt(i)>s.charAt(i1)) {i2=i;break;} char c=s.charAt(i1); sb.setCharAt(i1,s.charAt(i2)); sb.setCharAt(i2,c); sb.replace(i1,s.length(),new StringBuilder(sb.substring(i1,s.length())).reverse().toString()); return sb.toString(); } public static int[] nextPermutation(int[] s){ if(s.length<=1) return null; int i1=-1,i2=-1; for (int i = s.length-1; i >0 ; i--) if(s[i-1]<s[i]) {i1=i-1;break;} if(i1==-1) return null; for (int i = s.length-1; i >=0 ; i--) if(s[i]>s[i1]) {i2=i;break;} int t=s[i1]; s[i1]=s[i2]; s[i2]=t; t=s.length-i1-1; int[] temp=new int[t]; for (int i = 0; i < t; i++) { temp[i]=s[s.length-i-1]; } System.arraycopy(temp,0,s,i1+1,t); return s; } /*public static long cantorExpansion(int[] arr){ if(arr.length>=20) return -1; HashSet<Integer> used=new HashSet<>(); HashMap<Integer,Integer> little=new HashMap<>(); long res=0; }*/ } class OT extends OtherTool{} class BinaryIndexTree{ long[] tree1,tree2; int size; public BinaryIndexTree(int size) { tree1=new long[100008]; tree2=new long[100008]; this.size=size; } public void add(int index,long value){ long p=index*value; while (index<=size) { tree1[index]+=value; tree2[index]+=p; index+=index&-index; } } public void RangeAdd(int start,int end,int value){ add(start,value); //if(end<100003) add(end+1,-value); } public long query(int start,int end){ return sum(end)-sum(start-1); } public long sum(int i){ long res=0,p=i; while (i>0) { res+=tree1[i]*(p+1)-tree2[i]; i-=i&-i; } return res; } } class BipartiteGraph implements AdjacencyTable{ ConnectRelationNode[] map; int[] matched; boolean[] used; public BipartiteGraph(int i) { map=new ConnectRelationNode[i+1]; used=new boolean[i+1];matched=new int[i+1]; Arrays.fill(matched,-1); for (int i1 = 0; i1 < map.length; i1++) { map[i1]= new ConnectRelationNode(); } } @Override public void setRoad(int start, int next, int value){ ConnectRelationNode c=new ConnectRelationNode(next,value); c.nextConnectNode=map[start].nextConnectNode; map[start].nextConnectNode=c; } public int Hungarian(){ int maxWays=0; for (int i = 0; i < map.length; i++) { Arrays.fill(used,true); if(map[i].nextConnectNode==null)continue; if(canMatch(i)) maxWays++; } return maxWays; } public boolean canMatch(int from){ ConnectRelationNode n=map[from].nextConnectNode; int to=n.nowConnectNode; while (!used[to]&&n.nextConnectNode!=null) {n=n.nextConnectNode;to=n.nowConnectNode;} if(used[to]){ used[to]=false; if(matched[to]==-1||canMatch(matched[to])){ matched[to]=from; return true; } } return false; } } interface AdjacencyTable{ class ConnectRelationNode { int nowConnectNode,value; ConnectRelationNode nextConnectNode=null; public ConnectRelationNode(int i, int v) { nowConnectNode = i; value = v; } public ConnectRelationNode() {} public void setNext(ConnectRelationNode n){nextConnectNode=n;} } void setRoad(int i,int j,int k); } class ShortestRoad implements AdjacencyTable{ class QueueNode { int city,value; public QueueNode(int c, int v){city=c;value=v;} } ConnectRelationNode[] map; int[] city; int[][] f; public ShortestRoad(int i) { city = new int[i]; //at=new AdjacencyTable(i); map=new ConnectRelationNode[i]; for (int i1 = 0; i1 < map.length; i1++) { map[i1]= new ConnectRelationNode(); } } public ShortestRoad(int[][] f, int i) { city = new int[i]; this.f=f; } @Override public void setRoad(int start, int next, int value){ ConnectRelationNode c=new ConnectRelationNode(next,value); c.nextConnectNode=map[start].nextConnectNode; map[start].nextConnectNode=c; } public int dijkstra(int start ,int end){ int[] distance=new int[city.length],used=new int[city.length]; PriorityQueue<QueueNode> node=new PriorityQueue<QueueNode>(city.length, Comparator.comparingInt(o -> o.value)); Arrays.fill(distance,Integer.MAX_VALUE/2); node.add(new QueueNode(start,0)); distance[start]=0; while (!node.isEmpty()){ int city=node.poll().city; if(used[city]!=0) continue; used[city]=1; ConnectRelationNode n=map[city].nextConnectNode; if(n==null) return -1; while (true){ int nowcity=n.nowConnectNode; if(distance[nowcity]>distance[city]+n.value){ distance[nowcity]=distance[city]+n.value; node.offer(new QueueNode(nowcity,distance[nowcity])); } if(n.nextConnectNode==null) break; n=n.nextConnectNode; } } if(distance[end]!=Integer.MAX_VALUE/2) return distance[end]; else return Integer.MAX_VALUE; } public void floyd(int max){ for(int k=1; k<=max; k++) for(int i=1; i<=max; i++) if(f[i][k]!=Integer.MAX_VALUE/2) for(int j=1; j<=max; j++) f[i][j] = Math.min(f[i][j], f[i][k] + f[k][j]); } public int askFloyd(int i,int j){ return f[i][j]; } } class Pack { //含01、完全、多重背包,混合背包可并入多重,二维待定 int[] weight, value, multiple; int nums; public Pack(int nums) { weight = new int[nums]; value = new int[nums]; } public Pack(int[] w, int[] v) { weight = w; value = v; nums = weight.length; } public Pack(int[] w, int[] v, int[] m) { weight = w; value = v; multiple = m; nums = weight.length; } public int ZeroOnePack(int volume) { //dp[i] = Math.max(dp[i-weight[k]]+value[k], dp[i]); //dp【i】为容积使用i时的最大价值,考虑前k种物品的部分由循环实现 //确保没有物品放入多次,i从大到小————由没有选择k物品的状态转移而来 int[] dp = new int[volume + 8],hash=new int[9003]; for (int k = 0; k < nums; k++) { for (int i = volume; i > 0; i--) { hash[Math.max(0,dp[i])]=1; if(i>=weight[k]) hash[Math.max(0,dp[i - weight[k]] + value[k])]=1; if (i >= weight[k]) dp[i] = Math.max(dp[i - weight[k]] + value[k], dp[i]); } } for (int i =1; i < hash.length; i++) { if(hash[i]==0) {System.out.println(i);break;} } return dp[volume]; } public int CompletePack(int volume) { //与01的差别为i从小到大————每个状态可以由已经选中了k物品的状态转移而来 int[] dp = new int[volume + 1]; for (int k = 0; k < nums; k++) { for (int i = weight[k]; i < volume + 1; i++) { if (i >= weight[k]) dp[i] = Math.max(dp[i - weight[k]] + value[k], dp[i]); } } return dp[volume]; } public int MultiplePack(int volume) { //多重背包,将每种物品可选的数量二进制化:用1/2/4/8……拼出,化为O(lgn)个新的物品 ArrayList<Integer> w = new ArrayList<>(); ArrayList<Integer> v = new ArrayList<>(); for (int i = 0; i < multiple.length; i++) { int k = multiple[i]&-multiple[i]; while (true) { if (k <= multiple[i]) { w.add(i, k * weight[i]); v.add(i, k * value[i]); multiple[i] -= k; k *= 2; } else { k = multiple[i]; if(k!=0) { w.add(i, k * weight[i]); v.add(i, k * value[i]); } break; } } } int[] w1 = new int[w.size()], v1 = new int[v.size()]; for (int i = 0; i < w1.length; i++) { w1[i] = w.get(i); v1[i] = v.get(i); } weight = w1; value = v1; nums = weight.length; return ZeroOnePack(volume); } } class DisJointSet { int[] parent, rank; int unConnectNum; public DisJointSet(int size) { parent = new int[size]; rank = new int[size]; for (int i = 0; i < size; i++) parent[i] = i; Arrays.fill(rank, 1); unConnectNum = size; } private int find(int a) { if (a > parent.length || a < 0) { throw new IllegalArgumentException("FindIndexOutOfBounds:" + a); } while (parent[a] != a) { //路径压缩,两步一跳 parent[a] = parent[parent[a]]; a = parent[a]; } return a; } public boolean isConnect(int a, int b) { return find(a) == find(b); } public int union(int a, int b) { //按秩合并,仅当相同时高度+1;find会改变秩,仅作参考,是否会造成错误待定 int aroot = find(a); int broot = find(b); if (aroot == broot) return aroot; unConnectNum--; if (rank[aroot] > rank[broot]) { parent[broot] = aroot; return aroot; } else if (rank[aroot] < rank[broot]) { parent[aroot] = broot; return broot; } else { parent[broot] = aroot; rank[aroot]++; return aroot; } } } class Game{ /* //n为一次最多走的步数,m为可走的总步数(牌数),go为一次可以走的数量(不一定连续) int[] used,sg; public int sg(int i){ //boolean[] used =new boolean[1003]; for (int j : go) { if(j>n||i+j>m)break; if(sg[i+j]==-1){ used[sg(i+j)]=true; }else used[m-sg[i+j]]=true; } for (int k = 0; k <m; k++) { if(!used[m-k]) {sg[i]=k;return m-k;} } return 0; } */ } /*class Reader { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer = new StringTokenizer(""); String nextLine() throws IOException {// 读取下一行字符串 return reader.readLine(); } String next() throws IOException {// 读取下一个字符串 while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } int nextInt() throws IOException {// 读取下一个int型数值 return Integer.parseInt(next()); } double nextDouble() throws IOException {// 读取下一个double型数值 return Double.parseDouble(next()); } }*/ class GeneratingFunction{ ArrayList<Double>[] formulaCoefficient; int[] addPowerNum,less,more; int nowIndex=0,size; boolean isTwoPower=false; public GeneratingFunction(int size){ //size:括号数 //formulaCoefficient =new ArrayList[size]; this.size=size; less=new int[size]; more=new int[size]; addPowerNum=new int[size]; } public int setAdd(int a,int b,int c){ addPowerNum[nowIndex]=a; less[nowIndex]=b; more[nowIndex]=c; return nowIndex++; } public void setCoeIndex(int index,int value,int max){ //int num=max/addPowerNum[index]; ArrayList<Double> now=new ArrayList<Double>(max); now.add(0,1.0); for (int i = 1; i < max; i++) { now.add(i,value*i*1.0); } formulaCoefficient[index]=now; } public double[] Generate(int ask){ double[] a=new double[13],b=new double[13],fact=new double[12]; for (int i = 0; i < 11; i++) { int k=1; for (int j = i; j >0 ; j--) { k*=j; } fact[i]=k; } for (int i = 0; i <= more[0]; i+=addPowerNum[0]) { a[i]=1.0/fact[i]; }//构造第一个括号中多项式 for (int i = 1; i <= size; i++) { if(addPowerNum[i]==0) break; //if(addPowerNum[i]>size) break; /*for (int j = 0; j <= more[i]; j+=addPowerNum[i]) { b[j]=1.0/fact[j]; } double[] temp=new double[13];*/ for (int j = 0; j <= ask; j++) { //if(a[j]!=0) for (int k = 0; k+j<=ask&&k <= more[i]; k+=addPowerNum[i]) { b[j+k]+=a[j]/fact[k]; } } a=Arrays.copyOfRange(b,0,b.length); Arrays.fill(b,0); } for (int i = 0; i <= ask; i++) { a[i]*=fact[i]; } return a; } /** * fft * 未完成,之后待完善,暂时母函数使用n2复杂度实现 */ /* 完成非递归fft的下标二进制翻转预处理 */ public void fftChange(Complex[] a){ } /* fft,on为1时是dft,为-1时是idft 参考:https://blog.csdn.net/qq_37136305/article/details/81184873 https://www.cnblogs.com/RabbitHu/p/FFT.html https://www.jianshu.com/p/a765679b1826 */ /*public Complex[] FFT(Complex[] a,int on){ }*/ public void FFTRecursion(Complex[] a,int start,int end,int on){ int n=end-start+1,m=n/2,mid=(start+end)/2; if(n<=1) return; Complex[] buffer=new Complex[n]; for (int i = 0; i < m; i++) { buffer[i]=a[start+2*i]; buffer[i+m]=a[start+2*i+1]; } for (int i = 0; i < n; i++) a[start+i]=buffer[i]; FFTRecursion(a,start,mid,1); FFTRecursion(a,mid+1,end,1); for (int i = 0; i < m; i++) { //若on为1,x为w(k,n),实现dft,此处的on无影响 //若为-1则x为w(-k,n)即为w(k,n)的倒数,后面加一个判断:当为-1时/n,则可实现idft Complex x = Complex.omega(n, i * on); buffer[i] = a[i].plus(x.multiple(a[i + m])); buffer[i + m] = a[i].minus(x.multiple(a[i + m])); } if(on==-1){ for (int i = 0; i < buffer.length; i++) { buffer[i]=buffer[i].scale(1D/a.length); } } for (int i = 0; i < n; i++) { a[start+i]=buffer[i]; } } public static Complex[] fft(Complex[] x) { int N = x.length; // base case if (N == 1) return new Complex[]{x[0]}; // radix 2 Cooley-Tukey FFT if (N % 2 != 0) { throw new RuntimeException("N is not a power of 2"); } // fft of even terms Complex[] even = new Complex[N / 2]; for (int k = 0; k < N / 2; k++) { even[k] = x[2 * k]; } Complex[] q = fft(even); // fft of odd terms Complex[] odd = even; // reuse the array for (int k = 0; k < N / 2; k++) { odd[k] = x[2 * k + 1]; } Complex[] r = fft(odd); // combine Complex[] y = new Complex[N]; for (int k = 0; k < N / 2; k++) { double kth = -2 * k * Math.PI / N; Complex wk = new Complex(Math.cos(kth), Math.sin(kth)); y[k] = q[k].plus(wk.multiple(r[k])); y[k + N / 2] = q[k].minus(wk.multiple(r[k])); } return y; } public static Complex[] ifft(Complex[] x) { int N = x.length; Complex[] y = new Complex[N]; // take conjugate for (int i = 0; i < N; i++) { y[i] = x[i].conjugate(); } // compute forward FFT y = fft(y); // take conjugate again for (int i = 0; i < N; i++) { y[i] = y[i].conjugate(); } // divide by N for (int i = 0; i < N; i++) { y[i] = y[i].scale(1.0 / N); } return y; } public static Complex[] cconvolve(Complex[] x, Complex[] y) { // should probably pad x and y with 0s so that they have same length // and are powers of 2 if (x.length != y.length) { throw new RuntimeException("Dimensions don't agree"); } int N = x.length; // compute FFT of each sequence,求值 Complex[] a = fft(x); Complex[] b = fft(y); // point-wise multiply,点值乘法 Complex[] c = new Complex[N]; for (int i = 0; i < N; i++) { c[i] = a[i].multiple(b[i]); } // compute inverse FFT,插值 return ifft(c); } public static Complex[] convolve(Complex[] x, Complex[] y) { Complex ZERO = new Complex(0, 0); Complex[] a = new Complex[2*x.length];//2n次数界,高阶系数为0. for (int i = 0; i < x.length; i++) a[i] = x[i]; for (int i = x.length; i < 2*x.length; i++) a[i] = ZERO; Complex[] b = new Complex[2*y.length]; for (int i = 0; i < y.length; i++) b[i] = y[i]; for (int i = y.length; i < 2*y.length; i++) b[i] = ZERO; return cconvolve(a, b); } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
5e4393a1639d7ee9c3837d04939b35af
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
// letsgoooooooooooooooooooooooooooooooo import java.util.*; import java.io.*; public class Solution{ static int MOD=1000000007; static PrintWriter pw; static FastReader sc; 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());} public char nextChar() throws IOException {return next().charAt(0);} String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int[] intArr(int a,int b,int n) throws IOException {int[]in=new int[n];for(int i=a;i<b;i++)in[i]=nextInt();return in;} } static void Solve() throws Exception{ int n =sc.nextInt(); int [] arr=new int [n]; int ans=0; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); ans|= arr[i]; } pw.println(ans); } static int highestPowerof2(int x) { // check for the set bits x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; // Then we remove all but the top bit by xor'ing the // string of 1's with that string of 1's shifted one to // the left, and we end up with just the one top bit // followed by 0's. return x ^ (x >> 1); } public static void main(String[] args) throws Exception{ try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { System.err.println("Error"); } sc= new FastReader(); pw = new PrintWriter(System.out); int t=1; t=sc.nextInt(); for(int ii=1;ii<=t;ii++) { Solve(); } pw.flush(); } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
3f01365ceedc63188e89623974c31bae
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class temp { public static void main(String[] args) throws IOException { // File input = new File("input.txt"); // BufferedReader br = new BufferedReader(new FileReader(input)); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { // ------------------------------------------------------------ int t=Integer.parseInt(br.readLine()); while(t-->0) { int n=Integer.parseInt(br.readLine()); String arr[]=br.readLine().split(" "); int ans=Integer.parseInt(arr[0]); for(int i=1;i<n;i++) { ans=ans|Integer.parseInt(arr[i]); } System.out.println(ans); } // ------------------------------------------------------------ } catch (Exception e) { } } static int fastPow(int base, int exp, int m) { if (exp == 0) return 1; int half = fastPow(base, exp / 2, m); if (exp % 2 == 0) return mul(half, half, m); return mul(half, mul(half, base, m), m); } static int mul(int a, int b, int m) { return a * b % m; } static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static int partition(int[] arr, int low, int high) { int pivot = arr[high]; int i = (low - 1); for (int j = low; j <= high - 1; j++) { if (arr[j] < pivot) { i++; swap(arr, i, j); } } swap(arr, i + 1, high); return (i + 1); } static void sort(int[] arr, int low, int high) { if (low < high) { int pi = partition(arr, low, high); sort(arr, low, pi - 1); sort(arr, pi + 1, high); } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
7bd14c0b5fe9a3eeeca122de99df2ec1
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Random; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.util.function.BiFunction; public class Solution { // VM options: -Xmx1024m -Xss1024m private void solve() throws IOException { int n = nextInt(); int[] a = nextIntArray(n); int t = 0; for (int i : a) { t |= i; } out.println(t); } private static final String inputFilename = "input.txt"; private static final String outputFilename = "output.txt"; private BufferedReader in; private StringTokenizer line; private PrintWriter out; private final boolean isDebug; private Solution(boolean isDebug) { this.isDebug = isDebug; } public static void main(String[] args) throws IOException { new Solution(Arrays.asList(args).contains("DEBUG_MODE")).run(args); } private void run(String[] args) throws IOException { if (isDebug) { in = new BufferedReader(new InputStreamReader(new FileInputStream(inputFilename))); // in = new BufferedReader(new InputStreamReader(System.in)); } else { in = new BufferedReader(new InputStreamReader(System.in)); } out = new PrintWriter(System.out); // out = new PrintWriter(outputFilename); // int t = isDebug ? nextInt() : 1; int t = nextInt(); // int t = 1; for (int i = 0; i < t; i++) { // out.print("Case #" + (i + 1) + ": "); solve(); out.flush(); } in.close(); out.flush(); out.close(); } private void println(Object... objects) { boolean isFirst = true; for (Object o : objects) { if (!isFirst) { out.print(" "); } else { isFirst = false; } out.print(o.toString()); } out.println(); } private int[] nextIntArray(int n) throws IOException { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = nextInt(); } return res; } private long[] nextLongArray(int n) throws IOException { long[] res = new long[n]; for (int i = 0; i < n; i++) { res[i] = nextLong(); } return res; } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private long nextLong() throws IOException { return Long.parseLong(nextToken()); } private double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } private String nextToken() throws IOException { while (line == null || !line.hasMoreTokens()) { line = new StringTokenizer(in.readLine()); } return line.nextToken(); } private static void assertPredicate(boolean p) { if (!p) { throw new RuntimeException(); } } private static void assertPredicate(boolean p, String message) { if (!p) { throw new RuntimeException(message); } } private static void assertNotEqual(int unexpected, int actual) { if (actual == unexpected) { throw new RuntimeException("assertNotEqual: " + unexpected + " == " + actual); } } private static void assertEqual(int expected, int actual) { if (expected != actual) { throw new RuntimeException("assertEqual: " + expected + " != " + actual); } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
a30f88b9ceaeed238173e51d069a4487
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.StringTokenizer; public class Min_Or_Sum { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) { return -ret; } return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) { buffer[0] = -1; } } private byte read() throws IOException { if (bufferPointer == bytesRead) { fillBuffer(); } return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) { return; } din.close(); } } public static void main(String Args[]) throws java.lang.Exception { try { Reader obj = new Reader(); int t = obj.nextInt(); while (t > 0) { t--; int n = obj.nextInt(); int arr[] = new int[n]; int bits[] = new int[31]; int i; long sum = 0; for (i=0;i<n;i++) { arr[i] = obj.nextInt(); sum += arr[i]; } for (i=0;i<n;i++) { int j = 0; while (arr[i] > 0) { if ((arr[i] & 1) == 1) { bits[j]++; } arr[i] = arr[i] >> 1; j++; } } for (i=0;i<31;i++) { if (bits[i] > 1) { bits[i]--; sum -= bits[i] * (long)Math.pow(2,i); } } System.out.println (sum); } }catch(Exception e){ return; } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
374d4c71a0e93054b7f6bab6f62a8615
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.awt.*; import java.util.*; import java.io.*; // You have that in you, you can do it........ public class Codeforces { static FScanner sc = new FScanner(); static PrintWriter out = new PrintWriter(System.out); static final Random random = new Random(); static long mod = 1000000007L; static HashMap<String, Integer> map = new HashMap<>(); static boolean[] sieve = new boolean[1000000]; static double[] fib = new double[1000000]; public static void main(String args[]) throws IOException { int T = sc.nextInt(); while (T-- > 0) { int n = sc.nextInt(); int[] a = new int[n]; long s=0; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { long s1=a[i]+a[j]; long s2=a[i]|a[j]; if(s2<s1) { a[i]=0; a[j]=(int)s2; } } } for(int i=0;i<n;i++) s+=a[i]; out.println(s); } out.close(); } // TemplateCode static void fib() { fib[0] = 0; fib[1] = 1; for (int i = 2; i < fib.length; i++) fib[i] = fib[i - 1] + fib[i - 2]; } static void primeSieve() { for (int i = 0; i < sieve.length; i++) sieve[i] = true; for (int i = 2; i * i <= sieve.length; i++) { if (sieve[i]) { for (int j = i * i; j < sieve.length; j += i) { sieve[j] = false; } } } } static int max(int a, int b) { if (a < b) return b; return a; } static int min(int a, int b) { if (a < b) return a; return b; } static void ruffleSort(int[] a) { int n = a.length; 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 <E> void print(E res) { System.out.println(res); } static int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static int abs(int a) { if (a < 0) return -1 * a; return a; } static class FScanner { BufferedReader br; StringTokenizer st; public FScanner() { 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; } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
01855efb3def45c11c00bdca2c02a2d1
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.util.*; import java.io.*; import java.io.*; import java.util.*; import java.math.*; import static java.lang.Math.sqrt; import static java.lang.Math.floor; public class topcoder { public static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode() {} TreeNode(int val) { this.val = val; } TreeNode(int val, TreeNode left, TreeNode right) { this.val = val; this.left = left; this.right = right; } static int x = -1; static int y = -1; public static int first_search(TreeNode root, TreeNode main_root) { if(root == null) return 0; int a = first_search(root.left,main_root); int b = first_search(root.right,main_root); if(a > main_root.val) x = a; if(b < main_root.val) y = b; return root.val; } public static void fix(TreeNode root, TreeNode main_root) { if(root == null) return; fix(root.left,main_root); if(root.val > main_root.val) { root.val = y; } fix(root.right,main_root); if(root.val < main_root.val); root.val = x; } public static int max(int []nums, int s, int e) { int max = Integer.MIN_VALUE; for(int i = s; i <= e; i++) { max = Math.max(max, nums[i]); } return max; } public static TreeNode new_node(int []nums, int s, int e) { int max = max(nums,s,e); TreeNode node = new TreeNode(max); return node; } public static TreeNode root; public static void res(int []nums, int s, int e) { if(s > e)return; if(root == null) { root = new_node(nums,s,e); } root.left = new_node(nums,s,e); root.right = new_node(nums,s,e); return; } static int depth(TreeNode root) { if(root == null)return 0; int a = 1+ depth(root.left); int b = 1+ depth(root.right); return Math.max(a,b); } static HashSet<Integer>set = new HashSet<>(); static void deepestLeaves(TreeNode root, int cur_depth, int depth) { if(root == null)return; if(cur_depth == depth)set.add(root.val); deepestLeaves(root.left,cur_depth+1,depth); deepestLeaves(root.right,cur_depth+1,depth); } public static void print(TreeNode root) { if(root == null)return; System.out.print(root.val+" "); System.out.println("er"); print(root.left); print(root.right); } public static HashSet<Integer>original(TreeNode root){ int d = depth(root); deepestLeaves(root,0,d); return set; } static HashSet<Integer>set1 = new HashSet<>(); static void leaves(TreeNode root) { if(root == null)return; if(root.left == null && root.right == null)set1.add(root.val); leaves(root.left); leaves(root.right); } public static boolean check(HashSet<Integer>s, HashSet<Integer>s1) { if(s.size() != s1.size())return false; for(int a : s) { if(!s1.contains(a))return false; } return true; } static TreeNode subTree; public static void smallest_subTree(TreeNode root) { if(root == null)return; smallest_subTree(root.left); smallest_subTree(root.right); set1 = new HashSet<>(); leaves(root); boolean smallest = check(set,set1); if(smallest) { subTree = root; return; } } public static TreeNode answer(TreeNode root) { smallest_subTree(root); return subTree; } } static class pair{ long first; long second; public pair(long first, long second) { this.first = first; this.second = second; } public long compareTo(pair p) { if(first == p.first)return second-p.second; return first-p.first; } } static class Compare{ static void compare(ArrayList<pair>arr) { Collections.sort(arr,new Comparator<pair>() { public int compare(pair p1, pair p2) { return (int) (p1.first-p2.first); } }); } } public static HashMap<Integer,Integer>sortByValue(HashMap<Integer,Integer>hm){ List<Map.Entry<Integer,Integer>>list = new LinkedList<Map.Entry<Integer,Integer>>(hm.entrySet()); Collections.sort(list,new Comparator<Map.Entry<Integer,Integer>>(){ public int compare(Map.Entry<Integer,Integer>o1, Map.Entry<Integer,Integer>o2) { return (o1.getValue()).compareTo(o2.getValue()); }}); HashMap<Integer,Integer>temp = new LinkedHashMap<Integer,Integer>(); for(Map.Entry<Integer,Integer>aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static class pairr implements Comparable<pairr>{ static Long value; Long index; public pairr(Long value, Long index) { this.value = value; this.index = index; } public int compareTo(pairr o) { return (int)(value-o.value); } } static class Key<K1, K2> { public K1 key1; public K2 key2; public Key(K1 key1, K2 key2) { this.key1 = key1; this.key2 = key2; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Key key = (Key) o; if (key1 != null ? !key1.equals(key.key1) : key.key1 != null) { return false; } if (key2 != null ? !key2.equals(key.key2) : key.key2 != null) { return false; } return true; } @Override public int hashCode() { int result = key1 != null ? key1.hashCode() : 0; result = 31 * result + (key2 != null ? key2.hashCode() : 0); return result; } @Override public String toString() { return "[" + key1 + ", " + key2 + "]"; } } public static int sumOfDigits (long n) { int sum = 0; while(n > 0) { sum += n%10; n /= 10; } return sum; } public static long binary_search(int s, int e, long num, long []ar) { if(s > e) { return -1; } int mid = (s+e)/2; if(s == e && ar[s] >= num) { return ar[s]; }else if(s == e && ar[s] < num) { return -1; }else if(ar[mid] < num) { return binary_search(mid+1,e,num,ar); }else if(ar[mid] >= num) { return binary_search(s,mid,num,ar); } return -1; } public static int index_search(int s, int e, long num, long []ar) { if(s > e) { return -1; } int mid = (s+e)/2; if(s == e && ar[s] >= num) { return s; }else if(s == e && ar[s] < num) { return -1; }else if(ar[mid] < num) { return index_search(mid+1,e,num,ar); }else if(ar[mid] >= num) { return index_search(s,mid,num,ar); } return -1; } public static void swap(int []ar, int i, int j) { for(int k= j; k >= i; k--) { int temp = ar[k]; ar[k] = ar[k+1]; ar[k+1] = temp; } } public static boolean digit_exists(long n) { while(n > 0) { if(n%10 == 9) return true; n = n/10; } return false; } public static int log(int n) { int c = 0; while(n > 0) { c++; n /=2; } return c; } public static int findOr(int[]bits){ int or=0; for(int i=0;i<32;i++){ or=or<<1; if(bits[i]>0) or=or+1; } return or; } static void simpleSieve(int limit, Vector<Integer> prime) { // Create a boolean array "mark[0..n-1]" and initialize // all entries of it as true. A value in mark[p] will // finally be false if 'p' is Not a prime, else true. boolean mark[] = new boolean[limit+1]; for (int i = 0; i < mark.length; i++) mark[i] = true; for (int p=2; p*p<limit; p++) { // If p is not changed, then it is a prime if (mark[p] == true) { // Update all multiples of p for (int i=p*p; i<limit; i+=p) mark[i] = false; } } // Print all prime numbers and store them in prime for (int p=2; p<limit; p++) { if (mark[p] == true) { prime.add(p); } } } // Prints all prime numbers smaller than 'n' public static void segmentedSieve(int n, ArrayList<Integer>l) { // Compute all primes smaller than or equal // to square root of n using simple sieve int limit = (int) (floor(sqrt(n))+1); Vector<Integer> prime = new Vector<>(); simpleSieve(limit, prime); // Divide the range [0..n-1] in different segments // We have chosen segment size as sqrt(n). int low = limit; int high = 2*limit; // While all segments of range [0..n-1] are not processed, // process one segment at a time while (low < n) { if (high >= n) high = n; // To mark primes in current range. A value in mark[i] // will finally be false if 'i-low' is Not a prime, // else true. boolean mark[] = new boolean[limit+1]; for (int i = 0; i < mark.length; i++) mark[i] = true; // Use the found primes by simpleSieve() to find // primes in current range for (int i = 0; i < prime.size(); i++) { // Find the minimum number in [low..high] that is // a multiple of prime.get(i) (divisible by prime.get(i)) // For example, if low is 31 and prime.get(i) is 3, // we start with 33. int loLim = (int) (floor(low/prime.get(i)) * prime.get(i)); if (loLim < low) loLim += prime.get(i); /* Mark multiples of prime.get(i) in [low..high]: We are marking j - low for j, i.e. each number in range [low, high] is mapped to [0, high-low] so if range is [50, 100] marking 50 corresponds to marking 0, marking 51 corresponds to 1 and so on. In this way we need to allocate space only for range */ for (int j=loLim; j<high; j+=prime.get(i)) mark[j-low] = false; } // Numbers which are not marked as false are prime for (int i = low; i<high; i++) if (mark[i - low] == true) l.add(i); // Update low and high for next segment low = low + limit; high = high + limit; } } public static int find_indexNum(long k) { long k1 = k; int power = 0; while(k > 0) { power++; k /=2 ; } long check = (long)Math.pow(2, power-1); if(k1 == check) { return power; } // System.out.println(power); long f = (long)Math.pow(2, power-1); long rem = k1-f; return find_indexNum(rem); } public static void shuffle(int []array, int num,int t_index, boolean []vis, int m ) { for(int i = 0; i < m; i++) { if(vis[i] == false) { int temp = array[i]; if(i < t_index) { vis[i] = true; } array[i] = num; array[t_index] = temp; // System.out.println(array[t_index]+" "+array[i]); break; } } } public static void rotate(int []arr,int j, int times, int m) { if(j == 0) { int temp1 = arr[0]; arr[0] = arr[times]; arr[times] = temp1; }else { int temp = arr[j]; int z = arr[0]; arr[0] = arr[times]; arr[j] = z; arr[times] = temp; } } public static void recur(int i,int A, int B,int []dp,int []metal, int n, boolean took,int ind) { if(i-A <= 0 && i-B <= 0)return; int count = 0; for(int j = 1; j <= n; j++) { if(dp[j] >= metal[j]) { count++; } } if(count == n)return; if(i-A >= 0 && i-B >= 0 && dp[i] > 0 && dp[i] > metal[i]) { dp[i]--; dp[i-A]++; dp[i-B]++; } if(ind == 6) { // System.out.println(Arrays.toString(dp)); } recur(i-A,A,B,dp,metal,n,took,ind); recur(i-B,A,B,dp,metal,n,took,ind); } public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static boolean[] getSieve(int n) { boolean[] isPrime = new boolean[n+1]; for (int i = 2; i <= n; i++) isPrime[i] = true; for (int i = 2; i*i <= n; i++) if (isPrime[i]) for (int j = i; i*j <= n; j++) isPrime[i*j] = false; return isPrime; } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } public static void dfs(LinkedList<Integer>[]list, HashMap<Integer,Integer>map, int parent, int n) { Stack<Integer>st = new Stack<>(); } public static boolean pos(int n) { int i = 1; boolean pos = false; while(i*i <= n) { if(i*i*2 == n || i*i*4 == n) { pos = true; break; } i++; } if(pos)return true; return false; } static long count = 0; public static void pairs (int []ar, int s, int e) { if(e <= s)return; // System.out.println(ar[s]+" "+ar[e]+" "+s+" "+e); if(ar[e]-ar[s] == e-s) { count++; //System.out.println("sdf"); } pairs(ar,s+1,e); pairs(ar,s,e-1); } public static class Pair1 implements Comparable <Pair1>{ int value; int index; public Pair1(int value, int index) { this.value = value; this.index = index; } public int compareTo(Pair1 o) { return o.value-value; } } public static long ways(long n) { return (n*(n-1))/2; } static boolean isPrime(long n) { if(n <= 1)return false; if(n <= 3)return true; if(n%2 == 0 || n%3 == 0)return false; for(int i = 5; i*i <= n; i+= 6) { if(n%i == 0 || n%(i+2) == 0) return false; } return true; } static long nextPrime(long n) { boolean found = false; long prime = n; while(!found) { prime++; if(isPrime(prime)) found = true; } return prime; } public static boolean isValid(int h, int m, int hour, int minute) { int a = flip(hour / 10); if (a == -1) return false; int b = flip(hour % 10); if (b == -1) return false; int c = flip(minute / 10); if (c == -1) return false; int d = flip(minute % 10); if (d == -1) return false; if (10 * d + c >= h) return false; if (10 * b + a >= m) return false; return true; } public static int flip(int x) { if (x == 0) return 0; if (x == 1) return 1; if (x == 2) return 5; if (x == 5) return 2; if (x == 8) return 8; return -1; } static long maximum(long a, long b, long c, long d) { long m = Math.max(a, b); long m1 = Math.max(c, d); return Math.max(m1, m1); } static long minimum(long a, long b, long c,long d) { long m = Math.min(a, b); long m1 = Math.min(c, d); return Math.min(m, m1); } static long ans = 0; public static void solve1(boolean [][]vis,long [][]mat, int r, int c, int r2, int c2, int r1, int c1, int r3, int c3) { if(r > r1 || c > c1 || r > r2 || c > c2 || r1 > r3 || c1 > c3 || r3 < r2 || c3 < c2 || vis[r][c] || vis[r1][c1]|| vis[r2][c2] || vis[r3][c3]) return; vis[r][c] = true; vis[r1][c1] = true; vis[r2][c2] = true; vis[r3][c3] = true; long max = maximum(mat[r][c],mat[r1][c1],mat[r2][c2],mat[r3][c3]); long min = minimum(mat[r][c],mat[r1][c1],mat[r2][c2],mat[r3][c3]); long a = mat[r][c]; long b = mat[r1][c1]; long c4 = mat[r2][c2]; long d =mat[r3][c3]; long []p = {a,b,c4,d}; Arrays.sort(p); long temp = (p[2]+p[3]-p[0]-p[1]); if(r == r1 && r == r2 && r2 == r3 && r1 == r3) temp /= 2; System.out.println(Arrays.toString(p)); ans += temp; solve1(vis,mat,r+1,c,r2+1,c2,r1-1,c1,r3-1,c3); solve1(vis,mat,r,c+1,r2,c2-1,r1,c1+1,r3,c3-1); solve1 (vis,mat,r+1,c+1,r2+1,c2-1,r1-1,c1+1,r3-1,c3-1); } private static int solve(int[][] mat, int i, int c, int j, int c2, int k, int c1, int l, int c3) { // TODO Auto-generated method stub return 0; } public static int dfs(int parent, LinkedList<Integer>[]list) { for(int i : list[parent]) { if(list[parent].size() == 0) { return 0; }else { return 1 + dfs(i,list); } } return 0; } public static long answer = Integer.MAX_VALUE; public static void min_Time(int [][]dp, int i, HashSet<Integer>set, int min, int r, int c) { if(i > r) { answer = Math.min(answer, min); return; } if(min > answer)return; for(int j = i; j <= c; j++) { if(!set.contains(j)) { set.add(j); min += dp[i][j]; min_Time(dp,i+1,set,min,r,c); min -= dp[i][j]; set.remove(j); } } } public static void dp(int [][]dp, int r, int c, int o, int z, long sum) { if(r > o) { answer = Math.min(answer, sum); } if(r > o || c > z) { return; } if(sum > answer)return; sum += dp[r][c]; dp(dp,r+1,c+1,o,z,sum); sum -= dp[r][c]; dp(dp,r,c+1,o,z,sum); } static HashSet<ArrayList<Integer>>l = new HashSet<>(); public static void fourSum(Deque<Integer>ll, int i, int target, int []ar, int n) { if(ll.size() == 4) { int sum = 0; ArrayList<Integer>list = new ArrayList<>(); for(int a : ll) { sum += a; list.add(a); } if(sum == target) { Collections.sort(list); l.add(list); // System.out.println(ll); } return; } for(int j = i; j < n; j++) { ll.add(ar[j]); fourSum(ll,j+1,target,ar,n); ll.removeLast(); } } static int max_bottles(int cur, int exchange, int n){ if(cur == exchange){ cur = 0; n++; } if(n == 0)return 0; return 1+ max_bottles(cur+1,exchange,n-1); } public static void fill(int [][]mat, List<Integer>ans, int row_start, int row_end, int col_start, int col_end) { for(int i = col_start; i <= col_end; i++) { ans.add(mat[row_start][i]); } for(int i = row_start+1; i <= row_end; i++) { ans.add(mat[i][col_end]); } if(col_start == col_end)return; if(row_start == row_end)return; for(int i = col_end-1; i >= col_start; i--) { ans.add(mat[row_end][i]); } for(int i = row_end-1; i >= row_start+1; i--) { ans.add(mat[i][col_start]); } } public static void create(int [][]mat, int j, int i, int k) { if(i < 1 || j >= mat.length)return; mat[j][i] = k; create(mat,j+1,i-1,k+1); } public static long sum(int [][]mat, int x1, int y1, int x2, int y2) { long sum = 0; while(x1 <= x2) { sum += mat[x1][y1]; // System.out.println(mat[x1][y1]); x1++; } y1++; while(y1 <= y2) { sum += mat[x2][y1]; y1++; } return sum; } public static boolean allneg(int []ar, int n) { for(int i = 0; i < n; i++) { if(ar[i] >= 0)return false; } return true; } public static boolean allpos(int []ar, int n) { for(int i = 0; i < n; i++) { if(ar[i] <= 0)return false; } return true; } public static int max_pos(int []ar, int n) { int min = Integer.MAX_VALUE; for(int i = 1; i < n; i++) { if(ar[i] > 0) { break; } int a = Math.abs(ar[i]-ar[i-1]); min = Math.min(min, a); } int c = 0; boolean zero = false; TreeSet<Integer>set = new TreeSet<>(); int neg = 0; for(int i = 0; i < n; i++) { if(ar[i] <= 0) {neg++; if(ar[i] == 0) zero = true; continue;} if(ar[i] <= min) { c = 1; } } neg += c; return neg; } static final int MAX = 10000000; // prefix[i] is going to store count // of primes till i (including i). static int prefix[] = new int[MAX + 1]; static void buildPrefix() { // Create a boolean array "prime[0..n]". A // value in prime[i] will finally be false // if i is Not a prime, else true. boolean prime[] = new boolean[MAX + 1]; Arrays.fill(prime, true); for (int p = 2; p * p <= MAX; p++) { // If prime[p] is not changed, then // it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i <= MAX; i += p) prime[i] = false; } } // Build prefix array prefix[0] = prefix[1] = 0; for (int p = 2; p <= MAX; p++) { prefix[p] = prefix[p - 1]; if (prime[p]) prefix[p]++; } } static int query(int L, int R) { return prefix[R] - prefix[L - 1]; } static void alter(int n) { int ans = 0; boolean []vis = new boolean[n+1]; for(int i = 2; i <= n; i++){ boolean p = false; if(vis[i] == false) { for(int j = i; j <= n; j+=i) { if(vis[j] == true) { p = true; }else { vis[j] = true; } } if(!p)ans++; } } System.out.println(ans); } public static void solveDK(int []dp, int i, int D, int K) { int d = D/K; int ans = -1; int ind = d+1; while(ind < i) { int temp = i/ind; temp--; if(dp[temp*ind] == temp) { ans = dp[temp*ind]+1; dp[i] = ans; break; } ind = ind*2; } if(ans == -1) dp[i] = 1; } public static void solveKD(int []dp, int i, int D, int K) { int d = K/D; int ans = -1; int ind = d+1; while(ind < i) { int temp = i/ind; temp--; if(dp[temp*ind] == temp) { ans = dp[temp*ind]+1; dp[i] = ans; break; } ind = ind*2; } if(ans == -1) dp[i] = 1; } static int countGreater(int arr[], int n, int k) { int l = 0; int r = n - 1; // Stores the index of the left most element // from the array which is greater than k int leftGreater = n; // Finds number of elements greater than k while (l <= r) { int m = l + (r - l) / 2; // If mid element is greater than // k update leftGreater and r if (arr[m] > k) { leftGreater = m; r = m - 1; } // If mid element is less than // or equal to k update l else l = m + 1; } // Return the count of elements greater than k return (n - leftGreater); } static ArrayList<Integer>printDivisors(int n) { // Note that this loop runs till square root ArrayList<Integer>list = new ArrayList<>(); for (int i=1; i<=Math.sqrt(n); i++) { if (n%i==0) { // If divisors are equal, print only one if (n/i == i) list.add(i); else // Otherwise print both list.add(i); list.add(n/i); } } return list; } static boolean isPossible(String s, String str, int i, int j) { // System.out.println(i+" "+j); int x = i; int y = j; while(i >= 0 && j < str.length()) { if(s.charAt(i) != str.charAt(j)) { break; } i--; j++; } if(j == str.length()) { System.out.println(x+" "+y); return true; } return false; } static void leftRotate(int l, int r,int arr[], int d) { for (int i = 0; i < d; i++) leftRotatebyOne(l,r,arr); } static void leftRotatebyOne(int l, int r,int arr[]) { int i, temp; temp = arr[l]; for (i = l; i < r; i++) arr[i] = arr[i + 1]; arr[r] = temp; } static long modInverse(long a, long m) { long m0 = m; long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { // q is quotient long q = a / m; long t = m; // m is remainder now, process // same as Euclid's algo m = a % m; a = t; t = y; // Update x and y y = x - q * y; x = t; } // Make x positive if (x < 0) x += m0; return x; } static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long solvetheproblem(long n, long k) { long mod = 1000000007; long ansss = 0; while(n > 0) { // Nearest power of 2<=N long p = (long)(Math.log(n) / Math.log(2));; // Now insert k^p in the answer long temp = (long)power(k,p,mod); ansss += temp; ansss %= mod; // update n n %= (long)Math.pow(2, p); } // Print the ans in sorted order return ansss%mod ; } static boolean pos (int [][]mat, int r, int c, int n, boolean [][]vis) { if(r <= 0 || c <= 0 || r > 2 || c > n )return false; if(r == 2 && c == n)return true; if(vis[r][c])return false; vis[r][c] = true; if(mat[r][c] == 1)return false; boolean a = pos(mat,r+1,c,n,vis); boolean b = pos(mat,r,c+1,n,vis); boolean d = pos(mat,r+1,c+1,n,vis); boolean e = pos(mat,r-1,c+1,n,vis); return a || b || d || e; } static long sameremdiv(long x, long y) { if(x <= y) { y = x-1; } long sq = (long)Math.sqrt(x); if(y <= sq) { y--; long ans = (y*(y+1))/2; return ans; }else { long ans = 0; long dif = y-sq; sq -= 2; if(sq > 1) ans += (sq*(sq+1))/2; long d = x/y; sq+=2; for(int i = (int)sq; i <= y; i++) { if(i > 1 ) { long temp = x/i; if(x%i < temp)temp--; ans += temp; } } return ans; } } static int binary(long []ar, long element, int s, int e) { int mid = (s+e)/2; // System.out.println(mid); if(s > e)return mid; if(ar[mid] == element) { return mid; }else if(ar[mid] > element) { return binary(ar,element,s,mid-1); }else { return binary(ar,element,mid+1,e); } } static boolean isGibbrish(HashSet<String>set, String str, int j) { StringBuilder sb = new StringBuilder(); if(j >= str.length()) { return true; } for(int i = j; i < str.length(); i++) { sb.append(str.charAt(i)); String temp = sb.toString(); if(set.contains(temp)) { boolean test = isGibbrish(set,str,i+1); if(test)return true; } } return false; } static TreeNode buildTree(TreeNode root,int []ar, int l, int r){ if(l > r)return null; int len = l+r; if(len%2 != 0)len++; int mid = (len)/2; int v = ar[mid]; TreeNode temp = new TreeNode(v); root = temp; root.left = buildTree(root.left,ar,l,mid-1); root.right = buildTree(root.right,ar,mid+1,r); return root; } public static int getClosest(int val1, int val2, int target) { if (target - val1 >= val2 - target) return val2; else return val1; } public static int findClosest(int start, int end, int arr[], int target) { int n = arr.length; // Corner cases if (target <= arr[start]) return arr[start]; if (target >= arr[end]) return arr[end]; // Doing binary search int i = start, j = end+1, mid = 0; while (i < j) { mid = (i + j) / 2; if (arr[mid] == target) return arr[mid]; /* If target is less than array element, then search in left */ if (target < arr[mid]) { // If target is greater than previous // to mid, return closest of two if (mid > 0 && target > arr[mid - 1]) return getClosest(arr[mid - 1], arr[mid], target); /* Repeat for left half */ j = mid; } // If target is greater than mid else { if (mid < n-1 && target < arr[mid + 1]) return getClosest(arr[mid], arr[mid + 1], target); i = mid + 1; // update i } } // Only single element left after search return arr[mid]; } static int lis(int arr[], int n) { int lis[] = new int[n]; int i, j, max = 0; /* Initialize LIS values for all indexes */ for (i = 0; i < n; i++) lis[i] = 1; /* Compute optimized LIS values in bottom up manner */ for (i = 1; i < n; i++) for (j = 0; j < i; j++) if (arr[i] >= arr[j] && lis[i] < lis[j] + 1) lis[i] = lis[j] + 1; /* Pick maximum of all LIS values */ for (i = 0; i < n; i++) if (max < lis[i]) max = lis[i]; return max; } static List<List<Integer>>res = new ArrayList<>(); public static void lists(List<Integer>list, List<Integer>temp, int target, int j) { int sum = 0; for(int i = 0; i < temp.size(); i++) { sum += temp.get(i); } if(sum > target) { return; }else if(sum == target) { Collections.sort(temp); boolean exist = false; for(List<Integer>l : res) { if(l.size() == temp.size()) { int c = 0; for(int i = 0; i < l.size(); i++) { if(l.get(i) == temp.get(i))c++; } if(c == l.size()) {exist = true; break;} } } if(!exist) { res.add(new ArrayList<>(temp)); } }else if(j == list.size())return; for(int i = j; i < list.size(); i++){ temp.add(list.get(i)); lists(list,temp,target,i+1); temp.remove(temp.size()-1); } return; } static ArrayList<Integer>dfs(int p, int c, ArrayList<Integer>l,HashMap<Integer,Integer>map, LinkedList<Integer>[]list, int v) { l.add(map.get(c)); System.out.println(c); if(c == v) { //System.out.println(c); return l; } for(int j : list[c]) { if(c == 1) { // System.out.println("Yes"+" "+j); // System.out.println("yes"+" "+list[c]); } if(j != p) { dfs(c,j,l,map,list,v); } } return new ArrayList<>(); } static boolean pos(char []a, int j) { char []ar = new char[a.length]; for(int i = 0; i < a.length; i++) { ar[i] = a[i]; } ar[j] = 'c'; ar[j-1] = 'a'; ar[j-2] = 'b'; ar[j-3] = 'a'; ar[j+1] = 'a'; ar[j+2] = 'b'; ar[j+3] = 'a'; int count = 0; for(int i = 3; i < ar.length-3; i++) { if(ar[i] == 'c' && ar[i-1] == 'a' && ar[i-2] == 'b' && ar[i-3] == 'a' && ar[i+1] == 'a' && ar[i+2] == 'b' && ar[i+3] == 'a') { count++; } } if(count == 1)return true; return false; } static void bruteforce(String s) { String ans = s.charAt(0)+""; ans += ans; StringBuilder sb = new StringBuilder(); sb.append(s.charAt(0)); for(int i = 1;i < s.length(); i++){ String d = sb.toString(); sb.reverse(); d += sb.toString(); boolean con = true; System.out.println(d+" "+s); for(int j = 0; j < Math.min(d.length(), s.length()); j++) { if(s.charAt(j) > d.charAt(j)) { con = false; break; } } sb.reverse(); sb.append(s.charAt(i)); if(con) { ans = d; break; } } System.out.println(ans+" "+"yes"); } static void permute(String s , String answer) { if (s.length() == 0) { System.out.print(answer + " "); return; } for(int i = 0 ;i < s.length(); i++) { char ch = s.charAt(i); String left_substr = s.substring(0, i); String right_substr = s.substring(i + 1); String rest = left_substr + right_substr; permute(rest, answer + ch); } } static List<List<Integer>>result = new ArrayList<>(); public static void comb(List<Integer>list, int n,int []ar, HashSet<Integer>set) { if(list.size() == n) { boolean exist = false; for(List<Integer>l : result) { int c = 0; for(int i = 0; i < l.size(); i++) { if(l.get(i) == list.get(i))c++; } if(c == n) { exist = true; break;} } if(!exist)result.add(new ArrayList<>(list)); } for(int j = 0; j < n; j++) { if(!set.contains(j)) { list.add(ar[j]); set.add(j); comb(list,n,ar,set); set.remove(j); list.remove(list.size()-1); } } return; } static void pinkSeat(int [][]mat, int i, int j, int n, int m, boolean vis[][], int [][]res) { if(i <= 0 || j <= 0 || i > n || j > m || vis[i][j])return; int a = Math.abs(i-1); int b = Math.abs(j-1); int c = Math.abs(i-1); int d = Math.abs(j-m); int e = Math.abs(i-n); int f = Math.abs(j-m); int x = Math.abs(i-n); int y = Math.abs(j-1); vis[i][j] = true; int max = Math.max(a+b,c+d); max = Math.max(max, e+f); max = Math.max(max, x+y); res[i][j] = max; pinkSeat(mat,i-1,j-1,n,m,vis,res); pinkSeat(mat,i+1,j-1,n,m,vis,res); pinkSeat(mat,i-1,j+1,n,m,vis,res); pinkSeat(mat,i+1,j+1,n,m,vis,res); pinkSeat(mat,i,j-1,n,m,vis,res); pinkSeat(mat,i,j+1,n,m,vis,res); pinkSeat(mat,i-1,j,n,m,vis,res); pinkSeat(mat,i+1,j,n,m,vis,res); } static boolean isPowerOfTwo(long n) { if(n==0) return false; return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2))))); } static long ksearch(long s,long e, long []ar, long ans, long h, int n, long k) { if(s > e)return k; long mid = (s+e)/2; long count = 0; for(int i = 0; i < n; i++) { count += (ar[i])/mid; if(ar[i]%mid > 0)count++; } if(count <= h) { if(mid <= k) { ans = count; k = mid; } return ksearch(s, mid-1,ar,ans,h,n,k); }else { return ksearch(mid+1,e,ar,ans,h,n,k); } } static long mod = 1000000000; static long ans2 = 0; static void shirtCombo(long x, HashSet<Integer>set,int j, ArrayList<List<Integer>>list) { if(set.size() == list.size()) { ans2 = (ans2+x)%mod; } if(j == list.size()) { return; } int c = 0; List<Integer>temp = list.get(j); for(int a : temp ) { if(!set.contains(a)) { c++; set.add(a); shirtCombo(1,set,j+1,list); set.remove(a); } } x = (x*c)%mod; } public static class Pair { int x; int y; // Constructor public Pair(int x, int y) { this.x = x; this.y = y; } } static class Compare1{ static void compare(Pair arr[], int n) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { return p1.x - p2.x; } }); } } static long gcd1(long a, long b) { if (a == 0) return b; return gcd1(b % a, a); } // method to return LCM of two numbers static long lcm(long a, long b) { return (a / gcd1(a, b)) * b; } static ArrayList<List<Integer>>res1 = new ArrayList<>(); static void permuteNumbers(LinkedHashSet<Integer>set, int n) { if(res1.size() == n)return; if(set.size() == n) { List<Integer>l = new ArrayList<>(); for(int a : set) { l.add(a); } boolean pos = true; for(int i = 2; i < l.size(); i++) { if(l.get(i-1) + l.get(i-2) == l.get(i)) { pos = false; break; } } if(pos) { res1.add(new ArrayList(l)); } return; } for(int i = 1; i <= n; i++) { if(!set.contains(i)) { set.add(i); permuteNumbers(set,n); set.remove(i); } } } public static void main(String args[])throws IOException { BufferedReader ob = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(ob.readLine()); while(t --> 0) { /* StringTokenizer st = new StringTokenizer(ob.readLine()); int n = Integer.parseInt(st.nextToken()); int n = Integer.parseInt(ob.readLine()); int []ar = new int[n]; */ int n = Integer.parseInt(ob.readLine()); StringTokenizer st = new StringTokenizer(ob.readLine()); long []ar = new long[n]; for(int i = 0; i < n; i++) { ar[i] = Long.parseLong(st.nextToken()); } Arrays.sort(ar); for(int i = 0; i < n; i++) { int j = i; while(j < n && ar[j] == 0) { j++; } if(j >= n-1) { break; }else { long d = ar[j]|ar[j+1]; ar[j] = 0; ar[j+1] = d; } Arrays.sort(ar); } System.out.println(ar[n-1]); } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
4042c2a47f4b8d7190c708200bbbb5fb
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.lang.*; import java.io.*; import java.util.*; public class MinOrSum { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try { br = new BufferedReader( new FileReader("input.txt")); PrintStream out = new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); } catch (Exception e) { br = new BufferedReader(new InputStreamReader(System.in)); } } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class DisjointSet { int parent[] = null; int ranks[] = null; DisjointSet(int len) { parent = new int[len]; ranks = new int[len]; for (int indx = 0; indx < parent.length; indx++) { parent[indx] = indx; ranks[indx] = 0; } } // Time complexity: O(Alpha(n)), Alpha(n) <= 4, therefore O(1) and auxiliary // space: O(1) for recursion stack public void union(int x, int y) { int xRep = find(x); int yRep = find(y); if (xRep == yRep) return; if (ranks[xRep] < ranks[yRep]) parent[xRep] = yRep; else if (ranks[yRep] < ranks[xRep]) parent[yRep] = xRep; else { parent[yRep] = xRep; ranks[xRep]++; } } // Time complexity: O(LogN) and auxiliary space: O(1) for recursion stack public int find(int x) { if (parent[x] == x) { return x; } return parent[x] = find(parent[x]); } } private static int findGCDArr(int arr[], int n) { int result = arr[0]; for (int element: arr){ result = gcd(result, element); if(result == 1) { return 1; } } return result; } private static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static List<Integer> findFactors(int n) { List<Integer> res = new ArrayList<>(); // Note that this loop runs till square root for (int i=1; i<=Math.sqrt(n); i++) { if (n%i==0) { // If divisors are equal, add only one if (n/i == i) res.add(i); else // Otherwise add both res.add(i); res.add(n/i); } } return res; } private static long binPow(int a, int b, int m){ if(b==0){ return a%m; } if(b % 2 == 0){ long pw = binPow(a, b/2, m); return (1l * pw * pw % m); }else{ long pw = binPow(a, (b-1)/2, m); return 1l * pw * pw * a % m; } } private static long mulInvMod(int x, int m){ return binPow(x, m-2, m); } // end of fast i/o code public static void main(String[] args) { FastReader reader = new FastReader(); int t = reader.nextInt(); while(t-->0){ int n = reader.nextInt(); long sum = 0; for(int ind = 0; ind<n; ind++){ int a = reader.nextInt(); sum |= a; } System.out.println(sum); } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
888ab144dd7d674a7e66722d088f91d0
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.util.Arrays; 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 m = 0; m < t; m++){ int n = s.nextInt(); long[] temp = new long[n]; for(int i = 0; i < n; i++) temp[i] = s.nextInt(); //System.out.println(Arrays.toString(temp)); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++){ //System.out.println(temp[i]+" "+temp[j]+" "+(temp[i] & temp[j])); if(i == j) continue; if((temp[i] & temp[j]) == 0) continue; long g = temp[i] & temp[j]; for(int k = 0; k < 31; k++){ if(((1L << k) & g) == 0) continue;; temp[i] -= (1L << k); } } } //System.out.println(Arrays.toString(temp)); long ans = 0; for(long i : temp) ans += i; System.out.println(ans); } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
aa0b764550c1d18c2f7dca613e95b674
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
//package codeforces.vp.vp_div_2_772.A; import java.util.Scanner; /** * https://codeforces.com/contest/1635/problem/A * */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { solve(sc); } } static void solve(Scanner sc) { int n = sc.nextInt(); int[] a = new int[n]; long ans = 0; for(int i = 0; i < n; i++) { a[i] = sc.nextInt(); ans |= a[i]; } System.out.println(ans); } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
b4d63125558124de04df855f5a8388e5
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.io.*; import java.util.StringTokenizer; public class MinORSum { //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- 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 void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int test = sc.nextInt(); while (test-- > 0) { int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } solve(arr); } out.close(); } private static void solve(int[] arr) { int ans = 0; for (int j : arr) { ans |= j; } System.out.println(ans); } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
dffd5afb5a907e82b7dd2587287c0281
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.io.*; import java.util.*; public class Main { static int c = 0; public static void sort(int[] arr, int l, int r) { if (l < r) { int mid = (r + l) / 2; sort(arr, l, mid); sort(arr, mid + 1, r); merge(arr, l, mid, mid + 1, r); } } public static void merge(int[] arr, int l1, int r1, int l2, int r2) { int[] temp = new int[r2 - l1 + 1]; int p = 0; int l = l1; while (l1 <= r1 && l2 <= r2) { if (arr[l1] <= arr[l2]) temp[p++] = arr[l1++]; else { temp[p++] = arr[l2++]; c += r1 - l1 + 1; } } while (l1 <= r1) { temp[p++] = arr[l1++]; } while (l2 <= r2) { temp[p++] = arr[l2++]; } for (int i = 0; i < p; i++) { arr[i + l] = temp[i]; } } static void swap(int[] a, int i, int j) { int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } public static int weakness(int[] arr, int i) { if (i <= 1) return 0; if (arr[i] == 2) return arr[1]; return weakness(arr, i - 1) + arr[i - 1]; } public static void main(String[] args) throws IOException, InterruptedException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int r=0; for (int i = 0; i < n; i++) r|=sc.nextInt(); pw.println(r); } pw.flush(); } // static class Pair implements Comparable<Pair> { String x; int y; public Pair(String x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public int compareTo(Pair p) { if (x.charAt(0) == p.x.charAt(0)) { String s1 = x + p.x; String s2 = p.x + x; return s1.compareTo(s2); } else return x.charAt(0) - p.x.charAt(0); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
0f339bdb86a0ea30768da506a38cd49b
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.io.*; import java.util.*; public class Main { static int c = 0; public static void sort(int[] arr, int l, int r) { if (l < r) { int mid = (r + l) / 2; sort(arr, l, mid); sort(arr, mid + 1, r); merge(arr, l, mid, mid + 1, r); } } public static void merge(int[] arr, int l1, int r1, int l2, int r2) { int[] temp = new int[r2 - l1 + 1]; int p = 0; int l = l1; while (l1 <= r1 && l2 <= r2) { if (arr[l1] <= arr[l2]) temp[p++] = arr[l1++]; else { temp[p++] = arr[l2++]; c += r1 - l1 + 1; } } while (l1 <= r1) { temp[p++] = arr[l1++]; } while (l2 <= r2) { temp[p++] = arr[l2++]; } for (int i = 0; i < p; i++) { arr[i + l] = temp[i]; } } static void swap(int[] a, int i, int j) { int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } public static int weakness(int[] arr, int i) { if(i<=1) return 0; if (arr[i] == 2) return arr[1]; return weakness(arr, i - 1) + arr[i - 1]; } public static void main(String[] args) throws IOException, InterruptedException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0) { int n=sc.nextInt(); PriorityQueue<Integer> p = new PriorityQueue<>(Collections.reverseOrder()); for(int i=0;i<n;i++) p.add(sc.nextInt()); while(p.size()>1) { int x=p.poll(); int y=p.poll(); p.add(x|y); } pw.println(p.poll()); } pw.flush(); } // static class Pair implements Comparable<Pair> { String x; int y; public Pair(String x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public int compareTo(Pair p) { if (x.charAt(0) == p.x.charAt(0)) { String s1 = x + p.x; String s2 = p.x + x; return s1.compareTo(s2); } else return x.charAt(0) - p.x.charAt(0); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
873be2ff524d69f3196b14521c5b214f
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner s =new Scanner (System.in); int t=s.nextInt(); while(t-->0){ int n=s.nextInt(); int a[]=new int[n]; for (int i=0;i<n;i++){ a[i]=s.nextInt(); } int sum=0; for (int i=0;i<n;i++){ sum|=a[i]; } System.out.println(sum); } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
158168391a1aba2d9ee81e8247eb249d
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Scanner; import java.util.Set; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.util.stream.Collector; import java.util.stream.Collectors; import javax.print.DocFlavor.INPUT_STREAM; public class Main { public static void main(String[] args) throws Exception { Sol obj=new Sol(); obj.runner(); } } class Sol{ FastScanner fs=new FastScanner(); Scanner sc=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); List<Integer> sieve; Set<Integer> sieveSet; void runner() throws Exception{ int T=1; //sieve=sieve(); //sieveSet=new HashSet<>(sieve); //T=sc.nextInt(); T=fs.nextInt(); while(T-->0) { solve(T); } out.close(); System.gc(); } private void solve(int T) throws Exception { int n=fs.nextInt(); long arr[]=new long[n]; for(int i=0;i<n;i++) { arr[i]=fs.nextLong(); } long ans=0; for(int i=0;i<n;i++) { ans|=arr[i]; } System.out.println(ans); /**/ } static class FastScanner { BufferedReader br; StringTokenizer st ; FastScanner(){ br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } FastScanner(String file) { try { br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); st = new StringTokenizer(""); } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("file not found"); e.printStackTrace(); } } String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } String readLine() throws IOException{ return br.readLine(); } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
051f0cd128289df92c3fa49c11cfb2f6
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; public class Main { static class Reader { BufferedReader r; StringTokenizer str; Reader() { r=new BufferedReader(new InputStreamReader(System.in)); } Reader(String fileName) throws FileNotFoundException { r=new BufferedReader(new FileReader(fileName)); } public String read() throws IOException { return r.readLine(); } public String getNextToken() throws IOException { if(str==null||!str.hasMoreTokens()) { str=new StringTokenizer(r.readLine()); } return str.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(getNextToken()); } public long nextLong() throws IOException { return Long.parseLong(getNextToken()); } public Double nextDouble() throws IOException { return Double.parseDouble(getNextToken()); } public String nextString() throws IOException { return getNextToken(); } public int[] intArray(int n) throws IOException { int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=nextInt(); return a; } public long[] longArray(int n) throws IOException { long a[]=new long[n]; for(int i=0;i<n;i++) a[i]=nextLong(); return a; } public String[] stringArray(int n) throws IOException { String a[]=new String[n]; for(int i=0;i<n;i++) a[i]=nextString(); return a; } } public static void main(String args[]) throws IOException{ Reader r=new Reader(); PrintWriter pr=new PrintWriter(System.out); int t=r.nextInt(); while(t-->0){ int n=r.nextInt(); int a[]=r.intArray(n); int ans=0; for(int i:a){ ans=ans|i; } pr.println(ans); } pr.flush(); pr.close(); } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
222cb0ef570b5f4f3241e675e41e0ab7
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner input = new Scanner(System.in); StringBuilder output = new StringBuilder(); int t = input.nextInt(); while(t > 0) { int n = input.nextInt(); int[] a = new int[n]; for(int i = 0; i < n; ++i) { a[i] = input.nextInt(); } int[] count = new int[32]; for(int i = 0; i < n; ++i) { int j = 31, num = a[i]; while(num > 0) { if((num|1) == num) ++count[j]; --j; num = num >> 1; } } int mask = 1, answer = 0; for(int i = 31; i > -1; --i) { if(count[i] > 0) answer += mask; mask = mask<<1; } output.append(answer); output.append("\n"); --t; } System.out.print(output); } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
4de0eefbd642446e2f799e152f8a1029
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; import java.io.*; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; public class A { public static void main(String[] args) { FastReader sc = new FastReader(); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int a[] = sc.readArray(n); int sum = 0; for(int i=0;i<n;i++) { sum|=a[i]; } System.out.println(sum); } } public static void rev(int a[], int x, int y) { for (int i = x, j = y; i < j; i++, j--) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } int[] readArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(next()); } return a; } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void debugInt(int[] arr) { for (int i = 0; i < arr.length; ++i) System.out.print(arr[i] + " "); System.out.println(); } static void debugIntInt(int[][] arr) { for (int i = 0; i < arr.length; ++i) { for (int j = 0; j < arr[0].length; ++j) System.out.print(arr[i][j] + " "); System.out.println(); } System.out.println(); } static void debugLong(long[] arr) { for (int i = 0; i < arr.length; ++i) System.out.print(arr[i] + " "); System.out.println(); } static void debugLongLong(long[][] arr) { for (int i = 0; i < arr.length; ++i) { for (int j = 0; j < arr[0].length; ++j) System.out.print(arr[i][j] + " "); System.out.println(); } System.out.println(); } static int MOD = (int) (1e9 + 7); static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static void debugArrayList(ArrayList<Integer> arr) { for (int i = 0; i < arr.size(); i++) { System.out.print(arr.get(i) + " "); } System.out.println(); } static void sieveOfEratosthenes(int n) { boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int i = 2; i <= n; i++) { // if (prime[i] == true) // add it to HashSet } } static boolean isPalindrome(String s) { for (int i = 0, j = s.length() - 1; i < j; i++, j--) { if (s.charAt(i) != s.charAt(j)) return false; } return true; } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } /* * * if RTE inside for loop: - check for increment/decrement in loop, (i++,i--) if * after custom sorting an array using lamda , numbers are messed up - check the * order in which input is taken * */ }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
e5529a7358131d169de316248625acd8
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
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 ankur.y */ 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); AMinOrSum solver = new AMinOrSum(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class AMinOrSum { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); long ar[] = in.getLongArray(n); long ans = 0; for (int i = 0; i < n; i++) { ans = ans | ar[i]; } out.println(ans); } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar; private int snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { //*-*------clare-----anjlika--- //remeber while comparing 2 non primitive data type not to use == //remember Arrays.sort for primitive data has worst time case complexity of 0(n^2) bcoz it uses quick sort //again silly mistakes ,yr kb tk krta rhega ye mistakes //try to write simple codes ,break it into simple things //knowledge>rating /* public class Main implements Runnable{ public static void main(String[] args) { new Thread(null,new Main(),"Main",1<<26).start(); } public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; libraries.InputReader in = new libraries.InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC();//chenge the name of task solver.solve(1, in, out); out.close(); } */ if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long[] getLongArray(int n) { long[] ar = new long[n]; for (int i = 0; i < n; i++) { ar[i] = nextLong(); } return ar; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String next() { return readString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
aab10c91eec9d83ae8f638e6c4759bbf
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
//package CodeForces; import java.util.*; public class Main { public static void main(String[] arg) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t > 0) { int n = sc.nextInt(); int[] arr = new int[n]; int ans = 0; for(int i = 0;i < n;i++) { arr[i] = sc.nextInt(); ans |= arr[i]; } System.out.println(ans); t--; } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
f8c265984adab77f69a03de72feef7a9
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.io.BufferedReader; import java.io.InputStreamReader; public class First { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); //int a = 1; int t; t = in.nextInt(); //t = 1; while (t > 0) { //out.print("Case #"+(a++)+": "); solver.call(in,out); t--; } out.close(); } static class TaskA { public void call(InputReader in, PrintWriter out) { int n; n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } int min = 0; for (int i = 0; i < n; i++) { min = arr[i] | min; } out.println(min); } } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static class answer implements Comparable<answer>{ int a, b; public answer(int a, int b) { this.a = a; this.b = b; } @Override public int compareTo(answer o) { return this.a - o.a; } } static class answer1 implements Comparable<answer1>{ int a, b, c; public answer1(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } @Override public int compareTo(answer1 o) { if(o.c==this.c){ return this.a - o.a; } return o.c - this.c; } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void sort(long[] a) { ArrayList<Long> l=new ArrayList<>(); for (Long i:a) l.add(i); l.sort(Collections.reverseOrder()); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static final Random random=new Random(); static void shuffleSort(int[] a) { int n=a.length; 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 InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
62abfe140bb3291883d8045540f1df3e
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.util.*; import java.io.*; // cd C:\Users\Lenovo\Desktop\New //ArrayList<Integer> a=new ArrayList<>(); //List<Integer> lis=new ArrayList<>(); //StringBuilder ans = new StringBuilder(); //HashMap<Integer,Integer> map=new HashMap<>(); public class cf { static FastReader in=new FastReader(); static final Random random=new Random(); static long mod=1000000007L; public static void main(String args[]) throws IOException { FastReader sc=new FastReader(); //Scanner s=new Scanner(System.in); int tt=sc.nextInt(); //int tt=1; while(tt-->0){ //StringBuilder ar = new StringBuilder(); int n=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } long ans=0l; for(int i=0;i<32;i++){ for(int j=0;j<n;j++){ if((arr[j]&(1<<i))!=0){ ans+=(1<<i); break; } } } System.out.println(ans); } } static int countprimefactors(int n){ int ans=0; int z=(int)Math.sqrt(n); for(int i=2;i<=z;i++){ while(n%i==0){ ans++; n=n/i; } } if(n>1){ ans++; } return ans; } static int count(int arr[],int idx,long sum,int []dp){ if(idx>=arr.length){ return 0; } if(dp[idx]!=-1){ return dp[idx]; } if(arr[idx]<0){ return dp[idx]=Math.max(1+count(arr,idx+1,sum+arr[idx],dp),count(arr,idx+1,sum,dp)); } else{ return dp[idx]=1+count(arr,idx+1,sum+arr[idx],dp); } } static String reverse(String s){ String ans=""; for(int i=s.length()-1;i>=0;i--){ ans+=s.charAt(i); } return ans; } static int find_max(int []check,int y){ int max=0; for(int i=y;i>=0;i--){ if(check[i]!=0){ max=i; break; } } return max; } static void dfs(int [][]arr,int row,int col,boolean [][]seen,int n){ if(row<0 || col<0 || row==n || col==n){ return; } if(arr[row][col]==1){ return; } if(seen[row][col]==true){ return; } seen[row][col]=true; dfs(arr,row+1,col,seen,n); dfs(arr,row,col+1,seen,n); dfs(arr,row-1,col,seen,n); dfs(arr,row,col-1,seen,n); } static int msb(int x){ int ans=0; while(x!=0){ x=x/2; ans++; } return ans; } static void ruffleSort(int[] a) { int n=a.length; 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 int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } /* static boolean checkprime(int n1) { if(n1%2==0||n1%3==0) return false; else { for(int i=5;i*i<=n1;i+=6) { if(n1%i==0||n1%(i+2)==0) return false; } return true; } } */ /* Iterator<Map.Entry<Integer, Integer>> iterator = map.entrySet().iterator(); while(iterator.hasNext()){ Map.Entry<Integer, Integer> entry = iterator.next(); int value = entry.getValue(); if(value==1){ iterator.remove(); } else{ entry.setValue(value-1); } } */ static class Pair implements Comparable { int a,b; public String toString() { return a+" " + b; } public Pair(int x , int y) { a=x;b=y; } @Override public int compareTo(Object o) { Pair p = (Pair)o; if(p.a!=a){ return a-p.a;//in } else{ return b-p.b;// } } } /* public static boolean checkAP(List<Integer> lis){ for(int i=1;i<lis.size()-1;i++){ if(2*lis.get(i)!=lis.get(i-1)+lis.get(i+1)){ return false; } } return true; } public static int minBS(int[]arr,int val){ int l=-1; int r=arr.length; while(r>l+1){ int mid=(l+r)/2; if(arr[mid]>=val){ r=mid; } else{ l=mid; } } return r; } public static int maxBS(int[]arr,int val){ int l=-1; int r=arr.length; while(r>l+1){ int mid=(l+r)/2; if(arr[mid]<=val){ l=mid; } else{ r=mid; } } return l; } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; }*/ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
8e0192dd499a300a12b89a484f021fa3
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Solution { static Long MOD = (long) (1e9 + 7); static long[] sieve; static long[][] dp; static long[]dpp; static long[][] matrix; static ArrayList<Integer>arr; static ArrayList<Character> chararr; static boolean[] vis; static boolean[][] visarr; static ArrayList<ArrayList<Integer>>adj; public static void main(String[] args) { FastReader sc = new FastReader(); int t = sc.nextInt(); while (t != 0) { int n=sc.nextInt(); int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } Arrays.sort(arr); for(int i=1;i<n;i++){ arr[i]=arr[i]|arr[i-1]; arr[i-1]=0; } System.out.println(arr[n-1]); t--; } } public static int numIslands(long[][] grid){ int n=grid.length,m=grid[0].length; matrix=grid; visarr=new boolean[n][m]; int count=0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if(!visarr[i][j]){ if(matrix[i][j]==1){ dfs(matrix,visarr,i,j); count++; }else{ visarr[i][j]=true; } } } } return count; } private static void dfs(long[][] grid, boolean[][] vis, int i, int j) { if(vis[i][j])return; if(grid[i][j]==0){ vis[i][j]=true; }else{ vis[i][j]=true; if(i>0){ dfs(grid,vis,i-1,j); } if(i<grid.length-1){ dfs(grid,vis,i+1,j); } if(j>0){ dfs(grid,vis,i,j-1); } if(j<grid[0].length-1){ dfs(grid,vis,i,j+1); } if(i<grid.length-1&&j<grid[0].length-1){ dfs(grid,vis,i+1,j+1); } if(i>0&&j>0){ dfs(grid,vis,i-1,j-1); } if(i>0&&j<grid[0].length-1){ dfs(grid,vis,i-1,j+1); } if(j>0&&i<grid.length-1){ dfs(grid,vis,i+1,j-1); } } return; } private static void dfs(int i) { vis[i]=true; arr.add(i); for(int x:adj.get(i)) { if(vis[x]==false)dfs(x); } } public static boolean isAlphabet(char c){ if(( c>= 'a' && c<='z')||(c >= 'A' && c <='Z')){ return true; } return false; } public static boolean isNumber(char c){ if( c>= '0' && c<='9'){ return true; } return false; } public static int reverseNumber(int num){ int rev_num = 0; while (num > 0) { rev_num = rev_num * 10 + num % 10; num = num / 10; } return rev_num; } static boolean coPrime(int a, long l) { return (gcd(a, l) == 1); } private static boolean isPrime(int number) { return sieve[number] == 1; } //Brian Kernighan’s Algorithm static long countSetBits(long n) { if (n == 0) return 0; return 1 + countSetBits(n & (n - 1)); } //Factorial static long factorial(long n) { if (n == 0) return 1; if (n == 1) return 1; if (n == 2) return 2; if (n == 3) return 6; return n * factorial(n - 1); } // function to search a character lying between index l and r // which is closest greater (just greater) than val // and return it's index static int binarySearch(StringBuilder sb,int l,int r,char val) { int index = -1; while (l <= r) { int mid = (l+r)/2; if (sb.charAt(mid) <= val) { r = mid - 1; } else { l = mid + 1; if (index == -1 || sb.charAt(index) >= sb.charAt(mid)) index = mid; } } return index; } //Modular Exponentiation static long fastExpo(long x, long n) { if (n == 0) return 1; if ((n & 1) == 0) return fastExpo((x * x) % MOD, n / 2); return ((x % MOD) * fastExpo((x * x) % MOD, (n - 1) / 2)); } // check if array is sorted or not public static boolean isSorted(int[] arr, int n) { if (n < 2) return true; for (int i = 1; i < n; i++) { if (arr[i] < arr[i - 1]) return false; } return true; } static boolean isPalindrome(String str, int i, int j) {//i and j including while (i < j) { if (str.charAt(i) != str.charAt(j)) return false; i++; j--; } return true; } static boolean sign(long num) { return num > 0; } static boolean isSquare(long x) { if (x == 1) return true; long y = (long) Math.sqrt(x); return y * y == x; } //Euclidean Algorithm public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } public static void swap(long x, long max1)//swap two variable { long temp = x; x = max1; max1 = temp; } static long LowerBound(long[] a2, long x) { // x is the target value or key int l = -1, r = a2.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a2[m] >= x) r = m; else l = m; } return r; } static int UpperBound(int a[], int x) {// x is the key or target value int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] <= x) l = m; else r = m; } return l + 1; } public static String revStr(String str) { String input = str; StringBuilder input1 = new StringBuilder(); input1.append(input); input1.reverse(); return input1.toString(); } // public static String sortString(String inputString) { // char tempArray[] = inputString.toCharArray(); // Arrays.sort(tempArray); // return new String(tempArray); // } public static String sortString(String inputString) { // Converting input string to Character array Character tempArray[] = new Character[inputString.length()]; for (int i = 0; i < inputString.length(); i++) { tempArray[i] = inputString.charAt(i); } // Sort, ignoring case during sorting Arrays.sort(tempArray, new Comparator<Character>() { // Method 2 // To compare characters @Override public int compare(Character c1, Character c2) { // Ignoring case return Character.compare( Character.toLowerCase(c1), Character.toLowerCase(c2)); } }); // Using StringBuilder to convert Character array to // String StringBuilder sb = new StringBuilder(tempArray.length); for (Character c : tempArray) sb.append(c.charValue()); return sb.toString(); } public static void sieve() { int nnn = (int) 1e6 + 1; long nn = (int) 1e6; sieve = new long[(int) nnn]; int[] freq = new int[(int) nnn]; sieve[0] = 0; sieve[1] = 1; for (int i = 2; i <= nn; i++) { sieve[i] = 1; freq[i] = 1; } for (int i = 2; i * i <= nn; i++) { if (sieve[i] == 1) { for (int j = i * i; j <= nn; j += i) { if (sieve[j] == 1) { sieve[j] = 0; } } } } } public static void doSomething() { return; } // *******----segement tree implement---***** // -------------START-------------------------- void buildTree(int[] arr, int[] tree, int start, int end, int treeNode) { if (start == end) { tree[treeNode] = arr[start]; return; } buildTree(arr, tree, start, end, 2 * treeNode); buildTree(arr, tree, start, end, 2 * treeNode + 1); tree[treeNode] = tree[treeNode * 2] + tree[2 * treeNode + 1]; } void updateTree(int[] arr, int[] tree, int start, int end, int treeNode, int idx, int value) { if (start == end) { arr[idx] = value; tree[treeNode] = value; return; } int mid = (start + end) / 2; if (idx > mid) { updateTree(arr, tree, mid + 1, end, 2 * treeNode + 1, idx, value); } else { updateTree(arr, tree, start, mid, 2 * treeNode, idx, value); } tree[treeNode] = tree[2 * treeNode] + tree[2 * treeNode + 1]; } // disjoint set implementation --start static void makeSet(int n) { parent = new int[n]; rank = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; rank[i] = 0; } } static void union(int u, int v) { u = findpar(u); v = findpar(v); if (rank[u] < rank[v]) parent[u] = v; else if (rank[v] < rank[u]) parent[v] = u; else { parent[v] = u; rank[u]++; } } private static int findpar(int node) { if (node == parent[node]) return node; return parent[node] = findpar(parent[node]); } static int parent[]; static int rank[]; // *************end static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } class pair { long x; long y; long c; public pair(long x, long y) { this.x = x; this.y = y; } public pair(long x, long y, long c) { this.x = x; this.y = y; this.c = c; } } class Pair { int idx; String str; public Pair(String str, int idx) { this.str = str; this.idx = idx; } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
56d36add32a5e4a6b5d73e940a3fd60e
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.lang.*; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.File; import java.io.PrintStream; import java.io.PrintWriter; import java.math.BigInteger; public class Main { /* 10^(7) = 1s. * ceilVal = (a+b-1) / b */ static final int mod = 1000000007; static final long temp = 998244353; static final long MOD = 1000000007; static final long M = (long)1e9+7; static class Pair implements Comparable<Pair> { int first, second; public Pair(int first, int second) { this.first = first; this.second = second; } public int compareTo(Pair ob) { return (int)(first - ob.first); } } static class Tuple implements Comparable<Tuple> { int first, second,third; public Tuple(int first, int second, int third) { this.first = first; this.second = second; this.third = third; } public int compareTo(Tuple o) { return (int)(o.third - this.third); } } public static class DSU { int count = 0; int[] parent; int[] rank; public DSU(int n) { count = n; parent = new int[n]; rank = new int[n]; Arrays.fill(parent, -1); Arrays.fill(rank, 1); } public int find(int i) { return parent[i] < 0 ? i : (parent[i] = find(parent[i])); } public void union(int a, int b) //Union Find by Rank { a = find(a); b = find(b); if(a == b) return; if(rank[a] < rank[b]) { parent[a] = b; } else if(rank[a] > rank[b]) { parent[b] = a; } else { parent[b] = a; rank[a] = 1 + rank[a]; } count--; } public int countConnected() { return count; } } static class Reader { 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) throws IOException { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] longReadArray(int n) throws IOException { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static int gcd(int a, int b) { if(b == 0) return a; else return gcd(b,a%b); } public static long lcm(long a, long b) { return (a / LongGCD(a, b)) * b; } public static long LongGCD(long a, long b) { if(b == 0) return a; else return LongGCD(b,a%b); } public static long LongLCM(long a, long b) { return (a / LongGCD(a, b)) * b; } //Count the number of coprime's upto N public static long phi(long n) //euler totient/phi function { long ans = n; // for(long i = 2;i*i<=n;i++) // { // if(n%i == 0) // { // while(n%i == 0) n/=i; // ans -= (ans/i); // } // } // // if(n > 1) // { // ans -= (ans/n); // } for(long i = 2;i<=n;i++) { if(isPrime(i)) { ans -= (ans/i); } } return ans; } public static long fastPow(long x, long n) { if(n == 0) return 1; else if(n%2 == 0) return fastPow(x*x,n/2); else return x*fastPow(x*x,(n-1)/2); } public static long powMod(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) { res = (res * x) % p; } y = y >> 1; x = (x * x) % p; } return res; } static long modInverse(long n, long p) { return powMod(n, p - 2, p); } // Returns nCr % p using Fermat's little theorem. public static long nCrModP(long n, long r,long p) { if (n<r) return 0; if (r == 0) return 1; long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[(int)(n)] * modInverse(fac[(int)(r)], p) % p * modInverse(fac[(int)(n - r)], p) % p) % p; } public static long fact(long n) { long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (long i = 1; i <= n; i++) fac[(int)(i)] = fac[(int)(i - 1)] * i; return fac[(int)(n)]; } public static long nCr(long n, long k) { long ans = 1; for(long i = 0;i<k;i++) { ans *= (n-i); ans /= (i+1); } return ans; } //Modular Operations for Addition and Multiplication. public static long perfomMod(long x) { return ((x%M + M)%M); } public static long addMod(long a, long b) { return perfomMod(perfomMod(a)+perfomMod(b)); } public static long subMod(long a, long b) { return perfomMod(perfomMod(a)-perfomMod(b)); } public static long mulMod(long a, long b) { return perfomMod(perfomMod(a)*perfomMod(b)); } public static boolean isPrime(long n) { if(n == 1) { return false; } //check only for sqrt of the number as the divisors //keep repeating so only half of them are required. So,sqrt. for(int i = 2;i*i<=n;i++) { if(n%i == 0) { return false; } } return true; } public static List<Long> SieveList(int n) { boolean prime[] = new boolean[(int)(n+1)]; Arrays.fill(prime, true); List<Long> l = new ArrayList<>(); for (long p = 2; p*p<=n; p++) { if (prime[(int)(p)] == true) { for(long i = p*p; i<=n; i += p) { prime[(int)(i)] = false; } } } for (long p = 2; p<=n; p++) { if (prime[(int)(p)] == true) { l.add(p); } } return l; } public static int countDivisors(int x) { int c = 0; for(int i = 1;i*i<=x;i++) { if(x%i == 0) { if(x/i != i) { c+=2; } else { c++; } } } return c; } public static long log2(long n) { long ans = (long)(log(n)/log(2)); return ans; } public static boolean isPow2(long n) { return (n != 0 && ((n & (n-1))) == 0); } public static boolean isSq(int x) { long s = (long)Math.round(Math.sqrt(x)); return s*s==x; } /* * * >= <= 0 1 2 3 4 5 6 7 5 5 5 6 6 6 7 7 lower_bound for 6 at index 3 (>=) upper_bound for 6 at index 6(To get six reduce by one) (<=) */ public static int LowerBound(int a[], int x) { int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } public static int UpperBound(int a[], int x) { int l=-1, r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } public static void Sort(int[] a) { List<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); Collections.reverse(l); //Use to Sort decreasingly for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static void ssort(char[] a) { List<Character> l = new ArrayList<>(); for (char i : a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static void main(String[] args) throws Exception { Reader sc = new Reader(); PrintWriter fout = new PrintWriter(System.out); int tt = sc.nextInt(); fr:while(tt-- > 0) { int n= sc.nextInt(); long[] a = sc.longReadArray(n); long res = 0; for(long it : a) res |= it; fout.println(res); } fout.close(); } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
26638e0a059afa508d755c7a208fcb90
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.util.*; public class Practice { static int dp[][]; int M = Integer.MIN_VALUE; int m = Integer.MIN_VALUE; public static void main(String args[]) { //--------------start------------ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t>0){ //----------------code----------------- int n = sc.nextInt(); int[] a = new int[n]; for(int i = 0;i<a.length;i++){ a[i] = sc.nextInt(); } int ans = 0; for(int i:a){ ans = ans|i; } System.out.println(ans); //----------------code---------------------- t = t-1; } //----------------end--------------------- } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
d132fc547829c66febab908c25507f88
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class q1 { static HashSet<Long> hs; static int[][] dp; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = Integer.parseInt(sc.next()); int cn=1; while (t-- > 0) { int n = Integer.parseInt(sc.next()); long[] arr = new long[n]; long sum=0; for(int i=0;i<n;i++) { arr[i]=Long.parseLong(sc.next()); sum=sum|arr[i]; } System.out.println(sum); } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
04f9220a4b30deef1f5ea314da35d919
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.io.*; import java.util.*; public class Main { static int mod = 1000_000_007; static long mod1 = 998244353; static boolean memory = true; static FastScanner f; static PrintWriter pw; static double eps = 1e-6; static int oo = (int) 1e9; public static void solve() throws Exception { int[] cnt = new int[30]; int n = f.ni(); for (int i = n; i > 0; --i) { int x = f.ni(); for (int j = 29; j >= 0; --j) { if ((x & (1 << j)) > 0) ++cnt[j]; } } int ans = 0; for (int j = 0; j < 30; ++j) { if (cnt[j] > 0) ans += (1 << j); } pn(ans); } public static void main(String[] args) throws Exception { if (memory) new Thread(null, new Runnable() { public void run() { try { Main.run(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } }, "", 1 << 28).start(); else { Main.run(); } } static void run() throws Exception { if (System.getProperty("ONLINE_JUDGE") == null) { f = new FastScanner(""); File file = new File("!out.txt"); pw = new PrintWriter(file); } else { f = new FastScanner(); pw = new PrintWriter(System.out); } int t = f.ni(); while (t --> 0) { solve(); } pw.flush(); pw.close(); } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String str) throws Exception { try { br = new BufferedReader(new FileReader("!a.txt")); } catch (Exception e) { e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int ni() throws IOException { return Integer.parseInt(next()); } public long nl() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nd() throws IOException { return Double.parseDouble(next()); } } public static void pn(Object... o) { for (int i = 0; i < o.length; ++i) pw.print(o[i] + (i + 1 < o.length ? " " : "\n")); } public static void p(Object... o) { for (int i = 0; i < o.length; ++i) pw.print(o[i] + (i + 1 < o.length ? " " : "")); } public static void pni(Object... o) { for (Object obj : o) pw.print(obj + " "); pw.println(); pw.flush(); } public static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; ++i) a[i] = l.get(i); } public static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; ++i) a[i] = l.get(i); } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
f035e810778ef01c8ba7b1d40c2f8aeb
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.util.*; import java.io.*; public class A { public static void main(String[] args) throws IOException { FastReader in = new FastReader(System.in); PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); int t = in.nextInt(); for (int tt = 0; tt < t; tt++) { int n = in.nextInt(); long sum = in.nextLong(); for (int i = 1; i < n; i++) sum |= in.nextInt(); pw.println(sum); } pw.close(); } static int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } static boolean checkPalindrome(String str) { int len = str.length(); for (int i = 0; i < len / 2; i++) { if (str.charAt(i) != str.charAt(len - i - 1)) return false; } return true; } public static boolean isSorted(int[] arr) { for (int i = 1; i < arr.length; i++) if (arr[i] < arr[i - 1]) return false; return true; } static long binaryExponentiation(long x, long n) { if (n == 0) return 1; else if (n % 2 == 0) return binaryExponentiation(x * x, n / 2); else return x * binaryExponentiation(x * x, (n - 1) / 2); } public static void Sort(int[] a) { ArrayList<Integer> lst = new ArrayList<>(); for (int i : a) lst.add(i); Collections.sort(lst); for (int i = 0; i < lst.size(); i++) a[i] = lst.get(i); } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } static class Pair implements Comparable<Pair> { int first, second; public Pair(int first, int second) { this.first = first; this.second = second; } public int compareTo(Pair ob) { return first - ob.first; } } static class FastReader { InputStream is; private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; public FastReader(InputStream is) { this.is = is; } public int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } public boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } public String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public String nextLine() { int c = skip(); StringBuilder sb = new StringBuilder(); while (!isEndOfLine(c)) { sb.appendCodePoint(c); c = readByte(); } return sb.toString(); } public int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = (num << 3) + (num << 1) + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } public long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = (num << 3) + (num << 1) + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } public double nextDouble() { return Double.parseDouble(next()); } public char[] next(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } public char readChar() { return (char) skip(); } public long[] readArrayL(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } public int[] readArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
bbab71f397cf2c5591687829ba98dc6b
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(); int t = in.nextInt(); for (int i = 0; i < t; i++) { int n = in.nextInt(); int res = 0; for (int j = 0; j < n; j++) { int a = in.nextInt(); res |= a; } System.out.println(res); } } public static <K, V extends Comparable<V>> Map<K, V> valueSort(final Map<K, V> map) { Comparator<K> valueComparator = new Comparator<K>() { public int compare(K k1, K k2) { return 0; } }; Map<K, V> sorted = new TreeMap<K, V>(valueComparator); sorted.putAll(map); return sorted; } static class Pair implements Comparator<Pair> { long first; long second; public Pair() { } public Pair(long ki, long i) { first = ki; second = i; } @Override public String toString() { return "Pair{" + "first=" + first + ", second=" + second + '}'; } @Override public int compare(Pair o1, Pair o2) { return (int) (o1.first - o2.first); } } private static int upper_bound(long[] arr, int target) { int l = -1; int r = arr.length; while (r > l + 1) { int m = l + (r - l) / 2; if (arr[m] < target) { l = m; } else { r = m; } } return r; } static int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } static List<Long> div(long n) { List<Long> a = new ArrayList<>(); for (long i = 1; i <= Math.sqrt(n); i++) { if (n % i == 0) { a.add(i); if (n / i != i) { a.add(n / i); } } } return a; } static void sort(long arr[], int l, int r) { if (l < r) { int m = l + (r - l) / 2; sort(arr, l, m); sort(arr, m + 1, r); merge(arr, l, m, r); } } static void reverseArr(int[] arr) { int l = 0; int r = arr.length - 1; while (l <= r) { int temp = arr[l]; arr[l] = arr[r]; arr[r] = temp; l++; r--; } } static void merge(long arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; long L[] = new long[n1]; long R[] = new long[n2]; for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } static final long INF = (long) (1e9 + 7); static class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); long sum; 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()); } public char[] readChar() { String t = next(); return t.toCharArray(); } } static long add(long a, long b) { return (a + b) % INF; } static long sub(long a, long b) { return ((a - b) % INF + INF) % INF; } static long mul(long a, long b) { return (a * b) % INF; } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
02c37310ca45dcf84b01e0e4835c8ae3
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.HashSet; import java.util.Set; import java.util.StringTokenizer; public class R772A { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st.nextToken()); while (t-- > 0) { st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(st.nextToken()); } solve(arr, n); } } private static void solve(int[] arr, int n) { int sum = 0; for (int i = 0; i < arr.length; i++) { sum = sum | arr[i]; } System.out.println(sum); } } /* 1 1 0 1 0 1 1 0 0 0 1 */
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
8cb1babad0ee1af29c81aea09c7e2c5a
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.util.*; public class Solution { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int k=0;k<t;k++) { int n=sc.nextInt(); int[] arr=new int[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } int x=0; for(int i=0;i<n;i++) { x=x|arr[i]; } System.out.println(x); } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
3385db14796d2a1e3d6f9de76a59428a
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public final class A_Min_Or_Sum { //int 2e9 - long 9e18 static PrintWriter out = new PrintWriter(System.out); static FastReader fs = new FastReader(); static Pair[] moves = new Pair[]{new Pair(-1, 0), new Pair(0, 1), new Pair(1, 0), new Pair(0, -1)}; static Pair[] movesDiagonal = new Pair[]{new Pair(-1, -1), new Pair(-1, 1), new Pair(1, -1), new Pair(1, 1)}; static int mod = (int) (1e9 + 7); static int mod2 = 998244353; public static void main(String[] args) { int tt = fs.nextInt(); while (tt-- > 0) { solve(); } out.flush(); } public static void solve() { int n=fs.nextInt(); int res=0; for(int i=0;i<n;i++) { int x=fs.nextInt(); res |=x; } out.println(res); } // (10,5) = 2 ,(11,5) = 3 static long upperDiv(long a, long b) { return (a / b) + ((a % b == 0) ? 0 : 1); } static long sum(int[] a) { long sum = 0; for (int x : a) { sum += x; } return sum; } static int[] preint(int[] a) { int[] pre = new int[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static long[] pre(int[] a) { long[] pre = new long[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static long[] post(int[] a) { long[] post = new long[a.length + 1]; post[0] = 0; for (int i = 0; i < a.length; i++) { post[i + 1] = post[i] + a[a.length - 1 - i]; } return post; } static long[] pre(long[] a) { long[] pre = new long[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static void print(char A[]) { for (char c : A) { out.print(c); } out.println(); } static void print(boolean A[]) { for (boolean c : A) { out.print(c + " "); } out.println(); } static void print(int A[]) { for (int c : A) { out.print(c + " "); } out.println(); } static void print(long A[]) { for (long i : A) { out.print(i + " "); } out.println(); } static void print(List<Integer> A) { for (int a : A) { out.print(a + " "); } } static long mul(long a, long b) { return (a*b)%mod; } static long exp(long base, long exp) { if (exp==0) return 1; long half=exp(base, exp/2); if (exp%2==0) return mul(half, half); return mul(half, mul(half, base)); } static long[] factorials=new long[2_000_001]; static long[] invFactorials=new long[2_000_001]; static void precompFacts() { factorials[0]=invFactorials[0]=1; for (int i=1; i<factorials.length; i++) factorials[i]=mul(factorials[i-1], i); invFactorials[factorials.length-1]=exp(factorials[factorials.length-1], mod-2); for (int i=invFactorials.length-2; i>=0; i--) invFactorials[i]=mul(invFactorials[i+1], i+1); } static long nCk(int n, int k) { return mul(factorials[n], mul(invFactorials[k], invFactorials[n-k])); } static int[][] inputWithIdx(int N) { int A[][] = new int[N][2]; for (int i = 0; i < N; i++) { A[i] = new int[]{i, fs.nextInt()}; } return A; } static int[] input(int N) { int A[] = new int[N]; for (int i = 0; i < N; i++) { A[i] = fs.nextInt(); } return A; } static long[] inputLong(int N) { long A[] = new long[N]; for (int i = 0; i < A.length; i++) { A[i] = fs.nextLong(); } return A; } static int GCD(int a, int b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } static long GCD(long a, long b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } static long LCM(int a, int b) { return (long) a / GCD(a, b) * b; } static long LCM(long a, long b) { return a / GCD(a, b) * b; } // find highest i which satisfy a[i]<=x static int lowerbound(int[] a, int x) { int l = 0; int r = a.length - 1; while (l < r) { int m = (l + r + 1) / 2; if (a[m] <= x) { l = m; } else { r = m - 1; } } return l; } static void shuffle(int[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } } static void shuffleAndSort(int[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static void shuffleAndSort(int[][] arr, Comparator<? super int[]> comparator) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int[] temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr, comparator); } static void shuffleAndSort(long[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); long temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static boolean isPerfectSquare(double number) { double sqrt = Math.sqrt(number); return ((sqrt - Math.floor(sqrt)) == 0); } static void swap(int A[], int a, int b) { int t = A[a]; A[a] = A[b]; A[b] = t; } static void swap(char A[], int a, int b) { char t = A[a]; A[a] = A[b]; A[b] = t; } static long pow(long a, long b, int mod) { long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) { pow = (pow * x) % mod; } x = (x * x) % mod; b /= 2; } return pow; } static long pow(long a, long b) { long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) { pow *= x; } x = x * x; b /= 2; } return pow; } static long modInverse(long x, int mod) { return pow(x, mod - 2, mod); } static boolean isPrime(long N) { if (N <= 1) { return false; } if (N <= 3) { return true; } if (N % 2 == 0 || N % 3 == 0) { return false; } for (int i = 5; i * i <= N; i = i + 6) { if (N % i == 0 || N % (i + 2) == 0) { return false; } } return true; } public static String reverse(String str) { if (str == null) { return null; } return new StringBuilder(str).reverse().toString(); } public static void reverse(int[] arr) { int l = 0; int r = arr.length - 1; while (l < r) { swap(arr, l, r); l++; r--; } } public static String repeat(char ch, int repeat) { if (repeat <= 0) { return ""; } final char[] buf = new char[repeat]; for (int i = repeat - 1; i >= 0; i--) { buf[i] = ch; } return new String(buf); } public static int[] manacher(String s) { char[] chars = s.toCharArray(); int n = s.length(); int[] d1 = new int[n]; for (int i = 0, l = 0, r = -1; i < n; i++) { int k = (i > r) ? 1 : Math.min(d1[l + r - i], r - i + 1); while (0 <= i - k && i + k < n && chars[i - k] == chars[i + k]) { k++; } d1[i] = k--; if (i + k > r) { l = i - k; r = i + k; } } return d1; } public static int[] kmp(String s) { int n = s.length(); int[] res = new int[n]; for (int i = 1; i < n; ++i) { int j = res[i - 1]; while (j > 0 && s.charAt(i) != s.charAt(j)) { j = res[j - 1]; } if (s.charAt(i) == s.charAt(j)) { ++j; } res[i] = j; } return res; } } class Pair { int i; int j; Pair(int i, int j) { this.i = i; this.j = j; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Pair pair = (Pair) o; return i == pair.i && j == pair.j; } @Override public int hashCode() { return Objects.hash(i, j); } } class ThreePair { int i; int j; int k; ThreePair(int i, int j, int k) { this.i = i; this.j = j; this.k = k; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } ThreePair pair = (ThreePair) o; return i == pair.i && j == pair.j && k == pair.k; } @Override public int hashCode() { return Objects.hash(i, j); } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } class Node { int val; public Node(int val) { this.val = val; } } class ST { int n; Node[] st; ST(int n) { this.n = n; st = new Node[4 * Integer.highestOneBit(n)]; } void build(Node[] nodes) { build(0, 0, n - 1, nodes); } private void build(int id, int l, int r, Node[] nodes) { if (l == r) { st[id] = nodes[l]; return; } int mid = (l + r) >> 1; build((id << 1) + 1, l, mid, nodes); build((id << 1) + 2, mid + 1, r, nodes); st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]); } void update(int i, Node node) { update(0, 0, n - 1, i, node); } private void update(int id, int l, int r, int i, Node node) { if (i < l || r < i) { return; } if (l == r) { st[id] = node; return; } int mid = (l + r) >> 1; update((id << 1) + 1, l, mid, i, node); update((id << 1) + 2, mid + 1, r, i, node); st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]); } Node get(int x, int y) { return get(0, 0, n - 1, x, y); } private Node get(int id, int l, int r, int x, int y) { if (x > r || y < l) { return new Node(0); } if (x <= l && r <= y) { return st[id]; } int mid = (l + r) >> 1; return comb(get((id << 1) + 1, l, mid, x, y), get((id << 1) + 2, mid + 1, r, x, y)); } Node comb(Node a, Node b) { if (a == null) { return b; } if (b == null) { return a; } return new Node(GCD(a.val, b.val)); } static int GCD(int a, int b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
c5c6cfdf1304aa73e1f956e18ffaee62
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.io.*; import java.util.StringTokenizer; public class MinOrSum { public static void main(String[]args)throws Exception{ Scanner sc=new Scanner(System.in); PrintWriter pw=new PrintWriter(System.out); int n=sc.nextInt(); while(n-->0){ int x=sc.nextInt(); String [] list=sc.nextLine().split(" "); int y=0; for(int i=0;i<list.length;i++) y|=Integer.parseInt(list[i]); pw.println(y); } pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(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 int[] nextIntArray(int n) throws IOException{ int []a=new int[n]; for(int i=0;i<=n;i++) a[i]=nextInt(); return a; } 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
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
25c3a4dd9cbcd220b872b3baf527d433
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.util.*; import java.io.*; import java.util.stream.*; public class App { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws Exception { if (System.getProperty("ONLINE_JUDGE") == null) { File inputFile = new File("/Users/vipinjain/self/cp/input.txt"); File outputFile = new File("/Users/vipinjain/self/cp/output.txt"); br = new BufferedReader(new FileReader(inputFile)); bw = new BufferedWriter(new FileWriter(outputFile)); } int tests; tests = Integer.parseInt(br.readLine()); //tests = 1; while (tests-- > 0) { solve(); } bw.flush(); bw.close(); br.close(); } static void solve() throws Exception { // String[] tmp = br.readLine().split(" "); // int n = Integer.parseInt(tmp[0]); // int m = Integer.parseInt(tmp[1]); // long n = Long.parseLong(tmp[0]); // long k = Long.parseLong(tmp[1]); // int b = Integer.parseInt(tmp[2]); // int c = Integer.parseInt(tmp[3]); // int d = Integer.parseInt(tmp[4]); int n = Integer.parseInt(br.readLine()); int[] arr = Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); int res = 0; for (int i = 0; i < n; ++i) { res |= arr[i]; } bw.write(res + "\n"); } static int gcd(int a, int b) { if (a == 0) { return b; } return gcd(b % a, a); } static boolean isPrime(long n) { for (long i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; } static long minDivisor(long n) { for (long i = 2; i * i <= n; ++i) { if (n % i == 0) { return i; } } return n; } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
5e31c53c031aa4d266031cc47d1a83c2
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.util.*; public class Solution { public static void main(String[] args){ Scanner scn = new Scanner(System.in); int cases = scn.nextInt(); while(cases-->0){ int len = scn.nextInt(); int ans = 0; for(int i =0;i<len;i++){ int t = scn.nextInt(); ans |= t; } System.out.println(ans); } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
c3c724cb6ea5cd7b11688a7bd5127428
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.io.*; import java.util.*; public class codeforces { static Random rn = new Random(); static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws IOException { int test = sc.nextInt(); while (test-->0) { int n = sc.nextInt(); int arr[] = sc.nextIntArray(n); int res = 0; for (int e : arr) { res|=e; } System.out.println(res); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } 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 long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } static class pair implements Comparable<pair> { int x; int y; public pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) { return this.z - other.z; } return this.y - other.y; } else { return this.x - other.x; } } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output
PASSED
a1c9885216002002d4af85bf48cf685c
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i &lt; j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
256 megabytes
import java.util.*; import java.lang.*; public class Main { public static void main(String[] args) { Scanner in =new Scanner(System.in); int num =in.nextInt(); for ( int c = 1 ;c <= num ;c++ ){ int num2 = in.nextInt(); int sum = 0 ; for (int c2 = 0 ; c2 < num2 ; c2++){ int num3 = in.nextInt(); if (c2 == 0) sum = num3 ; else sum = num3 | sum ; } System.out.println(sum); } } }
Java
["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"]
1 second
["3\n31\n6\n7"]
NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations.
Java 8
standard input
[ "bitmasks", "greedy" ]
7fca54a73076ddfeb30b921b9e341f26
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i &lt; 2^{30})$$$.
800
For each test case, print one number in a line — the minimum possible sum of the array.
standard output