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
66da2b1c3e5ec0a276c6923c8ed2cbd9
train_108.jsonl
1654266900
Is it really?! The robot only existing in my imagination?! The Colossal Walking Robot?!!— Kochiya Sanae Sanae made a giant robot — Hisoutensoku, but something is wrong with it. To make matters worse, Sanae can not figure out how to stop it, and she is forced to fix it on-the-fly.The state of a robot can be represented by an array of integers of length $$$n$$$. Initially, the robot is at state $$$a$$$. She wishes to turn it into state $$$b$$$. As a great programmer, Sanae knows the art of copy-and-paste. In one operation, she can choose some segment from given segments, copy the segment from $$$b$$$ and paste it into the same place of the robot, replacing the original state there. However, she has to ensure that the sum of $$$a$$$ does not change after each copy operation in case the robot go haywire. Formally, Sanae can choose segment $$$[l,r]$$$ and assign $$$a_i = b_i$$$ ($$$l\le i\le r$$$) if $$$\sum\limits_{i=1}^n a_i$$$ does not change after the operation.Determine whether it is possible for Sanae to successfully turn the robot from the initial state $$$a$$$ to the desired state $$$b$$$ with any (possibly, zero) operations.
256 megabytes
import java.io.*; import java.util.*; public class gaint_robot { public static void main(String[] args)throws IOException { // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(br.readLine()); for(int i=0; i<t; i++) { StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); long[] sums = new long[n+1]; st = new StringTokenizer(br.readLine()); for(int j=1; j<=n; j++) sums[j] = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); for(int j=1; j<=n; j++) sums[j] -= Integer.parseInt(st.nextToken()); for(int j=1; j<=n; j++) sums[j] += sums[j-1]; ArrayList<Integer>[] edges = new ArrayList[n+1]; for(int j=0; j<=n; j++)edges[j] = new ArrayList<>(); for(int j=0; j<m; j++) { st = new StringTokenizer(br.readLine()); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); edges[a-1].add(b); edges[b].add(a-1); } TreeSet<Integer> zero = new TreeSet<>(); zero.add(1000000000); LinkedList<Integer> que = new LinkedList<>(); for(int j=1; j<=n; j++) { if(sums[j]!=0)zero.add(j); else que.add(j); } while(!que.isEmpty()) { int j = que.removeFirst(); for(int k: edges[j]) { if(sums[k]==0) { int p = zero.higher(Math.min(j, k)); while(p<Math.max(j, k)) { zero.remove(p); sums[p] = 0; que.add(p); p = zero.higher(p); } } } } if(zero.size()==1)out.write("YES\n"); else out.write("NO\n"); } out.flush(); out.close(); } }
Java
["2\n\n5 2\n\n1 5 4 2 3\n\n3 2 5 4 1\n\n1 3\n\n2 5\n\n5 2\n\n1 5 4 2 3\n\n3 2 4 5 1\n\n1 2\n\n2 4"]
1 second
["YES\nNO"]
NoteTest case 1:One possible way of turning $$$a$$$ to $$$b$$$:First, select $$$[1,3]$$$. After the operation, $$$a=[3,2,5,2,3]$$$.Then, select $$$[2,5]$$$. After the operation, $$$a=[3,2,5,4,1]=b$$$.Test case 2:It can be shown that it is impossible to turn $$$a$$$ into $$$b$$$.
Java 8
standard input
[ "binary search", "brute force", "data structures", "dsu", "greedy", "sortings" ]
2627417136abced8481ea5728d5c1f06
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 2\cdot 10^4$$$) — the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n\leq 2\cdot 10^5$$$, $$$1 \leq m\leq 2\cdot 10^5$$$) — the length of $$$a$$$, $$$b$$$ and the number of segments. The second line contains $$$n$$$ intergers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the initial state $$$a$$$. The third line contains $$$n$$$ intergers $$$b_1,b_2,\ldots,b_n$$$ ($$$1 \leq b_i \leq 10^9$$$) — the desired state $$$b$$$. Then $$$m$$$ lines follow, the $$$i$$$-th line contains two intergers $$$l_i,r_i$$$ ($$$1 \leq l_i &lt; r_i \leq n$$$) — the segments that can be copy-pasted by Sanae. It is guaranteed that both the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$.
2,500
For each test case, print "YES" (without quotes) if $$$a$$$ can be turned into $$$b$$$, or "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
ab910240b3d4bdc6f75aee36c9de91d6
train_108.jsonl
1654266900
Is it really?! The robot only existing in my imagination?! The Colossal Walking Robot?!!— Kochiya Sanae Sanae made a giant robot — Hisoutensoku, but something is wrong with it. To make matters worse, Sanae can not figure out how to stop it, and she is forced to fix it on-the-fly.The state of a robot can be represented by an array of integers of length $$$n$$$. Initially, the robot is at state $$$a$$$. She wishes to turn it into state $$$b$$$. As a great programmer, Sanae knows the art of copy-and-paste. In one operation, she can choose some segment from given segments, copy the segment from $$$b$$$ and paste it into the same place of the robot, replacing the original state there. However, she has to ensure that the sum of $$$a$$$ does not change after each copy operation in case the robot go haywire. Formally, Sanae can choose segment $$$[l,r]$$$ and assign $$$a_i = b_i$$$ ($$$l\le i\le r$$$) if $$$\sum\limits_{i=1}^n a_i$$$ does not change after the operation.Determine whether it is possible for Sanae to successfully turn the robot from the initial state $$$a$$$ to the desired state $$$b$$$ with any (possibly, zero) operations.
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Locale; import java.util.StringTokenizer; import java.util.TreeSet; public class Solution2 implements Runnable { private PrintStream out; private BufferedReader in; private StringTokenizer st; public void solve() throws IOException { long time0 = System.currentTimeMillis(); int t = nextInt(); for (int test = 1; test <= t; test++) { int n = nextInt(); int m = nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } int[] b = new int[n]; for (int i = 0; i < n; i++) { b[i] = nextInt(); } int[] l = new int[m]; int[] r = new int[m]; for (int i = 0; i < m; i++) { l[i] = nextInt() - 1; r[i] = nextInt(); } boolean answer = solve(n, m, a, b, l, r); if (answer) { out.println("YES"); } else { out.println("NO"); } } System.err.println("time: " + (System.currentTimeMillis() - time0)); } private boolean solve(int n, int m, int[] a, int[] b, int[] l, int[] r) { long[] prefix = new long[n + 1]; for (int i = 1; i <= n; i++) { prefix[i] = prefix[i - 1] + b[i - 1] - a[i - 1]; } int[] first = new int[n + 1]; Arrays.fill(first, -1); int[] to = new int[2 * m]; int[] next = new int[2 * m]; for (int i = 0; i < m; i++) { to[2 * i + 0] = r[i]; next[2 * i + 0] = first[l[i]]; first[l[i]] = 2 * i + 0; to[2 * i + 1] = l[i]; next[2 * i + 1] = first[r[i]]; first[r[i]] = 2 * i + 1; } TreeSet<Integer> notzero = new TreeSet<>(); int[] zero = new int[n + 1]; int zerocnt = 0; for (int i = 0; i <= n; i++) { if (prefix[i] == 0) { zero[zerocnt++] = i; } else { notzero.add(i); } } while (zerocnt > 0) { int z = zero[--zerocnt]; for (int i = first[z]; i != -1; i = next[i]) { int x = to[i]; if (prefix[x] == 0) { int u = Math.min(z, x); int v = Math.max(z, x); List<Integer> subzero = new ArrayList<>(notzero.subSet(u, v)); for (Integer sz : subzero) { int szi = sz; notzero.remove(sz); prefix[szi] = 0; zero[zerocnt++] = szi; } } } } return notzero.isEmpty(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public String next() throws IOException { while (!st.hasMoreTokens()) { String line = in.readLine(); if (line == null) { return null; } st = new StringTokenizer(line); } return st.nextToken(); } @Override public void run() { try { solve(); out.close(); } catch (Throwable e) { throw new RuntimeException(e); } } public Solution2(String name) throws IOException { Locale.setDefault(Locale.US); if (name == null) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintStream(new BufferedOutputStream(System.out)); } else { in = new BufferedReader(new InputStreamReader(new FileInputStream(name + ".in"))); out = new PrintStream(new BufferedOutputStream(new FileOutputStream(name + ".out"))); } st = new StringTokenizer(""); } public static void main(String[] args) throws IOException { new Thread(new Solution2(null)).start(); } }
Java
["2\n\n5 2\n\n1 5 4 2 3\n\n3 2 5 4 1\n\n1 3\n\n2 5\n\n5 2\n\n1 5 4 2 3\n\n3 2 4 5 1\n\n1 2\n\n2 4"]
1 second
["YES\nNO"]
NoteTest case 1:One possible way of turning $$$a$$$ to $$$b$$$:First, select $$$[1,3]$$$. After the operation, $$$a=[3,2,5,2,3]$$$.Then, select $$$[2,5]$$$. After the operation, $$$a=[3,2,5,4,1]=b$$$.Test case 2:It can be shown that it is impossible to turn $$$a$$$ into $$$b$$$.
Java 8
standard input
[ "binary search", "brute force", "data structures", "dsu", "greedy", "sortings" ]
2627417136abced8481ea5728d5c1f06
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 2\cdot 10^4$$$) — the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n\leq 2\cdot 10^5$$$, $$$1 \leq m\leq 2\cdot 10^5$$$) — the length of $$$a$$$, $$$b$$$ and the number of segments. The second line contains $$$n$$$ intergers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the initial state $$$a$$$. The third line contains $$$n$$$ intergers $$$b_1,b_2,\ldots,b_n$$$ ($$$1 \leq b_i \leq 10^9$$$) — the desired state $$$b$$$. Then $$$m$$$ lines follow, the $$$i$$$-th line contains two intergers $$$l_i,r_i$$$ ($$$1 \leq l_i &lt; r_i \leq n$$$) — the segments that can be copy-pasted by Sanae. It is guaranteed that both the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$.
2,500
For each test case, print "YES" (without quotes) if $$$a$$$ can be turned into $$$b$$$, or "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
c89e18345575edb51559f883d3502340
train_108.jsonl
1654266900
Is it really?! The robot only existing in my imagination?! The Colossal Walking Robot?!!— Kochiya Sanae Sanae made a giant robot — Hisoutensoku, but something is wrong with it. To make matters worse, Sanae can not figure out how to stop it, and she is forced to fix it on-the-fly.The state of a robot can be represented by an array of integers of length $$$n$$$. Initially, the robot is at state $$$a$$$. She wishes to turn it into state $$$b$$$. As a great programmer, Sanae knows the art of copy-and-paste. In one operation, she can choose some segment from given segments, copy the segment from $$$b$$$ and paste it into the same place of the robot, replacing the original state there. However, she has to ensure that the sum of $$$a$$$ does not change after each copy operation in case the robot go haywire. Formally, Sanae can choose segment $$$[l,r]$$$ and assign $$$a_i = b_i$$$ ($$$l\le i\le r$$$) if $$$\sum\limits_{i=1}^n a_i$$$ does not change after the operation.Determine whether it is possible for Sanae to successfully turn the robot from the initial state $$$a$$$ to the desired state $$$b$$$ with any (possibly, zero) operations.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.stream.IntStream; import java.io.ByteArrayOutputStream; import java.io.BufferedWriter; import java.util.Collection; import java.io.FileOutputStream; import java.util.HashMap; import java.io.IOException; import java.util.InputMismatchException; import java.util.TreeSet; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.io.Writer; import java.util.Queue; import java.util.LinkedList; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author tauros */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; RealFastReader in = new RealFastReader(inputStream); RealFastWriter out = new RealFastWriter(outputStream); CF1688F solver = new CF1688F(); solver.solve(1, in, out); out.close(); } static class CF1688F { public void solve(int testNumber, RealFastReader in, RealFastWriter out) { int cases = in.ni(); while (cases-- > 0) { int n = in.ni(), m = in.ni(); int[] a = in.na(n), b = in.na(n); int[] c = new int[n]; for (int i = 0; i < n; i++) { c[i] = a[i] - b[i]; } long[] preSum = new long[n + 1]; for (int i = 1; i <= n; i++) { preSum[i] = preSum[i - 1] + c[i - 1]; } int[][] ranges = new int[m][2]; for (int i = 0; i < m; i++) { ranges[i][0] = in.ni(); ranges[i][1] = in.ni(); } if (preSum[n] != 0) { out.println("NO"); continue; } TreeSet<Integer> nonZeroSet = new TreeSet<>(); for (int i = 1; i <= n; i++) { if (preSum[i] != 0) { nonZeroSet.add(i); } } Map<Integer, List<Integer>> rangeMap = new HashMap<>(0); Queue<int[]> q = new LinkedList<>(); for (int i = 0; i < m; i++) { int[] range = ranges[i]; if (preSum[range[0] - 1] == 0 && preSum[range[1]] == 0) { q.offer(range); } else { rangeMap.computeIfAbsent(range[0] - 1, k -> new ArrayList<>(1)).add(i); rangeMap.computeIfAbsent(range[1], k -> new ArrayList<>(1)).add(i); } } while (!q.isEmpty()) { int[] opRange = q.poll(); Integer iterKey = nonZeroSet.ceiling(opRange[0]); while (iterKey != null && iterKey <= opRange[1]) { preSum[iterKey] = 0; List<Integer> rangeIndexes = rangeMap.getOrDefault(iterKey, Collections.emptyList()); for (int rangeIndex : rangeIndexes) { if (preSum[ranges[rangeIndex][0] - 1] == 0 && preSum[ranges[rangeIndex][1]] == 0) { q.offer(ranges[rangeIndex]); } } nonZeroSet.remove(iterKey); iterKey = nonZeroSet.higher(iterKey); } } out.println(nonZeroSet.isEmpty() ? "YES" : "NO"); } out.flush(); } } static class RealFastWriter { private static final int BUF_SIZE = 1 << 13; private final byte[] buf = new byte[BUF_SIZE]; private OutputStream out; private Writer writer; private int ptr = 0; private RealFastWriter() { out = null; } public RealFastWriter(Writer writer) { this.writer = new BufferedWriter(writer); out = new ByteArrayOutputStream(); } public RealFastWriter(OutputStream os) { this.out = os; } public RealFastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public RealFastWriter write(byte b) { buf[ptr++] = b; if (ptr == BUF_SIZE) { innerflush(); } return this; } public RealFastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) { innerflush(); } }); return this; } public RealFastWriter writeln() { return write((byte) '\n'); } public RealFastWriter writeln(String s) { return write(s).writeln(); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { if (writer != null) { writer.write(((ByteArrayOutputStream) out).toString()); out = new ByteArrayOutputStream(); writer.flush(); } else { out.flush(); } } catch (IOException e) { throw new RuntimeException("flush"); } } public RealFastWriter println(String s) { return writeln(s); } public void close() { flush(); try { out.close(); } catch (Exception e) { } } } static class RealFastReader { InputStream is; private byte[] inbuf = new byte[1024]; public int lenbuf = 0; public int ptrbuf = 0; public RealFastReader(final 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 int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public int ni() { return (int) nl(); } public long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } } }
Java
["2\n\n5 2\n\n1 5 4 2 3\n\n3 2 5 4 1\n\n1 3\n\n2 5\n\n5 2\n\n1 5 4 2 3\n\n3 2 4 5 1\n\n1 2\n\n2 4"]
1 second
["YES\nNO"]
NoteTest case 1:One possible way of turning $$$a$$$ to $$$b$$$:First, select $$$[1,3]$$$. After the operation, $$$a=[3,2,5,2,3]$$$.Then, select $$$[2,5]$$$. After the operation, $$$a=[3,2,5,4,1]=b$$$.Test case 2:It can be shown that it is impossible to turn $$$a$$$ into $$$b$$$.
Java 8
standard input
[ "binary search", "brute force", "data structures", "dsu", "greedy", "sortings" ]
2627417136abced8481ea5728d5c1f06
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 2\cdot 10^4$$$) — the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n\leq 2\cdot 10^5$$$, $$$1 \leq m\leq 2\cdot 10^5$$$) — the length of $$$a$$$, $$$b$$$ and the number of segments. The second line contains $$$n$$$ intergers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the initial state $$$a$$$. The third line contains $$$n$$$ intergers $$$b_1,b_2,\ldots,b_n$$$ ($$$1 \leq b_i \leq 10^9$$$) — the desired state $$$b$$$. Then $$$m$$$ lines follow, the $$$i$$$-th line contains two intergers $$$l_i,r_i$$$ ($$$1 \leq l_i &lt; r_i \leq n$$$) — the segments that can be copy-pasted by Sanae. It is guaranteed that both the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$.
2,500
For each test case, print "YES" (without quotes) if $$$a$$$ can be turned into $$$b$$$, or "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
37ab1515d617144015e76753accdfbae
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
import java.util.*; import java.io.*; // res.append("Case #"+(p+1)+": "+hh+" \n"); ////*************************************************************************** /* public class E_Gardener_and_Tree implements Runnable{ public static void main(String[] args) throws Exception { new Thread(null, new E_Gardener_and_Tree(), "E_Gardener_and_Tree", 1<<28).start(); } public void run(){ WRITE YOUR CODE HERE!!!! JUST WRITE EVERYTHING HERE WHICH YOU WRITE IN MAIN!!! } } */ /////************************************************************************** public class B_AquaMoon_and_Chess{ public static void main(String[] args) { FastScanner s= new FastScanner(); //PrintWriter out=new PrintWriter(System.out); //end of program //out.println(answer); //out.close(); StringBuilder res = new StringBuilder(); int t=s.nextInt(); int p=0; while(p<t){ int n=s.nextInt(); String str=s.nextToken(); long facts[]= new long[n+3]; facts[0]=1; facts[1]=1; long mod=998244353; for(long i=2;i<n+3;i++){ facts[(int)i]=(i*facts[(int)(i-1)])%mod; } long infacts[]= new long[n+3]; for(int i=0;i<n+3;i++){ infacts[i]=modpower(facts[i],mod-2,mod); } long zeroes=0; long ones=0; long hh=0; for(int i=0;i<n;i++){ char ch=str.charAt(i); if(i==0){ if(ch=='0'){ zeroes++; } else{ hh++; } } else{ char ch2=str.charAt(i-1); if(ch2!=ch){ if(ch2=='1'){ long alpha=hh/2; ones+=alpha; hh=0; } } if(ch=='0'){ zeroes++; } else{ hh++; } } } if(hh>0){ long alpha=hh/2; ones+=alpha; hh=0; } long total=zeroes+ones; // System.out.println("zeroes "+zeroes); // System.out.println("ones "+ones); long ans=(facts[(int)total]); ans=(ans*infacts[(int)zeroes])%mod; ans=(ans*infacts[(int)ones])%mod; //res.append(ans+" \n"); System.out.println(ans); p++; } // System.out.println(res); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } } static long modpower(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; } // SIMPLE POWER FUNCTION=> static long power(long x, long y) { long res = 1; // Initialize result while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = res * x; // y must be even now y = y >> 1; // y = y/2 x = x * x; // Change x to x^2 } return res; } }
Java
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 8
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
a88543384d4d150fa57ba2271dcc5778
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String args[]) { new Main().run(); } FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); long[] rec=new long[200002]; long[] inv=new long[200002]; void run() { rec[0]=1; for(int i=1;i<200002;i++){ rec[i]=rec[i-1]*i%mod; } inv[200001]=pow(rec[200001],mod-2); for(int i=200000;i>=0;i--){ inv[i]=inv[i+1]*(i+1)%mod; } for(int q=ni();q>0;q--){ work(); } out.flush(); } long mod = 998244353; long gcd(long a, long b) { return a == 0 ? b : gcd(b % a, a); } long inf = Long.MAX_VALUE / 3; ArrayList<Integer>[] graph; void work() { int n=ni(); char[] chs=ns().toCharArray(); int c=0,len=0,cur=0; for(int i=0;i<n;i++){ if(chs[i]=='0'){ len++; cur=0; }else{ cur++; if(cur==2){ cur=0; c++; } } } out.println(C(len+c,len)); } private long C(int a, int b) { return rec[a]*inv[b]%mod*inv[a-b]%mod; } long pow(long a,long b){ long ret=1; while(b>0){ if(b%2==1){ ret=ret*a%mod; } a=a*a%mod; b/=2; } return ret; } private ArrayList<long[]>[] ngw(int n, int m) { ArrayList<long[]>[] graph = (ArrayList<long[]>[]) new ArrayList[n]; for (int i = 0; i < n; i++) { graph[i] = new ArrayList<>(); } for (int i = 1; i <= m; i++) { long s = in.nextLong() - 1, e = in.nextLong() - 1, w = in.nextLong(); graph[(int) s].add(new long[]{e, w}); graph[(int) e].add(new long[]{s, w}); } return graph; } private ArrayList<Integer>[] ng(int n, int m) { ArrayList<Integer>[] graph = (ArrayList<Integer>[]) new ArrayList[n]; for (int i = 0; i < n; i++) { graph[i] = new ArrayList<>(); } for (int i = 1; i <= m; i++) { int s = in.nextInt() - 1, e = in.nextInt() - 1; graph[s].add(e); // graph[e].add(s); } return graph; } private int ni() { return in.nextInt(); } private long nl() { return in.nextLong(); } private String ns() { return in.next(); } private long[] na(int n) { long[] A = new long[n]; for (int i = 0; i < n; i++) { A[i] = in.nextLong(); } return A; } private int[] nia(int n) { int[] A = new int[n]; for (int i = 0; i < n; i++) { A[i] = in.nextInt(); } return A; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreElements())//回车,空行情况 { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } }
Java
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 8
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
1d94162efb4eb7a4f023183b31b32b7f
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
import java.io.*; import java.util.*; public final class Solution { static PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static Pair[] moves = new Pair[]{new Pair(-1, 0), new Pair(1, 0), new Pair(0, -1), new Pair(0, 1)}; public static void main(String[] args) { int tt = i(); int mod = 998244353; initializeFactorial((int) (1e5 + 1), mod); out: while (tt-- > 0) { int n = i(); String s = s(); int i = 0; int x = 0; int y = 0; while (i < n) { if (s.charAt(i) == '0') { y++; i++; } else if (i + 1 < n && s.charAt(i) == '1' && s.charAt(i + 1) == '1') { x ++; i += 2; } else { i++; } } out.println(binomialCoefficient(x + y, x, mod)); } out.flush(); } static long[] fac; static long[] inv; static void initializeFactorial(int n, int mod) { fac = new long[n + 1]; inv = new long[n + 1]; long[] v = new long[fac.length]; fac[0] = inv[0] = 1; for (int i = 1; i < fac.length; i++) { v[i] = i == 1 ? 1 : v[i - mod % i] * (mod / i + 1); v[i] %= mod; fac[i] = fac[i - 1] * i; fac[i] %= mod; inv[i] = inv[i - 1] * v[i]; inv[i] %= mod; } } static int binomialCoefficient(int n, int k, int m) { long res = fac[n] * inv[k]; res %= m; res *= inv[n - k]; return (int) (res % m); } 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 void printYes() { out.println("YES"); } static void printNo() { out.println("NO"); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static String s() { return in.nextLine(); } static int[][] inputWithIdx(int N) { int A[][] = new int[N][2]; for (int i = 0; i < N; i++) { A[i] = new int[]{i, in.nextInt()}; } return A; } static int[] input(int N) { int A[] = new int[N]; for (int i = 0; i < N; i++) { A[i] = in.nextInt(); } return A; } static long[] inputLong(int N) { long A[] = new long[N]; for (int i = 0; i < A.length; i++) { A[i] = in.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; } 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(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 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; } } class Pair { int i, j; Pair(int i, int j) { this.i = i; this.j = 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; } }
Java
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 8
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
9d047538084a50f8cae7b9610d9eb48f
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
import java.io.*; import java.util.*; public final class Solution { static PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static Pair[] moves = new Pair[]{new Pair(-1, 0), new Pair(1, 0), new Pair(0, -1), new Pair(0, 1)}; public static void main(String[] args) { int tt = i(); int mod = 998244353; initializeFactorial((int) (1e5 + 1), mod); out: while (tt-- > 0) { int n = i(); String s = s(); int i = 0; int x = 0; int y = 0; while (i < n && s.charAt(i) == '1') { x++; i++; } while (i < n) { if (s.charAt(i) == '0') { y++; i++; } else if (i + 1 < n && s.charAt(i) == '1' && s.charAt(i + 1) == '1') { x += 2; i += 2; } else { i++; } } x = x / 2; out.println(binomialCoefficient(x + y, x, mod)); } out.flush(); } static long[] fac; static long[] inv; static void initializeFactorial(int n, int mod) { fac = new long[n + 1]; inv = new long[n + 1]; long[] v = new long[fac.length]; fac[0] = inv[0] = 1; for (int i = 1; i < fac.length; i++) { v[i] = i == 1 ? 1 : v[i - mod % i] * (mod / i + 1); v[i] %= mod; fac[i] = fac[i - 1] * i; fac[i] %= mod; inv[i] = inv[i - 1] * v[i]; inv[i] %= mod; } } static int binomialCoefficient(int n, int k, int m) { long res = fac[n] * inv[k]; res %= m; res *= inv[n - k]; return (int) (res % m); } 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 void printYes() { out.println("YES"); } static void printNo() { out.println("NO"); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static String s() { return in.nextLine(); } static int[][] inputWithIdx(int N) { int A[][] = new int[N][2]; for (int i = 0; i < N; i++) { A[i] = new int[]{i, in.nextInt()}; } return A; } static int[] input(int N) { int A[] = new int[N]; for (int i = 0; i < N; i++) { A[i] = in.nextInt(); } return A; } static long[] inputLong(int N) { long A[] = new long[N]; for (int i = 0; i < A.length; i++) { A[i] = in.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; } 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(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 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; } } class Pair { int i, j; Pair(int i, int j) { this.i = i; this.j = 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; } }
Java
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 8
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
9e33953de6cb66d4a1424d2b7be12092
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.math.BigInteger; import java.util.Locale; import java.util.StringTokenizer; public class Solution implements Runnable { public static final int M = 998244353; public static final BigInteger BM = BigInteger.valueOf(M); private PrintStream out; private BufferedReader in; private StringTokenizer st; public void solve() throws IOException { long time0 = System.currentTimeMillis(); int t = nextInt(); for (int test = 1; test <= t; test++) { int n = nextInt(); String start = next(); boolean[] p = new boolean[n]; for (int i = 0; i < n; i++) { if (start.charAt(i) == '1') { p[i] = true; } } int answer = solve(n, p); out.println(answer); } System.err.println("time: " + (System.currentTimeMillis() - time0)); } private int solve(int n, boolean[] p) { int pairs = 0; int zeroes = 0; for (int i = 0; i < n; i++) { if (p[i]) { if (i + 1 < n && p[i + 1]) { i++; pairs++; } } else { zeroes++; } } return solve(pairs, zeroes); } private int solve(int pairs, int zeroes) { int n = pairs + zeroes; int k = pairs; int[] f = new int[n + 1]; f[0] = 1; for (int i = 1; i < f.length; i++) { f[i] = mult(f[i - 1], i % M); } int nom = f[n]; int denom = mult(f[k], f[n - k]); return div(nom, denom); } private int div(int nom, int denom) { BigInteger db = BigInteger.valueOf(denom); int dbi = db.modInverse(BM).intValue(); return mult(nom, dbi); } private int mult(int a, int b) { long ab = ((long) a) * ((long) b); return (int) (ab % M); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public String next() throws IOException { while (!st.hasMoreTokens()) { String line = in.readLine(); if (line == null) { return null; } st = new StringTokenizer(line); } return st.nextToken(); } @Override public void run() { try { solve(); out.close(); } catch (Throwable e) { throw new RuntimeException(e); } } public Solution(String name) throws IOException { Locale.setDefault(Locale.US); if (name == null) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintStream(new BufferedOutputStream(System.out)); } else { in = new BufferedReader(new InputStreamReader(new FileInputStream(name + ".in"))); out = new PrintStream(new BufferedOutputStream(new FileOutputStream(name + ".out"))); } st = new StringTokenizer(""); } public static void main(String[] args) throws IOException { new Thread(new Solution(null)).start(); } }
Java
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 8
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
21fa878b8f9e058982b6e6cba292b527
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
import java.util.*; public class AquaMoonAndChess_732B { public static void main(String[] args) { // TODO Auto-generated method stub long[] p = new long[100001]; int mod = 998244353; p[1] = 1; p[0] = 1; for(int i = 2; i < 100001; i ++) p[i] = p[i - 1] * i % mod; Scanner sc = new Scanner(System.in); int t = Integer.parseInt(sc.nextLine()); while(t -- > 0) { int n = Integer.parseInt(sc.nextLine()); String s = sc.nextLine(); int ct0 = 0, ct1 = 0, group = 0; for(int i = 0; i < n; i ++) { if(s.charAt(i) == '0') { ct0 ++; group += ct1 / 2; ct1 = 0; } else { ct1 ++; } } group += ct1 / 2; System.out.println(fastPow(p[group + ct0], p[group] * p[ct0] % mod, mod)); } } private static long fastPow(long a, long b, int mod) { int c = mod - 2; while(c > 0) { if(c % 2 == 1) a = a * b % mod; b = b * b % mod; c >>= 1; } return a; } }
Java
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 8
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
509cc1abdff7153763ce611db679b6c5
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
import java.io.*; import java.util.*; public class B { static final int MOD = 998244353; public static void main(String[] args) { scan = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t = scan.nextInt(); while (t-->0){ solve(); } out.close(); } private static void solve() { int n = scan.nextInt(); String input = scan.next(); int pairs = 0, zeroes = 0; boolean predecessor = false; for (int i = 0; i < n; i++) { if(input.charAt(i) == '1'){ if(predecessor){ pairs++; predecessor = false; }else predecessor = true; }else { zeroes++; predecessor = false; } } out.println(nCr(zeroes+pairs, pairs)); } private static int nCr(int n, int k) { return convertFraction(fac(n), (int) (((long)fac(k)*fac(n-k))%MOD)); } private static int fac(int n) { long fac = 1; for (int i = 2; i <= n; i++) { fac = (i*fac) % MOD; } return (int) fac; } public static int convertFraction(int counter, int denominator){ return mulMod(counter, powMod(denominator, MOD-2)); } public static int powMod(int a, int b){ int pow = 1; while (b > 0){ if((b & 1) == 1){ pow = mulMod(pow, a); } a = mulMod(a, a); b >>= 1; } return pow; } static int mulMod(int a, int b){ return (int)(((long)a*b) % MOD); } public static MyScanner scan; 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
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 8
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
4a81ce4fce2bf77a193e57d0ac7df849
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
/* stream Butter! eggyHide eggyVengeance I need U xiao rerun when */ import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; public class x1545B { static final long MOD = 998244353L; public static void main(String hi[]) throws Exception { fac = new long[100005]; invfac = new long[100005]; fac[0] = invfac[0] = 1L; for(int i=1; i <= 100000; i++) fac[i] = (fac[i-1]*i)%MOD; invfac[100000] = power(fac[100000], MOD-2, MOD); for(int i=99999; i >= 1; i--) invfac[i] = (invfac[i+1]*(i+1))%MOD; BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int T = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); while(T-->0) { st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); String input = infile.readLine(); char[] arr = input.substring(0, N).toCharArray(); int tag = arr[0]-'0'; int size = 1; int evenblocks = 0; for(int i=1; i < N; i++) { if(arr[i]-'0' == tag) size++; else { if(tag == 1) { evenblocks += size/2; } tag ^= 1; size = 1; } } if(tag == 1) { evenblocks += size/2; } int zc = 0; for(char c: arr) if(c == '0') zc++; long res = (fac[zc+evenblocks]*invfac[zc])%MOD; res = (res*invfac[evenblocks])%MOD; sb.append(res+"\n"); } System.out.print(sb); } static long[] fac, invfac; public static long nCr(int a, int b) { long res = (fac[a]*invfac[b])%MOD; return (res*invfac[a-b])%MOD; } public static long power(long x, long y, long p) { //0^0 = 1 long res = 1L; x = x%p; while(y > 0) { if((y&1)==1) res = (res*x)%p; y >>= 1; x = (x*x)%p; } return res; } } /* What stays the same after a move: -parity of pawn locations -equivalence class of the state What changes after a move: -blocks of pawns Alternate logic: Think about buckets of 1's Doesn't work because parity is NOT preserved 3 20 10001111110110111000 20 00110110100110111101 20 11101111011000100010 */
Java
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 8
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
f5500671381c96bac407db69cc4629ae
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
//make sure to make new file! import java.io.*; import java.util.*; public class B732{ public static int N = 100005; public static long MOD = 998244353L; public static long[] fac; public static long[] ifac; public static void main(String[] args)throws IOException{ BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); fac = new long[N]; ifac = new long[N]; fac[0] = 1L; ifac[0] = 1L; for(int k = 1; k < N; k++){ fac[k] = (fac[k-1] * (long)k + MOD)%MOD; ifac[k] = modInverse(fac[k],MOD); } int t = Integer.parseInt(f.readLine()); for(int q = 1; q <= t; q++){ int n = Integer.parseInt(f.readLine()); char[] array = f.readLine().toCharArray(); int total = 0; int blocks = 0; int chain = 0; for(int k = 0; k < n; k++){ if(array[k] == '0'){ total++; total += chain/2; blocks += chain/2; chain = 0; } else { chain++; } } total += chain/2; blocks += chain/2; long answer = choose(total,blocks); out.println(answer); } out.close(); } public static long choose(int n, int r){ long prod = (fac[n] * ifac[r] + MOD)%MOD; return (prod * ifac[n-r])%MOD; } //from geeksforgeeks public static long modInverse(long a, long m) { long m0 = m; long y = 0; long 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 y and x y = x - q * y; x = t; } // Make x positive if (x < 0) x += m0; return x; } }
Java
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 8
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
77365188e94554f8d627eb56cb3a6a5e
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; import java.lang.reflect.Field; import java.nio.charset.StandardCharsets; import java.io.UncheckedIOException; import java.io.Closeable; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 29); thread.start(); thread.join(); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInput in = new FastInput(inputStream); FastOutput out = new FastOutput(outputStream); BAquaMoonAndChess solver = new BAquaMoonAndChess(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } } static class BAquaMoonAndChess { int mod = 998244353; Combination comb = new Combination((int) 2e5, mod); public void solve(int testNumber, FastInput in, FastOutput out) { int n = in.ri(); char[] s = new char[n]; in.rs(s); int empty = 0; int block = 0; for (int i = 0; i < n; i++) { int l = i; int r = i; while (r + 1 < n && s[r + 1] == s[r]) { r++; } i = r; if (s[l] == '0') { empty += r - l + 1; } else { block += DigitUtils.floorDiv(r - l + 1, 2); } } long ans = comb.combination(empty + block, empty); out.println(ans); } } static class FastOutput implements AutoCloseable, Closeable, Appendable { private static final int THRESHOLD = 32 << 10; private OutputStream writer; private StringBuilder cache = new StringBuilder(THRESHOLD * 2); private static Field stringBuilderValueField; static { try { stringBuilderValueField = StringBuilder.class.getSuperclass().getDeclaredField("value"); stringBuilderValueField.setAccessible(true); } catch (Exception e) { stringBuilderValueField = null; } } public FastOutput append(CharSequence csq) { cache.append(csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append(csq, start, end); return this; } private void afterWrite() { if (cache.length() < THRESHOLD) { return; } flush(); } public FastOutput(OutputStream writer) { this.writer = writer; } public FastOutput append(char c) { cache.append(c); afterWrite(); return this; } public FastOutput append(long c) { cache.append(c); afterWrite(); return this; } public FastOutput println(long c) { return append(c).println(); } public FastOutput println() { return append('\n'); } public FastOutput flush() { try { if (stringBuilderValueField != null) { try { byte[] value = (byte[]) stringBuilderValueField.get(cache); writer.write(value, 0, cache.length()); } catch (Exception e) { stringBuilderValueField = null; } } if (stringBuilderValueField == null) { //slow writer.write(cache.toString().getBytes(StandardCharsets.UTF_8)); // writer.append(cache); } writer.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { writer.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } static class Combination implements IntCombination { final Factorial factorial; int modVal; public Combination(Factorial factorial) { this.factorial = factorial; this.modVal = factorial.getMod(); } public Combination(int limit, int mod) { this(new Factorial(limit, mod)); } public int combination(int m, int n) { if (n > m || n < 0) { return 0; } return (int) ((long) factorial.fact(m) * factorial.invFact(n) % modVal * factorial.invFact(m - n) % modVal); } } static class Factorial { int[] fact; int[] inv; int mod; public int getMod() { return mod; } public Factorial(int[] fact, int[] inv, int mod) { this.mod = mod; this.fact = fact; this.inv = inv; fact[0] = inv[0] = 1; int n = Math.min(fact.length, mod); for (int i = 1; i < n; i++) { fact[i] = i; fact[i] = (int) ((long) fact[i] * fact[i - 1] % mod); } if (n - 1 >= 0) { inv[n - 1] = BigInteger.valueOf(fact[n - 1]).modInverse(BigInteger.valueOf(mod)).intValue(); } for (int i = n - 2; i >= 1; i--) { inv[i] = (int) ((long) inv[i + 1] * (i + 1) % mod); } } public Factorial(int limit, int mod) { this(new int[Math.min(limit + 1, mod)], new int[Math.min(limit + 1, mod)], mod); } public int fact(int n) { if (n >= mod) { return 0; } return fact[n]; } public int invFact(int n) { if (n >= mod) { throw new IllegalArgumentException(); } return inv[n]; } } static class DigitUtils { private DigitUtils() { } public static int floorDiv(int a, int b) { return a < 0 ? -ceilDiv(-a, b) : a / b; } public static int ceilDiv(int a, int b) { if (a < 0) { return -floorDiv(-a, b); } int c = a / b; if (c * b < a) { return c + 1; } return c; } } static interface IntCombination { } static class FastInput { private final InputStream is; private StringBuilder defaultStringBuf = new StringBuilder(1 << 13); private byte[] buf = new byte[1 << 13]; private int bufLen; private int bufOffset; private int next; public FastInput(InputStream is) { this.is = is; } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { bufLen = -1; } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public String next() { return readString(); } public int ri() { return readInt(); } public int readInt() { boolean rev = false; skipBlank(); if (next == '+' || next == '-') { rev = next == '-'; next = read(); } int val = 0; while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } return rev ? val : -val; } public String readString(StringBuilder builder) { skipBlank(); while (next > 32) { builder.append((char) next); next = read(); } return builder.toString(); } public String readString() { defaultStringBuf.setLength(0); return readString(defaultStringBuf); } public int rs(char[] data, int offset) { return readString(data, offset); } public int rs(char[] data) { return rs(data, 0); } public int readString(char[] data, int offset) { skipBlank(); int originalOffset = offset; while (next > 32) { data[offset++] = (char) next; next = read(); } return offset - originalOffset; } } }
Java
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 8
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
89e2ba1f91fcdaaf879aa068eee5d521
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
import java.io.*; import java.util.*; import java.util.StringTokenizer; public class AquaMoonAndChess { static long mod = 998244353; static long x, y; static void gcdExtended(long a, long b){ if(a%b==0){ x = 1; y = 1 - (a/b); return; } gcdExtended(b, a%b); long t = y; y = x - ((a/b)*y)%mod; x = t; } static long inverseModulo(long a){ gcdExtended(a, mod); x = (x%mod + mod)%mod; return x; } static long fact(long n){ long f = 1; for(long i=1;i<=n;i++) f = (f * i)%mod; return f; } static long nCr(long n, long r){ long num = fact(n); long deno = (fact(r) * fact(n - r))%mod; deno = inverseModulo(deno); num = (num * deno)%mod; return num; } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); StringBuilder sb = new StringBuilder(); while (t-->0){ int n = sc.nextInt(); String s = sc.next(); long pairs = 0; long zeros = 0; for(int i=0;i<n;i++) { if(s.charAt(i)=='1'){ if(i<n-1 && s.charAt(i+1)=='1'){ i++; pairs++; } } else zeros++; } //System.out.println(pairs+" "+zeros); long ans = nCr(zeros + pairs, pairs); sb.append(ans).append("\n"); } System.out.println(sb); sc.close(); } static class Soumit { final private int BUFFER_SIZE = 1 << 18; final private DataInputStream din; final private byte[] buffer; private PrintWriter pw; private int bufferPointer, bytesRead; StringTokenizer st; public Soumit() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Soumit(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public void streamOutput(String file) throws IOException { FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); pw = new PrintWriter(bw); } public void println(String a) { pw.println(a); } public void print(String a) { pw.print(a); } public String readLine() throws IOException { byte[] buf = new byte[3000064]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public void sort(int[] arr) { ArrayList<Integer> arlist = new ArrayList<>(); for (int i : arr) arlist.add(i); Collections.sort(arlist); for (int i = 0; i < arr.length; i++) arr[i] = arlist.get(i); } public void sort(long[] arr) { ArrayList<Long> arlist = new ArrayList<>(); for (long i : arr) arlist.add(i); Collections.sort(arlist); for (int i = 0; i < arr.length; i++) arr[i] = arlist.get(i); } public int[] nextIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } public double[] nextDoubleArray(int n) throws IOException { double[] arr = new double[n]; for (int i = 0; i < n; i++) { arr[i] = nextDouble(); } return arr; } 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;*/ if (din != null) din.close(); if (pw != null) pw.close(); } } }
Java
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 11
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
d7453e727bbb59f666ae89e24760fdde
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
import java.util.*; import java.util.Map.Entry; import java.lang.*; import java.io.*; import java.math.BigInteger; public class CF { private static FS sc = new FS(); private static class FS { 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) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } private static class extra { static int[] intArr(int size) { int[] a = new int[size]; for(int i = 0; i < size; i++) a[i] = sc.nextInt(); return a; } static long[] longArr(int size) { long[] a = new long[size]; for(int i = 0; i < size; i++) a[i] = sc.nextLong(); return a; } static long intSum(int[] a) { long sum = 0; for(int i = 0; i < a.length; i++) { sum += a[i]; } return sum; } static long longSum(long[] a) { long sum = 0; for(int i = 0; i < a.length; i++) { sum += a[i]; } return sum; } static LinkedList[] graphD(int vertices, int edges) { LinkedList<Integer>[] temp = new LinkedList[vertices+1]; for(int i = 0; i <= vertices; i++) temp[i] = new LinkedList<>(); for(int i = 0; i < edges; i++) { int x = sc.nextInt(); int y = sc.nextInt(); temp[x].add(y); } return temp; } static LinkedList[] graphUD(int vertices, int edges) { LinkedList<Integer>[] temp = new LinkedList[vertices+1]; for(int i = 0; i <= vertices; i++) temp[i] = new LinkedList<>(); for(int i = 0; i < edges; i++) { int x = sc.nextInt(); int y = sc.nextInt(); temp[x].add(y); temp[y].add(x); } return temp; } static void printG(LinkedList[] temp) { for(LinkedList<Integer> aa:temp) System.out.println(aa); } static long cal(long val, long pow) { if(pow == 0) return 1; long res = cal(val, pow/2); long ret = (res*res)%mod; if(pow%2 == 0) return ret; return (val*ret)%mod; } static long gcd(long a, long b) { return b == 0 ? a:gcd(b, a%b); } } // static int mod = (int) 1e9 + 7; static int mod = (int) 998244353; //static int max = (int) 1e6;//, sq = 316; static LinkedList<Integer>[] temp; static double ans; public static void main(String[] args) { int t = sc.nextInt(); // int t = 1; StringBuilder ret = new StringBuilder(); // int k = 1; long[] fact = new long[(int)(1e5+100)], inv = new long[(int)(1e5+100)]; fact[0] = inv[0] = 1; for(int i = 1; i < 1e5+100; i++) { fact[i] = (fact[i-1]*i)%mod; inv[i] = extra.cal(fact[i], mod-2); } while(t-- > 0) { int n = sc.nextInt(); String s = sc.next(); int z = 0, g = 0; for(int i = 0; i < n; i++) { if(s.charAt(i) == '0') z++; else { if(i+1 < n && s.charAt(i+1) == '1') { g++; i++; } } } // System.out.println(g + " " + z); // for(int i = 1; i < 10; i++) System.out.println((inv[i]*fact[i])%mod); long ans = fact[g+z]*inv[g]%mod*inv[z]%mod; ret.append(ans + "\n"); } System.out.println(ret); } }
Java
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 11
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
33f519b7b948439a889c3094be116b70
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class B { public static void main(String[] args) { init(100000); Scanner s = new Scanner(System.in); int lines = s.nextInt(); s.nextLine(); for (int i = 0; i < lines; i += 1) { solve(s.nextInt(), s.next()); } } public static void solve(int n, String p) { int one = 0; int select = 0; int out = 0; for (int i = 0; i < n; i++) { if (p.charAt(i) == '1') { if (one % 2 == 1) select++; one++; } else { out++; one = 0; } } System.out.println(out(select, out + select)); } static long mod = 998244353; public static long out(int select, int out) { return (((fact[out] * invFact[select]) % mod) * invFact[out - select]) % mod; } static long inv(long a, long m) { if (a == 1) return 1; return inv(m%a, m) * (m - m/a) % m; } static long fact[]; static long invFact[]; static void init(int l) { fact = new long[l + 1]; invFact = new long[l + 1]; fact[0] = 1; invFact[0] = 1; long a = 1; long inv = 1; for (long i = 1; i <= l; i++) { a = (a * i) % mod; inv = (inv * inv(i, mod)) % mod; fact[(int) (i)] = a; invFact[(int) (i)] = inv; } } }
Java
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 11
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
b27752bd265372e8f0df976a4756fb84
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); private static int nextInt() { try { in.nextToken(); } catch (IOException e) { e.printStackTrace(); } return (int) in.nval; } private static String next() { try { in.nextToken(); } catch (IOException e) { e.printStackTrace(); } return in.sval; } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(new BufferedInputStream(System.in)); int t = sc.nextInt(); F[0] = rF[0] = 1; for (int i = 1; i < MAXN; i++) { F[i] = F[i-1] * i % MOD; rF[i] = rF[i-1] * inv(i, MOD) % MOD; } for (int c = 0; c < t; c++) { int n = sc.nextInt(); String s = sc.next(); int group = 0; int zero = 0; int one = 0; for (int i = 0; i < n; i++) { char cc = s.charAt(i); if(cc=='0'){ zero++; }else if(i+1<n&&s.charAt(i+1)=='1'){ group++; i++; }else { one++; } } long ans = F[group+zero] * rF[zero] % MOD * rF[group] % MOD; System.out.println(ans); } } static int MOD = 998244353; static int MAXN = 100010; static long[] F = new long[MAXN]; static long[] rF = new long[MAXN]; static long[] INV = new long[(int) (1e5 + 10)]; static long inv(int t, int p) { return INV[t] = t == 1 ? 1 : (p - p / t) * inv(p % t, p) % p; } }
Java
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 11
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
9f05a137d798e9cec07bc3187d93c143
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
public class Main { public static void main(String[] args) { new Main(); } public Main() { FastScanner fs = new FastScanner(); java.io.PrintWriter out = new java.io.PrintWriter(System.out); solve(fs, out); out.flush(); } public void solve(FastScanner fs, java.io.PrintWriter out) { int t = fs.nextInt(); calc(2_000_000); while(t --> 0) { int n = fs.nextInt(); char[] s = fs.next().toCharArray(); int zero = 0, one = 0; for (char c : s) if (c == '0') ++ zero; for (int i = 1;i < s.length; ++ i) { if (s[i - 1] == '1' && s[i] == '1') { ++ one; s[i - 1] = s[i] = '0'; } } out.println(comb(zero + one, zero)); } } final int MOD = 998_244_353; int plus(int n, int m) { int sum = n + m; if (sum >= MOD) sum -= MOD; return sum; } int minus(int n, int m) { int sum = n - m; if (sum < 0) sum += MOD; return sum; } int times(int n, int m) { return (int)((long)n * m % MOD); } int divide(int n, int m) { return times(n, IntMath.pow(m, MOD - 2, MOD)); } int[] fact, invf; void calc(int len) { len += 2; fact = new int[len]; invf = new int[len]; fact[0] = fact[1] = invf[0] = invf[1] = 1; for (int i = 2;i < fact.length;++ i) fact[i] = times(fact[i - 1], i); invf[len - 1] = divide(1, fact[len - 1]); for (int i = len - 1;i > 1;-- i) invf[i - 1] = times(i, invf[i]); } int comb(int n, int m) { if (n < m) return 0; return times(fact[n], times(invf[n - m], invf[m])); } } class FastScanner { private final java.io.InputStream in = System.in; private final byte[] buffer = new byte[8192]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) return true; ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } return buflen > 0; } private byte readByte() { return hasNextByte() ? buffer[ptr++ ] : -1; } private static boolean isPrintableChar(byte c) { return 32 < c || c < 0; } private static boolean isNumber(int c) { return '0' <= c && c <= '9'; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++ ; return hasNextByte(); } public String next() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); byte b; while (isPrintableChar(b = readByte())) sb.appendCodePoint(b); return sb.toString(); } public final char nextChar() { if (!hasNext()) throw new java.util.NoSuchElementException(); return (char)readByte(); } public final long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; try { byte b = readByte(); if (b == '-') { while (isNumber(b = readByte())) n = n * 10 + '0' - b; return n; } else if (!isNumber(b)) throw new NumberFormatException(); do n = n * 10 + b - '0'; while (isNumber(b = readByte())); } catch (java.util.NoSuchElementException e) {} return n; } public final int nextInt() { if (!hasNext()) throw new java.util.NoSuchElementException(); int n = 0; try { byte b = readByte(); if (b == '-') { while (isNumber(b = readByte())) n = n * 10 + '0' - b; return n; } else if (!isNumber(b)) throw new NumberFormatException(); do n = n * 10 + b - '0'; while (isNumber(b = readByte())); } catch (java.util.NoSuchElementException e) {} return n; } public double nextDouble() { return Double.parseDouble(next()); } } class Arrays { public static void sort(final int[] array) { sort(array, 0, array.length); } public static void sort(final int[] array, int fromIndex, int toIndex) { if (toIndex - fromIndex <= 512) { java.util.Arrays.sort(array, fromIndex, toIndex); return; } sort(array, fromIndex, toIndex, 0, new int[array.length]); } private static final void sort(int[] a, final int from, final int to, final int l, final int[] bucket) { if (to - from <= 512) { java.util.Arrays.sort(a, from, to); return; } final int BUCKET_SIZE = 256; final int INT_RECURSION = 4; final int MASK = 0xff; final int shift = l << 3; final int[] cnt = new int[BUCKET_SIZE + 1]; final int[] put = new int[BUCKET_SIZE]; for (int i = from; i < to; i++) ++ cnt[(a[i] >>> shift & MASK) + 1]; for (int i = 0; i < BUCKET_SIZE; i++) cnt[i + 1] += cnt[i]; for (int i = from; i < to; i++) { int bi = a[i] >>> shift & MASK; bucket[cnt[bi] + put[bi]++] = a[i]; } for (int i = BUCKET_SIZE - 1, idx = from; i >= 0; i--) { int begin = cnt[i]; int len = cnt[i + 1] - begin; System.arraycopy(bucket, begin, a, idx, len); idx += len; } final int nxtL = l + 1; if (nxtL < INT_RECURSION) { sort(a, from, to, nxtL, bucket); if (l == 0) { int lft, rgt; lft = from - 1; rgt = to; while (rgt - lft > 1) { int mid = lft + rgt >> 1; if (a[mid] < 0) lft = mid; else rgt = mid; } reverse(a, from, rgt); reverse(a, rgt, to); } } } public static void sort(final long[] array) { sort(array, 0, array.length); } public static void sort(final long[] array, int fromIndex, int toIndex) { if (toIndex - fromIndex <= 512) { java.util.Arrays.sort(array, fromIndex, toIndex); return; } sort(array, fromIndex, toIndex, 0, new long[array.length]); } private static final void sort(long[] a, final int from, final int to, final int l, final long[] bucket) { final int BUCKET_SIZE = 256; final int LONG_RECURSION = 8; final int MASK = 0xff; final int shift = l << 3; final int[] cnt = new int[BUCKET_SIZE + 1]; final int[] put = new int[BUCKET_SIZE]; for (int i = from; i < to; i++) ++ cnt[(int) ((a[i] >>> shift & MASK) + 1)]; for (int i = 0; i < BUCKET_SIZE; i++) cnt[i + 1] += cnt[i]; for (int i = from; i < to; i++) { int bi = (int) (a[i] >>> shift & MASK); bucket[cnt[bi] + put[bi]++] = a[i]; } for (int i = BUCKET_SIZE - 1, idx = from; i >= 0; i--) { int begin = cnt[i]; int len = cnt[i + 1] - begin; System.arraycopy(bucket, begin, a, idx, len); idx += len; } final int nxtL = l + 1; if (nxtL < LONG_RECURSION) { sort(a, from, to, nxtL, bucket); if (l == 0) { int lft, rgt; lft = from - 1; rgt = to; while (rgt - lft > 1) { int mid = lft + rgt >> 1; if (a[mid] < 0) lft = mid; else rgt = mid; } reverse(a, from, rgt); reverse(a, rgt, to); } } } public static void reverse(int[] array) { reverse(array, 0, array.length); } public static void reverse(int[] array, int fromIndex, int toIndex) { for (-- toIndex;fromIndex < toIndex;++ fromIndex, -- toIndex) { int swap = array[fromIndex]; array[fromIndex] = array[toIndex]; array[toIndex] = swap; } } public static void reverse(long[] array) { reverse(array, 0, array.length); } public static void reverse(long[] array, int fromIndex, int toIndex) { for (-- toIndex;fromIndex < toIndex;++ fromIndex, -- toIndex) { long swap = array[fromIndex]; array[fromIndex] = array[toIndex]; array[toIndex] = swap; } } } class IntMath { public static int gcd(int a, int b) { while (a != 0) if ((b %= a) != 0) a %= b; else return a; return b; } public static int gcd(int... array) { int ret = array[0]; for (int i = 1; i < array.length; ++i) ret = gcd(ret, array[i]); return ret; } public static long gcd(long a, long b) { while (a != 0) if ((b %= a) != 0) a %= b; else return a; return b; } public static long gcd(long... array) { long ret = array[0]; for (int i = 1; i < array.length; ++i) ret = gcd(ret, array[i]); return ret; } public static long lcm(long a, long b) { return a / gcd(a, b) * b; } public static int pow(int a, int b) { int ans = 1; for (int mul = a; b > 0; b >>= 1, mul *= mul) if ((b & 1) != 0) ans *= mul; return ans; } public static long pow(long a, long b) { long ans = 1; for (long mul = a; b > 0; b >>= 1, mul *= mul) if ((b & 1) != 0) ans *= mul; return ans; } public static int pow(int a, long b, int mod) { if (b < 0) b = b % (mod - 1) + mod - 1; long ans = 1; for (long mul = a; b > 0; b >>= 1, mul = mul * mul % mod) if ((b & 1) != 0) ans = ans * mul % mod; return (int)ans; } public static int pow(long a, long b, int mod) { return pow((int)(a % mod), b, mod); } public static int floorsqrt(long n) { return (int)Math.sqrt(n + 0.1); } public static int ceilsqrt(long n) { return n <= 1 ? (int)n : (int)Math.sqrt(n - 0.1) + 1; } }
Java
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 11
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
787b28ea8863e96049974e91ba20a928
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); BAquaMoonAndChess solver = new BAquaMoonAndChess(); solver.solve(1, in, out); out.close(); } static class BAquaMoonAndChess { int mod = 998244353; long[] fact = new long[2_000_001]; long[] invfact = new long[2_000_001]; public void solve(int testNumber, InputReader in, OutputWriter out) { precompFacts(); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); char[] arr = in.next().toCharArray(); int r = 0; int x = 0; for (int i = 0; i < n - 1; i++) { if (arr[i] == '0') x++; else if (arr[i] == '1') { if (arr[i + 1] == '1') { x++; r++; i++; } } } if (arr[n - 1] == '0') x++; out.println(nCk(x, r)); } } long mul(long a, long b) { return (a * b) % mod; } 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)); } void precompFacts() { fact[0] = invfact[0] = 1; for (int i = 1; i < fact.length; i++) fact[i] = mul(fact[i - 1], i); invfact[fact.length - 1] = exp(fact[fact.length - 1], mod - 2); for (int i = invfact.length - 2; i >= 0; i--) invfact[i] = mul(invfact[i + 1], i + 1); } long nCk(int n, int k) { return mul(fact[n], mul(invfact[k], invfact[n - k])); } } static class InputReader { BufferedReader reader; 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()); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void println(long i) { writer.println(i); } } }
Java
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 11
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
648525c350f8974177457373599b7dc3
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
import java.util.*; import java.io.*; public class _1545_B { static final long MOD = 998244353; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int t = Integer.parseInt(in.readLine()); while(t-- > 0) { int n = Integer.parseInt(in.readLine()); String board = in.readLine(); int pairs = 0; int zeros = 0; if(board.charAt(0) == '0') { zeros++; } for(int i = 1; i < n; i++) { char prev = board.charAt(i - 1); char cur = board.charAt(i); if(cur == '1' && prev == '1') { if(i < n - 1 && board.charAt(i + 1) == '0') { zeros++; } pairs++; i++; }else if(cur == '0') { zeros++; } } out.println(binom(pairs + zeros, zeros)); } in.close(); out.close(); } static long binom(int n, int k) { long res = 1; for(int i = n; i >= n - k + 1; i--) { res = modmult(res, i); } for(int i = 2; i <= k; i++) { res = modmult(res, modinv(i)); } return res; } static long modadd(long a, long b) { return (a + b + MOD) % MOD; } static long modmult(long a, long b) { return a * b % MOD; } static long modinv(long a) { return binpow(a, MOD - 2); } static long binpow(long a, long b) { if (b == 0) { return 1; } long small = binpow(a, b / 2); if (b % 2 == 0) { return modmult(small, small); } else { return modmult(modmult(small, small), a); } } }
Java
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 11
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
60e88b04f7d0285c5167a42a8afb4e8e
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.*; public class cf1545b { public static void main(String[] args) throws IOException { int t = ri(), fact[] = new int[100005], ifact[] = new int[100005]; fact[0] = fact[1] = ifact[0] = ifact[1] = 1; for (int i = 2; i <= 100000; ++i) { fact[i] = mmul(i, fact[i - 1]); ifact[i] = minv(fact[i]); } while (t --> 0) { int n = ri(), slot = 0, slider = 0; char[] s = rcha(); for (int i = 0; i < n; ++i) { if (s[i] == '0') { ++slot; } else if (i < n - 1 && s[i + 1] == '1') { ++slider; ++i; } } prln(mmul(fact[slot + slider], ifact[slot], ifact[slider])); } close(); } static int mmod = 998244353; static int madd(int a, int b) { return (a + b) % mmod; } static int madd(int... a) { int ans = a[0]; for (int i = 1; i < a.length; ++i) { ans = madd(ans, a[i]); } return ans; } static int msub(int a, int b) { return (a - b + mmod) % mmod; } static int mmul(int a, int b) { return (int) ((long) a * b % mmod); } static int mmul(int... a) { int ans = a[0]; for (int i = 1; i < a.length; ++i) { ans = mmul(ans, a[i]); } return ans; } static int minv(int x) { // return mpow(x, mmod - 2); return (exgcd(x, mmod)[0] % mmod + mmod) % mmod; } static int mpow(int a, long b) { if (a == 0) { return 0; } int ans = 1; while (b > 0) { if ((b & 1) > 0) { ans = mmul(ans, a); } a = mmul(a, a); b >>= 1; } return ans; } static BufferedReader __i = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter __o = new PrintWriter(new OutputStreamWriter(System.out)); static StringTokenizer input; static Random __r = new Random(); // references // IBIG = 1e9 + 7 // IMAX ~= 2e9 // LMAX ~= 9e18 // constants static final int IBIG = 1000000007; static final int IMAX = 2147483647; static final long LMAX = 9223372036854775807L; // math util static int minof(int a, int b, int c) {return min(a, min(b, c));} static int minof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;} static long minof(long a, long b, long c) {return min(a, min(b, c));} static long minof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;} static int maxof(int a, int b, int c) {return max(a, max(b, c));} static int maxof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;} static long maxof(long a, long b, long c) {return max(a, max(b, c));} static long maxof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;} static int powi(int a, int b) {if (a == 0) return 0; int ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static long powl(long a, int b) {if (a == 0) return 0; long ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static int fli(double d) {return (int) d;} static int cei(double d) {return (int) ceil(d);} static long fll(double d) {return (long) d;} static long cel(double d) {return (long) ceil(d);} static int gcd(int a, int b) {return b == 0 ? a : gcd(b, a % b);} static long gcd(long a, long b) {return b == 0 ? a : gcd(b, a % b);} static int[] exgcd(int a, int b) {if (b == 0) return new int[] {1, 0}; int[] y = exgcd(b, a % b); return new int[] {y[1], y[0] - y[1] * (a / b)};} static long[] exgcd(long a, long b) {if (b == 0) return new long[] {1, 0}; long[] y = exgcd(b, a % b); return new long[] {y[1], y[0] - y[1] * (a / b)};} static int randInt(int min, int max) {return __r.nextInt(max - min + 1) + min;} static long mix(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);} // array util static void reverse(int[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void reverse(long[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void reverse(double[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void reverse(char[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void shuffle(int[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void shuffle(long[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void shuffle(double[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void rsort(int[] a) {shuffle(a); sort(a);} static void rsort(long[] a) {shuffle(a); sort(a);} static void rsort(double[] a) {shuffle(a); sort(a);} static int[] copy(int[] a) {int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static long[] copy(long[] a) {long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static double[] copy(double[] a) {double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static char[] copy(char[] a) {char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} // input static void r() throws IOException {input = new StringTokenizer(rline());} static int ri() throws IOException {return Integer.parseInt(rline());} static long rl() throws IOException {return Long.parseLong(rline());} static double rd() throws IOException {return Double.parseDouble(rline());} static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni(); return a;} static void ria(int[] a) throws IOException {int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = ni();} static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; return a;} static void riam1(int[] a) throws IOException {int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1;} static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for (int i = 0; i < n; ++i) a[i] = nl(); return a;} static void rla(long[] a) throws IOException {int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = nl();} static double[] rda(int n) throws IOException {double[] a = new double[n]; r(); for (int i = 0; i < n; ++i) a[i] = nd(); return a;} static void rda(double[] a) throws IOException {int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = nd();} static char[] rcha() throws IOException {return rline().toCharArray();} static void rcha(char[] a) throws IOException {int n = a.length, i = 0; for (char c : rline().toCharArray()) a[i++] = c;} static String rline() throws IOException {return __i.readLine();} static String n() {return input.nextToken();} static int rni() throws IOException {r(); return ni();} static int ni() {return Integer.parseInt(n());} static long rnl() throws IOException {r(); return nl();} static long nl() {return Long.parseLong(n());} static double rnd() throws IOException {r(); return nd();} static double nd() {return Double.parseDouble(n());} // output static void pr(int i) {__o.print(i);} static void prln(int i) {__o.println(i);} static void pr(long l) {__o.print(l);} static void prln(long l) {__o.println(l);} static void pr(double d) {__o.print(d);} static void prln(double d) {__o.println(d);} static void pr(char c) {__o.print(c);} static void prln(char c) {__o.println(c);} static void pr(char[] s) {__o.print(new String(s));} static void prln(char[] s) {__o.println(new String(s));} static void pr(String s) {__o.print(s);} static void prln(String s) {__o.println(s);} static void pr(Object o) {__o.print(o);} static void prln(Object o) {__o.println(o);} static void prln() {__o.println();} static void pryes() {prln("yes");} static void pry() {prln("Yes");} static void prY() {prln("YES");} static void prno() {prln("no");} static void prn() {prln("No");} static void prN() {prln("NO");} static boolean pryesno(boolean b) {prln(b ? "yes" : "no"); return b;}; static boolean pryn(boolean b) {prln(b ? "Yes" : "No"); return b;} static boolean prYN(boolean b) {prln(b ? "YES" : "NO"); return b;} static void prln(int... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();} static void prln(long... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();} static void prln(double... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();} static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i); if (n >= 0) prln(iter.next()); else prln();} static void h() {prln("hlfd"); flush();} static void flush() {__o.flush();} static void close() {__o.close();} }
Java
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 11
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
854287fb1f61a767bc67bcdae7c0f905
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
import java.util.*; import java.io.*; public class Sol{ // ************ n==1 ***************** important test case static ArrayList<Integer> g[]; static HashSet<Integer> s[]; public static void main(String []args){ int times=ni(); while(times-->0){ int n=ni(); String s=ns(); s+='0';n++;//out.println(s); int odd_count=0;int even_count=0; int curr_count=0;int one_count=0; int i=0; while(i<n){ if(s.charAt(i)=='0'){ if(curr_count%2==0){even_count++;} else odd_count++; curr_count=0; } else {curr_count++;one_count++;} i++; } one_count-=odd_count; int r=odd_count+even_count; one_count/=2; long N=one_count+r-1; long R=r-1; //n c r long ans= factorial(N)*inv(factorial(R)); ans%=mod; ans=ans*inv(factorial(N-R)); ans%=mod; //out.println(ans+" "+one_count+" "+odd_count+" "+even_count+" "+N+" "+R); out.println(ans); }out.close();} //-----------------Utility-------------------------------------------- static int gcd(int a,int b){if(b==0)return a; return gcd(b,a%b);} static int Max=Integer.MAX_VALUE; static long mod=998244353; static int v(char c){return (int)(c-'a')+1;} static long factorial(long n){ long ans=1l; long i=1l; while(i<=n){ans*=i;ans%=mod;i++;} return ans; } static long inv(long n){ return power(n,mod-2); } public static long power(long x, long y ) { //0^0 = 1 long res = 1L; x = x%mod; while(y > 0) { if((y&1)==1) res = (res*x)%mod; y >>= 1; x = (x*x)%mod; } return res; } static class Pair implements Comparable<Pair>{ int id;int out; public Pair(int id,int out) { this.id=id;this.out=out; } @Override public int compareTo(Pair p){return Long.compare(out,p.out);} //public int compareTo(Pair p){return id-p.id;} } //----------------------I/O--------------------------------------------- static InputStream inputStream = System.in; static OutputStream outputStream = System.out; static FastReader in=new FastReader(inputStream); static PrintWriter out=new PrintWriter(outputStream); static class FastReader { BufferedReader br; StringTokenizer st; FastReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int ni() { return in.nextInt(); } static long nl(){return in.nextLong();} static String ns(){return in.nextLine();} }
Java
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 11
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
92871c846885c8c8890ff3a897366baf
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
import java.util.*; import java.io.*; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.Math.max; public class EdA { static long[] mods = {1000000007, 998244353, 1000000009}; static long mod = mods[1]; public static MyScanner sc; public static PrintWriter out; static long [] fact; public static void main(String[] largewang) throws Exception{ // TODO Auto-generated method stub sc = new MyScanner(); out = new PrintWriter(System.out); int t = sc.nextInt(); fact = new long[100001]; fact[0] = 1; for(int j = 1;j<=100000;j++){ fact[j] = fact[j-1] * j; fact[j] %= mod; } while (t-->0) { int n = sc.nextInt(); char[] arr = sc.next().toCharArray(); char prev = '.'; int sum = 0; int zero = 0; int run = 0; for(int j = 0;j<arr.length;j++){ if (arr[j] == '1') { run++; } else { zero++; sum += run/2; run = 0; } } sum += run/2; long prod = fact[sum+zero]; prod *= inv(fact[zero]); prod %= mod; prod *= inv(fact[sum]); prod %= mod; out.println(prod); } out.close(); } public static long inv(long n){ return power(n, mod-2); } static class Pair implements Comparable<Pair>{ private int x; private int index; public Pair(int x, int index){ this.x = x; this.index= index; } public int compareTo(Pair other){ return Integer.compare(x, other.x) != 0 ? Integer.compare(x, other.x) : Integer.compare(index, other.index); } } public static void sort(int[] array){ ArrayList<Integer> copy = new ArrayList<>(); for (int i : array) copy.add(i); Collections.sort(copy); for(int i = 0;i<array.length;i++) array[i] = copy.get(i); } static String[] readArrayString(int n){ String[] array = new String[n]; for(int j =0 ;j<n;j++) array[j] = sc.next(); return array; } static int[] readArrayInt(int n){ int[] array = new int[n]; for(int j = 0;j<n;j++) array[j] = sc.nextInt(); return array; } static int[] readArrayInt1(int n){ int[] array = new int[n+1]; for(int j = 1;j<=n;j++){ array[j] = sc.nextInt(); } return array; } static long[] readArrayLong(int n){ long[] array = new long[n]; for(int j =0 ;j<n;j++) array[j] = sc.nextLong(); return array; } static double[] readArrayDouble(int n){ double[] array = new double[n]; for(int j =0 ;j<n;j++) array[j] = sc.nextDouble(); return array; } static int minIndex(int[] array){ int minValue = Integer.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static int minIndex(long[] array){ long minValue = Long.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static int minIndex(double[] array){ double minValue = Double.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static long power(long x, long y){ if (y == 0) return 1; if (y%2 == 1) return (x*power(x, y-1))%mod; return power((x*x)%mod, y/2)%mod; } static void verdict(boolean a){ out.println(a ? "YES" : "NO"); } 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; } } } //StringJoiner sj = new StringJoiner(" "); //sj.add(strings) //sj.toString() gives string of those stuff w spaces or whatever that sequence is
Java
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 11
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
ed2b61e66a59b4b6103e60b7d806ece9
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
import java.util.*; import java.io.*; import java.text.*; public class CF_1545_B{ //SOLUTION BEGIN long MOD = 998244353; void pre() throws Exception{} void solve(int TC) throws Exception{ int N = ni(); List<Integer> l = new ArrayList<>(); String S = n(); int c = 0; for(int i = 0; i< N; i++)if(S.charAt(i) == '0')c++; int d = 0; for(int i = 0, ii = 0; i< N; i = ii){ while(i < N && S.charAt(i) == '0')i++; ii = i; if(i == N)break; while (ii< N && S.charAt(ii) == '1')ii++; d += (ii-i)/2; } pn(C(c+d, d)); } long C(long n, long r){ long num = 1, den = 1; r = Math.max(r, n-r); for(long i = r+1; i<= n; i++){ num = num*i%MOD; den = den*(i-r)%MOD; } return num*inv(den)%MOD; } long inv(long a){return pow(a, MOD-2);} long pow(long a, long p){ long o = 1; while(p>0){ if(p%2==1)o = (a*o)%MOD; a = (a*a)%MOD; p>>=1; } return o; } //SOLUTION END void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");} void exit(boolean b){if(!b)System.exit(0);} static void dbg(Object... o){System.err.println(Arrays.deepToString(o));} final long IINF = (long)1e17; final int INF = (int)1e9+2; DecimalFormat df = new DecimalFormat("0.00000000000"); double PI = 3.141592653589793238462643383279502884197169399, eps = 1e-8; static boolean multipleTC = true, memory = true, fileIO = false; FastReader in;PrintWriter out; void run() throws Exception{ long ct = System.currentTimeMillis(); if (fileIO) { in = new FastReader(""); out = new PrintWriter(""); } else { in = new FastReader(); out = new PrintWriter(System.out); } //Solution Credits: Taranpreet Singh int T = multipleTC? ni():1; pre(); for (int t = 1; t <= T; t++) solve(t); out.flush(); out.close(); System.err.println(System.currentTimeMillis() - ct); } public static void main(String[] args) throws Exception{ if(memory)new Thread(null, new Runnable() {public void run(){try{new CF_1545_B().run();}catch(Exception e){e.printStackTrace();System.exit(1);}}}, "1", 1 << 28).start(); else new CF_1545_B().run(); } int[][] make(int n, int e, int[] from, int[] to, boolean f){ int[][] g = new int[n][];int[]cnt = new int[n]; for(int i = 0; i< e; i++){ cnt[from[i]]++; if(f)cnt[to[i]]++; } for(int i = 0; i< n; i++)g[i] = new int[cnt[i]]; for(int i = 0; i< e; i++){ g[from[i]][--cnt[from[i]]] = to[i]; if(f)g[to[i]][--cnt[to[i]]] = from[i]; } return g; } int[][][] makeS(int n, int e, int[] from, int[] to, boolean f){ int[][][] g = new int[n][][];int[]cnt = new int[n]; for(int i = 0; i< e; i++){ cnt[from[i]]++; if(f)cnt[to[i]]++; } for(int i = 0; i< n; i++)g[i] = new int[cnt[i]][]; for(int i = 0; i< e; i++){ g[from[i]][--cnt[from[i]]] = new int[]{to[i], i, 0}; if(f)g[to[i]][--cnt[to[i]]] = new int[]{from[i], i, 1}; } return g; } int find(int[] set, int u){return set[u] = (set[u] == u?u:find(set, set[u]));} int digit(long s){int ans = 0;while(s>0){s/=10;ans++;}return ans;} long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);} int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);} int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));} void p(Object... o){for(Object oo:o)out.print(oo+" ");} void pn(Object... o){for(int i = 0; i< o.length; i++)out.print(o[i]+(i+1 < o.length?" ":"\n"));} void pni(Object... o){for(Object oo:o)out.print(oo+" ");out.println();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());} 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
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 11
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
9ed389463bf5e45197a7f1a9609df1db
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
import java.util.*; import java.io.*; public class B { static long mod = 998244353; public static void main(String[] args) { FastScanner sc = new FastScanner(); int T = sc.nextInt(); while(T-->0) { int n = sc.nextInt(); char[] s = sc.next().toCharArray(); int ps = 0, lones = 0; for(int i = 0; i < n; i++) { if(s[i] == '1') { if(i == n-1) lones++; else if(s[i+1] == '1') { ps++; i++; } else lones++; } } System.out.println(choose(n-lones-ps, ps)); } } static int N = 100000; static long[] fac = new long[N+1]; static long[] invfac = new long[N+1]; static long fac(int n){ if(n == 0) return fac[0] = 1; else if(fac[n] > 0) return fac[n]; else return fac[n] = n * fac(n-1) % mod; } static long invfac(int n){ if(invfac[n] > 0) return invfac[n]; else return invfac[n] = inv(fac[n]); } static long choose(int n, int k){ return fac(n) * invfac(k) % mod * invfac(n-k) % mod; } static long inv(long a){ return (gcdex(a, mod)[0] + mod) % mod; } static long[] gcdex(long a, long b){ if(b > a) { long[] p = gcdex(b, a); return new long[] {p[1], p[0]}; } else if(b == 0) return new long[] {1, 0}; else{ long[] p = gcdex(b, a % b); return new long[] {p[1], p[0] - p[1]*(a/b)}; } } static class FastScanner { public BufferedReader reader; public StringTokenizer tokenizer; public FastScanner() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return reader.readLine(); } catch(IOException e) { throw new RuntimeException(e); } } } }
Java
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 11
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
4efb5b3da79b9dc7b5a8b0be8f67eeba
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class B { public static final long MOD = 998244353; void solve() throws IOException { int t = nextInt(); for (int testCase = 0; testCase < t ; testCase++) { int n = nextInt(); String s = nextToken(); int ones = 0; int zeros = 0; int i = 0; while (i < s.length()) { if (s.charAt(i) == '0') { zeros++; i++; } else { i++; if (i < s.length() && s.charAt(i) == '1') { ones++; i++; } } } out.println(calc(ones + zeros, ones)); } } long calc(int n, int k) { long ans = 1; for (int i = k + 1; i <= n; i++) { ans *= i; if (ans >= MOD) { ans %= MOD; } } for (int i = 2; i <= (n - k); i++) { int j = i; long g = gcd(ans, j, new long[2]); j /= g; ans /= g; if (j != 1) { ans *= rev(j, MOD); if (ans >= MOD) { ans %= MOD; } } } return ans; } long rev(long a, long m) { long[] x = new long[2]; gcd(a, m, x); return (x[0] % m + m) % m; } long gcd (long a, long b, long[] x) { if (a == 0) { x[0] = 0; x[1] = 1; return b; } long[] x1 = new long[2]; long d = gcd (b % a, a, x1); x[0] = x1[1] - (b / a) * x1[0]; x[1] = x1[0]; return d; } public static void main(String[] args) throws IOException { new B().run(); } void run() throws IOException { reader = new BufferedReader(new InputStreamReader(System.in)); // reader = new BufferedReader(new FileReader("input.txt")); tokenizer = null; out = new PrintWriter(new OutputStreamWriter(System.out)); // out = new PrintWriter(new FileWriter("output.txt")); solve(); reader.close(); out.flush(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter out; int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } int[] nextIntArray(int n) throws IOException { int[] result = new int[n]; for (int i = 0; i < n; i++) { result[i] = nextInt(); } return result; } long[] nextLongArray(int n) throws IOException { long[] result = new long[n]; for (int i = 0; i < n; i++) { result[i] = nextLong(); } return result; } double[] nextDoubleArray(int n) throws IOException { double[] result = new double[n]; for (int i = 0; i < n; i++) { result[i] = nextDouble(); } return result; } int[][] nextIntArray(int n, int m) throws IOException { int[][] result = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { result[i][j] = nextInt(); } } return result; } long[][] nextLongArray(int n, int m) throws IOException { long[][] result = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { result[i][j] = nextLong(); } } return result; } double[][] nextDoubleArray(int n, int m) throws IOException { double[][] result = new double[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { result[i][j] = nextDouble(); } } return result; } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
Java
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 11
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
c0fc027b3fbb6a377cd62ff0e517a438
train_108.jsonl
1626012300
Cirno gave AquaMoon a chessboard of size $$$1 \times n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 \leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 \geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998\,244\,353$$$.
256 megabytes
//package round732; import java.io.*; import java.util.*; public class B { InputStream is; FastWriter out; String INPUT = ""; static final int mod = 998244353; int[][] fif = enumFIF(200000, mod); public static int[][] enumFIF(int n, int mod) { int[] f = new int[n + 1]; int[] invf = new int[n + 1]; f[0] = 1; for (int i = 1; i <= n; i++) { f[i] = (int) ((long) f[i - 1] * i % mod); } long a = f[n]; long b = mod; long p = 1, q = 0; while (b > 0) { long c = a / b; long d; d = a; a = b; b = d % b; d = p; p = q; q = d - c * q; } invf[n] = (int) (p < 0 ? p + mod : p); for (int i = n - 1; i >= 0; i--) { invf[i] = (int) ((long) invf[i + 1] * (i + 1) % mod); } return new int[][]{f, invf}; } void solve() { for(int T = ni();T > 0;T--)go(); } void go() { int n = ni(); char[] s = ns(n); LST lst = new LST(n); for(int i = 0;i < n;i++){ if(s[i] == '0')lst.set(i); } for(int i = 0;i < n;i++){ if(!lst.get(i))continue; int pre = lst.prev(i-1); int one = (i - pre) - 1; one = one / 2 * 2; if(one > 0) { assert !lst.get(i - one); lst.set(i - one); lst.unset(i); } } int last = lst.prev(n-1); int aone = n-1-last; aone = aone / 2; int z = 0; for(char c : s)if(c == '0')z++; out.println(C(z+aone, aone, mod, fif)); } public static long C(int n, int r, int mod, int[][] fif) { if (n < 0 || r < 0 || r > n) return 0; return (long) fif[0][n] * fif[1][r] % mod * fif[1][n - r] % mod; } public static class LST { public long[][] set; public int n; // public int size; public LST(int n) { this.n = n; int d = 1; for(int m = n;m > 1;m>>>=6, d++); set = new long[d][]; for(int i = 0, m = n>>>6;i < d;i++, m>>>=6){ set[i] = new long[m+1]; } // size = 0; } // [0,r) public LST setRange(int r) { for(int i = 0;i < set.length;i++, r=r+63>>>6){ for(int j = 0;j < r>>>6;j++){ set[i][j] = -1L; } if((r&63) != 0)set[i][r>>>6] |= (1L<<r)-1; } return this; } // [0,r) public LST unsetRange(int r) { if(r >= 0){ for(int i = 0;i < set.length;i++, r=r+63>>>6){ for(int j = 0;j < r+63>>>6;j++){ set[i][j] = 0; } if((r&63) != 0)set[i][r>>>6] &= ~((1L<<r)-1); } } return this; } public LST set(int pos) { if(pos >= 0 && pos < n){ // if(!get(pos))size++; for(int i = 0;i < set.length;i++, pos>>>=6){ set[i][pos>>>6] |= 1L<<pos; } } return this; } public LST unset(int pos) { if(pos >= 0 && pos < n){ // if(get(pos))size--; for(int i = 0;i < set.length && (i == 0 || set[i-1][pos] == 0L);i++, pos>>>=6){ set[i][pos>>>6] &= ~(1L<<pos); } } return this; } public boolean get(int pos) { return pos >= 0 && pos < n && set[0][pos>>>6]<<~pos<0; } public LST toggle(int pos) { return get(pos) ? unset(pos) : set(pos); } public int prev(int pos) { for(int i = 0;i < set.length && pos >= 0;i++, pos>>>=6, pos--){ int pre = prev(set[i][pos>>>6], pos&63); if(pre != -1){ pos = pos>>>6<<6|pre; while(i > 0)pos = pos<<6|63-Long.numberOfLeadingZeros(set[--i][pos]); return pos; } } return -1; } public int next(int pos) { for(int i = 0;i < set.length && pos>>>6 < set[i].length;i++, pos>>>=6, pos++){ int nex = next(set[i][pos>>>6], pos&63); if(nex != -1){ pos = pos>>>6<<6|nex; while(i > 0)pos = pos<<6|Long.numberOfTrailingZeros(set[--i][pos]); return pos; } } return -1; } private static int prev(long set, int n) { long h = set<<~n; if(h == 0L)return -1; return -Long.numberOfLeadingZeros(h)+n; } private static int next(long set, int n) { long h = set>>>n; if(h == 0L)return -1; return Long.numberOfTrailingZeros(h)+n; } @Override public String toString() { List<Integer> list = new ArrayList<Integer>(); for(int pos = next(0);pos != -1;pos = next(pos+1)){ list.add(pos); } return list.toString(); } } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new FastWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new B().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private long[] nal(int n) { long[] a = new long[n]; for(int i = 0;i < n;i++)a[i] = nl(); return a; } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[][] nmi(int n, int m) { int[][] map = new int[n][]; for(int i = 0;i < n;i++)map[i] = na(m); return map; } private int ni() { return (int)nl(); } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } public static class FastWriter { private static final int BUF_SIZE = 1<<13; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastWriter(){out = null;} public FastWriter(OutputStream os) { this.out = os; } public FastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public FastWriter write(byte b) { buf[ptr++] = b; if(ptr == BUF_SIZE)innerflush(); return this; } public FastWriter write(char c) { return write((byte)c); } public FastWriter write(char[] s) { for(char c : s){ buf[ptr++] = (byte)c; if(ptr == BUF_SIZE)innerflush(); } return this; } public FastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte)c; if(ptr == BUF_SIZE)innerflush(); }); return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } public FastWriter write(int x) { if(x == Integer.MIN_VALUE){ return write((long)x); } if(ptr + 12 >= BUF_SIZE)innerflush(); if(x < 0){ write((byte)'-'); x = -x; } int d = countDigits(x); for(int i = ptr + d - 1;i >= ptr;i--){ buf[i] = (byte)('0'+x%10); x /= 10; } ptr += d; return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastWriter write(long x) { if(x == Long.MIN_VALUE){ return write("" + x); } if(ptr + 21 >= BUF_SIZE)innerflush(); if(x < 0){ write((byte)'-'); x = -x; } int d = countDigits(x); for(int i = ptr + d - 1;i >= ptr;i--){ buf[i] = (byte)('0'+x%10); x /= 10; } ptr += d; return this; } public FastWriter write(double x, int precision) { if(x < 0){ write('-'); x = -x; } x += Math.pow(10, -precision)/2; // if(x < 0){ x = 0; } write((long)x).write("."); x -= (long)x; for(int i = 0;i < precision;i++){ x *= 10; write((char)('0'+(int)x)); x -= (int)x; } return this; } public FastWriter writeln(char c){ return write(c).writeln(); } public FastWriter writeln(int x){ return write(x).writeln(); } public FastWriter writeln(long x){ return write(x).writeln(); } public FastWriter writeln(double x, int precision){ return write(x, precision).writeln(); } public FastWriter write(int... xs) { boolean first = true; for(int x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter write(long... xs) { boolean first = true; for(long x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter writeln() { return write((byte)'\n'); } public FastWriter writeln(int... xs) { return write(xs).writeln(); } public FastWriter writeln(long... xs) { return write(xs).writeln(); } public FastWriter writeln(char[] line) { return write(line).writeln(); } public FastWriter writeln(char[]... map) { for(char[] line : map)write(line).writeln(); return this; } public FastWriter writeln(String s) { return write(s).writeln(); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } public FastWriter print(byte b) { return write(b); } public FastWriter print(char c) { return write(c); } public FastWriter print(char[] s) { return write(s); } public FastWriter print(String s) { return write(s); } public FastWriter print(int x) { return write(x); } public FastWriter print(long x) { return write(x); } public FastWriter print(double x, int precision) { return write(x, precision); } public FastWriter println(char c){ return writeln(c); } public FastWriter println(int x){ return writeln(x); } public FastWriter println(long x){ return writeln(x); } public FastWriter println(double x, int precision){ return writeln(x, precision); } public FastWriter print(int... xs) { return write(xs); } public FastWriter print(long... xs) { return write(xs); } public FastWriter println(int... xs) { return writeln(xs); } public FastWriter println(long... xs) { return writeln(xs); } public FastWriter println(char[] line) { return writeln(line); } public FastWriter println(char[]... map) { return writeln(map); } public FastWriter println(String s) { return writeln(s); } public FastWriter println() { return writeln(); } } public void trnz(int... o) { for(int i = 0;i < o.length;i++)if(o[i] != 0)System.out.print(i+":"+o[i]+" "); System.out.println(); } // print ids which are 1 public void trt(long... o) { Queue<Integer> stands = new ArrayDeque<>(); for(int i = 0;i < o.length;i++){ for(long x = o[i];x != 0;x &= x-1)stands.add(i<<6|Long.numberOfTrailingZeros(x)); } System.out.println(stands); } public void tf(boolean... r) { for(boolean x : r)System.out.print(x?'#':'.'); System.out.println(); } public void tf(boolean[]... b) { for(boolean[] r : b) { for(boolean x : r)System.out.print(x?'#':'.'); System.out.println(); } System.out.println(); } public void tf(long[]... b) { if(INPUT.length() != 0) { for (long[] r : b) { for (long x : r) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } System.out.println(); } } public void tf(long... b) { if(INPUT.length() != 0) { for (long x : b) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
Java
["6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010"]
1 second
["3\n6\n1\n1287\n1287\n715"]
NoteIn the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Java 11
standard input
[ "combinatorics", "math" ]
930b296d11d6d5663a14144a491f2c0a
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,900
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998\,244\,353$$$.
standard output
PASSED
1f974d8dc435f31e369d4f53b9bd0773
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
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 { // pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); // sc = new FastScanner(new BufferedReader(new FileReader("input.txt"))); pw = new PrintWriter(System.out); sc = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); } catch (Exception e) { throw new RuntimeException(); } int t = sc.nextInt(); // int t = 1; while (t-- > 0) { // sc.nextLine(); solve(); } pw.close(); } public long mod = 1_000_000_007; public class Friend { int number; boolean isRight; int index; Friend(int number, int index) { this.number = number; this.index = index; isRight = true; } public void flip() { this.isRight = !this.isRight; } } public void solve() { int n = sc.nextInt(); Friend[] arr = new Friend[n]; for (int i = 0; i < n; i++) { arr[i] = new Friend(sc.nextInt(), i); } Friend temp; boolean found = false; Arrays.sort(arr, (o1, o2)-> { return o1.number - o2.number; }); for (int i = 0; i < n; i++) { if (Math.abs(arr[i].index - i) % 2 == 1) { arr[i].flip(); } // pw.print(arr[i].number); // if (arr[i].isRight) pw.print("R "); // else pw.print("L "); } // pw.println(); // for (int i = 0; i < n - 1; i++) { // found = false; // for (int j = 0; j < n - 1; j++) { // if (arr[j].number > arr[j + 1].number) { // temp = arr[j]; // arr[j] = arr[j + 1]; // arr[j + 1] = temp; // found = true; // arr[j].flip(); // arr[j + 1].flip(); // } // } // if (!found) break; // } for (int i = 0; i < n - 1; i++) { if (!arr[i].isRight) { if (arr[i + 1].number == arr[i].number) { for (int j = i + 1; j < n && arr[j].number == arr[i].number; j += 2) { if (!arr[j].isRight) { arr[i].flip(); arr[j].flip(); break; } } } else { pw.println("NO"); return; } } } for (int i = 0; i < n; i++) { if (!arr[i].isRight) { pw.println("NO"); return; } } pw.println("YES"); } 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; } } 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
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
e186029a178df0c14ba3636e3542ef95
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.util.*; public class temp { public static void main(String[] args) throws IOException { new temp().run(); } void run() throws IOException { BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); int t= Integer.parseInt(br.readLine()); int mx= (int)1e5; while(t-->0) { int n= Integer.parseInt(br.readLine()); StringTokenizer st= new StringTokenizer(br.readLine()); ArrayList<Integer> a= new ArrayList<Integer>(); for(int i=0;i<n;i++) a.add(i,Integer.parseInt(st.nextToken())); int incnt[][]= new int[mx+1][2]; for(int i=0;i<n;i++) { if(i%2==0) incnt[a.get(i)][0]++; else incnt[a.get(i)][1]++; } Collections.sort(a); boolean f=true; for(int i=0;i<n;i++) { if(i%2==0) { if(--incnt[a.get(i)][0]<0) { f=false; break; } } else { if(--incnt[a.get(i)][1]<0) { f= false; break; } } } if(f) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
41ea106263ec8a83031b831e7efc7d4c
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.lang.*; import java.util.*; public class ComdeFormces { public static HashMap<Integer,ArrayList<Integer>> hm; public static boolean dp[][]; public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub FastReader sc=new FastReader(); BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); int t=sc.nextInt(); while(t--!=0) { int n=sc.nextInt(); int a[]=new int[n]; ArrayList<Integer> arL=new ArrayList<>(); ArrayList<Integer> arR=new ArrayList<>(); for(int i=0;i<n;i++) { if(i%2==0)arL.add(sc.nextInt()); else arR.add(sc.nextInt()); } Collections.sort(arL); Collections.sort(arR); int ans[]=new int[n]; boolean anss=true; int ptr1=0,ptr2=0; for(int i=0;i<n;i++) { if(i%2==0)ans[i]=arL.get(ptr1++); else ans[i]=arR.get(ptr2++); } for(int i=1;i<n;i++) { if(ans[i]<ans[i-1]) { anss=false; break; } } log.write((anss?"YES":"NO")+"\n"); log.flush(); } } public static int bs(int a[],int el,int s) { int e=a.length-1; int ind=-1; while(s<=e) { int m=s+(e-s)/2; if(a[m]==el) { ind=m; e=m-1; } else if(a[m]<el) { s=m+1; } else e=m-1; } return ind; } public static void dfs(int src,ArrayList<ArrayList<Integer>> ar,int vis[],int col) { if(vis[src]>0)return; if(hm.containsKey(col))hm.get(col).add(src); else { ArrayList<Integer> ap=new ArrayList<>(); ap.add(src); hm.put(col, ap); } vis[src]=col; for(int i=0;i<ar.get(src).size();i++) { if(vis[ar.get(src).get(i)]==0) { dfs(ar.get(src).get(i),ar,vis,col); } } } public static void radixSort(int a[]) { int n=a.length; int res[]=new int[n]; int p=1; for(int i=0;i<=8;i++) { int cnt[]=new int[10]; for(int j=0;j<n;j++) { a[j]=res[j]; cnt[(a[j]/p)%10]++; } for(int j=1;j<=9;j++) { cnt[j]+=cnt[j-1]; } for(int j=n-1;j>=0;j--) { res[cnt[(a[j]/p)%10]-1]=a[j]; cnt[(a[j]/p)%10]--; } p*=10; } } static void dfs(ArrayList<ArrayList<Integer>> ar,int src,int vis[],int col) { vis[src]=col; for(int i=0;i<ar.get(src).size();i++) { if(vis[ar.get(src).get(i)]==0) { dfs(ar,ar.get(src).get(i),vis,col); } } } static ArrayList<ArrayList<Integer>> initGraph(int n){ ArrayList<ArrayList<Integer>> ar=new ArrayList<>(); for(int i=0;i<=n;i++) { ar.add(new ArrayList<>()); } return ar; } static void undirected(ArrayList<ArrayList<Integer>> a, int n) { FastReader sc=new FastReader(); for(int i=0;i<n;i++) { // System.out.println("Helloo"); int p=sc.nextInt(); int q=sc.nextInt(); a.get(p).add(q); a.get(q).add(p); } } static int eval(ArrayList<Long> ar, long n, int j) { if(j<0)return bits(n); if(n==0)return 0; if(n-ar.get(j)>=0) { return Math.min(eval(ar,n,j-1), 1+eval(ar,n-ar.get(j),j-1)); } return eval(ar,n,j-1); } static int bits(long n) { int ans=0; while(n!=0) { if((n&1)==1)ans++; n>>=1; } return ans; } static long flor(ArrayList<Long> ar,long el) { int s=0; int e=ar.size()-1; while(s<=e) { int m=s+(e-s)/2; if(ar.get(m)==el)return ar.get(m); else if(ar.get(m)<el)s=m+1; else e=m-1; } return e>=0?e:-1; } //static int fd(pair a[], long wd,int e) { // int s=0; // while(s<=e) { // int m=s+(e-s)/2; // } // // return -2; //} public static int kadane(int a[]) { int sum=0,mx=Integer.MIN_VALUE; for(int i=0;i<a.length;i++) { sum+=a[i]; mx=Math.max(mx, sum); if(sum<0) sum=0; } return mx; } public static int rec( int l,int[] dp) { if(l<=2)return 1; if(dp[l]!=0)return dp[l]; int f=0,s=0; if(dp[l-1]!=0)f=dp[l-1]; else f=rec(l-1,dp); if(dp[l-2]!=0)s=dp[l-2]; else s=rec(l-2,dp); return dp[l]=add(f,s); } public static int m=1000000007; public static int mul(int a, int b) { return ((a%m)*(b%m))%m; } public static long mul(long a, long b) { return ((a%m)*(b%m))%m; } public static int add(int a, int b) { return ((a%m)+(b%m))%m; } public static long add(long a, long b) { return ((a%m)+(b%m))%m; } public static String rever(String s) { char a[]=s.toCharArray(); for(int i=0;i<a.length/2;i++) { char temp=a[i]; a[i]=a[a.length-1-i]; a[a.length-1-i]=temp; } return String.valueOf(a); } //static ArrayList<Integer> pr(int n){ // boolean vis[]=new boolean[n+1]; // vis[0]=false; // vis[1]=false; // for(int i=2;i<=n;i++) { // if(vis[i]) { // for(int j=2*i;j<=n;j+=i) { // vis[j]=false; // } // } // } //} static int subset(int a[],int j,int dp[][], int sum) { if(j==0) { if(sum==0 || sum-a[j]==0)return 1; else return 0; } if(sum==0)return 1; if(dp[j][sum]>0)return dp[j][sum]; // p("for j="+j+"sum="+sum,"s"); if(sum-a[j]>=0) { int p=subset(a,j-1,dp,sum-a[j]); int q=subset(a,j-1,dp,sum); return dp[j][sum]=p+q; } else{ return dp[j][sum]=subset(a,j-1,dp,sum); } } static long slv(int a[],int b[],long dp[][],int end,int k,int i) { if(i<1 ) { if(k==0) { return (end-a[0])*b[0]; } else return Integer.MAX_VALUE; } if(k<0)return Integer.MAX_VALUE; if(k==0) { return (end-a[0])*b[0]; } if(dp[i][k]!=0)return dp[i][k]; long ans1=slv(a,b,dp,a[i],k-1,i-1); long ans2=slv(a,b,dp,end,k,i-1); long val=(end-a[i])*b[i]; return dp[i][k]=Math.min(val+ans1,ans2); } static int bss(int[] a,int s, long k) { int e=a.length-1; // int max=-1; while(s<=e) { int m=s+(e-s)/2; if(a[m]==k) { return m; } else if(a[m]<k)s=m+1; else e=m-1; } return -1; } static int solv(int[] a,int len) { if(len%2==0) { int ans=0; for(int i=0;i<a.length;i++){ if(a[i]>0){ if(a[i]%2==0) { ans+=a[i]; } else if(a[i]%2!=0) { ans+=(a[i]/2)*2; } } } int cnt=ans/len; return cnt; } else { int ans=0,one=0; for(int i=0;i<a.length;i++){ if(a[i]>0){ if(a[i]%2==0) { ans+=a[i]; } else { ans+=(a[i]/2)*2; one++; } } } int n=len-1; int cnt=ans/n; int mod=cnt%n+one; if(cnt>=mod)return cnt; return mod; } } //debug static pair bss(ArrayList<pair> a,int el,int ind) { int s=0; int e=a.size()-1; pair ans=new pair(-1,-1); while(s<=e) { int m=s+(e-s)/2; if(a.get(m).a==el) { ans=a.get(m); e=m-1; } if(a.get(m).a>el)e=m-1; else s=m+1; } return ans; } public static <E> void p(E[][] a,String s) { System.out.println(s); for(int i=0;i<a.length;i++) { for(int j=0;j<a[0].length;j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } public static void p(int[] a,String s) { System.out.print(s+"="); for(int i=0;i<a.length;i++)System.out.print(a[i]+" "); System.out.println(); } public static void p(long[] a,String s) { System.out.print(s+"="); for(int i=0;i<a.length;i++)System.out.print(a[i]+" "); System.out.println(); } public static <E> void p(E a,String s){ System.out.println(s+"="+a); } public static <E> void p(ArrayList<E> a,String s){ System.out.println(s+"="+a); } public static <E> void p(LinkedList<E> a,String s){ System.out.println(s+"="+a); } public static <E> void p(HashSet<E> a,String s){ System.out.println(s+"="+a); } public static <E> void p(Stack<E> a,String s){ System.out.println(s+"="+a); } public static <E> void p(Queue<E> a,String s){ System.out.println(s+"="+a); } //utils static ArrayList<Integer> divisors(int n){ ArrayList<Integer> ar=new ArrayList<>(); for (int i=2; i<=Math.sqrt(n); i++){ if (n%i == 0){ if (n/i == i) { ar.add(i); } else { ar.add(i); ar.add(n/i); } } } return ar; } static int primeDivisor(int n){ ArrayList<Integer> ar=new ArrayList<>(); int cnt=0; boolean pr=false; while(n%2==0) { pr=true; n/=2; } if(pr)ar.add(2); for(int i=3;i*i<=n;i+=2) { pr=false; while(n%i==0) { n/=i; pr=true; } if(pr)ar.add(i); } if(n>2) ar.add(n); return ar.size(); } static char[] rev(char s[]) { char temp[]=s; for(int i=0;i<temp.length/2;i++) { char tp=temp[i]; temp[i]=temp[temp.length-1-i]; temp[temp.length-1-i]=tp; } return temp; } static int bs(ArrayList<pair> arr,int el) { int start=0; int end=arr.size()-1; while(start<=end) { int mid=start+(end-start)/2; if(arr.get(mid).a==el)return mid; else if(arr.get(mid).a<el)start=mid+1; else end=mid-1; } if(start>arr.size()-1)return -2; return -1; } static long find(int s,long a[]) { if(s>=a.length)return -1; long num=a[s]; for(int i=s;i<a.length;i+=2) { num=gcd(num,a[i]); if(num==1 || num==0)return -1; } return num; } static long gcd(long a,long b) { if(b==0)return a; else return gcd(b,a%b); } static int gcd(int a,int b) { if(b==0)return a; else return gcd(b,a%b); } static long factmod(long n,long mod,long img) { if(n==0)return 1; long ans=1; long temp=1; while(n--!=0) { if(temp!=img) { ans=((ans%mod)*((temp)%mod))%mod; } temp++; } return ans%mod; } static int bs(long a[] ,long num) { int start=0; int end=a.length-1; while(start<=end) { int mid=start+(end-start)/2; if(a[mid]==num) { return mid; } else if(a[mid]<num)start=mid+1; else end=mid-1; } return start; } static int ncr(int n, int r){ if(r>n-r)r=n-r; int ans=1; for(int i=0;i<r;i++){ ans*=(n-i); ans/=(i+1); } return ans; } public static class trip{ int a,b; int c; public trip(int a,int b,int c) { this.a=a; this.b=b; this.c=c; } public int compareTo(trip q) { return this.b-q.b; } } static void mergesort(long[] a,int start,int end) { if(start>=end)return ; int mid=start+(end-start)/2; mergesort(a,start,mid); mergesort(a,mid+1,end); merge(a,start,mid,end); } static void merge(long[] a, int start,int mid,int end) { int ptr1=start; int ptr2=mid+1; long b[]=new long[end-start+1]; int i=0; while(ptr1<=mid && ptr2<=end) { if(a[ptr1]<=a[ptr2]) { b[i]=a[ptr1]; ptr1++; i++; } else { b[i]=a[ptr2]; ptr2++; i++; } } while(ptr1<=mid) { b[i]=a[ptr1]; ptr1++; i++; } while(ptr2<=end) { b[i]=a[ptr2]; ptr2++; i++; } for(int j=start;j<=end;j++) { a[j]=b[j-start]; } } public static class FastReader { BufferedReader b; StringTokenizer s; public FastReader() { b=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(s==null ||!s.hasMoreElements()) { try { s=new StringTokenizer(b.readLine()); } catch(IOException e) { e.printStackTrace(); } } return s.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=b.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } boolean hasNext() { if (s != null && s.hasMoreTokens()) { return true; } String tmp; try { b.mark(1000); tmp = b.readLine(); if (tmp == null) { return false; } b.reset(); } catch (IOException e) { return false; } return true; } } public static class pair{ int a; int b; public pair(int a,int b) { this.a=a; this.b=b; } public int compareTo(pair b) { return this.a-b.a; } // public int compareToo(pair b) { // return this.b-b.b; // } @Override public String toString() { return "{"+this.a+" "+this.b+"}"; } } static long pow(long a, long pw) { long temp; if(pw==0)return 1; temp=pow(a,pw/2); if(pw%2==0)return temp*temp; return a*temp*temp; } static long mpow(long a, long pw) { long temp; if(pw==0)return 1; temp=pow(a,pw/2); if(pw%2==0)return mul(temp,temp); return mul(a,mul(temp,temp)); } static int pow(int a, int pw) { int temp; if(pw==0)return 1; temp=pow(a,pw/2); if(pw%2==0)return temp*temp; return a*temp*temp; } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
723fc04b46691a617e216bd64788d83e
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
// have faith in yourself!!!!! import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class CodeForces { /*-------------------------------------------EDITING CODE STARTS HERE-------------------------------------------*/ public static void main(String[] args) throws IOException { openIO(); int testCase = 1; testCase = sc.nextInt(); for (int i = 1; i <= testCase; i++) solve(i); closeIO(); } public static void solve(int tCase) throws IOException { int n = sc.nextInt(); int[] arr = new int[n]; int m = 100000+10; int[] oddCount = new int[m]; int[] evenCount = new int[m]; for(int i=0;i<n;i++){ arr[i] = sc.nextInt(); if(i%2==0)evenCount[arr[i]]++; else oddCount[arr[i]]++; } _sort(arr,true); for(int i=0;i<n;i++){ if(i%2==0)evenCount[arr[i]]--; else oddCount[arr[i]]--; } for(int i=0;i<m;i++) { if (oddCount[i] != 0 || evenCount[i] != 0) { out.println("NO"); return; } } out.println("YES"); } public static int mod = (int) 1e9 + 7; // private static int mod = 998244353; public static int inf_int = (int) 1e9 ; public static long inf_long = (long) 2e14; public static void _sort(int[] arr,boolean isAscending){ int n = arr.length; List<Integer> list = new ArrayList<>(); for(int ele : arr)list.add(ele); Collections.sort(list); if(!isAscending)Collections.reverse(list); for(int i=0;i<n;i++)arr[i] = list.get(i); } public static void _sort(long[] arr,boolean isAscending){ int n = arr.length; List<Long> list = new ArrayList<>(); for(long ele : arr)list.add(ele); Collections.sort(list); if(!isAscending)Collections.reverse(list); for(int i=0;i<n;i++)arr[i] = list.get(i); } static class Pair<K,V>{ K f; V s; public Pair(K f) { this.f = f; } public Pair(K f, V s) { this.f = f; this.s = s; } } /*-------------------------------------------EDITING CODE ENDS HERE-------------------------------------------*/ static FastestReader sc; static PrintWriter out; private static void openIO() throws IOException{ sc = new FastestReader(); out = new PrintWriter(System.out); } /*-------------------------------------------FAST INPUT STARTS HERE---------------------------------------------*/ public static void closeIO() throws IOException{ out.flush(); out.close(); sc.close(); } private static final class FastestReader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; public FastestReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastestReader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() throws IOException { int b; //noinspection StatementWithEmptyBody while ((b = read()) != -1 && isSpaceChar(b)) {} return b; } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final 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(); } final 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(); } final 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 { din.close(); } } /*---------------------------------------------FAST INPUT ENDS HERE ---------------------------------------------*/ }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
e63d9cef216a41fc012799487001915f
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.util.*; /* */ public class B { static FastReader sc=null; public static void main(String[] args) { sc=new FastReader(); int t=sc.nextInt(); outer:while(t-->0) { int n=sc.nextInt(); int even[]=new int[(n+1)/2],odd[]=new int[n/2]; int a[]=sc.readArray(n); for(int i=0,j=0,k=0;i<n;i++) { if(i%2==0)even[j++]=a[i]; else odd[k++]=a[i]; } ruffleSort(odd); ruffleSort(even); for(int i=0,j=0,k=0;i<n;i++) { if(i%2==0)a[i]=even[j++]; else a[i]=odd[k++]; } for(int i=1;i<n;i++) { if(a[i]<a[i-1]) { System.out.println("NO"); continue outer; } } System.out.println("YES"); } } static int[] ruffleSort(int a[]) { ArrayList<Integer> al=new ArrayList<>(); for(int i:a)al.add(i); Collections.sort(al); for(int i=0;i<a.length;i++)a[i]=al.get(i); return a; } static void print(int a[]) { for(int e:a) { System.out.print(e+" "); } System.out.println(); } static class FastReader{ 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()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[]=new int[n]; for(int i=0;i<n;i++)a[i]=sc.nextInt(); return a; } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
99ebebc5df7939ecccaff71310f1da4e
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.text.DecimalFormat; import java.util.*; public class Main { static class Pair { long a,b; public Pair(long a,long b) { this.a=a; this.b=b; } // @Override // public int compareTo(Pair p) { // return Long.compare(l, p.l); // } } static final int INF = 1 << 30; static final long INFL = 1L << 60; static final long NINF = INFL * -1; static final long mod = (long) 1e9 + 7; static final long mod2 = 998244353; static DecimalFormat df = new DecimalFormat("0.00000000000000"); public static final double PI = 3.141592653589793d, eps = 1e-9; public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public static void solve(int testNumber, InputReader in, OutputWriter out) { int t = in.ni(); //int t=1; while (t-->0) { int n=in.ni(); int[] arr=new int[n]; int[] even=new int[100005]; int[] odd=new int[100005]; for(int i=0;i<n;i++) { arr[i]=in.ni(); if((i&1)==1) { ++odd[arr[i]]; } else { ++even[arr[i]]; } } CP.sort(arr); int f=1; for(int i=0;i<n;i++) { if((i&1)==1) { if(odd[arr[i]]>0) { --odd[arr[i]]; } else { f=0; break; } } else { if(even[arr[i]]>0) { --even[arr[i]]; } else { f=0; break; } } } out.printLine((f==1)?"YES":"NO"); } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int ni() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private char nc() { return (char)skip(); } public int[] na(int arraySize) { int[] array = new int[arraySize]; for (int i = 0; i < arraySize; i++) { array[i] = ni(); } return array; } private int skip() { int b; while ((b = read()) != -1 && isSpaceChar(b)) ; return b; } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = read(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } int[][] nim(int h, int w) { int[][] a = new int[h][w]; for (int i = 0; i < h; i++) { a[i] = na(w); } return a; } long[][] nlm(int h, int w) { long[][] a = new long[h][w]; for (int i = 0; i < h; i++) { a[i] = nla(w); } return a; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isNewLine(int c) { return c == '\n'; } public String nextLine() { int c = read(); StringBuilder result = new StringBuilder(); do { result.appendCodePoint(c); c = read(); } while (!isNewLine(c)); return result.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sign = 1; if (c == '-') { sign = -1; c = read(); } long result = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } result *= 10; result += c & 15; c = read(); } while (!isSpaceChar(c)); return result * sign; } public long[] nla(int arraySize) { long array[] = new long[arraySize]; for (int i = 0; i < arraySize; i++) { array[i] = nextLong(); } return array; } public double nextDouble() { double ret = 0, div = 1; byte c = (byte) read(); while (c <= ' ') { c = (byte) read(); } boolean neg = (c == '-'); if (neg) { c = (byte) read(); } do { ret = ret * 10 + c - '0'; } while ((c = (byte) read()) >= '0' && c <= '9'); if (c == '.') { while ((c = (byte) read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) { return -ret; } return ret; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } boolean oj = System.getProperty("ONLINE_JUDGE") != null; void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } } public static class CP { static boolean isPrime(long 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; (long) i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) return false; } return true; } public static int log2(int N) { int result = (int) (Math.log(N) / Math.log(2)); return result; } public static long pow(long a, long n, long mod) { // a %= mod; long ret = 1; int x = 63 - Long.numberOfLeadingZeros(n); for (; x >= 0; x--) { ret = ret * ret % mod; if (n << 63 - x < 0) ret = ret * a % mod; } return ret; } public static boolean isComposite(long n) { if (n < 2) return true; if (n == 2 || n == 3) return false; if (n % 2 == 0 || n % 3 == 0) return true; for (long i = 6L; i * i <= n; i += 6) if (n % (i - 1) == 0 || n % (i + 1) == 0) return true; return false; } static int ifnotPrime(int[] prime, int x) { return (prime[x / 64] & (1 << ((x >> 1) & 31))); } static long log2(long n) { return (long)(Math.log10(n)/Math.log10(2L)); } static void makeComposite(int[] prime, int x) { prime[x / 64] |= (1 << ((x >> 1) & 31)); } public static String swap(String a, int i, int j) { char temp; char[] charArray = a.toCharArray(); temp = charArray[i]; charArray[i] = charArray[j]; charArray[j] = temp; return String.valueOf(charArray); } static void reverse(long arr[]){ int l = 0, r = arr.length-1; while(l<r){ long temp = arr[l]; arr[l] = arr[r]; arr[r] = temp; l++; r--; } } static void reverse(int arr[]){ int l = 0, r = arr.length-1; while(l<r){ int temp = arr[l]; arr[l] = arr[r]; arr[r] = temp; l++; r--; } } public static int[] sieveEratosthenes(int n) { if (n <= 32) { int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; for (int i = 0; i < primes.length; i++) { if (n < primes[i]) { return Arrays.copyOf(primes, i); } } return primes; } int u = n + 32; double lu = Math.log(u); int[] ret = new int[(int) (u / lu + u / lu / lu * 1.5)]; ret[0] = 2; int pos = 1; int[] isnp = new int[(n + 1) / 32 / 2 + 1]; int sup = (n + 1) / 32 / 2 + 1; int[] tprimes = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; for (int tp : tprimes) { ret[pos++] = tp; int[] ptn = new int[tp]; for (int i = (tp - 3) / 2; i < tp << 5; i += tp) ptn[i >> 5] |= 1 << (i & 31); for (int j = 0; j < sup; j += tp) { for (int i = 0; i < tp && i + j < sup; i++) { isnp[j + i] |= ptn[i]; } } } // 3,5,7 // 2x+3=n int[] magic = {0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13, 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14}; int h = n / 2; for (int i = 0; i < sup; i++) { for (int j = ~isnp[i]; j != 0; j &= j - 1) { int pp = i << 5 | magic[(j & -j) * 0x076be629 >>> 27]; int p = 2 * pp + 3; if (p > n) break; ret[pos++] = p; if ((long) p * p > n) continue; for (int q = (p * p - 3) / 2; q <= h; q += p) isnp[q >> 5] |= 1 << q; } } return Arrays.copyOf(ret, pos); } static long digit(long s) { long brute = 0; while (s > 0) { brute+=s%10; s /= 10; } return brute; } public static int[] primefacts(int n, int[] primes) { int[] ret = new int[15]; int rp = 0; for (int p : primes) { if (p * p > n) break; int i; for (i = 0; n % p == 0; n /= p, i++) ; if (i > 0) ret[rp++] = p; } if (n != 1) ret[rp++] = n; return Arrays.copyOf(ret, rp); } static ArrayList<Integer> bitWiseSieve(int n) { ArrayList<Integer> al = new ArrayList<>(); int prime[] = new int[n / 64 + 1]; for (int i = 3; i * i <= n; i += 2) { if (ifnotPrime(prime, i) == 0) for (int j = i * i, k = i << 1; j < n; j += k) makeComposite(prime, j); } al.add(2); for (int i = 3; i <= n; i += 2) if (ifnotPrime(prime, i) == 0) al.add(i); return al; } public static long[] sort(long arr[]) { List<Long> list = new ArrayList<>(); for (long n : arr) { list.add(n); } Collections.sort(list); for (int i = 0; i < arr.length; i++) { arr[i] = list.get(i); } return arr; } public static long[] revsort(long[] arr) { List<Long> list = new ArrayList<>(); for (long n : arr) { list.add(n); } Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < arr.length; i++) { arr[i] = list.get(i); } return arr; } public static int[] revsort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int n : arr) { list.add(n); } Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < arr.length; i++) { arr[i] = list.get(i); } return arr; } public static ArrayList<Integer> reverse( ArrayList<Integer> data, int left, int right) { // Reverse the sub-array while (left < right) { int temp = data.get(left); data.set(left++, data.get(right)); data.set(right--, temp); } // Return the updated array return data; } static ArrayList<Integer> sieve(long size) { ArrayList<Integer> pr = new ArrayList<Integer>(); boolean prime[] = new boolean[(int) size]; for (int i = 2; i < prime.length; i++) prime[i] = true; for (int i = 2; i * i < prime.length; i++) { if (prime[i]) { for (int j = i * i; j < prime.length; j += i) { prime[j] = false; } } } for (int i = 2; i < prime.length; i++) if (prime[i]) pr.add(i); return pr; } static boolean[] sieve1(long size) { ArrayList<Integer> pr = new ArrayList<Integer>(); boolean prime[] = new boolean[(int) size]; for (int i = 2; i < prime.length; i++) prime[i] = true; for (int i = 2; i * i < prime.length; i++) { if (prime[i]) { for (int j = i * i; j < prime.length; j += i) { prime[j] = false; } } } //for (int i = 2; i < prime.length; i++) if (prime[i]) pr.add(i); return prime; } static ArrayList<Integer> segmented_sieve(int l, int r, ArrayList<Integer> primes) { ArrayList<Integer> al = new ArrayList<>(); if (l == 1) ++l; int max = r - l + 1; int arr[] = new int[max]; for (int p : primes) { if (p * p <= r) { int i = (l / p) * p; if (i < l) i += p; for (; i <= r; i += p) { if (i != p) { arr[i - l] = 1; } } } } for (int i = 0; i < max; ++i) { if (arr[i] == 0) { al.add(l + i); } } return al; } static boolean isfPrime(long n, int iteration) { if (n == 0 || n == 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; Random rand = new Random(); for (int i = 0; i < iteration; i++) { long r = Math.abs(rand.nextLong()); long a = r % (n - 1) + 1; if (modPow(a, n - 1, n) != 1) return false; } return true; } static long modPow(long a, long b, long c) { long res = 1; for (int i = 0; i < b; i++) { res *= a; res %= c; } return res % c; } private static long binPower(long a, long l, long mod) { long res = 0; while (l > 0) { if ((l & 1) == 1) { res = mulmod(res, a, mod); l >>= 1; } a = mulmod(a, a, mod); } return res; } private static long mulmod(long a, long b, long c) { long x = 0, y = a % c; while (b > 0) { if (b % 2 == 1) { x = (x + y) % c; } y = (y * 2L) % c; b /= 2; } return x % c; } static long binary_Expo(long a, long b) { long res = 1; while (b != 0) { if ((b & 1) == 1) { res *= a; --b; } a *= a; b /= 2; } return res; } static void swap(int a, int b) { int tp = b; b = a; a = tp; } static long Modular_Expo(long a, long b, long mod) { long res = 1; while (b != 0) { if ((b & 1) == 1) { res = (res * a) % mod; --b; } a = (a * a) % mod; b /= 2; } return res % mod; } static int i_gcd(int a, int b) { while (true) { if (b == 0) return a; int c = a; a = b; b = c % b; } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static int gcd(int a,int b) { if (b == 0) return a; return gcd(b, a % b); } static long ceil_div(long a, long b) { return (a + b - 1) / b; } static int getIthBitFromInt(int bits, int i) { return (bits >> (i - 1)) & 1; } static long getIthBitFromLong(long bits, int i) { return (bits >> (i - 1)) & 1; } static boolean isPerfectSquare(long a) { long sq=(long)(Math.floor(Math.sqrt(a))*Math.floor(Math.sqrt(a))); return sq==a; } private static TreeMap<Long, Long> primeFactorize(long n) { TreeMap<Long, Long> pf = new TreeMap<>(Collections.reverseOrder()); long cnt = 0; long total = 1; for (long i = 2; (long) i * i <= n; ++i) { if (n % i == 0) { cnt = 0; while (n % i == 0) { ++cnt; n /= i; } pf.put(cnt, i); //total*=(cnt+1); } } if (n > 1) { pf.put(1L, n); //total*=2; } return pf; } //less than or equal private static int lower_bound(ArrayList<Long> list, long val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(int[] arr, int val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(long[] arr, long val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid]>=val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } // greater than or equal private static int upper_bound(List<Integer> list, int val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(int[] arr, int val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(long[] arr, long val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } // ==================== LIS & LNDS ================================ private static int[] LIS(long arr[], int n) { List<Long> list = new ArrayList<>(); int[] cnt=new int[n]; for (int i = 0; i < n; i++) { int idx = find1(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); cnt[i]=list.size(); } return cnt; } private static int find1(List<Long> list, long val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid)>=val) { ret = mid; j = mid - 1; } else { i = mid + 1; } } return ret; } private static int LNDS(int[] arr, int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find2(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find2(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) <= val) { i = mid + 1; } else { ret = mid; j = mid - 1; } } return ret; } private static long nCr(long n, long r,long mod) { if (n - r > r) r = n - r; long ans = 1L; for (long i = r + 1; i <= n; i++) ans = (ans%mod*i%mod)%mod; for (long i = 2; i <= n - r; i++) ans /= i; return ans%mod; } private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } static ArrayList<StringBuilder> permutations(StringBuilder s) { int n=s.length(); ArrayList<StringBuilder> al=new ArrayList<>(); getpermute(s,al,0,n); return al; } // static String longestPalindrome(String s) // { // int st=0,ans=0,len=0; // // // } static void getpermute(StringBuilder s,ArrayList<StringBuilder> al,int l, int r) { if(l==r) { al.add(s); return; } for(int i=l+1;i<r;++i) { char c=s.charAt(i); s.setCharAt(i,s.charAt(l)); s.setCharAt(l,c); getpermute(s,al,l+1,r); c=s.charAt(i); s.setCharAt(i,s.charAt(l)); s.setCharAt(l,c); } } static void initStringHash(String s,long[] dp,long[] inv,long p) { long pow=1; inv[0]=1; int n=s.length(); dp[0]=((s.charAt(0)-'a')+1); for(int i=1;i<n;++i) { pow=(pow*p)%mod; dp[i]=(dp[i-1]+((s.charAt(i)-'a')+1)*pow)%mod; inv[i]=CP.modinv(pow,mod)%mod; } } static long getStringHash(long[] dp,long[] inv,int l,int r) { long ans=dp[r]; if(l-1>=0) { ans-=dp[l-1]; } ans=(ans*inv[l])%mod; return ans; } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } private static String sortString(String str) { int[] arr = new int[256]; for (char ch : str.toCharArray()) arr[ch]++; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 256; i++) while (arr[i]-- > 0) sb.append((char) i); return sb.toString(); } static boolean isSquarefactor(int x, int factor, int target) { int s = (int) Math.round(Math.sqrt(x)); return factor * s * s == target; } static boolean isSquare(long x) { long s = (long) Math.round(Math.sqrt(x)); return x * x == s; } static int bs(ArrayList<Integer> al, int val) { int l = 0, h = al.size() - 1, mid = 0, ans = -1; while (l <= h) { mid = (l + h) >> 1; if (al.get(mid) == val) { return mid; } else if (al.get(mid) > val) { h = mid - 1; } else { l = mid + 1; } } return ans; } static void sort(int a[]) // heap sort { PriorityQueue<Integer> q = new PriorityQueue<>(); for (int i = 0; i < a.length; i++) q.add(a[i]); for (int i = 0; i < a.length; i++) a[i] = q.poll(); } static void shuffle(int[] in) { for (int i = 0; i < in.length; i++) { int idx = (int) (Math.random() * in.length); fast_swap(in, idx, i); } } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for (int v : a) { c0[(v & 0xff) + 1]++; c1[(v >>> 8 & 0xff) + 1]++; c2[(v >>> 16 & 0xff) + 1]++; c3[(v >>> 24 ^ 0x80) + 1]++; } for (int i = 0; i < 0xff; i++) { c0[i + 1] += c0[i]; c1[i + 1] += c1[i]; c2[i + 1] += c2[i]; c3[i + 1] += c3[i]; } int[] t = new int[n]; for (int v : a) t[c0[v & 0xff]++] = v; for (int v : t) a[c1[v >>> 8 & 0xff]++] = v; for (int v : a) t[c2[v >>> 16 & 0xff]++] = v; for (int v : t) a[c3[v >>> 24 ^ 0x80]++] = v; return a; } static int[] computeLps(String pat) { int len = 0, i = 1, m = pat.length(); int lps[] = new int[m]; lps[0] = 0; while (i < m) { if (pat.charAt(i) == pat.charAt(len)) { ++len; lps[i] = len; ++i; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = len; ++i; } } } return lps; } static ArrayList<Integer> kmp(String s, String pat) { ArrayList<Integer> al = new ArrayList<>(); int n = s.length(), m = pat.length(); int lps[] = computeLps(pat); int i = 0, j = 0; while (i < n) { if (s.charAt(i) == pat.charAt(j)) { i++; j++; if (j == m) { al.add(i - j); j = lps[j - 1]; } } else { if (j != 0) { j = lps[j - 1]; } else { i++; } } } return al; } static void reverse_ruffle_sort(int a[]) { shuffle(a); Arrays.sort(a); for (int l = 0, r = a.length - 1; l < r; ++l, --r) fast_swap(a, l, r); } static void ruffle_sort(int a[]) { shuffle(a); Arrays.sort(a); } static int getMax(int arr[], int n) { int mx = arr[0]; for (int i = 1; i < n; i++) if (arr[i] > mx) mx = arr[i]; return mx; } static ArrayList<Long> primeFact(long n) { ArrayList<Long> al = new ArrayList<>(); al.add(1L); while (n % 2 == 0) { if (!al.contains(2L)) { al.add(2L); } n /= 2L; } for (long i = 3; (long) i * i <= n; i += 2) { while ((n % i == 0)) { if (!al.contains((long) i)) { al.add((long) i); } n /= i; } } if (n > 2) { if (!al.contains(n)) { al.add(n); } } return al; } public static long totFact(long n) { long cnt = 0, tot = 1; while (n % 2 == 0) { n /= 2; ++cnt; } tot *= (cnt + 1); for (int i = 3; i <= Math.sqrt(n); i += 2) { cnt = 0; while (n % i == 0) { n /= i; ++cnt; } tot *= (cnt + 1); } if (n > 2) { tot *= 2; } return tot; } static int[] z_function(String s) { int n = s.length(), z[] = new int[n]; for (int i = 1, l = 0, r = 0; i < n; ++i) { if (i <= r) z[i] = Math.min(z[i - l], r - i + 1); while (i + z[i] < n && s.charAt(z[i]) == s.charAt(i + z[i])) ++z[i]; if (i + z[i] - 1 > r) { l = i; r = i + z[i] - 1; } } return z; } static void fast_swap(int[] a, int idx1, int idx2) { if (a[idx1] == a[idx2]) return; a[idx1] ^= a[idx2]; a[idx2] ^= a[idx1]; a[idx1] ^= a[idx2]; } public static void fast_sort(long[] array) { ArrayList<Long> copy = new ArrayList<>(); for (long i : array) copy.add(i); Collections.sort(copy); for (int i = 0; i < array.length; i++) array[i] = copy.get(i); } static int factorsCount(int n) { boolean hash[] = new boolean[n + 1]; Arrays.fill(hash, true); for (int p = 2; p * p < n; p++) if (hash[p] == true) for (int i = p * 2; i < n; i += p) hash[i] = false; int total = 1; for (int p = 2; p <= n; p++) { if (hash[p]) { int count = 0; if (n % p == 0) { while (n % p == 0) { n = n / p; count++; } total = total * (count + 1); } } } return total; } static long binomialCoeff(long n, long k) { long res = 1; if (k > n - k) k = n - k; for (int i = 0; i < k; ++i) { res = (res * (n - i)); res /= (i + 1); } return res; } static long nck(long fact[], long inv[], long n, long k) { if (k > n) return 0; long res = fact[(int) n]%mod; res = (int) ((res%mod* inv[(int) k]%mod))%mod; res = (int) ((res%mod*inv[(int) (n - k)]%mod))%mod; return res % mod; } public static long fact(long x) { long fact = 1; for (int i = 2; i <= x; ++i) { fact = fact * i; } return fact; } public static ArrayList<Long> getFact(long x) { ArrayList<Long> facts = new ArrayList<>(); facts.add(1L); for (long i = 2; i * i <= x; ++i) { if (x % i == 0) { facts.add(i); if (i != x / i) { facts.add(x / i); } } } return facts; } static void matrix_ex(long n, long[][] A, long[][] I) { while (n > 0) { if (n % 2 == 0) { Multiply(A, A); n /= 2; } else { Multiply(I, A); n--; } } } static void Multiply(long[][] A, long[][] B) { int n = A.length, m = A[0].length, p = B[0].length; long[][] C = new long[n][p]; for (int i = 0; i < n; i++) { for (int j = 0; j < p; j++) { for (int k = 0; k < m; k++) { C[i][j] += ((A[i][k] % mod) * (B[k][j] % mod)) % mod; C[i][j] = C[i][j] % mod; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < p; j++) { A[i][j] = C[i][j]; } } } public static HashMap<Long, Integer> sortMapDesc(HashMap<Long,Integer> map) { List<Map.Entry<Long, Integer>> list = new LinkedList<>(map.entrySet()); Collections.sort(list, (o1, o2) -> o2.getValue() - o1.getValue()); HashMap<Long, Integer> temp = new LinkedHashMap<>(); for (Map.Entry<Long, Integer> i : list) { temp.put(i.getKey(), i.getValue()); } return temp; } public static HashMap<Character, Integer> sortMapDescch(HashMap<Character, Integer> map) { List<Map.Entry<Character, Integer>> list = new LinkedList<>(map.entrySet()); Collections.sort(list, (o1, o2) -> o2.getValue() - o1.getValue()); HashMap<Character, Integer> temp = new LinkedHashMap<>(); for (Map.Entry<Character, Integer> i : list) { temp.put(i.getKey(), i.getValue()); } return temp; } public static HashMap<Long,Long> sortMapAsclng(HashMap<Long,Long> map) { List<Map.Entry<Long,Long>> list = new LinkedList<>(map.entrySet()); Collections.sort(list, (o1, o2) -> (int)(o1.getValue() - o2.getValue())); HashMap<Long,Long> temp = new LinkedHashMap<>(); for (Map.Entry<Long,Long> i : list) { temp.put(i.getKey(), i.getValue()); } return temp; } public static HashMap<Integer, Integer> sortMapAsc(HashMap<Integer, Integer> map) { List<Map.Entry<Integer, Integer>> list = new LinkedList<>(map.entrySet()); Collections.sort(list, (o1, o2) -> o1.getValue() - o2.getValue()); HashMap<Integer, Integer> temp = new LinkedHashMap<>(); for (Map.Entry<Integer, Integer> i : list) { temp.put(i.getKey(), i.getValue()); } return temp; } public static long lcm(long l, long l2) { long val = gcd(l, l2); return (l * l2) / val; } public static boolean isSubsequence(String s, String t) { int n = s.length(); int m = t.length(); if (m > n) { return false; } int i = 0, j = 0, skip = 0; while (i < n && j < m) { if (s.charAt(i) == t.charAt(j)) { --skip; ++j; } ++skip; ++i; } return j==m; } public static long[][] combination(int l, int r) { long[][] pascal = new long[l + 1][r + 1]; pascal[0][0] = 1; for (int i = 1; i <= l; ++i) { pascal[i][0] = 1; for (int j = 1; j <= r; ++j) { pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j]; } } return pascal; } public static long gcd(long... array) { long ret = array[0]; for (int i = 1; i < array.length; ++i) ret = gcd(ret, array[i]); return ret; } public static long lcm(int... array) { long ret = array[0]; for (int i = 1; i < array.length; ++i) ret = lcm(ret, array[i]); return ret; } public static int min(int a, int b) { return a < b ? a : b; } public static int min(int... array) { int ret = array[0]; for (int i = 1; i < array.length; ++i) ret = min(ret, array[i]); return ret; } public static long min(long a, long b) { return a < b ? a : b; } public static long min(long... array) { long ret = array[0]; for (int i = 1; i < array.length; ++i) ret = min(ret, array[i]); return ret; } public static int max(int a, int b) { return a > b ? a : b; } public static int max(int... array) { int ret = array[0]; for (int i = 1; i < array.length; ++i) ret = max(ret, array[i]); return ret; } public static long max(long a, long b) { return a > b ? a : b; } public static long max(long... array) { long ret = array[0]; for (int i = 1; i < array.length; ++i) ret = max(ret, array[i]); return ret; } public static long sum(int... array) { long ret = 0; for (int i : array) ret += i; return ret; } public static long sum(long... array) { long ret = 0; for (long i : array) ret += i; return ret; } public static long[] facts(int n,long m) { long[] fact=new long[n]; fact[0]=1; fact[1]=1; for(int i=2;i<n;i++) { fact[i]=(long)(i*fact[i-1])%m; } return fact; } public static long[] inv(long[] fact,int n,long mod) { long[] inv=new long[n]; inv[n-1]= CP.Modular_Expo(fact[n-1],mod-2,mod)%mod; for(int i=n-2;i>=0;--i) { inv[i]=(inv[i+1]*(i+1))%mod; } return inv; } public static int modinv(long x, long mod) { return (int) (CP.Modular_Expo(x, mod - 2, mod) % mod); } public static int lcs(String s, String t) { int n = s.length(), m = t.length(); int dp[][] = new int[n + 1][m + 1]; for (int i = 0; i <= n; ++i) { for (int j = 0; j <= m; ++j) { if (i == 0 || j == 0) { dp[i][j] = 0; } else if (s.charAt(i - 1) == t.charAt(j - 1)) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); } } } return dp[n][m]; } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) { writer.print(' '); } writer.print(array[i]); } } public void print(long[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) { writer.print(' '); } writer.print(array[i]); } } public void printLine(int[] array) { print(array); writer.println(); } public void printLine(long[] array) { print(array); writer.println(); } public void close() { writer.close(); } public void printLine(long i) { writer.println(i); } public void printLine(int i) { writer.println(i); } public void printLine(char c) { writer.println(c); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void py() { print("YES"); writer.println(); } public void pn() { print("NO"); writer.println(); } public void printLine(Object... objects) { print(objects); writer.println(); } void flush() { writer.flush(); } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
74d10b2ee1442229113fc6c19c4bb19e
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
//package Codeforces; import java.io.*; import java.util.*; import java.util.StringTokenizer; public class AquaMoonAndStrangeSort { public static void main(String[] args) throws IOException { Soumit sc = new Soumit(); int t = sc.nextInt(); StringBuilder sb = new StringBuilder(); while (t-->0){ int n = sc.nextInt(); int[] arr = sc.nextIntArray(n); Map<Integer, Integer> evenhash = new HashMap<>(); Map<Integer, Integer> oddhash = new HashMap<>(); for(int i=0;i<n;i++){ if(i%2==0){ evenhash.put(arr[i], evenhash.getOrDefault(arr[i], 0)+1); } else{ oddhash.put(arr[i], oddhash.getOrDefault(arr[i], 0)+1); } } sc.sort(arr); for(int i=0;i<n;i++){ if(i%2==0){ evenhash.put(arr[i], evenhash.getOrDefault(arr[i], 0)-1); } else{ oddhash.put(arr[i], oddhash.getOrDefault(arr[i], 0)-1); } } //System.out.println(evenhash+" "+oddhash); boolean flag = true; for(int i: evenhash.keySet()){ if(evenhash.get(i)!=0){ flag = false; break; } } for(int i: oddhash.keySet()){ if(oddhash.get(i)!=0){ flag = false; break; } } if(flag){ sb.append("YES\n"); } else{ sb.append("NO\n"); } } System.out.println(sb); sc.close(); } static class Soumit { final private int BUFFER_SIZE = 1 << 18; final private DataInputStream din; final private byte[] buffer; private PrintWriter pw; private int bufferPointer, bytesRead; StringTokenizer st; public Soumit() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Soumit(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public void streamOutput(String file) throws IOException { FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); pw = new PrintWriter(bw); } public void println(String a) { pw.println(a); } public void print(String a) { pw.print(a); } public String readLine() throws IOException { byte[] buf = new byte[3000064]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public void sort(int[] arr) { ArrayList<Integer> arlist = new ArrayList<>(); for (int i : arr) arlist.add(i); Collections.sort(arlist); for (int i = 0; i < arr.length; i++) arr[i] = arlist.get(i); } public void sort(long[] arr) { ArrayList<Long> arlist = new ArrayList<>(); for (long i : arr) arlist.add(i); Collections.sort(arlist); for (int i = 0; i < arr.length; i++) arr[i] = arlist.get(i); } public int[] nextIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } public double[] nextDoubleArray(int n) throws IOException { double[] arr = new double[n]; for (int i = 0; i < n; i++) { arr[i] = nextDouble(); } return arr; } 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;*/ if (din != null) din.close(); if (pw != null) pw.close(); } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
bb1663b655d920e286b7fca3eef26c86
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; public class Solution { private static Scanner in = new Scanner(System.in); public static void main(String[] args) { int t = in.nextInt(); while(t-- > 0) solve(); } private static void solve() { int n = in.nextInt(); int a[][] = new int[n][2]; for(int i=0; i<n; i++) { a[i][0] = in.nextInt(); a[i][1] = i; } Arrays.sort(a, new Comparator<int[]>() { public int compare(int[] o1, int[] o2) { return o1[0]-o2[0]; } }); for(int i=0;i<n;i++) { int oo=0,oe=0,no=0,ne=0; int temp = a[i][0]; while(i<n && a[i][0]==temp) { if(i%2==0) oe++; else oo++; if(a[i][1]%2==0) ne++; else no++; i++; } i--; if(oo!=no || oe!=ne) { System.out.println("NO"); return; } } System.out.println("YES"); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
874d6ba5e84d0ce321cc651a38715386
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
// Imports import java.io.*; import java.util.*; public class A1545 { // Implement binary search for lowest element public static int minBinary(Integer value, Integer[] sorted) { int low = 0; int high = sorted.length - 1; while(low < high) { // Can't be too low? int mid = (low + high) / 2; if(sorted[mid] == value) { high = mid; } else if(sorted[mid] > value) { high = mid - 1; } else { low = mid + 1; } } return low; } public static void main(String[] args) throws IOException { // Test once done BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); // Guess: Inversion counting int T = Integer.parseInt(f.readLine()); for(int i = 0; i < T; i++) { // Do stuff int N = Integer.parseInt(f.readLine()); int[] arr = new int[N]; Integer[] cpy = new Integer[N]; HashMap<Integer, Integer> oddCount = new HashMap<>(); HashMap<Integer, Integer> evenCount = new HashMap<>(); HashMap<Integer, Integer> oddCount2 = new HashMap<>(); HashMap<Integer, Integer> evenCount2 = new HashMap<>(); StringTokenizer st = new StringTokenizer(f.readLine()); for(int j = 0; j < N; j++) { arr[j] = Integer.parseInt(st.nextToken()); cpy[j] = arr[j]; if(j % 2 == 1) { if(oddCount.containsKey(arr[j])) { oddCount.put(arr[j], oddCount.get(arr[j]) + 1); } else { oddCount.put(arr[j], 1); } } else { if(evenCount.containsKey(arr[j])) { evenCount.put(arr[j], evenCount.get(arr[j]) + 1); } else { evenCount.put(arr[j], 1); } } } // Avoid quicksort weirdness Arrays.sort(cpy); for(int j = 0; j < N; j++) { if(j % 2 == 1) { if(oddCount2.containsKey(cpy[j])) { oddCount2.put(cpy[j], oddCount2.get(cpy[j]) + 1); } else { oddCount2.put(cpy[j], 1); } } else { if(evenCount2.containsKey(cpy[j])) { evenCount2.put(cpy[j], evenCount2.get(cpy[j]) + 1); } else { evenCount2.put(cpy[j], 1); } } } boolean ok = true; for(int j : oddCount.keySet()) { if(!Objects.equals(oddCount.get(j), oddCount2.get(j))) { ok = false; break; } } for(int j : evenCount.keySet()) { if(!Objects.equals(evenCount.get(j), evenCount2.get(j))) { ok = false; break; } } if(!ok) { System.out.println("NO"); } else { System.out.println("YES"); } } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
ec8f57c252c84621dd3edb66b92be5b4
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import com.sun.jdi.IntegerValue; import javax.lang.model.type.IntersectionType; import javax.swing.*; import java.sql.Array; import java.util.*; import java.lang.*; import java.io.*; public class Main { static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static FastReader sc = new FastReader(); static int mod = 1000000007; public static void main (String[] args) throws java.lang.Exception { int t = sc.nextInt(); while(t-->0){ solve(); } } public static void solve() { int n = i(); int[] arr = new int[n]; HashMap<Integer, Pair> hm = new HashMap<>(); for(int i = 0;i<n;++i){ arr[i] = i(); if(i%2==0){ if(hm.containsKey(arr[i])){ hm.get(arr[i]).even++; }else{ hm.put(arr[i], new Pair(0, 1)); } }else{ if(hm.containsKey(arr[i])){ hm.get(arr[i]).odd++; }else{ hm.put(arr[i], new Pair(1, 0)); } } } mysort(arr); HashMap<Integer, Pair> sorted = new HashMap<>(); for(int i = 0;i<n;++i){ if(i%2==0){ if(sorted.containsKey(arr[i])){ sorted.get(arr[i]).even++; }else{ sorted.put(arr[i], new Pair(0, 1)); } }else{ if(sorted.containsKey(arr[i])){ sorted.get(arr[i]).odd++; }else{ sorted.put(arr[i], new Pair(1, 0)); } } } for(int el : hm.keySet()){ Pair p = hm.get(el); Pair ps = sorted.get(el); if(ps.odd==p.odd&&ps.even==p.even){ continue; }else{ out.println("NO");out.flush();return; } } out.println("YES"); out.flush(); } /* int [] arr = new int[n]; for(int i = 0;i<n;++i){ arr[i] = sc.nextInt(); } */ static int csb(long n) { int count = 0; while (n > 0) { n &= (n - 1); count++; } return count; } static void mysort(int[] arr) { for(int i=0;i<arr.length;i++) { int rand = (int) (Math.random() * arr.length); int loc = arr[rand]; arr[rand] = arr[i]; arr[i] = loc; } Arrays.sort(arr); } static long power(long x, long y) { long res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res; } static class Pair implements Comparable<Pair>{ int odd, even; Pair(int val, int count){ this.odd=val; this.even = count; } public int compareTo(Pair o) { return this.odd - o.odd; } } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
59f07b14143d6e15b5e315294c46ffaf
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Comparator; import java.util.StringTokenizer; public class AquamoonAndStrangeSort { 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 ignored) { } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } static class Pair { int x; int y; public Pair (int x, int y) { this.x = x; this.y = y; } } public static void main(String[] args) { FastScanner sc = new FastScanner(); int t = sc.nextInt(); pls: while (t-- > 0) { int n = sc.nextInt(); Pair[] a = new Pair[n]; for (int i = 0;i < n;i++) { a[i] = new Pair(sc.nextInt(), i); } Arrays.sort(a, Comparator.comparingInt(p -> p.x)); for (int i = 0;i < n; i++) { int oo = 0, oe = 0, no = 0, ne = 0; int temp = a[i].x; while (i < n && temp == a[i].x) { if (i % 2 == 0) oe++; else oo++; if (a[i].y % 2 == 0) ne++; else no++; i++; } i--; if(oo!=no || oe!=ne) { System.out.println("NO"); continue pls; } } System.out.println("YES"); } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
c1771f91bc8e8bd1467d198818d467fd
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; //import java.lang.*; import java.io.*; public class Solution { static long[] fac; static int m = (int)1e9+7; static int c = 1; // static int[] x = {1,-1,0,0}; // static int[] y = {0,0,1,-1}; // static int cycle_node; public static void main(String[] args) throws IOException { Reader.init(System.in); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); // Do not delete this // //Uncomment this before using nCrModPFermat // fac = new long[200000 + 1]; // fac[0] = 1; // // for (int i = 1; i <= 200000; i++) // fac[i] = (fac[i - 1] * i % 1000000007); // long[] fact = factorial((int)1e6); int te = Reader.nextInt(); // int te = 1; while(te-->0) { int n = Reader.nextInt(); Integer[] arr = new Integer[n]; for(int i = 0;i<n;i++) arr[i] = Reader.nextInt(); HashMap<Integer,Integer> map = new HashMap<>(); HashMap<Integer,Integer> count = new HashMap<>(); Integer[] copy = arr.clone(); Arrays.parallelSort(copy); for(int i = 0;i<n;i++){ if(!map.containsKey(copy[i])){ map.put(copy[i],i); } count.put(arr[i],count.getOrDefault(arr[i],0)+1); } HashMap<Integer,Integer> map2 = new HashMap<>(); for(int i = 0;i<n;i++){ int dist = Math.abs(i-map.get(arr[i])); if(dist%2==0){ map2.put(arr[i],map2.getOrDefault(arr[i],0)+1); } else{ map2.put(arr[i],map2.getOrDefault(arr[i],0)); } } boolean ok = true; for(int k : count.keySet()){ if(count.get(k)%2==0){ if(map2.get(k)!=count.get(k)/2){ ok = false; break; } } else{ if(map2.get(k)!=count.get(k)/2+1){ ok = false; break; } } } if(ok) output.write("YES\n"); else output.write("NO\n"); } output.close(); } // Recursive function to return (x ^ n) % m static long modexp(long x, long n) { if (n == 0) { return 1; } else if (n % 2 == 0) { return modexp((x * x) % m, n / 2); } else { return (x * modexp((x * x) % m, (n - 1) / 2) % m); } } // Function to return the fraction modulo mod static long getFractionModulo(long a, long b) { long c = gcd(a, b); a = a / c; b = b / c; // (b ^ m-2) % m long d = modexp(b, m - 2); // Final answer long ans = ((a % m) * (d % m)) % m; return ans; } public static boolean isP(String n){ StringBuilder s1 = new StringBuilder(n); StringBuilder s2 = new StringBuilder(n); s2.reverse(); for(int i = 0;i<s1.length();i++){ if(s1.charAt(i)!=s2.charAt(i)) return false; } return true; } public static long[] factorial(int n){ long[] factorials = new long[n+1]; factorials[0] = 1; factorials[1] = 1; for(int i = 2;i<=n;i++){ factorials[i] = (factorials[i-1]*i)%1000000007; } return factorials; } public static long numOfBits(long n){ long ans = 0; while(n>0){ n = n & (n-1); ans++; } return ans; } public static long ceilOfFraction(long x, long y){ // ceil using integer division: ceil(x/y) = (x+y-1)/y // using double may go out of range. return (x+y-1)/y; } public static long power(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; } // Returns n^(-1) mod p public static long modInverse(long n, long p) { return power(n, p - 2, p); } // Returns nCr % p using Fermat's // little theorem. public static long nCrModPFermat(int n, int r, int p) { if (n<r) return 0; if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } public static long ncr(long n, long r) { long p = 1, k = 1; if (n - r < r) { r = n - r; } if (r != 0) { while (r > 0) { p *= n; k *= r; long m = gcd(p, k); p /= m; k /= m; n--; r--; } } else { p = 1; } return p; } 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 (int i = 5; (long) i * i <= n; i = i + 6){ if (n % i == 0 || n % (i + 2) == 0) { return false; } } return true; } public static int powOf2JustSmallerThanN(int n) { n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; return n ^ (n >> 1); } public static long merge(int[] arr, int[] aux, int low, int mid, int high) { int k = low, i = low, j = mid + 1; long inversionCount = 0; // while there are elements in the left and right runs while (i <= mid && j <= high) { if (arr[i] <= arr[j]) { aux[k++] = arr[i++]; } else { aux[k++] = arr[j++]; inversionCount += (mid - i + 1); // NOTE } } // copy remaining elements while (i <= mid) { aux[k++] = arr[i++]; } /* no need to copy the second half (since the remaining items are already in their correct position in the temporary array) */ // copy back to the original array to reflect sorted order for (i = low; i <= high; i++) { arr[i] = aux[i]; } return inversionCount; } public static long mergesort(int[] arr, int[] aux, int low, int high) { if (high <= low) { // if run size <= 1 return 0; } int mid = (low + ((high - low) >> 1)); long inversionCount = 0; // recursively split runs into two halves until run size <= 1, // then merges them and return up the call chain // split/merge left half inversionCount += mergesort(arr, aux, low, mid); // split/merge right half inversionCount += mergesort(arr, aux, mid + 1, high); // merge the two half runs inversionCount += merge(arr, aux, low, mid, high); return inversionCount; } public static void reverseArray(int[] arr,int start, int end) { int temp; while (start < end) { temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } public static long gcd(long a, long b){ if(a==0){ return b; } return gcd(b%a,a); } public static long lcm(long a, long b){ if(a>b) return a/gcd(b,a) * b; return b/gcd(a,b) * a; } public static long largeExponentMod(long x,long y,long mod){ // computing (x^y) % mod x%=mod; long ans = 1; while(y>0){ if((y&1)==1){ ans = (ans*x)%mod; } x = (x*x)%mod; y = y >> 1; } return ans; } public static boolean[] numOfPrimesInRange(long L, long R){ boolean[] isPrime = new boolean[(int) (R-L+1)]; Arrays.fill(isPrime,true); long lim = (long) Math.sqrt(R); for (long i = 2; i <= lim; i++){ for (long j = Math.max(i * i, (L + i - 1) / i * i); j <= R; j += i){ isPrime[(int) (j - L)] = false; } } if (L == 1) isPrime[0] = false; return isPrime; } public static ArrayList<Long> primeFactors(long n){ ArrayList<Long> factorization = new ArrayList<>(); if(n%2==0){ factorization.add(2L); } while(n%2==0){ n/=2; } if(n%3==0){ factorization.add(3L); } while(n%3==0){ n/=3; } if(n%5==0){ factorization.add(5L); } while(n%5==0){ // factorization.add(5L); n/=5; } int[] increments = {4, 2, 4, 2, 4, 6, 2, 6}; int i = 0; for (long d = 7; d * d <= n; d += increments[i++]) { if(n%d==0){ factorization.add(d); } while (n % d == 0) { // factorization.add(d); n /= d; } if (i == 8) i = 0; } if (n > 1) factorization.add(n); return factorization; } } class DSU { int[] size, parent; int n; public DSU(int n){ this.n = n; size = new int[n]; parent = new int[n]; for(int i = 0;i<n;i++){ parent[i] = i; size[i] = 1; } } public int find(int u){ if(parent[u]==u){ return u; } return parent[u] = find(parent[u]); } public void merge(int u, int v){ u = find(u); v = find(v); if(u!=v){ if(size[u]>size[v]){ parent[v] = u; size[u] += size[v]; } else{ parent[u] = v; size[v] += size[u]; } } } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static String nextLine() throws IOException { return reader.readLine(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static long nextLong() throws IOException{ return Long.parseLong(next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } } class SegTree{ int n; // array size // Max size of tree public int[] tree; public SegTree(int n){ this.n = n; tree = new int[4*n]; } // function to build the tree void update(int pos, int val, int s, int e, int treeIdx){ if(pos < s || pos > e){ return; } if(s == e){ tree[treeIdx] = val; return; } int mid = s + (e - s) / 2; update(pos, val, s, mid, 2 * treeIdx); update(pos, val, mid + 1, e, 2 * treeIdx + 1); tree[treeIdx] = tree[2 * treeIdx] + tree[2 * treeIdx + 1]; } void update(int pos, int val){ update(pos, val, 1, n, 1); } int query(int qs, int qe, int s, int e, int treeIdx){ if(qs <= s && qe >= e){ return tree[treeIdx]; } if(qs > e || qe < s){ return 0; } int mid = s + (e - s) / 2; int subQuery1 = query(qs, qe, s, mid, 2 * treeIdx); int subQuery2 = query(qs, qe, mid + 1, e, 2 * treeIdx + 1); int res = subQuery1 + subQuery2; return res; } int query(int l, int r){ return query(l, r, 1, n, 1); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
ffbfe66b9a5a491fbdd8d67865e7e340
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.lang.*; import java.util.*; public class Main { public static int mod = (int) 1e9 + 7; // **** -----> Disjoint Set Union(DSU) Start ********** public static int findPar(int node, int[] parent) { if (parent[node] == node) return node; return parent[node] = findPar(parent[node], parent); } public static boolean union(int u, int v, int[] rank, int[] parent) { u = findPar(u, parent); v = findPar(v, parent); if(u == v) return false; if (rank[u] < rank[v]) parent[u] = v; else if (rank[u] > rank[v]) parent[v] = u; else { parent[u] = v; rank[v]++; } return true; } // **** DSU Ends *********** public static String toBinary(int decimal) {StringBuilder sb = new StringBuilder();while (decimal > 0) {sb.append(decimal % 2);decimal = decimal / 2;}return sb.reverse().toString();} public static boolean isPalindrome(String s) {int i = 0, j = s.length() - 1;while (i < j) {if (s.charAt(i) != s.charAt(j))return false;i++;j--;}return true;} public static boolean isPalindrome(int[] arr) {int i = 0, j = arr.length - 1;while (i < j) {if (arr[i] != arr[j])return false;}return true;} public static int pow(int x, int y) {int res = 1;x = x % mod;if (x == 0)return 0;while (y > 0) {if ((y & 1) != 0)res = (res * x) % mod;y = y >> 1;x = (x * x) % mod;}return res;} public static int gcd(int a, int b) {if (b == 0)return a;return gcd(b, a % b);} public static long gcd(long a, long b) {if (b == 0)return a;return gcd(b, a % b);} public static void sort(long[] a) {Random rnd = new Random();for (int i = 0; i < a.length; i++) {int pos = i + rnd.nextInt(a.length - i);long temp = a[i];a[i] = a[pos];a[pos] = temp;}Arrays.sort(a);} public static void reverse(int a[]) {int i, k, t, n = a.length;for (i = 0; i < n / 2; i++) {t = a[i];a[i] = a[n - i - 1];a[n - i - 1] = t;}} public static void sort(int[] a) {Random rnd = new Random();for (int i = 0; i < a.length; i++) {int pos = i + rnd.nextInt(a.length - i);int temp = a[i];a[i] = a[pos];a[pos] = temp;}Arrays.sort(a);} public static void revSort(int[] a) {Random rnd = new Random();for (int i = 0; i < a.length; i++) {int pos = i + rnd.nextInt(a.length - i);int temp = a[i];a[i] = a[pos];a[pos] = temp;}Arrays.sort(a);reverse(a);} public static long LCM(long a, long b) {if (a > b) {long t = a;a = b;b = t;}a /= gcd(a, b);return (a * b);} public static int findMax(int[] a, int left, int right) {int res = left;int max = a[left];for (int i = left + 1; i <= right; i++) {if (a[i] > max) {max = a[i];res = i;}}return res;} public static long findClosest(long arr[], long target) {int n = arr.length;if (target <= arr[0])return arr[0];if (target >= arr[n - 1])return arr[n - 1];int i = 0, j = n, mid = 0;while (i < j) {mid = (i + j) / 2;if (arr[mid] == target)return arr[mid];if (target < arr[mid]) {if (mid > 0 && target > arr[mid - 1])return getClosest(arr[mid - 1], arr[mid], target);j = mid;} else {if (mid < n - 1 && target < arr[mid + 1])return getClosest(arr[mid], arr[mid + 1], target);i = mid + 1;}}return arr[mid];} public static long getClosest(long val1, long val2, long target) {if (target - val1 >= val2 - target)return val2;else return val1;} public static int findClosest(int arr[], int target) { int n = arr.length; if (target <= arr[0]) return arr[0]; if (target >= arr[n - 1]) return arr[n - 1]; int i = 0, j = n, mid = 0; while (i < j) { mid = (i + j) / 2; if (arr[mid] == target) return arr[mid];if (target < arr[mid]) {if (mid > 0 && target > arr[mid - 1])return getClosest(arr[mid - 1], arr[mid], target);j = mid;} else {if (mid < n - 1 && target < arr[mid + 1])return getClosest(arr[mid], arr[mid + 1], target);i = mid + 1;}}return arr[mid];} public static int getClosest(int val1, int val2, int target) {if (target - val1 >= val2 - target)return val2;else return val1;} public static String reverse(String str) {String nstr = "";char ch;for (int i = 0; i < str.length(); i++) {ch = str.charAt(i);nstr = ch + nstr;}return nstr;} 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 int xorSum(int arr[], int n){int bits = 0;for (int i = 0; i < n; ++i)bits |= arr[i];int ans = bits * (int)Math.pow(2, n-1);return ans;} public static ArrayList<Integer> primeFactors(int n) { ArrayList<Integer> res = new ArrayList<>();while (n%2 == 0) { res.add(2); n = n/2; } for (int i = 3; i <= Math.sqrt(n); i = i+2) { while (n%i == 0) { res.add(i); n = n/i; } } if (n > 2) res.add(n);return res;} public static ArrayList<Long> primeFactors(long n) { ArrayList<Long> res = new ArrayList<>();while (n%2 == 0) { res.add(2L); n = n/2; } for (long i = 3; i <= Math.sqrt(n); i = i+2) { while (n%i == 0) { res.add(i); n = n/i; } } if (n > 2) res.add(n);return res;} static int lower_bound(int array[], int low, int high, int key){ int mid; while (low < high) { mid = low + (high - low) / 2; if (key <= array[mid]) high = mid; else low = mid + 1; } if (low < array.length && array[low] < key) low++; return low; } /********************************* Start Here ***********************************/ // int mod = 1000000007; public static void main(String[] args) throws java.lang.Exception { if (System.getProperty("ONLINE_JUDGE") == null) { PrintStream ps = new PrintStream(new File("output.txt")); System.setOut(ps); } FastScanner sc = new FastScanner("input.txt"); int T = sc.nextInt(); // int T = 1; while (T-- > 0) { int n = sc.nextInt(); int a[][] = new int[n][2]; for(int i=0; i<n; i++) { a[i][0] = sc.nextInt(); a[i][1] = i; } Arrays.sort(a, (a1, b)->a1[0]-b[0]); boolean res = true; for(int i=0;i<n;i++) { int oo=0,oe=0,no=0,ne=0; int temp = a[i][0]; while(i<n && a[i][0]==temp) { if(i%2==0) oe++; else oo++; if(a[i][1]%2==0) ne++; else no++; i++; } i--; if(oo!=no || oe!=ne) { res = false; } } if(res) System.out.println("YES"); else System.out.println("NO"); } System.out.close(); } public static int solve(String[] arr, char ch){ int res = 0; return res; } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { br = new BufferedReader(new InputStreamReader(System.in)); } } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } public String nextLine() { if (st == null || !st.hasMoreTokens()) { try { return br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken("\n"); } public String[] readStringArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) { a[i] = next(); } return a; } int nextInt() { return Integer.parseInt(nextToken()); } String next() { return nextToken(); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(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[][] read2dArray(int n, int m) { int[][] a = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = nextInt(); } } return a; } long[][] read2dlongArray(int n, int m) { long[][] a = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = nextLong(); } } return a; } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
317c816769414c8cf103b020bb7124ad
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import java.io.*; public class Main{ static int mod = (int) (Math.pow(10, 9)+7); static final int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 }; static final int[] dx8 = { -1, -1, -1, 0, 0, 1, 1, 1 }, dy8 = { -1, 0, 1, -1, 1, -1, 0, 1 }; static final int[] dx9 = { -1, -1, -1, 0, 0, 0, 1, 1, 1 }, dy9 = { -1, 0, 1, -1, 0, 1, -1, 0, 1 }; static final int inf = Integer.MAX_VALUE / 2; static final long infL = Long.MAX_VALUE / 3; static final double infD = Double.MAX_VALUE / 3; static final double eps = 1e-10; static final double pi = Math.PI; static List<Integer> primeNumbers = new ArrayList<>(); 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[] even = new int[100_001]; int[] odd = new int[100_001]; //we have this for now, let's figure out something else int[] arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = sc.nextInt(); if(i%2 == 0) even[arr[i]]++; else odd[arr[i]]++; } //now simply sort the arrays for the ease of the question sort(arr); boolean flag = true; for(int i = 0; i < n ; i++){ if(i%2 == 0){ even[arr[i]]--; if(even[arr[i]] < 0){ flag = false; break; } }else{ odd[arr[i]]--; if(odd[arr[i]] < 0){ flag = false; break; } } } out.println(flag ? "YES":"NO"); //we have the elements stored too now } out.close(); } //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- public static class segmentTree{ public long[] arr; public long[] tree; public long[] lazy; segmentTree(long[] array){ int n = array.length; arr = new long[n]; for(int i= 0; i < n; i++) arr[i] = array[i]; tree = new long[4*n + 1]; lazy = new long[4*n + 1]; } public void build(int s, int e, int index){ if(s == e){ tree[index] = arr[s]; return; } int mid = (s + e)/2; build(s, mid, 2*index); build(mid+1, e, 2*index + 1); tree[index] = Math.max(tree[2*index], tree[2*index+1]); } //now we have to build query function //there are different overlap in it, not very complicated, but definitely a lot useful //you should learn them for sure public long query(int s, int e, int sr, int er, int index){ if(lazy[index] != 0){ tree[index] = lazy[index]; if(s != e){ lazy[2*index+1] = lazy[index]; lazy[2*index] = lazy[index]; } lazy[index] = 0; } if(er < s || sr > e) return Integer.MIN_VALUE; if(sr <= s && e <= er) return tree[index]; int mid = (s + e)/2; long left = query(s, mid, sr, er, 2*index); long right = query(mid+1, e, sr, er, 2*index+1); return Math.max(left, right); } public void update(int s, int e, int indexr, int increment, int index){ if(lazy[index] != 0){ tree[index] = lazy[index]; if(s != e){ lazy[2*index+1] = lazy[index]; lazy[2*index] = lazy[index]; } lazy[index] = 0; } if(indexr < s || indexr > e) return; if(s == e) tree[indexr] = increment; int mid = (s + e)/2; update(s, mid, indexr, increment, 2*index); update(mid+1, e, indexr, increment, 2*index + 1); tree[index] = Math.max(tree[2*index+1], tree[2*index]); } public void updateRangeLazy(int s, int e, int sr, int er, long increment, int index){ if(lazy[index] != 0){ tree[index] = lazy[index]; if(s != e){ lazy[2*index + 1] = lazy[index]; lazy[2*index] = lazy[index]; } lazy[index] = 0; } if(sr > e || er < s) return; if(sr <= s && e <= er){ tree[index] = increment; if(s != e){ lazy[2*index + 1] = increment; lazy[2*index] = increment; } return; } int mid = (s + e)/2; updateRangeLazy(s, mid, sr, er, increment, 2*index); updateRangeLazy(mid+1, e, sr, er, increment, 2*index + 1); tree[index] = Math.max(tree[2*index], tree[2*index+1]); } } //prime sieve public static void primeSieve(int n){ BitSet bitset = new BitSet(n+1); for(long i = 0; i < n ; i++){ if (i == 0 || i == 1) { bitset.set((int) i); continue; } if(bitset.get((int) i)) continue; primeNumbers.add((int)i); for(long j = i; j <= n ; j+= i) bitset.set((int)j); } } //number of divisors public static int countDivisors(long number){ if(number == 1) return 1; List<Integer> primeFactors = new ArrayList<>(); int index = 0; long curr = primeNumbers.get(index); while(curr * curr <= number){ while(number % curr == 0){ number = number/curr; primeFactors.add((int) curr); } index++; curr = primeNumbers.get(index); } if(number != 1) primeFactors.add((int) number); int current = primeFactors.get(0); int totalDivisors = 1; int currentCount = 2; for (int i = 1; i < primeFactors.size(); i++) { if (primeFactors.get(i) == current) { currentCount++; } else { totalDivisors *= currentCount; currentCount = 2; current = primeFactors.get(i); } } totalDivisors *= currentCount; return totalDivisors; } //now adding next permutation function to java hehe public static boolean next_permutation(int[] p) { for (int a = p.length - 2; a >= 0; --a) if (p[a] < p[a + 1]) for (int b = p.length - 1;; --b) if (p[b] > p[a]) { int t = p[a]; p[a] = p[b]; p[b] = t; for (++a, b = p.length - 1; a < b; ++a, --b) { t = p[a]; p[a] = p[b]; p[b] = t; } return true; } return false; } //finding the value of NCR in O(RlogN) time and O(1) space public static long getNcR(int n, int r) { long p = 1, k = 1; if (n - r < r) r = n - r; if (r != 0) { while (r > 0) { p *= n; k *= r; long m = __gcd(p, k); p /= m; k /= m; n--; r--; } } else { p = 1; } return p; } //for ncr calculator public static long __gcd(long n1, long n2) { long gcd = 1; for (int i = 1; i <= n1 && i <= n2; ++i) { // Checks if i is factor of both integers if (n1 % i == 0 && n2 % i == 0) { gcd = i; } } return gcd; } //is vowel function 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'); } //two add two big numbers with some mod public static int add(int a, int b) { a+=b; if (a>=mod) return a-mod; return a; } //two sub two numbers public static int sub(int a, int b) { a-=b; if (a<0) a+=mod; else if (a>=mod) a-=mod; return a; } //to sort the array with better method 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); } //for calculating binomialCoeff public static int binomialCoeff(int n, int k) { int C[] = new int[k + 1]; // nC0 is 1 C[0] = 1; for (int i = 1; i <= n; i++) { // Compute next row of pascal // triangle using the previous row for (int j = Math.min(i, k); j > 0; j--) C[j] = C[j] + C[j - 1]; } return C[k]; } //Pair with int int public static class Pair{ public int a; public int b; Pair(int a , int b){ this.a = a; this.b = b; } @Override public String toString(){ return a + " -> " + b; } } //Triplet with int int int public static class Triplet{ public int a; public int b; public int c; Triplet(int a , int b, int c){ this.a = a; this.b = b; this.c = c; } @Override public String toString(){ return a + " -> " + b; } } //Shortcut function public static long lcm(long a , long b){ return a * (b/gcd(a,b)); } //let's make one for calculating lcm basically public static int lcm(int a , int b){ return (a * b)/gcd(a,b); } //int version for gcd public static int gcd(int a, int b){ if(b == 0) return a; return gcd(b , a%b); } //long version for gcd public static long gcd(long a, long b){ if(b == 0) return a; return gcd(b , a%b); } //swapping two elements in an array public static void swap(int[] arr, int left , int right){ int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } // //for char array public static void swap(char[] arr, int left , int right){ char temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } //reversing an array public static void reverse(int[] arr){ int left = 0; int right = arr.length-1; while(left <= right){ swap(arr, left,right); left++; right--; } } //-----------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; } } //-------------------------------------------------------- }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
7e3f98588a67fcdae4ac2cc31c7dc738
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.math.RoundingMode; import java.text.DecimalFormat; import java.util.*; public class copy{ public static int find(int a,int[] parent) { if(parent[a]==-1) return a; return find(parent[a],parent); } public static void union(int a,int b,int[] parent, int[] rank) { int x=find(a,parent); int y=find(b,parent); if(rank[x]>rank[y]) parent[y]=x; else if(rank[y]>rank[x]) parent[x]=y; else { parent[x]=y; rank[y]++; } } static double answer; public static int check(ArrayList<ArrayList<Integer>> edges,int x,int parent,int[] statues,int n) { ArrayList<Integer> arr=new ArrayList<>(); for(int i:edges.get(x)) { if(i!=parent) { arr.add(check(edges,i,x,statues,n)); } } int sum=0; for(int i=arr.size()-1;i>=0;i--) { answer+=((long)arr.get(i)*(long)sum*(long)statues[x])/((double)n*(double)(n-1)); sum+=arr.get(i); } answer+=((long)sum*(long)statues[x])/((double)n*(double)(n-1)); //System.out.println(x+" "+sum); return 1+sum; } public static boolean check(HashMap<Integer,ArrayList<Integer>> hp,HashMap<Integer,ArrayList<Integer>> hp1) { for(int i:hp.keySet()) { int sum=0,sum1=0; ArrayList<Integer> temp1=hp.get(i); ArrayList<Integer> temp2=hp1.get(i); for(int j=0;j<temp1.size();j++) { if(temp1.get(j)%2==0) ++sum; if(temp2.get(j)%2==0) ++sum1; } //System.out.println(i+" "+sum); if(sum!=sum1) return false; } return true; } public static void main(String[] args)throws IOException { Reader.init(System.in); BufferedWriter output=new BufferedWriter(new OutputStreamWriter(System.out)); int T=Reader.nextInt(); for(int m=1;m<=T;m++) { int N=Reader.nextInt(); ArrayList<Integer> arr=new ArrayList<>(); HashMap<Integer,ArrayList<Integer>> hp=new HashMap<>(); HashMap<Integer,ArrayList<Integer>> hp1=new HashMap<>(); for(int i=0;i<N;i++) { int X=Reader.nextInt(); if(hp.containsKey(X)) { hp.get(X).add(i); } else { hp.put(X,new ArrayList<>()); hp.get(X).add(i); } arr.add(X); } Collections.sort(arr); for(int i=0;i<N;i++) { int X=arr.get(i); if(hp1.containsKey(X)) { hp1.get(X).add(i); } else { hp1.put(X,new ArrayList<>()); hp1.get(X).add(i); } } if(check(hp,hp1)) output.write("YES"+"\n"); else output.write("NO"+"\n"); } output.flush(); } } class cord { int x,y; cord(int x1,int y1) { x=x1; y=y1; } } class cp { int a,b; cp(int a,int b) { this.a=a; this.b=b; } } class sort_cp implements Comparator<cp> { public int compare(cp o1,cp o2) { if(o1.b-o1.a>o2.b-o2.a) return -1; else if(o1.b-o1.a<o2.b-o2.a) return 1; else return 0; } } class g_node{ int val; long wt; g_node(int val, long wt) { this.val = val; this.wt = wt; } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
5f5709d484c75ea3a93d5214e3704841
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import java.io.*; public class Main { // For fast input output 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.setErr(new PrintStream(new FileOutputStream("error.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; } } // end of fast i/o code static StringBuilder str = new StringBuilder(""); public static void main(String[] args) { FastReader reader = new FastReader(); int t = reader.nextInt(); while(t-->0) { int n = reader.nextInt(); ArrayList<Integer> list = new ArrayList<>(); for(int i = 0; i < n; i++) list.add(reader.nextInt()); ArrayList<Integer> odd = new ArrayList<>(); ArrayList<Integer> even = new ArrayList<>(); for(int i = 0; i < n; i+=2) odd.add(list.get(i)); for(int i = 1; i < n; i+=2) even.add(list.get(i)); Collections.sort(odd); Collections.sort(even); Collections.sort(list); ArrayList<Integer> secondlist = new ArrayList<>(); for(int i = 0; i < Math.min(odd.size(), even.size()); i++) { secondlist.add(odd.get(i)); secondlist.add(even.get(i)); } if(even.size()>odd.size()) secondlist.add(even.get(even.size()-1)); if(odd.size()>even.size()) secondlist.add(odd.get(odd.size()-1)); if(list.equals(secondlist)) str.append("YES\n"); else str.append("NO\n"); // boolean no = false; // for(int i = 0, j = 0; i < n; i++, j++) // { // if(list.get(i)!=odd.get(j)) // { // no = true; // break; // } // i++; // if(i==n) // break; // if(list.get(i)!=even.get(j)) // { // no = true; // break; // } // } // if(no) // str.append("NO\n"); // else // str.append("YES\n"); } System.out.println(str.toString()); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
a6b1978b781f07705c76154bbb27df75
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.util.*; public class A { public static int[] sort(int arr[]) { List<Integer> list = new ArrayList<>(); for(int i:arr) list.add(i); Collections.sort(list); for(int i = 0;i<list.size();i++) { arr[i] = list.get(i); } return arr; } public static void main(String[] args) { FastScanner scan = new FastScanner(); int t = scan.nextInt(); for(int tt = 0;tt<t;tt++) { int n = scan.nextInt(); int arr[] = scan.readArray(n); int sorted[] = arr.clone(); sort(sorted); int even[] = new int[(n+1)/2]; int odd[] = new int[n/2]; for(int i = 0;i<n;i++) { if(i%2==0) even[i/2] = arr[i]; else odd[i/2] = arr[i]; } sort(even); sort(odd); boolean possible = true; for(int i = 0;i<n;i++) { if(i%2==0) { if(even[i/2]!=sorted[i]) { possible = false; break; } } else { if(odd[i/2]!=sorted[i]) { possible = false; break; } } } if(possible) System.out.println("YES"); else System.out.println("NO"); } } 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()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
b28dcb16dfae764395519fa0cd919c05
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.PriorityQueue; import java.util.Stack; import java.util.StringTokenizer; import java.util.stream.IntStream; public class QH { public static void main(String[] args) { FastScanner fs=new FastScanner(); int t=fs.nextInt(); int n; int arr[]; int temp; while(t-->0) { n=fs.nextInt(); Map<Integer,Integer> even =new HashMap<>(); int x; PriorityQueue<Integer> queue=new PriorityQueue<>(); for(int i=0;i<n;i++) { x=fs.nextInt(); queue.add(x); if(i%2==0) { even.put(x, even.getOrDefault(x, 0)+1); } } for(int i=0;i<n;i++) { x=queue.poll(); if(i%2==0) { even.put(x, even.getOrDefault(x, 0)-1); if(even.get(x)<=0) { even.remove(x); } } } if(even.size()>0) { System.out.println("No"); }else { System.out.println("Yes"); } } } 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) { } return st.nextToken(); } BigInteger nextBigInteger() { return new BigInteger(next()); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } ArrayList<Integer> readList(int n) { ArrayList<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(nextInt()); } return list; } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } double nextDouble() { return Double.parseDouble(next()); } int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } int lcm(int a, int b) { return (a / gcd(a, b)) * b; } boolean[] sieveOfEratosthenes(int n) { boolean prime[] = new boolean[n + 1]; Arrays.fill(prime, true); for (int j = 2; j * j <= n; j++) { if (prime[j] == true) { for (int i = j * j; i <= n; i += j) prime[i] = false; } } return prime; } public int arraySum(int[] S) { return IntStream.of(S).sum(); } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
2e035e5f22c8483f8bcc775770600dc5
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import java.io.*; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.Math.max; public class EdA { static long[] mods = {1000000007, 998244353, 1000000009}; static long mod = mods[0]; public static MyScanner sc; public static PrintWriter out; public static void main(String[] largewang) throws Exception{ // TODO Auto-generated method stub sc = new MyScanner(); out = new PrintWriter(System.out); int t = sc.nextInt(); while (t-->0) { int n = sc.nextInt(); int[] arr = readArrayInt(n); ArrayList<Integer> arr1 = new ArrayList<>(); ArrayList<Integer> arr2 = new ArrayList<>(); for(int j = 0;j<n;j++){ if (j%2 == 0) arr1.add(arr[j]); else arr2.add(arr[j]); } Collections.sort(arr1); Collections.sort(arr2); int prev = 0; boolean ans = true; for(int j = 0;j<n;j++){ if (j%2 == 0){ if (arr1.get(j/2) < prev) { ans = false; break; } prev = arr1.get(j/2); } else { if (arr2.get(j/2) < prev) { ans = false; break; } prev = arr2.get(j/2); } } verdict(ans); } out.close(); } static class Pair implements Comparable<Pair>{ private int x; private int index; public Pair(int x, int index){ this.x = x; this.index= index; } public int compareTo(Pair other){ return Integer.compare(x, other.x) != 0 ? Integer.compare(x, other.x) : Integer.compare(index, other.index); } } public static void sort(int[] array){ ArrayList<Integer> copy = new ArrayList<>(); for (int i : array) copy.add(i); Collections.sort(copy); for(int i = 0;i<array.length;i++) array[i] = copy.get(i); } static String[] readArrayString(int n){ String[] array = new String[n]; for(int j =0 ;j<n;j++) array[j] = sc.next(); return array; } static int[] readArrayInt(int n){ int[] array = new int[n]; for(int j = 0;j<n;j++) array[j] = sc.nextInt(); return array; } static int[] readArrayInt1(int n){ int[] array = new int[n+1]; for(int j = 1;j<=n;j++){ array[j] = sc.nextInt(); } return array; } static long[] readArrayLong(int n){ long[] array = new long[n]; for(int j =0 ;j<n;j++) array[j] = sc.nextLong(); return array; } static double[] readArrayDouble(int n){ double[] array = new double[n]; for(int j =0 ;j<n;j++) array[j] = sc.nextDouble(); return array; } static int minIndex(int[] array){ int minValue = Integer.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static int minIndex(long[] array){ long minValue = Long.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static int minIndex(double[] array){ double minValue = Double.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static long power(long x, long y){ if (y == 0) return 1; if (y%2 == 1) return (x*power(x, y-1))%mod; return power((x*x)%mod, y/2)%mod; } static void verdict(boolean a){ out.println(a ? "YES" : "NO"); } 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; } } } //StringJoiner sj = new StringJoiner(" "); //sj.add(strings) //sj.toString() gives string of those stuff w spaces or whatever that sequence is
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
64aa121af813ac20bf7c722695b13156
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { static final PrintWriter out =new PrintWriter(System.out); static final FastReader sc = new FastReader(); //I invented a new word!Plagiarism! //Did you hear about the mathematician who’s afraid of negative numbers?He’ll stop at nothing to avoid them //What do Alexander the Great and Winnie the Pooh have in common? Same middle name. //I finally decided to sell my vacuum cleaner. All it was doing was gathering dust! //ArrayList<Integer> a=new ArrayList <Integer>(); //PriorityQueue<Integer> pq=new PriorityQueue<>(); //char[] a = s.toCharArray(); // char s[]=sc.next().toCharArray(); public static boolean sorted(int a[]) { int n=a.length,i; int b[]=new int[n]; for(i=0;i<n;i++) b[i]=a[i]; Arrays.sort(b); for(i=0;i<n;i++) { if(a[i]!=b[i]) return false; } return true; } public static void main (String[] args) throws java.lang.Exception { int tes=sc.nextInt(); label:while(tes-->0) { int n=sc.nextInt(); int a[]=new int[n]; HashMap<Integer,Integer> odd=new HashMap<>(); HashMap<Integer,Integer> even=new HashMap<>(); int i; for(i=1;i<=n;i++) { a[i-1]=sc.nextInt(); if(i%2==0) even.put(a[i-1],even.getOrDefault(a[i-1],0)+1); else odd.put(a[i-1],odd.getOrDefault(a[i-1],0)+1); } Arrays.sort(a); for(i=1;i<=n;i++) { if(i%2==0) even.put(a[i-1],even.getOrDefault(a[i-1],0)-1); else odd.put(a[i-1],odd.getOrDefault(a[i-1],0)-1); } for(i=0;i<n;i++) { if(odd.getOrDefault(a[i],0)>0 || even.getOrDefault(a[i],0)>0) { System.out.println("NO"); continue label; } } System.out.println("YES"); } } public static int first(ArrayList<Integer> arr, int low, int high, int x, int n) { if (high >= low) { int mid = low + (high - low) / 2; if ((mid == 0 || x > arr.get(mid-1)) && arr.get(mid) == x) return mid; else if (x > arr.get(mid)) return first(arr, (mid + 1), high, x, n); else return first(arr, low, (mid - 1), x, n); } return -1; } public static int last(ArrayList<Long> arr, int low, int high, long x, int n) { if (high >= low) { int mid = low + (high - low) / 2; if ((mid == n - 1 || x < arr.get(mid+1)) && arr.get(mid) == x) return mid; else if (x < arr.get(mid)) return last(arr, low, (mid - 1), x, n); else return last(arr, (mid + 1), high, x, n); } return -1; } public static int lower_bound(ArrayList<Long> ar,int lo , int hi , long k) { //Collections.sort(ar); int s=lo; int e=hi; while (s !=e) { int mid = s+e>>1; if (ar.get((int)mid) <k) { s=mid+1; } else { e=mid; } } if(s==ar.size()) { return -1; } return s; } public static int upper_bound(ArrayList<Long> ar,int lo , int hi, long k) { //Collections.sort(ar); int s=lo; int e=hi; 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 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; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } static long fact(long N) { long mod=1000000007; long n=2; if(N<=1)return 1; else { for(int i=3; i<=N; i++)n=(n*i)%mod; } return n; } private static boolean isInteger(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
e5a2441e0bd322830c60ffb83b840c40
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import java.text.*; import java.io.*; public class Main { 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(); ArrayList<Integer> list = new ArrayList<>(); for(int i = 0; i < n; i++)list.add(sc.nextInt()); ArrayList<Integer> list2 = new ArrayList<>(); list2.addAll(list); Collections.sort(list2); HashMap<Integer, TreeSet<Integer>> adj = new HashMap<>(); for(int i = 0; i < n; i++) { TreeSet<Integer> lis = adj.getOrDefault(list2.get(i), new TreeSet<>()); if(i%2==1)lis.add(-i); else lis.add(i); adj.put(list2.get(i), lis); } boolean exist = true; for(int i = 0; i < n; i++) { int ele = list.get(i); TreeSet<Integer> lis = adj.get(ele); if(i%2==0) { int val = lis.lower(Integer.MAX_VALUE); if(val<0) { exist = false; break; }else { lis.remove(val); } }else { int val = lis.higher(Integer.MIN_VALUE); if(val>=0) { exist = false; break; }else { lis.remove(val); } } } if(exist)writer.println("YES"); else writer.println("NO"); } writer.flush(); writer.close(); } private static long power (long a, long n, long p) { long res = 1; while(n!=0) { if(n%2==1) { res=(res*a)%p; n--; }else { a= (a*a)%p; n/=2; } } return res; } 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[(int)a] = find(arr[(int)a], arr); return arr[(int)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 SegmentTree{ int size; long arr[]; SegmentTree(int n){ size = 1; while(size<n)size*=2; arr = new long[size*2]; } public void build(int input[]) { build(input, 0,0, size); } public void set(int i, int v) { set(i,v,0,0,size); } // sum from l + r-1 public long sum(int l, int r) { return sum(l,r,0,0,size); } private void build(int input[], int x, int lx, int rx) { if(rx-lx==1) { if(lx < input.length ) arr[x] = 1; return; } int mid = (lx+rx)/2; build(input, 2*x+1, lx, mid); build(input,2*x+2,mid,rx); arr[x] = arr[2*x+1] + arr[2*x+2]; } private void set(int i, int v, int x, int lx, int rx) { if(rx-lx==1) { arr[x]++; return; } int mid = (lx+rx)/2; if(i < mid) set(i, v, 2*x+1, lx, mid); else set(i,v,2*x+2,mid,rx); arr[x] = arr[2*x+1] + arr[2*x+2]; } private long sum(int l, int r, int x, int lx, int rx) { if(lx>=r || rx <= l)return 0; if(lx>=l && rx <= r)return arr[x]; int mid = (lx+rx)/2; long s1 = sum(l,r,2*x+1,lx,mid); long s2 = sum(l,r,2*x+2,mid,rx); return s2+s1; } // int first_above(int v, int l, int x, int lx, int rx) { // if(arr[x].a<v)return -1; // if(rx<=l)return -1; // if(rx-lx==1)return lx; // int m = (lx+rx)/2; // int res = first_above(v,l,2*x+1,lx,m); // if(res==-1)res = first_above(v,l,2*x+2,m,rx); // return res; // // } } class Pair implements Comparable<Pair>{ long a; long b; long c; Pair(long a, long b, long c){ this.a = a; this.b = b; this.c = c; } Pair(long a, long b){ this.a = a; this.b = b; this.c = 0; } @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 && pair.c == this.c); } @Override public int hashCode() { // return Objects.hash(a,b); return new Long(a).hashCode() * 31 + new Long(b).hashCode(); } @Override public int compareTo(Pair o) { if(o.a != this.a) return Long.compare(this.a, o.a); else return Long.compare(this.b, o.b); } @Override public String toString() { return this.a + " " + this.b; } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
941f648f016e561c5d3142e4b615fccc
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int[] arr = new int[n]; int even_size = n/2; int odd_size=n/2; if(n%2!=0){ even_size+=1; } int[] odd = new int[odd_size]; int[] even = new int[even_size]; int k =0; int l=0; for(int i =0;i<n;i++){ arr[i]=sc.nextInt(); if(i%2==0){ even[k++]=arr[i]; }else{ odd[l++]=arr[i]; } } Arrays.sort(arr); Arrays.sort(odd); Arrays.sort(even); int i =0; int j =0 ; boolean flag= false; k=0; for(k=0;k<arr.length;k++){ if(k%2==0 && i<n/2){ if(even[i++]!=arr[k]){ System.out.println("NO"); flag=true; break; } }else if(j<n/2){ if(odd[j++]!=arr[k]){ System.out.println("NO"); flag=true; break; } } } //if(flag==true)break if(flag==false) System.out.println("YES"); } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
3fc24192ad16f8ae475a5bc5ddf7b2e6
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.IOException; import java.util.*; public class A { static Scanner sc = new Scanner(System.in); public static void main(String[] args) throws IOException { int t = sc.nextInt(); while(t-- > 0){ solve(); } } static void solve(){ int n = sc.nextInt(); List<Integer> arr = new ArrayList<>(n); for(int i = 0; i < n; i++){ arr.add(sc.nextInt()); } // store odd positions of a number and even positions of a number // in new and old array if count of both same, then answer "yes" List<Integer> oddInitial = new ArrayList<>(); List<Integer> evenInitial = new ArrayList<>(); List<Integer> oddFinal = new ArrayList<>(); List<Integer> evenFinal = new ArrayList<>(); for(int i = 0; i <= 1e5; i++){ oddFinal.add(0); oddInitial.add(0); evenFinal.add(0); evenInitial.add(0); } for(int i = 0; i < n; i++){ if(i % 2 == 0) evenInitial.set(arr.get(i), evenInitial.get(arr.get(i)) + 1); else oddInitial.set(arr.get(i), oddInitial.get(arr.get(i)) + 1); } List<Integer> copy = new ArrayList<>(); for(int i = 0; i < n; i++) copy.add(arr.get(i)); Collections.sort(copy); for(int i = 0; i < n; i++){ if(i % 2 == 0) evenFinal.set(copy.get(i), evenFinal.get(copy.get(i)) + 1); else oddFinal.set(copy.get(i), oddFinal.get(copy.get(i)) + 1); } for(int i = 0; i < n; i++){ if(!evenInitial.get(arr.get(i)).equals(evenFinal.get(arr.get(i))) || !oddInitial.get(arr.get(i)).equals(oddFinal.get(arr.get(i)))){ System.out.println("NO"); return; } } System.out.println("YES"); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
31995f4c81f0842b3b4b33fef0b2094e
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; public class q1545 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int v[][] = new int[100001][2]; while (t-- > 0) { int N = sc.nextInt(); int arr[] = new int[N]; for (int i = 0; i < N; i++) { arr[i] = sc.nextInt(); v[arr[i]][i % 2]++; } Arrays.sort(arr); for (int i = 0; i < N; i++) { v[arr[i]][i % 2]--; } boolean printed = false; for (int i = 0; i < N; i++) { if (v[arr[i]][0] != 0 || v[arr[i]][1] != 0) { System.out.println("NO"); printed = true; break; } } if (!printed) { System.out.println("YES"); } for (int i = 0; i < N; i++) { v[arr[i]][0] = v[arr[i]][1] = 0; } } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
4e4f5ddd196cf6faf2a160d5481eec69
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
//package com.rajan.codeforces.level_1500; import java.io.*; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; public class AquaMoonAndStrangeSort { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out)); int tt = Integer.parseInt(reader.readLine()); tests: while (tt-- > 0) { int n = Integer.parseInt(reader.readLine()); int[] a = new int[n]; String[] temp = reader.readLine().split("\\s+"); PriorityQueue<int[]> heap = new PriorityQueue<>((x, y) -> Integer.compare(x[0], y[0])); for (int i = 0; i < n; i++) { int item = Integer.parseInt(temp[i]); heap.offer(new int[]{item, i}); } int idx = 0; while (!heap.isEmpty()) { int val = heap.peek()[0]; Queue<Integer> evenIdx = new LinkedList<>(), oddIdx = new LinkedList<>(); while (!heap.isEmpty() && heap.peek()[0] == val) { int[] data = heap.poll(); if (data[1] % 2 == 0) evenIdx.offer(data[1]); else oddIdx.offer(data[1]); } while (!evenIdx.isEmpty() || !oddIdx.isEmpty()) { if ((idx & 1) == 0) { if (evenIdx.isEmpty()) { writer.write("NO\n"); continue tests; } evenIdx.poll(); } else { if (oddIdx.isEmpty()) { writer.write("NO\n"); continue tests; } oddIdx.poll(); } idx++; } } writer.write("YES\n"); } writer.flush(); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
bbae7f6fbd065cc8a106c5868c5533f1
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; import static java.lang.Math.ceil; import static java.util.Arrays.*; public class Codeforces { static int mod = 998244353; public static void main(String[] args) throws IOException { int t = ri(); while (t-- > 0) { int n = rni(); int a[] = ria(n); ArrayList<Integer> odd = new ArrayList<>(); ArrayList<Integer> even = new ArrayList<>(); for (int i = 0; i < n; i++) { if ((i & 1) == 1) { odd.add(a[i]); } else { even.add(a[i]); } } rsort(a); Collections.sort(odd); Collections.sort(even); int oddIndex = 0, evenIndex = 0; int flag = 0; for (int i = 0; i < n; i++) { int el = -1; if ((i & 1) == 1) { el = odd.get(oddIndex); oddIndex++; } else { el = even.get(evenIndex); evenIndex++; } if (a[i] != el) { flag = -1; break; } } if (flag == 0) { prY(); } else { prN(); } } close(); } /* static boolean check(int a, int b, int h, int m) { if (ar[a % 10] == -1 || ar[a / 10] == -1 || ar[b % 10] == -1 || ar[b / 10] == -1) { return false; } int x = ar[b % 10] * 10 + ar[b / 10]; int y = ar[a % 10] * 10 + ar[a / 10]; if (x < h && y < m) { return true; } return false; } */ static boolean isPalindrome(char[] c) { int low = 0, high = c.length - 1; while (low < high) { if (c[low] != c[high] || c[low] == '?' || c[high] == '?') return false; low++; high--; } return true; } static char get(int a) { return (char) (a + 'a'); } static void addToPq(int n, PriorityQueue<Integer> p) { for (int i = 0; i < n; i++) { p.add(i); } } static boolean better(int a, int b, int[][] matrix) { int cnt = 0; for (int i = 0; i < 5; i++) { if (matrix[a][i] < matrix[b][i]) cnt++; } return cnt >= 3; } static BufferedReader __i = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter __o = new PrintWriter(new OutputStreamWriter(System.out)); static StringTokenizer input; static Random __r = new Random(); // references // IBIG = 1e9 + 7 // IMAX ~= 2e9 // LMAX ~= 9e18 // constants static final int IBIG = 1000000007; static final int IMAX = 2147483647; static final long LMAX = 9223372036854775807L; // math util static int minof(int a, int b, int c) { return min(a, min(b, c)); } static int minof(int... x) { if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min; } static long minof(long a, long b, long c) { return min(a, min(b, c)); } static long minof(long... x) { if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min; } static int maxof(int a, int b, int c) { return max(a, max(b, c)); } static int maxof(int... x) { if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max; } static long maxof(long a, long b, long c) { return max(a, max(b, c)); } static long maxof(long... x) { if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max; } static int powi(int a, int b) { if (a == 0) return 0; int ans = 1; while (b > 0) { if ((b & 1) > 0) ans *= a; a *= a; b >>= 1; } return ans; } static long powl(long a, int b) { if (a == 0) return 0; long ans = 1; while (b > 0) { if ((b & 1) > 0) ans *= a; a *= a; b >>= 1; } return ans; } static int fli(double d) { return (int) d; } static int cei(double d) { return (int) ceil(d); } static long fll(double d) { return (long) d; } static long cel(double d) { return (long) ceil(d); } static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static int[] exgcd(int a, int b) { if (b == 0) return new int[]{1, 0}; int[] y = exgcd(b, a % b); return new int[]{y[1], y[0] - y[1] * (a / b)}; } static long[] exgcd(long a, long b) { if (b == 0) return new long[]{1, 0}; long[] y = exgcd(b, a % b); return new long[]{y[1], y[0] - y[1] * (a / b)}; } static int randInt(int min, int max) { return __r.nextInt(max - min + 1) + min; } static long mix(long x) { x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31); } public static boolean[] findPrimes(int limit) { assert limit >= 2; final boolean[] nonPrimes = new boolean[limit]; nonPrimes[0] = true; nonPrimes[1] = true; int sqrt = (int) Math.sqrt(limit); for (int i = 2; i <= sqrt; i++) { if (nonPrimes[i]) continue; for (int j = i; j < limit; j += i) { if (!nonPrimes[j] && i != j) nonPrimes[j] = true; } } return nonPrimes; } // array util static void reverse(int[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(long[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(double[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(char[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void shuffle(int[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(long[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(double[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void rsort(int[] a) { shuffle(a); sort(a); } static void rsort(long[] a) { shuffle(a); sort(a); } static void rsort(double[] a) { shuffle(a); sort(a); } static int[] copy(int[] a) { int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static long[] copy(long[] a) { long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static double[] copy(double[] a) { double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static char[] copy(char[] a) { char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static void resetBoolean(boolean[] vis, int n) { for (int i = 0; i < n; i++) { vis[i] = false; } } static void setMinusOne(int[][] matrix) { int row = matrix.length; int col = matrix[0].length; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { matrix[i][j] = -1; } } } // input static void r() throws IOException { input = new StringTokenizer(rline()); } static int ri() throws IOException { return Integer.parseInt(rline().split(" ")[0]); } static long rl() throws IOException { return Long.parseLong(rline()); } static double rd() throws IOException { return Double.parseDouble(rline()); } static int[] ria(int n) throws IOException { int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni(); return a; } static void ria(int[] a) throws IOException { int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = ni(); } static int[] riam1(int n) throws IOException { int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; return a; } static void riam1(int[] a) throws IOException { int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; } static long[] rla(int n) throws IOException { long[] a = new long[n]; r(); for (int i = 0; i < n; ++i) a[i] = nl(); return a; } static void rla(long[] a) throws IOException { int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = nl(); } static double[] rda(int n) throws IOException { double[] a = new double[n]; r(); for (int i = 0; i < n; ++i) a[i] = nd(); return a; } static void rda(double[] a) throws IOException { int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = nd(); } static char[] rcha() throws IOException { return rline().toCharArray(); } static void rcha(char[] a) throws IOException { int n = a.length, i = 0; for (char c : rline().toCharArray()) a[i++] = c; } static String rline() throws IOException { return __i.readLine(); } static String n() { return input.nextToken(); } static int rni() throws IOException { r(); return ni(); } static int ni() { return Integer.parseInt(n()); } static long rnl() throws IOException { r(); return nl(); } static long nl() { return Long.parseLong(n()); } static double rnd() throws IOException { r(); return nd(); } static double nd() { return Double.parseDouble(n()); } // output static void pr(int i) { __o.print(i); } static void prln(int i) { __o.println(i); } static void pr(long l) { __o.print(l); } static void prln(long l) { __o.println(l); } static void pr(double d) { __o.print(d); } static void prln(double d) { __o.println(d); } static void pr(char c) { __o.print(c); } static void prln(char c) { __o.println(c); } static void pr(char[] s) { __o.print(new String(s)); } static void prln(char[] s) { __o.println(new String(s)); } static void pr(String s) { __o.print(s); } static void prln(String s) { __o.println(s); } static void pr(Object o) { __o.print(o); } static void prln(Object o) { __o.println(o); } static void prln() { __o.println(); } static void pryes() { prln("yes"); } static void pry() { prln("Yes"); } static void prY() { prln("YES"); } static void prno() { prln("no"); } static void prn() { prln("No"); } static void prN() { prln("NO"); } static boolean pryesno(boolean b) { prln(b ? "yes" : "no"); return b; } ; static boolean pryn(boolean b) { prln(b ? "Yes" : "No"); return b; } static boolean prYN(boolean b) { prln(b ? "YES" : "NO"); return b; } static void prln(int... a) { for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i) ; if (a.length > 0) prln(a[a.length - 1]); else prln(); } static void prln(long... a) { for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i) ; if (a.length > 0) prln(a[a.length - 1]); else prln(); } static void prln(double... a) { for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i) ; if (a.length > 0) prln(a[a.length - 1]); else prln(); } static <T> void prln(Collection<T> c) { int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i) ; if (n >= 0) prln(iter.next()); else prln(); } static void h() { prln("hlfd"); flush(); } static void flush() { __o.flush(); } static void close() { __o.close(); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
26a57346b41058f87ac1bab6f314b337
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.nio.charset.StandardCharsets; import java.util.*; public class Main { 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[1000]; // 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(); } } private static int[][] dirs = {{-1,0}, {0, 1}, {1, 0}, {0, -1}}; private static int[] offsets = {3, 2, 1, 0}; private static long inf = (long) 1e9 + 7; private static long div = 998_244_353L; private static long div1 = (long)(1e9) + 7; // private static void swap(int[] arr, int i, int j) { // int tmp = arr[i]; // arr[i] = arr[j]; // arr[j] = tmp; // } // // private static void perms(int offset, List<List<Integer>> perms, int[] arr) { // if (offset == arr.length) { // List<Integer> perm = new ArrayList<>(); // for (int j : arr) perm.add(j); // perms.add(perm); // return; // } // // for(int i = offset; i < arr.length; ++i) { // swap(arr, offset, i); // perms(offset + 1, perms, arr); // swap(arr, offset, i); // } // // } // private static long pow(long num, long pow, long div) { // if (pow == 0) return 1; // if (pow == 1) return num % div; // long start = 1; // if (pow % 2 != 0) { // start = num % div; // pow--; // } // long res = pow(num, pow / 2, div); // start = (start * res) % div; // start = (start * res) % div; // return start; // } public static class FenwickTree { int[] array; // 1-indexed array, In this array We save cumulative information to perform efficient range queries and updates public FenwickTree(int size) { array = new int[size + 1]; } /** * Range Sum query from 1 to ind * ind is 1-indexed * <p> * Time-Complexity: O(log(n)) * * @param ind index * @return sum */ public int rsq(int ind) { assert ind > 0; int sum = 0; while (ind > 0) { sum += array[ind]; //Extracting the portion up to the first significant one of the binary representation of 'ind' and decrementing ind by that number ind -= ind & (-ind); } return sum; } /** * Range Sum Query from a to b. * Search for the sum from array index from a to b * a and b are 1-indexed * <p> * Time-Complexity: O(log(n)) * * @param a left index * @param b right index * @return sum */ public int rsq(int a, int b) { assert b >= a && a > 0 && b > 0; return rsq(b) - rsq(a - 1); } /** * Update the array at ind and all the affected regions above ind. * ind is 1-indexed * <p> * Time-Complexity: O(log(n)) * * @param ind index * @param value value */ public void update(int ind, int value) { assert ind > 0; while (ind < array.length) { array[ind] += value; //Extracting the portion up to the first significant one of the binary representation of 'ind' and incrementing ind by that number ind += ind & (-ind); } } public int size() { return array.length - 1; } } private static long[] gcd(long p, long q) { if (q == 0) return new long[] { p, 1, 0 }; long[] vals = gcd(q, p % q); long d = vals[0]; long a = vals[2]; long b = vals[1] - (p / q) * vals[2]; return new long[] { d, a, b }; } public static int gcd(int a, int b) { return b==0 ? a : gcd(b, a%b); } public static void main(String[] commands) throws Exception { // Reader rd = new Reader(); BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); // Scanner rd = new Scanner(System.in); BufferedOutputStream bfo = new BufferedOutputStream(System.out); int Q = Integer.parseInt(st.nextToken()); for(int q = 0;q < Q; ++q) { st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); var odd = new PriorityQueue<Integer>(); var even = new PriorityQueue<Integer>(); st = new StringTokenizer(infile.readLine()); for(int i = 0;i < N; ++i) { int num = Integer.parseInt(st.nextToken()); if (i % 2 == 0) { even.add(num); } else { odd.add(num); } } boolean ok = true; for(int i = 0;i < N && ok; ++i) { if (i % 2 == 0) { if (even.isEmpty() || (!odd.isEmpty() && odd.peek() < even.peek()) ) { ok = false; continue; } even.remove(); } else { if (odd.isEmpty() || (!even.isEmpty() && even.peek() < odd.peek())) { ok = false; continue; } odd.remove(); } } String resp = "YES\n"; if (!ok) { resp = "NO\n"; } bfo.write(resp.getBytes()); } bfo.flush(); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
5f8ae529af4086e1efc2a3ab65d0998e
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
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 Pupil { static FastReader sc = new FastReader(); public static void main (String[] args) throws java.lang.Exception { // your code goes here int t=sc.nextInt(); while(t>0){ int n=sc.nextInt(); int arr[]=new int[n]; int brr[]=new int[n]; int even[]=new int[(int) 10e5]; int odd[]=new int[(int) 10e5]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); brr[i]=arr[i]; if(i%2==0) { even[arr[i]]++; } else { odd[arr[i]]++; } } Arrays.sort(brr); boolean check=true; for(int i=0;i<brr.length;i++) { if(i%2==0) { even[brr[i]]--; } else { odd[brr[i]]--; } } for(int i=0;i<n;i++) { if(even[arr[i]]==odd[arr[i]] && even[arr[i]]==0) { continue; } else { check=false; break; } } if(check) { System.out.println("YES"); } else { System.out.println("NO"); } t--; } } // FAST I/O 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 boolean two(int n)//power of two { if((n&(n-1))==0) { return true; } else{ return false; } } public static boolean isPrime(long n){ for (int i = 2; i * i <= n; ++i) { if (n % i == 0) { return false; } } return true; } public static int digit(int n) { int n1=(int)Math.floor((int)Math.log10(n)) + 1; return n1; } } //CAAL THE BELOW FUNCTION IF PARING PRIORITY IS NEEDED // // PriorityQueue<pair> pq = new PriorityQueue<>(); **********declare the syntax in the main function****** // pq.add(1,2)/////// // class pair implements Comparable<pair> { // int value, index; // pair(int v, int i) { index = i; value = v; } // @Override // public int compareTo(pair o) { return o.value - value; } // } // User defined Pair class // class Pair { // int x; // int y; // // Constructor // public Pair(int x, int y) // { // this.x = x; // this.y = y; // } // } // Arrays.sort(arr, new Comparator<Pair>() { // @Override public int compare(Pair p1, Pair p2) // { // return p1.x - p2.x; // } // }); // class Pair { // int height, id; // // public Pair(int i, int s) { // this.height = s; // this.id = i; // } // } //Arrays.sort(trips, (a, b) -> Integer.compare(a[1], b[1])); // ArrayList<ArrayList<Integer>> connections = new ArrayList<ArrayList<Integer>>(); // for(int i = 0; i<n;i++) // connections.add(new ArrayList<Integer>());
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
cf1f2205859c33a28535d65a904e2750
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
/* _oo0oo_ o8888888o 88" . "88 (| -_- |) 0\ = /0 ___/`---'\___ .' \\| |// '. / \\||| : |||// \ / _||||| -:- |||||- \ | | \\\ - /// | | | \_| ''\---/'' |_/ | \ .-\__ '-' ___/-. / ___'. .' /--.--\ `. .'___ ."" '< `.___\_<|>_/___.' >' "". | | : `- \`.;`\ _ /`;.`/ - ` : | | \ \ `_. \_ __\ /__ _/ .-` / / =====`-.____`.___ \_____/___.-`___.-'===== `=---=' */ import java.util.*; import java.math.*; import java.io.*; import java.lang.Math.*; public class KickStart2020 { 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()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long lcm(long a, long b) { return a / gcd(a, b) * b; } public static class Pair implements Comparable<Pair> { public long index; public long value; public Pair(long index, long value) { this.index = index; this.value = value; } @Override public int compareTo(Pair other) { // multiplied to -1 as the author need descending sort order if(other.index < this.index) return 1; if(other.index > this.index) return -1; if(other.value < this.value) return -1; if(other.value > this.value) return 1; else return 0; } @Override public String toString() { return this.index + " " + this.value; } } static boolean isPrime(long d) { if (d == 1) return false; for (int i = 2; i <= (long) Math.sqrt(d); i++) { if (d % i == 0) return false; } return true; } static void decimalTob(int n, int k, Stack<Integer> ss) { int x = n % k; int y = n / k; ss.push(x); if(y > 0) { decimalTob(y, k, ss); } } static long powermod(long x, long y, long mod) { long ans = 1; x = x % mod; if (x == 0) return 0; int i = 1; while (y > 0) { if ((y & 1) != 0) ans = (ans * x) % mod; y = y >> 1; x = (x * x) % mod; } return ans; } public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); outerloop: while(t-- > 0) { int n = sc.nextInt(); int arr[][] = new int[100000 + 1][2]; int brr[] = new int[n]; for(int i = 0; i < n; i++) { brr[i] = sc.nextInt(); arr[brr[i]][i % 2]++; } Arrays.sort(brr); for(int i = 0; i < n; i++) arr[brr[i]][i % 2]--; for(int i = 0; i <= 100000; i++) if(arr[i][0] != 0 || arr[i][1] != 0) { out.println("NO"); continue outerloop; } out.println("YES"); } out.close(); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
163536bef1f7a0d554f20ee8a21c0088
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.*; import java.math.*; import static java.lang.Math.min; import static java.lang.Math.max; import static java.lang.Math.abs; /* https://codeforces.com/problemset/problem/1545/A*/ public class a1545A { static FastScanner fs = new FastScanner(); static PrintWriter fop = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(); public static void main(String[] args) throws IOException { solve(); } static void solve() throws IOException { int T = fs.nextInt(); next: while (T-- > 0) { boolean flag = true; int n = fs.nextInt(); int[] res = new int[n]; int a[][] = new int[100001][2]; for (int i = 0; i < n; i++) { res[i] = fs.nextInt(); ++a[res[i]][i % 2]; } Arrays.sort(res); for (int i = 0; i < n; i++) { --a[res[i]][i % 2]; } for (int i = 0; i < n; i++) { if (a[res[i]][0] != 0 || a[res[i]][1] != 0) { fop.println("NO"); continue next; } } fop.println("YES"); } fop.flush(); fop.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(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
71f249be20c5dd1009ff33250a616a3a
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; public class AquaMoonandStrangeSort { static Scanner scn = new Scanner(System.in); public static void main(String[] args) { int t = scn.nextInt(); while(t-- >0) { int n = scn.nextInt(); String ans = "YES"; int[] arr = new int[n]; int[] ofreq = new int[(int) 10e5]; int[] efreq = new int[(int) 10e5]; for(int i= 0; i<n; i++) { arr[i] = scn.nextInt(); if(i%2 == 0) { efreq[arr[i]]+=1; } else { ofreq[arr[i]]+=1; } } Arrays.sort(arr); for(int i=0; i<n; i++) { if(i%2 == 0) { efreq[arr[i]]-=1; } else { ofreq[arr[i]]-=1; } } for(int i=0; i<n; i++) { if(ofreq[arr[i]]==0 && efreq[arr[i]]==0) { } else { ans = "NO"; } } System.out.println(ans); } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
08207c1b67488ef548a831f16c146db3
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Solution implements Runnable { static int dx[] = {-1,1,0,0}; static int dy[] = {0,0,-1,1}; public static void main(String arg[]) { new Thread(null, new Solution(), "Solution", 1<<28).start(); } private static boolean isValid(int i, int j, int n,int m) { return i>=0 && i<n && j>=0 && j<m; } @Override public void run() { FastScanner fs = new FastScanner(); int t = fs.nextInt(),flag; while(t > 0) { t--; flag=0; int n = fs.nextInt(); int[] a = new int[n]; int [][] cnt = new int[100001][2]; for(int i=0;i<n;i++) { a[i]=fs.nextInt(); cnt[a[i]][i&1]++; } Arrays.sort(a); for(int i=0;i<n;i++) { cnt[a[i]][i&1]--; } for (int i = 0; i < n; ++i) if (cnt[a[i]][0] != 0 || cnt[a[i]][1] != 0) { System.out.println("NO"); flag = 1; break; } if (flag == 0) System.out.println("YES"); for (int i = 0; i < n; ++i) cnt[a[i]][0] = cnt[a[i]][1] = 0; } } class Point { int val,index; Point(int val,int index) { this.val = val; this.index = index; } } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
bfcc578124cc68a16c83d4d71db2762e
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
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 A_AquaMoon_and_Strange_Sort { static long mod = Long.MAX_VALUE; 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){ solve(f, out); } out.close(); } public static void solve(FastReader f, PrintWriter out) { int n = f.nextInt(); int arr[] = f.nextArray(n); int arr2[] = new int[n]; for(int i = 0; i < n; i++) { arr2[i] = arr[i]; } sort(arr2); int given[][] = new int[2][100001]; int sorted[][] = new int[2][100001]; for(int i = 0; i < n; i++) { given[i%2][arr[i]]++; sorted[i%2][arr2[i]]++; } for(int i = 0; i < 100001; i++) { if(given[0][i] != sorted[0][i] || given[1][i] != sorted[1][i]) { out.println("NO"); return; } } out.println("YES"); } // Sort an array public static void sort(int arr[]) { ArrayList<Integer> al = new ArrayList<>(); for(int i: arr) { al.add(i); } Collections.sort(al); for(int i = 0; i < arr.length; i++) { arr[i] = al.get(i); } } // Find all divisors of n 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 + " "); } } } } // Check if n is prime or not 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; } // Find gcd of a and b public static long gcd(long a, long b) { long dividend = a > b ? a : b; long divisor = a < b ? a : b; while(divisor > 0) { long reminder = dividend % divisor; dividend = divisor; divisor = reminder; } return dividend; } // Find lcm of a and b public static long lcm(long a, long b) { long lcm = gcd(a, b); long hcf = (a * b) / lcm; return hcf; } // Find factorial in O(n) time public static long fact(int n) { long res = 1; for(int i = 2; i <= n; i++) { res = res * i; } return res; } // Find power in O(logb) time public static long power(long a, long b) { long res = 1; while(b > 0) { if((b&1) == 1) { res = (res * a)%mod; } a = (a * a)%mod; b >>= 1; } return res; } // Find nCr public static long nCr(int n, int r) { if(r < 0 || r > n) { return 0; } long ans = fact(n) / (fact(r) * fact(n-r)); return ans; } // Find nPr public static long nPr(int n, int r) { if(r < 0 || r > n) { return 0; } long ans = fact(n) / fact(r); return ans; } // sort all characters of a string public static String sortString(String inputString) { char tempArray[] = inputString.toCharArray(); Arrays.sort(tempArray); return new String(tempArray); } // User defined class for fast I/O 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(); } boolean hasNext() { if (st != null && st.hasMoreTokens()) { return true; } String tmp; try { br.mark(1000); tmp = br.readLine(); if (tmp == null) { return false; } br.reset(); } catch (IOException e) { return false; } return true; } 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; } int[] nextArray(int n) { int[] a = new int[n]; for(int i=0; i<n; i++) { a[i] = nextInt(); } return a; } } } /** 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 */ // (a/b)%mod == (a * moduloInverse(b)) % mod; // moduloInverse(b) = power(b, mod-2);
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
2a6805951d40276cb46cfdab3aed5202
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Sol { public static void main(String[] args) { FastReader sc = new FastReader(); int t = sc.nextInt(); int maxNum = 100001; while(t --> 0){ int n = sc.nextInt(); int[] a = new int[n]; int[] evenBeforeSort = new int[maxNum], oddBeforeSort = new int[maxNum]; int[] evenAfterSort = new int[maxNum], oddAfterSort = new int[maxNum]; for(int i=0; i<n; i++){ a[i] = sc.nextInt(); if(i % 2 == 0) evenBeforeSort[a[i]]++; else oddBeforeSort[a[i]]++; } Arrays.sort(a); for(int i=0; i<n; i++){ if(i % 2 == 0) evenAfterSort[a[i]]++; else oddAfterSort[a[i]]++; } boolean no = false; for(int i=0; i<maxNum; i++){ if(evenBeforeSort[i] != evenAfterSort[i] || oddBeforeSort[i] != oddAfterSort[i]){ System.out.println("NO"); no = true; break; } } if(!no) System.out.println("YES"); } } 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 { if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
4232b6a8dd3803a7bd15a7b1069bf3cb
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import java.io.*; import static java.lang.Math.*; public class Main extends PrintWriter { Main() { super(System.out); } static boolean cases = true; // Solution void solve(int t) { int n = sc.nextInt(); int a[] = sc.readIntArray(n); int b[] = a.clone(); sort(b); HashMap<Integer, Integer> odd = new HashMap<>(); HashMap<Integer, Integer> even = new HashMap<>(); for (int i = 0; i < n; i++) { if (i % 2 != 0) { odd.put(b[i], odd.getOrDefault(b[i], 0) + 1); } else { even.put(b[i], even.getOrDefault(b[i], 0) + 1); } } for (int i = 0; i < n; i++) { if (i % 2 != 0) { if (odd.containsKey(a[i])) { odd.put(a[i], odd.get(a[i]) - 1); if (odd.get(a[i]) == 0) odd.remove(a[i]); } else { System.out.println("NO"); return; } } else { if (even.containsKey(a[i])) { even.put(a[i], even.get(a[i]) - 1); if (even.get(a[i]) == 0) even.remove(a[i]); } else { System.out.println("NO"); return; } } } System.out.println("YES"); } 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 main(String[] args) { Main obj = new Main(); int c = 1; for (int t = (cases ? sc.nextInt() : 0); t > 1; t--, c++) obj.solve(c); obj.solve(c); obj.flush(); } 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[] readIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } char[] readCharArray(int n) { char a[] = new char[n]; String s = sc.next(); for (int i = 0; i < n; i++) { a[i] = s.charAt(i); } 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()); } } final int ima = Integer.MAX_VALUE; final int imi = Integer.MIN_VALUE; final long lma = Long.MAX_VALUE; final long lmi = Long.MIN_VALUE; static final long mod = (long) 1e9 + 7; private static final FastScanner sc = new FastScanner(); private PrintWriter out = new PrintWriter(System.out); }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
04c8756d91d309e8976d246ba0a3a51f
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; public class Hello { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = Integer.parseInt(sc.nextLine()); int count[][] = new int[100001][2]; while (T-- > 0) { int N = sc.nextInt(); int arr[] = new int[N]; for (int index = 0; index < N; index++) { arr[index] = sc.nextInt(); count[arr[index]][index % 2]++; } Arrays.sort(arr); for (int index = 0; index < N; index++) { count[arr[index]][index % 2]--; } boolean printed = false; for (int index = 0; index < N; index++) { if (count[arr[index]][0] != 0 || count[arr[index]][1] != 0) { System.out.println("NO"); printed = true; break; } } if (!printed) { System.out.println("YES"); } for (int index = 0; index < N; index++) { count[arr[index]][0] = count[arr[index]][1] = 0; } } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
e5b3856179fef564eadbf160b5aa1b0d
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import java.util.Map.Entry; import java.math.*; import java.sql.Array; public class Simple{ public static class Pair implements Comparable<Pair>{ int x; int y; public Pair(int x,int y){ this.x = x; this.y = y; } public int compareTo(Pair other){ return (int) (this.y - other.y); } public boolean equals(Pair other){ if(this.x == other.x && this.y == other.y)return true; return false; } public int hashCode(){ return 31*x + y; } // @Override // public int compareTo(Simple.Pair o) { // // TODO Auto-generated method stub // return 0; // } } static int power(int x, int y, int p) { // Initialize result int res = 1; // Update x if it is more than or // equal to p x = x % p; while (y > 0) { // If y is odd, multiply x // with result if (y % 2 == 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // Returns n^(-1) mod p static int modInverse(int n, int p) { return power(n, p - 2, p); } // Returns nCr % p using Fermat's // little theorem. static int nCrModPFermat(int n, int r, int p) { if (n<r) return 0; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r int[] fac = new int[n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } static int nCrModp(int n, int r, int p) { if (r > n - r) r = n - r; // The array C is going to store last // row of pascal triangle at the end. // And last entry of last row is nCr int C[] = new int[r + 1]; C[0] = 1; // Top row of Pascal Triangle // One by constructs remaining rows of Pascal // Triangle from top to bottom for (int i = 1; i <= n; i++) { // Fill entries of current row using previous // row values for (int j = Math.min(i, r); j > 0; j--) // nCj = (n-1)Cj + (n-1)C(j-1); C[j] = (C[j] + C[j - 1]) % p; } return C[r]; } static int gcd(int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcd(a-b, b); return gcd(a, b-a); } public static class Node{ int root; ArrayList<Node> al; Node par; public Node(int root,ArrayList<Node> al,Node par){ this.root = root; this.al = al; this.par = par; } } // public static int helper(int arr[],int n ,int i,int j,int dp[][]){ // if(i==0 || j==0)return 0; // if(dp[i][j]!=-1){ // return dp[i][j]; // } // if(helper(arr, n, i-1, j-1, dp)>=0){ // return dp[i][j]=Math.max(helper(arr, n, i-1, j-1,dp)+arr[i-1], helper(arr, n, i-1, j,dp)); // } // return dp[i][j] = helper(arr, n, i-1, j,dp); // } public static void dfs(ArrayList<ArrayList<Integer>> al,int levelcount[],int node,int count){ levelcount[count]++; for(Integer x : al.get(node)){ dfs(al, levelcount, x, count+1); } } public static long __gcd(long a, long b) { if (b == 0) return a; return __gcd(b, a % b); } public static class Arraycomparator implements Comparator<long[]>{ @Override public int compare(long[] o1, long[] o2) { // TODO Auto-generated method stub if(o1[1]>o2[1]){ return 1; } else{ return -1; } } } public static long helper(int v,long dp[]){ if(v==0)return 0; if(dp[v]!=-1)return dp[v]; return dp[v]=Math.min(1+helper((v+1)%32768, dp),1+helper((2*v)%32768, dp)); } public static void main(String args[]){ Scanner s = new Scanner(System.in); int t =s.nextInt(); for(int t1 = 1;t1<=t;t1++){ int n = s.nextInt(); int arr[]= new int[n]; int brr[] = new int[n]; for(int i=0;i<n;i++){ arr[i] = s.nextInt(); brr[i] = arr[i]; } Arrays.sort(brr); ArrayList<Integer> odd =new ArrayList<>(); ArrayList<Integer> even = new ArrayList<>(); for(int i=0;i<n;i++){ if(i%2==0){ even.add(arr[i]); } else{ odd.add(arr[i]); } } Collections.sort(even); Collections.sort(odd); int oddi = 1; int eveni =0; boolean bool = true; for(int i=0;i<n;i++){ if(i%2==1){ if(odd.get(i/2)!=brr[i]){ bool = false; } // System.out.print(odd.get(i/2)+" "); oddi++; } else{ if(even.get(i/2)!=brr[i]){ bool = false; } // System.out.println(even.get(i/2)+" "); eveni++; } if(!bool)break; } // System.out.println(odd.toString()); // System.out.println(even.toString()); if(bool){ System.out.println("YES"); } else{ System.out.println("NO"); } } } } /* 4 2 2 7 0 2 5 -2 3 5 0*x1 + 1*x2 + 2*x3 + 3*x4 */
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
dc8d3ec739f65a864574bf573b855299
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; public class class373 { public static void main(String arg[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int a[]=new int[n]; int i; int ct1[][]=new int[100001][2]; for(i=0;i<n;i++) { a[i]=sc.nextInt(); if(i%2==0) { ct1[a[i]][0]++; } else { ct1[a[i]][1]++; } } Arrays.sort(a); int c1=0,c2=0,j,flag=0; for(i=0;i<n;i++) { c1=0; c2=0; if(i%2==0) { c1++; } else { c2++; } for( j=i+1;j<n;j++) { if(a[i]!=a[j]) { break; } if(j%2==0) { c1++; } else { c2++; } } i=j-1; if(ct1[a[i]][0]!=c1) { flag=1; break; } if(ct1[a[i]][1]!=c2) { flag=1; break; } } if(flag==1) { System.out.println("NO"); } else { System.out.println("YES"); } } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
4deee4a53013189585d5b110226a3fcd
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.Math.sqrt; import static java.lang.Math.pow; import static java.lang.System.out; import static java.lang.System.err; import java.util.*; import java.io.*; import java.math.*; public class Main { static FastReader sc; static long mod=((long)1e9)+7; // static FastWriter out; public static void main(String hi[]){ initializeIO(); sc=new FastReader(); int t=sc.nextInt(); // boolean[] seave=sieveOfEratosthenes((int)(1e7)); // int[] seave2=sieveOfEratosthenesInt((int)(1e4)); // int tp=1; while(t--!=0){ int n=sc.nextInt(); int[] arr=readIntArray(n); print(solve(arr,n)); } // System.out.println(String.format("%.9f", max)); } private static String solve(int[] arr,int n){ List<Integer> odd=new ArrayList<>(); List<Integer> even=new ArrayList<>(); for(int i=0;i<n;i++){ if(i%2==0)even.add(arr[i]); else odd.add(arr[i]); } Collections.sort(odd); Collections.sort(even); Arrays.sort(arr); int o=0; int e=0; for(int i=0;i<n;i++){ if(i%2==0){ if(even.get(e++)!=arr[i])return "No"; } else{ if(odd.get(o++)!=arr[i])return "No"; } } return "yes"; } private static int sizeOfSubstring(int st,int e){ int s=e-st+1; return (s*(s+1))/2; } private static int lessThen(long[] nums,long val){ int i=0,l=0,r=nums.length-1; while(l<=r){ int mid=l+((r-l)/2); if(nums[mid]<=val){ i=mid; l=mid+1; }else{ r=mid-1; } } return i; } private static int lessThen(List<Long> nums,long val){ int i=0,l=0,r=nums.size()-1; while(l<=r){ int mid=l+((r-l)/2); if(nums.get(mid)<=val){ i=mid; l=mid+1; }else{ r=mid-1; } } return i; } private static int greaterThen(long[] nums,long val){ int i=nums.length-1,l=0,r=nums.length-1; while(l<=r){ int mid=l+((r-l)/2); if(nums[mid]>=val){ i=mid; r=mid-1; }else{ l=mid+1; } } return i; } private static long sumOfAp(long a,long n,long d){ long val=(n*( 2*a+((n-1)*d))); return val/2; } //geometrics private static double areaOftriangle(double x1,double y1,double x2,double y2,double x3,double y3){ double[] mid_point=midOfaLine(x1,y1,x2,y2); // debug(Arrays.toString(mid_point)+" "+x1+" "+y1+" "+x2+" "+y2+" "+x3+" "+" "+y3); double height=distanceBetweenPoints(mid_point[0],mid_point[1],x3,y3); double wight=distanceBetweenPoints(x1,y1,x2,y2); // debug(height+" "+wight); return (height*wight)/2; } private static double distanceBetweenPoints(double x1,double y1,double x2,double y2){ double x=x2-x1; double y=y2-y1; return sqrt(Math.pow(x,2)+Math.pow(y,2)); } private static double[] midOfaLine(double x1,double y1,double x2,double y2){ double[] mid=new double[2]; mid[0]=(x1+x2)/2; mid[1]=(y1+y2)/2; return mid; } private static long sumOfN(long n){ return (n*(n+1))/2; } private 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; } /* Function to calculate x raised to the power y in O(logn)*/ static long power(long x, long y){ long temp; if( y == 0) return 1l; temp = power(x, y / 2); if (y % 2 == 0) return (temp*temp); else return (x*temp*temp); } private static StringBuilder reverseString(String s){ StringBuilder sb=new StringBuilder(s); int l=0,r=sb.length()-1; while(l<=r){ char ch=sb.charAt(l); sb.setCharAt(l,sb.charAt(r)); sb.setCharAt(r,ch); l++; r--; } return sb; } private static void swap(long arr[],int i,int j){ long t=arr[i]; arr[i]=arr[j]; arr[j]=t; } private static void swap(int arr[],int i,int j){ int t=arr[i]; arr[i]=arr[j]; arr[j]=t; } private static void swap(double arr[],int i,int j){ double t=arr[i]; arr[i]=arr[j]; arr[j]=t; } private static void swap(float arr[],int i,int j){ float t=arr[i]; arr[i]=arr[j]; arr[j]=t; } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long lcm(long a, long b){ return (a / gcd(a, b)) * b; } private static String decimalToString(int x){ return Integer.toBinaryString(x); } private static boolean isPallindrome(String s){ int l=0,r=s.length()-1; while(l<r){ if(s.charAt(l)!=s.charAt(r))return false; l++; r--; } return true; } private static StringBuilder removeLeadingZero(StringBuilder sb){ int i=0; while(i<sb.length()&&sb.charAt(i)=='0')i++; // debug("remove "+i); if(i==sb.length())return new StringBuilder(); return new StringBuilder(sb.substring(i,sb.length())); } private static int stringToDecimal(String binaryString){ // debug(decimalToString(n<<1)); return Integer.parseInt(binaryString,2); } private static int stringToInt(String s){ return Integer.parseInt(s); } private static String toString(int val){ return String.valueOf(val); } private static void debug(int[][] arr){ for(int i=0;i<arr.length;i++){ err.println(Arrays.toString(arr[i])); } } private static void debug(long[][] arr){ for(int i=0;i<arr.length;i++){ err.println(Arrays.toString(arr[i])); } } private static void debug(List<int[]> arr){ for(int[] a:arr){ err.println(Arrays.toString(a)); } } private static void debug(float[][] arr){ for(int i=0;i<arr.length;i++){ err.println(Arrays.toString(arr[i])); } } private static void debug(double[][] arr){ for(int i=0;i<arr.length;i++){ err.println(Arrays.toString(arr[i])); } } private static void debug(boolean[][] arr){ for(int i=0;i<arr.length;i++){ err.println(Arrays.toString(arr[i])); } } private static void print(String s){ out.println(s); } private static void debug(String s){ err.println(s); } private static int charToInt(char c){ return ((((int)(c-'0'))%48)); } private static void print(double s){ out.println(s); } private static void print(float s){ out.println(s); } private static void print(long s){ out.println(s); } private static void print(int s){ out.println(s); } private static void debug(double s){ err.println(s); } private static void debug(float s){ err.println(s); } private static void debug(long s){ err.println(s); } private static void debug(int s){ err.println(s); } private static boolean isPrime(int n){ // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } //read graph private static List<List<Integer>> readUndirectedGraph(int n){ List<List<Integer>> graph=new ArrayList<>(); for(int i=0;i<=n;i++){ graph.add(new ArrayList<>()); } for(int i=0;i<n;i++){ int x=sc.nextInt(); int y=sc.nextInt(); graph.get(x).add(y); graph.get(y).add(x); } return graph; } private static List<List<Integer>> readUndirectedGraph(int[][] intervals,int n){ List<List<Integer>> graph=new ArrayList<>(); for(int i=0;i<=n;i++){ graph.add(new ArrayList<>()); } for(int i=0;i<intervals.length;i++){ int x=intervals[i][0]; int y=intervals[i][1]; graph.get(x).add(y); graph.get(y).add(x); } return graph; } private static List<List<Integer>> readDirectedGraph(int[][] intervals,int n){ List<List<Integer>> graph=new ArrayList<>(); for(int i=0;i<=n;i++){ graph.add(new ArrayList<>()); } for(int i=0;i<intervals.length;i++){ int x=intervals[i][0]; int y=intervals[i][1]; graph.get(x).add(y); // graph.get(y).add(x); } return graph; } private static List<List<Integer>> readDirectedGraph(int n){ List<List<Integer>> graph=new ArrayList<>(); for(int i=0;i<=n;i++){ graph.add(new ArrayList<>()); } for(int i=0;i<n;i++){ int x=sc.nextInt(); int y=sc.nextInt(); graph.get(x).add(y); // graph.get(y).add(x); } return graph; } static String[] readStringArray(int n){ String[] arr=new String[n]; for(int i=0;i<n;i++){ arr[i]=sc.next(); } return arr; } private static Map<Character,Integer> freq(String s){ Map<Character,Integer> map=new HashMap<>(); for(char c:s.toCharArray()){ map.put(c,map.getOrDefault(c,0)+1); } return map; } static boolean[] sieveOfEratosthenes(long n){ boolean prime[] = new boolean[(int)n + 1]; for (int i = 2; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; 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 * p; i <= n; i += p) prime[i] = false; } } return prime; } static int[] sieveOfEratosthenesInt(long n){ boolean prime[] = new boolean[(int)n + 1]; Set<Integer> li=new HashSet<>(); for (int i = 1; i <= n; i++){ prime[i] = true; li.add(i); } for (int p = 2; p * p <= n; p++) { if (prime[p] == true){ for (int i = p * p; i <= n; i += p){ li.remove(i); prime[i] = false; } } } int[] arr=new int[li.size()+1]; int i=0; for(int x:li){ arr[i++]=x; } return arr; } public static long Kadens(List<Long> prices) { long sofar=0; long max_v=0; for(int i=0;i<prices.size();i++){ sofar+=prices.get(i); if (sofar<0) { sofar=0; } max_v=Math.max(max_v,sofar); } return max_v; } static boolean isMemberAC(int a, int d, int x){ // If difference is 0, then x must // be same as a. if (d == 0) return (x == a); // Else difference between x and a // must be divisible by d. return ((x - a) % d == 0 && (x - a) / d >= 0); } private static void sort(int[] arr){ int n=arr.length; List<Integer> li=new ArrayList<>(); for(int x:arr){ li.add(x); } Collections.sort(li); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static void sortReverse(int[] arr){ int n=arr.length; List<Integer> li=new ArrayList<>(); for(int x:arr){ li.add(x); } Collections.sort(li,Collections.reverseOrder()); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static void sort(double[] arr){ int n=arr.length; List<Double> li=new ArrayList<>(); for(double x:arr){ li.add(x); } Collections.sort(li); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static void sortReverse(double[] arr){ int n=arr.length; List<Double> li=new ArrayList<>(); for(double x:arr){ li.add(x); } Collections.sort(li,Collections.reverseOrder()); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static void sortReverse(long[] arr){ int n=arr.length; List<Long> li=new ArrayList<>(); for(long x:arr){ li.add(x); } Collections.sort(li,Collections.reverseOrder()); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static void sort(long[] arr){ int n=arr.length; List<Long> li=new ArrayList<>(); for(long x:arr){ li.add(x); } Collections.sort(li); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static long sum(int[] arr){ long sum=0; for(int x:arr){ sum+=x; } return sum; } private static long evenSumFibo(long n){ long l1=0,l2=2; long sum=0; while (l2<n) { long l3=(4*l2)+l1; sum+=l2; if(l3>n)break; l1=l2; l2=l3; } return sum; } private static void initializeIO(){ try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); System.setErr(new PrintStream(new FileOutputStream("error.txt"))); } catch (Exception e) { // System.err.println(e.getMessage()); } } private static int maxOfArray(int[] arr){ int max=Integer.MIN_VALUE; for(int x:arr){ max=Math.max(max,x); } return max; } private static long maxOfArray(long[] arr){ long max=Long.MIN_VALUE; for(long x:arr){ max=Math.max(max,x); } return max; } private static int[][] readIntIntervals(int n,int m){ int[][] arr=new int[n][m]; for(int j=0;j<n;j++){ for(int i=0;i<m;i++){ arr[j][i]=sc.nextInt(); } } return arr; } private static long gcd(long a,long b){ if(b==0)return a; return gcd(b,a%b); } private static int gcd(int a,int b){ if(b==0)return a; return gcd(b,a%b); } private static int[] readIntArray(int n){ int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } return arr; } private static double[] readDoubleArray(int n){ double[] arr=new double[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextDouble(); } return arr; } private static long[] readLongArray(int n){ long[] arr=new long[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextLong(); } return arr; } private static void print(int[] arr){ out.println(Arrays.toString(arr)); } private static void print(long[] arr){ out.println(Arrays.toString(arr)); } private static void print(String[] arr){ out.println(Arrays.toString(arr)); } private static void print(double[] arr){ out.println(Arrays.toString(arr)); } private static void debug(String[] arr){ err.println(Arrays.toString(arr)); } private static void debug(int[] arr){ err.println(Arrays.toString(arr)); } private static void debug(long[] arr){ err.println(Arrays.toString(arr)); } 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; } } 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(); } } static class Dsu { int[] parent, size; Dsu(int n) { parent = new int[n + 1]; size = new int[n + 1]; for (int i = 0; i <= n; i++) { parent[i] = i; size[i] = 1; } } private int findParent(int u) { if (parent[u] == u) return u; return parent[u] = findParent(parent[u]); } private boolean union(int u, int v) { // System.out.println("uf "+u+" "+v); int pu = findParent(u); // System.out.println("uf2 "+pu+" "+v); int pv = findParent(v); // System.out.println("uf3 " + u + " " + pv); if (pu == pv) return false; if (size[pu] <= size[pv]) { parent[pu] = pv; size[pv] += size[pu]; } else { parent[pv] = pu; size[pu] += size[pv]; } return true; } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
dd204b41e4bb52c99cbc0070f86b360d
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int cishu = sc.nextInt(); while (cishu-- != 0) { int n=sc.nextInt(); int[] arr=new int[n]; int[][] count=new int[100001][2]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); count[arr[i]][i%2]++; } boolean re=true; Arrays.sort(arr); for(int i=0;i<n;i++){ count[arr[i]][i%2]--; if(count[arr[i]][i%2]<0){ re=false; } } if(re){ System.out.println("YES"); }else{ System.out.println("NO"); } } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
a19f46ce9100630c5fb5cc23985c4b66
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
// package com.company; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); StringBuilder sb = new StringBuilder(); while(t-- > 0) { int n = in.nextInt(); int[] a = new int[n]; int[] acopy = new int[n]; Map<Integer, int[]> m = new HashMap<>(); for(int i = 0; i < n; ++i) { a[i] = in.nextInt(); m.putIfAbsent(a[i], new int[] {0, 0}); int[] tmp = m.get(a[i]); if(i % 2 == 0) tmp[0] += 1; else tmp[1] += 1; acopy[i] = a[i]; } Arrays.sort(acopy); boolean ok = true; for(int i = n - 1; i >= 0; --i) { int[] tmp = m.get(acopy[i]); if(i % 2 == 0 && tmp[0] > 0) { tmp[0] -= 1; } else if(i % 2 == 0) { ok = false; break; } if(i % 2 == 1 && tmp[1] > 0) { tmp[1] -= 1; } else if (i % 2 == 1) { ok = false; break; } m.put(acopy[i], tmp); } if(ok) sb.append("YES\n"); else sb.append("NO\n"); } System.out.print(sb.toString()); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
abb04514571b7cfa34d2749b91467eaa
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { static { try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) {} } void solve() { int n = in.nextInt(); int[] a = array(n); int max = (int)1e5 + 1; int[] evens = new int[max]; int[] odds = new int[max]; for (int i = 0 ; i < n; i++) { if (i % 2 == 0)evens[a[i]]++; else odds[a[i]]++; } Arrays.sort(a); for (int i = 0 ; i < n ; i++) { if (i % 2 == 0)evens[a[i]]--; else odds[a[i]]--; } for (int i = 0; i < max; i++) { if (evens[i] != 0 || odds[i] != 0) { out.append("NO\n"); return; } } out.append("YES\n"); } public static void main (String[] args) { // Its Not Over Untill I Win - Syed Mizbahuddin Main sol = new Main(); int t = 1; t = in.nextInt(); while (t-- != 0) { sol.solve(); } System.out.print(out); } <T> void println(T[] s) { System.out.println(Arrays.toString(s)); } <T> void println(T s) { System.out.println(s); } void println(int s) { System.out.println(s); } void println(int[] a) { println(Arrays.toString(a)); } int[] array(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } return a; } int[] array1(int n) { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) { a[i] = in.nextInt(); } return a; } int max(int[] a) { int max = a[0]; for (int i = 0; i < a.length; i++)max = Math.max(max, a[i]); return max; } int count(int[] a, int x) { int count = 0; for (int i = 0; i < a.length; i++)if (x == a[i])count++; return count; } static FastReader in; static StringBuffer out; final int MAX; final int MIN; int mod ; Main() { in = new FastReader(); out = new StringBuffer(); MAX = Integer.MAX_VALUE; MIN = Integer.MIN_VALUE; mod = (int)1e9 + 7; } 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 first , second; Pair(int first, int second) { this.first = first; this.second = second; } public int compareTo(Pair b) { return this.first - b.first; } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
ad2c3518299385946d4a97b48ccc0d26
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; public class Solution { private static Scanner in = new Scanner(System.in); public static void main(String[] args) { int t = in.nextInt(); while(t-- > 0) solve(); } private static void solve() { int n = in.nextInt(); int a[][] = new int[n][2]; for(int i=0; i<n; i++) { a[i][0] = in.nextInt(); a[i][1] = i; } Arrays.sort(a, new Comparator<int[]>() { public int compare(int[] o1, int[] o2) { return o1[0]-o2[0]; } }); for(int i=0;i<n;i++) { int oo=0,oe=0,no=0,ne=0; int temp = a[i][0]; while(i<n && a[i][0]==temp) { if(i%2==0) oe++; else oo++; if(a[i][1]%2==0) ne++; else no++; i++; } i--; if(oo!=no || oe!=ne) { System.out.println("NO"); return; } } System.out.println("YES"); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
f3b9cb38f04075386f4174acc4e414e6
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.util.*; import javax.sound.midi.SysexMessage; import java.math.*; public class Main { public String ns() throws IOException{return(read.nextLine());} public String wrd() throws IOException{return(read.next());} public int ni() throws IOException{return(read.nextInt());} public double nd() throws IOException{return(read.nextDouble());} public long nl() throws IOException{return(read.nextLong());} public int[] ai(int n) throws IOException{int arr[] = new int[n];for(int i = 0; i<n; i++)arr[i] = read.nextInt();return(arr);} public double[] ad(int n) throws IOException {double arr[] = new double[n];for(int i = 0; i<n; i++)arr[i] = read.nextDouble();return(arr);} public long[] al(int n) throws IOException {long arr[] = new long[n];for(int i = 0; i<n; i++)arr[i] = read.nextLong();return(arr);} class FastReader { BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); if (System.getProperty("ONLINE_JUDGE") == null) { try { InputStream inputStream = new FileInputStream("input.txt"); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); br = new BufferedReader(inputStreamReader); } catch (Exception e) { e.printStackTrace(); } } } String next(){while (st == null || !st.hasMoreElements()){try{st = new StringTokenizer(br.readLine());}catch (IOException e){e.printStackTrace();}}return st.nextToken();} int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble(){return Double.parseDouble(next());} String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;}} public static void main(String[] args) throws IOException{Main obj = new Main();obj.solve();} public int getParent(int parent[], int src) { if(parent[src] != src) { int ultimateparent = getParent(parent, parent[src]); parent[src] = ultimateparent; return ultimateparent; } return src; } public boolean union(int i, int j, int parent[] , int rank[]) { int root1 = getParent(parent, i); int root2 = getParent(parent, j); if(root1 == root2) return false; if(rank[root1] == rank[root2]) { parent[root1] = root2; rank[root2]++ ; } else if(rank[root1] > rank[root2]) { parent[root2] = root1; } else // rank[root2] > rank[root1] { parent[root1] = root2; } return true; } public int log(long x) { return (int) (Math.log(x) / Math.log(2) + 1e-10); } public int fact(int n) { if (n <= 1) return 1; return n * fact(n - 1); } public int nPr(int n, int r) { return fact(n) / fact(n - r); } long gcd (long a, long b) { //System.out.print("GCD OF "+a+" , "+b+" = "); long r, i; while(b!=0) { r = a % b; a = b; b = r; } //System.out.println(" "+a); return a; } public int getParent(int root, int parent[]) { if(parent[root] == root) { return(root); } parent[root] = getParent(parent[root], parent); return(parent[root]); } void sieveOfEratosthenes(boolean prime[], 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; } } } long power(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if ((y & 1) == 1) res = (res * x) % p; y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } boolean miillerTest(long d, long n) { long a = 2 + (int)(Math.random() % (n - 4)); long x = power(a, d, n); if (x == 1 || x == n - 1) return true; while (d != n - 1) { x = (x * x) % n; d *= 2; if (x == 1) return false; if (x == n - 1) return true;} return false; } boolean isPrime(long n, int k) { if (n <= 1 || n == 4) return false; if (n <= 3) return true; long d = n - 1; while (d % 2 == 0) d /= 2; for (long i = 0; i < k; i++) if (!miillerTest(d, n)) return false; return true;} //-------------------------------------------------------------------------------------------------------------- long MODULO = 1000000007; FastReader read; StringBuilder sb; class Elements { public int freq; public ArrayList<Integer> index; Elements() { freq = 0; index = new ArrayList<>(); } } public void solve() throws IOException { sb = new StringBuilder(); read = new FastReader(); int t = ni(); for(int tt = 0; tt < t; tt++) { int n = ni(); PriorityQueue<Integer> pq = new PriorityQueue<>(); int ele; int freq[][] = new int[100*1000 + 1][2]; for(int i = 0; i<n;i++) { ele = ni(); pq.add(ele); freq[ele][i%2] ++; } int idx = 0; while(! pq.isEmpty()) { ele = pq.poll(); freq[ele][idx%2]--; idx++; } boolean isPossible = true; for(int i = 0; i<freq.length; i++) { if(freq[i][0] != 0 || freq[i][1] != 0) { isPossible = false; break; } } sb.append(isPossible ? "YES" : "NO").append("\n"); } System.out.println(sb); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
fdff3d4d3c5a81947949ef17a69ee9dd
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.Scanner; import java.util.stream.IntStream; 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) ? "YES" : "NO"); } sc.close(); } static boolean solve(int[] a) { int[] evenSorted = IntStream.range(0, a.length) .filter(i -> i % 2 == 0) .map(i -> a[i]) .boxed() .sorted() .mapToInt(x -> x) .toArray(); int[] oddSorted = IntStream.range(0, a.length) .filter(i -> i % 2 != 0) .map(i -> a[i]) .boxed() .sorted() .mapToInt(x -> x) .toArray(); int[] combined = IntStream.range(0, a.length) .map(i -> (i % 2 == 0) ? evenSorted[i / 2] : oddSorted[i / 2]) .toArray(); return IntStream.range(0, combined.length - 1).allMatch(i -> combined[i] <= combined[i + 1]); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
cad91c4c6522f32905a34e51025d649e
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import java.math.*; import java.io.*; import java.util.stream.Collectors; import java.text.DecimalFormat; public class A{ static FastScanner scan=new FastScanner(); public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out)); public static void main(String[] args) throws Exception { int tt=1; tt=scan.nextInt(); // scan=new FastScanner("clumsy.in"); //out=new PrintWriter("clumsy.out"); outer:while(tt-->0) { int n=scan.nextInt(); int arr[]=new int[n]; ArrayList<Integer>odd=new ArrayList<Integer>(); ArrayList<Integer>even=new ArrayList<Integer>(); for(int i=0;i<n;i++) { int x=scan.nextInt(); if(i%2==0) even.add(x); else odd.add(x); } Collections.sort(even); Collections.sort(odd); ArrayList<Integer>MY=new ArrayList<Integer>(); int i1=0,i2=0; for(int i=0;i<n;i++) { if(i%2==0) MY.add(even.get(i1++)); else MY.add(odd.get(i2++)); } for(int i=1;i<n;i++) { if(MY.get(i)<MY.get(i-1)) { out.println("NO"); continue outer; } } out.println("YES"); } out.close(); } static class FastScanner { private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } static class Pair implements Comparable<Pair>{ public long x, y,z; public Pair(long x1, long y1,long z1) { x=x1; y=y1; z=z1; } public Pair(long x1, long y1) { x=x1; y=y1; } @Override public int hashCode() { return (int)(x + 31 * y); } public String toString() { return x + " " + y+" "+z; } @Override public boolean equals(Object o){ if (o == this) return true; if (o.getClass() != getClass()) return false; Pair t = (Pair)o; return t.x == x && t.y == y&&t.z==z; } public int compareTo(Pair o) { //if(o.x==x) // return (int)(y-o.y); return Long.compare(o.x,x); } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
fedee5a54b05535e93b83a49ab50bad9
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Anubhav */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); AAquaMoonAndStrangeSort solver = new AAquaMoonAndStrangeSort(); solver.solve(1, in, out); out.close(); } static class AAquaMoonAndStrangeSort { public void solve(int testNumber, InputReader in, OutputWriter out) { int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int ar[] = in.nextIntArray(n); int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = ar[i]; Arrays.sort(arr); int e[] = new int[100001]; int o[] = new int[100001]; for (int i = 0; i < n; i++) { int v = ar[i]; if (i % 2 == 0) e[v]++; else o[v]++; } int c = 0; for (int i = 0; i < n; i++) { int v = ar[i]; int v2 = arr[i]; if (i % 2 == 0) { int cou = e[v2] - 1; if (cou < 0) c++; else e[v2]--; } else { int cou = o[v2] - 1; if (cou < 0) c++; else o[v2]--; } } if (c == 0) out.println("YES"); else out.println("NO"); } } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
1a3724239d1692e811eb5bfbe825b128
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
/* Author : Kartikey Rana from MSIT New Delhi */ import java.util.*; import javax.sql.rowset.serial.SerialArray; import javax.swing.text.html.HTMLDocument.HTMLReader.PreAction; import java.io.*; import java.math.*; import java.sql.Array;; public class Main { static class FR { BufferedReader br; StringTokenizer st; public FR() { 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; } // NEXT INT ARRAY int[] NIA(int n) { int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } // NEXT DOUBLE ARRAY double[] NDA(int n) { double arr[] = new double[n]; for (int i = 0; i < n; i++) arr[i] = nextDouble(); return arr; } // NEXT LONG ARRAY long[] NLA(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } // NEXT STRING ARRAY String[] NSA(int n) { String[] arr = new String[n]; for (int i = 0; i < n; i++) arr[i] = next(); return arr; } // NEXT CHARACTER ARRAY char[] NCA(int n) { char[] arr = new char[n]; String s = sc.nextLine(); for (int i = 0; i < n; i++) arr[i] = s.charAt(i); return arr; } } //************************* FR CLASS ENDS ********************************** static long mod = (long) (1e9 + 7); public static int[] sieve(int n) { int[] primes = new int[n + 1]; for (int i = 0; i <= n; i++) { primes[i] = i; } for (int i = 2; i < n; i++) { if (primes[i] < 0) continue; if ((long) i * (long) i > n) break; for (int j = i * i; j < n; j++) { if (primes[j] > 0 && primes[j] % primes[i] == 0) primes[j] = -primes[j]; } } return primes; } static int lcm(int a, int b) { return (int) ((a / gcd(a, b)) * b); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long[][] ncr(int n, int k) { long C[][] = new long[n + 1][k + 1]; int i, j; // Calculate value of Binomial // Coefficient in bottom up manner for (i = 0; i <= n; i++) { for (j = 0; j <= Math.min(i, k); j++) { // Base Cases if (j == 0 || j == i) C[i][j] = 1; // Calculate value using // previously stored values else C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod; } } return C; } static long modInverse(long a, long m) { long g = gcd(a, m); return power(a, m - 2, m); } static long power(long x, long y, long m) { if (y == 0) return 1; long p = power(x, y / 2, m) % m; p = (int) ((p * (long) p) % m); if (y % 2 == 0) return p; else return (int) ((x * (long) p) % m); } static int XOR(int n) { if (n % 4 == 0) return n; if (n % 4 == 1) return 1; if (n % 4 == 2) return n + 1; return 0; } // ----------------------------------DSU-------------------------------- static int parent[]; static int rank[]; public static int find(int x) { if(x == parent[x]) { return x; } int temp = find(parent[x]); parent[x] = temp; return temp; } public static void union(int x, int y) { int lx = find(x); int ly = find(y); if(parent[x] == parent[y]) return; if(rank[lx] < rank[ly]) { parent[lx] = ly; } else if(rank[lx] >rank[ly]) { parent[ly] = lx; } else { parent[lx] = ly; rank[ly]++; } find(x); find(y); } /* ***************************************************************************************************************************************************/ static class Pair implements Comparable<Pair> { int x; int y; public Pair(int x, int y) { super(); this.x = x; this.y = y; } @Override public int compareTo(Pair o) { return this.x - o.x; } } static FR sc = new FR(); static StringBuilder sb = new StringBuilder(); public static void main(String args[]) throws IOException { int tc = sc.nextInt(); // int tc = 1; while (tc-- > 0) { TEST_CASE(); } sb.setLength(sb.length() - 1); System.out.println(sb); } // BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static void TEST_CASE() throws IOException { int n = sc.nextInt(); int[] arr = sc.NIA(n); HashMap<Integer, int[]> b = new HashMap<>(); for(int i = 0; i < n; i++) { if(!b.containsKey(arr[i])) { b.put(arr[i], new int[2]); } if(i%2 == 0) { int[] temp = b.get(arr[i]); b.put(arr[i], new int[]{temp[0], temp[1] + 1}); } else { int[] temp = b.get(arr[i]); b.put(arr[i], new int[]{temp[0] + 1, temp[1]}); } } Arrays.sort(arr); HashMap<Integer, int[]> a = new HashMap<>(); for(int i = 0; i < n; i++) { if(!a.containsKey(arr[i])) { a.put(arr[i], new int[2]); } if(i%2 == 0) { int[] temp = a.get(arr[i]); a.put(arr[i], new int[]{temp[0], temp[1] + 1}); } else { int[] temp = a.get(arr[i]); a.put(arr[i], new int[]{temp[0] + 1, temp[1]}); } } for(int i : a.keySet()) { int[] k1 = a.get(i); int[] k2 = b.get(i); if(k1[0] != k2[0] || k1[1] != k2[1]) { sb.append("NO\n"); return; } } sb.append("YES\n"); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
663937cced73f4f6933af91ebd44bf81
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main (String[] args) throws java.lang.Exception { try { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int ar[]=new int[n]; int dp[][]=new int[2][100005+1]; for(int i=0;i<n;i++){ ar[i]=sc.nextInt(); if((i&1)==1) dp[0][ar[i]]++; else dp[1][ar[i]]++; } Arrays.sort(ar); for(int i=0;i<n;i++){ if((i&1)==1) dp[0][ar[i]]--; else dp[1][ar[i]]--; } boolean flag=true; for(int i=0;i<2;i++){ for(int j=0;j<dp[i].length;j++){ if(dp[i][j]!=0) { flag=false; break;} } } if(flag==false) System.out.println("NO"); else System.out.println("YES"); } } catch(Exception e) { return; } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
97397d3bf4730751a1d7acf19e4b14a7
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.util.*; public class Main { static FastScanner sc = new FastScanner(); static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); 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[][]cnt = new int[100005][2]; for(int i = 0;i < n;i++){ cnt[arr[i]][i % 2]++; } Arrays.sort(arr); for(int i =0;i < n;i++){ cnt[arr[i]][i % 2]--; if(cnt[arr[i]][i % 2] < 0){ System.out.println("No"); return; } } System.out.println("YES"); } public static void main(String[] args) { int n = sc.nextInt(); for(int i = 0;i < n;i++){ solve(); } } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
305e81f52f23533e4d36feb4e5f19dd6
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
/** * @Created_by : Lucent868 * @Date : 26-08-2021 * @Time : 13:03 */ import java.util.*; import java.io.*; import java.math.*; public class A { static InputReader in; static PrintWriter out; public static void main(String args[]) { new Thread(null, new Runnable() { public void run() { try { solve(); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } }, "1", 1 << 26).start(); } public static void solve() { in = new InputReader(System.in); out = new PrintWriter(System.out); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int a[] = in.nextIntArray(n); int f[][] = new int[100001][2]; int c[] = new int[n]; for (int i = 0; i < n; i++) { if(i%2 == 0) f[a[i]][0]++; else f[a[i]][1]++; } // for (int i = 0; i < 1000; i++) { // out.println(f[i][0]+" "+f[i][1]); // } sort(a); int ttt = 0; for (int i = 0; i < n; i++) { if(i%2 == 0) { if(f[a[i]][0]>0) { ttt++; f[a[i]][0]--; } else { break; } } else { if(f[a[i]][1]>0) { ttt++; f[a[i]][1]--; } else { break; } } } if(ttt == n ) { out.println("YES"); } else out.println("NO"); } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static int[] sort(int[] arr) { List<Integer> temp = new ArrayList(); for (int i : arr) temp.add(i); Collections.sort(temp); int start = 0; for (int i : temp) arr[start++] = i; return arr; } public static long[] sort(long[] arr) { List<Long> temp = new ArrayList(); for (long i : arr) temp.add(i); Collections.sort(temp); int start = 0; for (long i : temp) arr[start++] = i; return arr; } public static int bs(int[] a, int fromIndex, int toIndex, double key) { int low = fromIndex; int high = toIndex - 1; while (low <= high) { int mid = (low + high) >>> 1; long midVal = a[mid]; if (midVal < key) low = mid + 1; else if (midVal > key) high = mid - 1; else return mid; // key found } return -(low + 1); // key not found. } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
b458a1ee959100eaace1255aa7254d28
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
/* Date: 12-08-2021 Time: 10:12 Author: Kartik Papney Linkedin: https://www.linkedin.com/in/kartik-papney-4951161a6/ Leetcode: https://leetcode.com/kartikpapney/ Codechef: https://www.codechef.com/users/kartikpapney */ import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void solve() { int n = sc.nextInt(); int[] arr = sc.readArray(n); int[] brr = new int[n]; for(int i=0; i<n; i++) brr[i] = arr[i]; Arrays.sort(arr); HashMap<Integer, int[]> map = new HashMap<>(); HashMap<Integer, int[]> nap = new HashMap<>(); for(int i=0 ; i<arr.length; i++) { map.putIfAbsent(arr[i], new int[2]); nap.putIfAbsent(brr[i], new int[2]); map.get(arr[i])[i%2]++; nap.get(brr[i])[i%2]++; } for(Map.Entry<Integer, int[]> mp : map.entrySet()) { if(mp.getValue()[0] != nap.get(mp.getKey())[0] || mp.getValue()[1] != nap.get(mp.getKey())[1]) { p.writeln("NO"); return; } } p.writeln("YES"); } public static void main(String[] args) { int t = 1; t = sc.nextInt(); while (t-- != 0) { solve(); } p.print(); } static final Integer MOD = (int) 1e9 + 7; static final FastReader sc = new FastReader(); static final Print p = new Print(); static class Functions { 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 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); } static int max(int[] a) { int max = Integer.MIN_VALUE; for (int val : a) max = Math.max(val, max); return max; } static int min(int[] a) { int min = Integer.MAX_VALUE; for (int val : a) min = Math.min(val, min); return min; } static long min(long[] a) { long min = Long.MAX_VALUE; for (long val : a) min = Math.min(val, min); return min; } static long max(long[] a) { long max = Long.MIN_VALUE; for (long val : a) max = Math.max(val, max); return max; } static long sum(long[] a) { long sum = 0; for (long val : a) sum += val; return sum; } static int sum(int[] a) { int sum = 0; for (int val : a) sum += val; return sum; } public static long mod_add(long a, long b) { return (a % MOD + b % MOD + MOD) % MOD; } public static long pow(long a, long b) { long res = 1; while (b > 0) { if ((b & 1) != 0) res = mod_mul(res, a); a = mod_mul(a, a); b >>= 1; } return res; } public static long mod_mul(long a, long b) { long res = 0; a %= MOD; while (b > 0) { if ((b & 1) > 0) { res = mod_add(res, a); } a = (2 * a) % MOD; b >>= 1; } return res; } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static long factorial(long n) { long res = 1; for (int i = 1; i <= n; i++) { res = (i % MOD * res % MOD) % MOD; } return res; } public static int count(int[] arr, int x) { int count = 0; for (int val : arr) if (val == x) count++; return count; } public static ArrayList<Integer> generatePrimes(int n) { boolean[] primes = new boolean[n]; for (int i = 2; i < primes.length; i++) primes[i] = true; for (int i = 2; i < primes.length; i++) { if (primes[i]) { for (int j = i * i; j < primes.length; j += i) { primes[j] = false; } } } ArrayList<Integer> arr = new ArrayList<>(); for (int i = 0; i < primes.length; i++) { if (primes[i]) arr.add(i); } return arr; } } static class Print { StringBuffer strb = new StringBuffer(); public void write(Object str) { strb.append(str); } public void writes(Object str) { strb.append(str).append(" "); } public void writeln(Object str) { strb.append(str).append("\n"); } public void writeln() { strb.append('\n'); } public void writes(int[] arr) { for (int val : arr) { write(val); write(' '); } } public void writes(long[] arr) { for (long val : arr) { write(val); write(' '); } } public void writeln(int[] arr) { for (int val : arr) { writeln(val); } } public void print() { System.out.print(strb); } public void println() { System.out.println(strb); } } 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()); } 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; } double[] readArrayDouble(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
0ccf9c43e7e3ba1fe0dee10b94caf75d
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
//package Practise; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.HashMap; import java.util.Random; import java.util.StringTokenizer; public class StrangeSort { public static void main(String args[]) { FastScanner fs=new FastScanner(); int t=fs.nextInt(); for(int t1=0;t1<t;t1++) { int n=fs.nextInt(); int []arr=new int[n]; HashMap<Integer,Occur> map=new HashMap<>(); for(int i=0;i<n;i++) { arr[i]=fs.nextInt(); boolean e=(i+1)%2==0?true:false; int even=0,odd=0; if(e)even++; else odd++; if(!map.containsKey(arr[i])) { map.put(arr[i],new Occur(even,odd)); }else { map.get(arr[i]).even+=even; map.get(arr[i]).odd+=odd; } } int []arr2=arr.clone(); Arrays.sort(arr2); HashMap<Integer,Occur> map2=new HashMap<>(); for(int i=0;i<n;i++) { boolean e=(i+1)%2==0?true:false; int even=0,odd=0; if(e)even++; else odd++; if(!map2.containsKey(arr2[i])) { map2.put(arr2[i],new Occur(even,odd)); }else { map2.get(arr2[i]).even+=even; map2.get(arr2[i]).odd+=odd; } } boolean flag=true; for(int key:map.keySet()) { Occur val1=map.get(key); Occur val2=map2.get(key); if(val1.even!=val2.even||val1.odd!=val2.odd) { flag=false; break; } } //Print(map); //System.out.println(); //Print(map2); if(flag) System.out.println("YES"); else System.out.println("NO"); } } public static void Print(HashMap<Integer,Occur> map) { for(int key:map.keySet()) { System.out.println(key+" "+map.get(key).odd+" "+map.get(key).even); } } static class Occur{ int val; int even; int odd; Occur(int even,int odd){ this.even=even; this.odd=odd; } } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
39947066e709d45faf7361ebbe328606
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; import java.awt.*; /*AUTHOR - SHIVAM GUPTA */ // U KNOW THAT IF THIS DAY WILL BE URS THEN NO ONE CAN DEFEAT U HERE ,JUst keep faith in ur strengths .................................................. // ASCII = 48 + i ;// 2^28 = 268,435,456 > 2* 10^8 // log 10 base 2 = 3.3219 // odd:: (x^2+1)/2 , (x^2-1)/2 ; x>=3// even:: (x^2/4)+1 ,(x^2/4)-1 x >=4 // FOR ANY ODD NO N : N,N-1,N-2,ALL ARE PAIRWISE COPRIME,THEIR COMBINED LCM IS PRODUCT OF ALL THESE NOS // two consecutive odds are always coprime to each other, two consecutive even have always gcd = 2 ; // Rectangle r = new Rectangle(int x , int y,int widht,int height) //Creates a rect. with bottom left cordinates as (x, y) and top right as ((x+width),(y+height)) //BY DEFAULT Priority Queue is MIN in nature in java //to use as max , just push with negative sign and change sign after removal // We can make a sieve of max size 1e7 .(no time or space issue) // In 1e7 starting nos we have about 66*1e4 prime nos // In 1e6 starting nos we have about 78,498 prime nos public class Main //public class Solution { static PrintWriter out; static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); out=new PrintWriter(System.out); } String next(){ while(st==null || !st.hasMoreElements()){ try{ st= new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str = ""; try{ str=br.readLine(); } catch(IOException e){ e.printStackTrace(); } return str; } } /////////////////////////////////////////////////////////////////////////////////////////////////////// public static int sumOfDigits(long n) { if( n< 0)return -1 ; int sum = 0; while( n > 0){ sum = sum + (int)( n %10) ; n /= 10 ; } return sum ; } ////////////////////////////////////////////////////////////////////////////////////////////////////// public static void swapArray(int[] arr , int start , int end) { while(start<end){ int temp = arr[start] ;arr[start] = arr[end]; arr[end] = temp; start++ ;end-- ;} } ////////////////////////////////////////////////////////////////////////////////// static long factorial(long a) { if(a== 0L || a==1L)return 1L ; return a*factorial(a-1L) ; } /////////////////////////////////////////////////////////////////////////////// public static int[][] rotate(int[][] input){ int n =input.length;int m = input[0].length ; int[][] output = new int [m][n]; for (int i=0; i<n; i++) for (int j=0;j<m; j++) output [j][n-1-i] = input[i][j]; return output; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////// public static String reverse(String input) { StringBuilder str = new StringBuilder("") ; for(int i =input.length()-1 ; i >= 0 ; i-- ) { str.append(input.charAt(i)); } return str.toString() ; } /////////////////////////////////////////////////////////////////////////////////////////// public static int xorOfFirstN(int n) { if( n % 4 ==0)return n ; else if( n % 4 == 1)return 1 ; else if( n % 4 == 2)return n+1 ; else return 0 ; } ////////////////////////////////////////////////////////////////////////////////////////////// public static int gcd(int a, int b ) { if(b==0)return a ;else return gcd(b,a%b) ; } public static long gcd(long a, long b ) { if(b==0)return a ;else return gcd(b,a%b) ; } /////////////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a, int b ,int c ) { int temp = lcm(a,b) ;int ans = lcm(temp ,c) ;return ans ; } //////////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a , int b ) { int gc = gcd(a,b);return (a/gc)*b ; } public static long lcm(long a , long b ) { long gc = gcd(a,b);return (a/gc)*b; } /////////////////////////////////////////////////////////////////////////////////////////// static boolean isPrime(long n) { if(n==1)return false ; for(long i = 2L; i*i <= n ;i++){ if(n% i ==0){return false; } } return true ; } static boolean isPrime(int n) { if(n==1)return false ; for(int i = 2; i*i <= n ;i++){ if(n% i ==0){return false ; } } return true ; } //////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// static int sieve = 1000000 ; static boolean[] prime = new boolean[sieve + 1] ; public static void sieveOfEratosthenes() { // FALSE == prime and 1 // TRUE == COMPOSITE // time complexity = 0(NlogLogN)== o(N) // gives prime nos bw 1 to N // size - 1e7(at max) for(int i = 4; i<= sieve ; i++) { prime[i] = true ; i++ ; } for(int p = 3; p*p <= sieve; p++) { if(prime[p] == false) { for(int i = p*p; i <= sieve; i += p) prime[i] = true; } p++ ; } } /////////////////////////////////////////////////////////////////////////////////// public static void sortD(int[] arr , int s , int e) { sort(arr ,s , e) ; int i =s ; int j = e ; while( i < j){ int temp = arr[i] ;arr[i] =arr[j] ; arr[j] = temp ; i++ ; j-- ;} } ///////////////////////////////////////////////////////////////////////////////////////// public static long countSubarraysSumToK(long[] arr ,long sum ) { HashMap<Long,Long> map = new HashMap<>() ; int n = arr.length ; long prefixsum = 0 ; long count = 0L ; for(int i = 0; i < n ; i++) { prefixsum = prefixsum + arr[i] ; if(sum == prefixsum)count = count+1 ; if(map.containsKey(prefixsum -sum))count = count + map.get(prefixsum -sum) ; if(map.containsKey(prefixsum )) map.put(prefixsum , map.get(prefixsum) +1 ); else map.put(prefixsum , 1L ); } return count ; } /////////////////////////////////////////////////////////////////////////////////////////////// // KMP ALGORITHM : TIME COMPL:O(N+M) // FINDS THE OCCURENCES OF PATTERN AS A SUBSTRING IN STRING //RETURN THE ARRAYLIST OF INDEXES // IF SIZE OF LIST IS ZERO MEANS PATTERN IS NOT PRESENT IN STRING public static ArrayList<Integer> kmpAlgorithm(String str , String pat) { ArrayList<Integer> list =new ArrayList<>(); int n = str.length() ; int m = pat.length() ; String q = pat + "#" + str ; int[] lps =new int[n+m+1] ; longestPefixSuffix(lps, q,(n+m+1)) ; for(int i =m+1 ; i < (n+m+1) ; i++ ) { if(lps[i] == m) { list.add(i-2*m) ; } } return list ; } public static void longestPefixSuffix(int[] lps ,String str , int n) { lps[0] = 0 ; for(int i = 1 ; i<= n-1; i++) { int l = lps[i-1] ; while( l > 0 && str.charAt(i) != str.charAt(l)) { l = lps[l-1] ; } if(str.charAt(i) == str.charAt(l)) {l++ ;} lps[i] = l ; } } /////////////////////////////////////////////////////////////////////////////////////////////////// // CALCULATE TOTIENT Fn FOR ALL VALUES FROM 1 TO n // TOTIENT(N) = count of nos less than n and grater than 1 whose gcd with n is 1 // or n and the no will be coprime in nature //time : O(n*(log(logn))) public static void eulerTotientFunction(int[] arr ,int n ) { for(int i = 1; i <= n ;i++)arr[i] =i ; for(int i= 2 ; i<= n ;i++) { if(arr[i] == i) { arr[i] =i-1 ; for(int j =2*i ; j<= n ; j+= i ) { arr[j] = (arr[j]*(i-1))/i ; } } } } ///////////////////////////////////////////////////////////////////////////////////////////// public static long nCr(int n,int k) { long ans=1L; k=k>n-k?n-k:k; int j=1; for(;j<=k;j++,n--) { if(n%j==0) { ans*=n/j; }else if(ans%j==0) { ans=ans/j*n; }else { ans=(ans*n)/j; } } return ans; } /////////////////////////////////////////////////////////////////////////////////////////// public static ArrayList<Integer> allFactors(int n) { ArrayList<Integer> list = new ArrayList<>() ; for(int i = 1; i*i <= n ;i++) { if( n % i == 0) { if(i*i == n)list.add(i) ; else{ list.add(i) ;list.add(n/i) ; } } } return list ; } public static ArrayList<Long> allFactors(long n) { ArrayList<Long> list = new ArrayList<>() ; for(long i = 1L; i*i <= n ;i++) { if( n % i == 0) { if(i*i == n) list.add(i) ; else{ list.add(i) ; list.add(n/i) ; } } } return list ; } //////////////////////////////////////////////////////////////////////////////////////////////////// static final int MAXN = 1000001; static int spf[] = new int[MAXN]; static void sieve() { for (int i=1; i<MAXN; i++) spf[i] = i; // ALL NOS HAVE SPF[i] =i AT START for (int i=4; i<MAXN; i+=2) //ALL EVEN NOS HAVE SPF[i] = 2 spf[i] = 2; for (int i=3; i*i<MAXN; i++) { if (spf[i] == i) { for (int j=i*i; j<MAXN; j+=i) if (spf[j]==j) spf[j] = i; } } } static ArrayList<Integer> getPrimeFactorization(int x) { ArrayList<Integer> ret = new ArrayList<Integer>(); while (x != 1) { ret.add(spf[x]); x = x / spf[x]; } return ret; } ////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////// public static void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int[n1]; int R[] = new int[n2]; //Copy data to temp arrays for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() public static void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } public static void sort(long arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } public static void merge(long arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ long L[] = new long[n1]; long R[] = new long[n2]; //Copy data to temp arrays for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } ///////////////////////////////////////////////////////////////////////////////////////// public static long knapsack(int[] weight,long value[],int maxWeight){ int n= value.length ; //dp[i] stores the profit with KnapSack capacity "i" long []dp = new long[maxWeight+1]; //initially profit with 0 to W KnapSack capacity is 0 Arrays.fill(dp, 0); // iterate through all items for(int i=0; i < n; i++) //traverse dp array from right to left for(int j = maxWeight; j >= weight[i]; j--) dp[j] = Math.max(dp[j] , value[i] + dp[j - weight[i]]); /*above line finds out maximum of dp[j](excluding ith element value) and val[i] + dp[j-wt[i]] (including ith element value and the profit with "KnapSack capacity - ith element weight") */ return dp[maxWeight]; } /////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// // to return max sum of any subarray in given array public static long kadanesAlgorithm(long[] arr) { if(arr.length == 0)return 0 ; long[] dp = new long[arr.length] ; dp[0] = arr[0] ;long max = dp[0] ; for(int i = 1; i < arr.length ; i++) { if(dp[i-1] > 0) dp[i] = dp[i-1] + arr[i] ; else dp[i] = arr[i] ; if(dp[i] > max)max = dp[i] ; } return max ; } ///////////////////////////////////////////////////////////////////////////////////////////// public static long kadanesAlgorithm(int[] arr) { if(arr.length == 0)return 0 ; long[] dp = new long[arr.length] ;dp[0] = arr[0] ; long max = dp[0] ; for(int i = 1; i < arr.length ; i++) { if(dp[i-1] > 0) dp[i] = dp[i-1] + arr[i] ; else dp[i] = arr[i] ; if(dp[i] > max)max = dp[i] ; } return max ; } ///////////////////////////////////////////////////////////////////////////////////////////////////////// public static long power(int a ,int b) { //time comp : o(logn) long x = (long)(a) ; long n = (long)(b) ; if(n==0)return 1 ;if(n==1)return x; long ans =1L ; while(n>0) { if(n % 2 ==1)ans = ans *x ; n = n/2L ;x = x*x ; } return ans ; } public static long power(long a ,long b) { //time comp : o(logn) long x = (a) ;long n = (b) ; if(n==0)return 1L ;if(n==1)return x; long ans =1L ; while(n>0) { if(n % 2 ==1)ans = ans *x ; n = n/2L ; x = x*x ; } return ans ; } ////////////////////////////////////////////////////////////////////////////////// /* lowerBound - finds largest element equal or less than value paased upperBound - finds smallest element equal or more than value passed if not present return -1; */ public static long lowerBound(long[] arr,long k) { long ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]<=k) { ans=arr[mid]; start=mid+1; } else { end=mid-1; } } return ans; } public static int lowerBound(int[] arr,int k) { int ans=-1;int start=0;int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]<=k) { ans=arr[mid]; start=mid+1; } else { end=mid-1; } } return ans; } public static long upperBound(long[] arr,long k) { long ans=-1;int start=0;int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]>=k) { ans=arr[mid]; end=mid-1; } else { start=mid+1; } } return ans; } public static int upperBound(int[] arr,int k) { int ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]>=k) { ans=arr[mid]; end=mid-1; } else { start=mid+1; } } return ans; } //////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// public static void printArray(int[] arr , int si ,int ei, int f) { for(int i = si ; i <= ei ; i++){ out.print(arr[i] +" ") ; } if(f==1)out.println() ; } public static void printArray(long[] arr , int si ,int ei, int f) { for(int i = si ; i <= ei ; i++){ out.print(arr[i] +" ") ; } if(f==1)out.println() ; } public static void printtwodArray(int[][] ans) { for(int i = 0; i< ans.length ; i++) { for(int j = 0 ; j < ans[0].length ; j++)out.print(ans[i][j] +" "); out.println() ; } out.println() ; } ///////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// static long modPow(long a, long x, long p) { //calculates a^x mod p in logarithmic time. a = a % p ; if(a == 0)return 0L ; if(x==0)return 1L; if(x==1)return a; long res = 1L; while(x > 0) { if( x % 2 != 0) { res = (res * a) % p; } a = (a * a) % p; x =x/2; } return res; } static long modInverse(long a, long p) { //calculates the modular multiplicative of a mod p. //(assuming p is prime). return modPow(a, p-2, p); } static long[] factorial = new long[1000001] ; static void modfac(long mod) { factorial[0]=1L ; factorial[1]=1L ; for(int i = 2; i<= 1000000 ;i++) { factorial[i] = factorial[i-1] *(long)(i) ; factorial[i] = factorial[i] % mod ; } } static long modBinomial(long n, long r, long p) { // calculates C(n,r) mod p (assuming p is prime). if(n < r) return 0L ; long num = factorial[(int)(n)] ; long den = (factorial[(int)(r)]*factorial[(int)(n-r)]) % p ; long ans = num*(modInverse(den,p)) ; ans = ans % p ; return ans ; } static void update(int val , long[] bit ,int n) { for( ; val <= n ; val += (val &(-val)) ) { bit[val]++ ; } } static long query(int val , long[] bit , int n) { long ans = 0L; for( ; val >=1 ; val-=(val&(-val)) )ans += bit[val]; return ans ; } static int countSetBits(long n) { int count = 0; while (n > 0) { n = (n) & (n - 1L); count++; } return count; } static int abs(int x) { if(x<0)x=-x ; return x ; } static long abs(long x) { if(x<0)x=-x ; return x ; } //////////////////////////////////////////////////////////////////////////////////////////// // calculate total no of nos greater than or equal to key in sorted array arr static int bs(int[] arr, int s ,int e ,int key) { if( s> e)return 0 ; int mid = (s+e)/2 ; if(arr[mid] <key) return bs(arr ,mid+1,e , key) ; else {return bs(arr ,s ,mid-1, key) + e-mid+1;} } // static ArrayList<Integer>[] adj ; // static int mod= 1000000007 ; static class pair { int u; int v; public pair(int u,int v) { this.u = u ; this.v=v; } } static class Dsu { int n ;int[] par ; int[] size ; public Dsu(int n) { this.n = n ; par = new int[n] ; size = new int[n] ; for(int i = 0 ; i< n ;i++) { par[i] =i ;size[i] =1 ; } } // cheack if given element is root element of its set public boolean isRoot(int x) { return par[x] == x ; } // find root of given set public int findRoot(int x) { if(par[x] == x) { return par[x] ; } par[x] = findRoot(par[x]) ; //path compression return par[x] ; } public boolean unionSet(int a, int b) { int rootA = findRoot(a); int rootB=findRoot(b) ; if(rootA==rootB)return true ;//they are alraedy present in same set/connected already(in graph) if(size[rootA] > size[rootB]) { size[rootA] += size[rootB] ; par[rootB] =rootA ; } else{ size[rootB] += size[rootA] ; par[rootA] =rootB ; } return false ; // means we union both the sets now } } ////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// public static void solve() { FastReader scn = new FastReader() ; //DEBUGGING - // 1 - I/O - CHECK PRINTING OF NEXT LINE , READ INPUT , EXTRA LINE PRINT. //2-OVERFLOW ERROR. //3 -STATIC VARIABLE INITIALIZATION (INT MAINLIY) //4-DIVISION ERROR(FLOOR/CEIL) or DIVIDE BY ZERO //5-MOD -(MOD BY 1 OR 0 ) // DOUBLE LOOP/TRIPLE LOOP BREAK (SEE IF DO NOT BREAK ALL THE LOOPS IF U GET ANS EARLIER) //Scanner scn = new Scanner(System.in); //int[] store = {2 ,3, 5 , 7 ,11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 } ; // product of first 11 prime nos is greater than 10 ^ 12; ArrayList<Integer> list = new ArrayList<>() ; ArrayList<Integer> listx = new ArrayList<>() ; ArrayList<Integer> listy = new ArrayList<>() ; HashMap<Integer,Integer> map = new HashMap<>() ; HashMap<Integer,Integer> mapx = new HashMap<>() ; HashMap<Integer,Integer> mapy = new HashMap<>() ; //HashMap<Point,Integer> point = new HashMap<>() ; Set<Integer> set = new HashSet<>() ; Set<Integer> setx = new HashSet<>() ; Set<Integer> sety = new HashSet<>() ; StringBuilder sb =new StringBuilder("") ; //Collections.sort(list);// Collections.reverse(list) ; //int bit =Integer.bitCount(n);// gives total no of set bits in n; // Arrays.sort(arr, new Comparator<Pair>() { // @Override // public int compare(Pair a, Pair b) { // if (a.first != b.first) { // return a.first - b.first; // for increasing order of first // } // return a.second - b.second ; //if first is same then sort on second basis // } // }); //if(map.containsKey(arr[i]))map.put(arr[i],map.get(arr[i])+1) ;else map.put(arr[i],1) ; //if(map.containsKey(temp))map.put(temp,map.get(temp)+1) ;else map.put(temp,1) ; int testcase = 1; testcase = scn.nextInt() ; for(int testcases =1 ; testcases <= testcase ;testcases++) { //adj = new ArrayList[n] ; // for(int i = 0; i< n; i++) // { // adj[i] = new ArrayList<Integer>(); // } // long n = scn.nextLong() ; //String s = scn.next() ; //Dsu dsu = new Dsu(n) ; int n= scn.nextInt() ; int[] a = new int[n] ; int[] b = new int[n] ; for(int i = 0 ; i < n;i++){ a[i] = scn.nextInt() ; b[i] = a[i] ; } int[][] f = new int[100001][2] ; sort(b,0,n-1) ; for(int i = 0 ; i < n ; i++) { if((i&1)==0)f[b[i]][0]++ ; else f[b[i]][1]++; } String ans ="Yes"; for(int i = 0 ; i < n ;i++) { int val= a[i] ; if((i&1)==0){ if(f[val][0] == 0){ans ="No";break;} else{ f[val][0]--; } } else{ if(f[val][1] == 0){ans ="No";break;} else{ f[val][1]--; } } } out.println(ans) ; //out.println("Case #" + testcases + ": " + ans ) ; //out.println("@") ; sb.delete(0 , sb.length()) ; list.clear() ;listx.clear() ;listy.clear() ; map.clear() ;mapx.clear() ;mapy.clear() ; set.clear() ;setx.clear() ;sety.clear() ; } // test case end loop out.flush() ; } // solve fn ends public static void main (String[] args) throws java.lang.Exception { solve() ; } } class Pair { int first ;int second ; public Pair(int x, int y) { this.first = x ;this.second = y ; } @Override public boolean equals(Object obj) { if(obj == this)return true ; if(obj == null)return false ; if(this.getClass() != obj.getClass()) return false ; Pair other = (Pair)(obj) ; if(this.first != other.first)return false ; if(this.second != other.second)return false ; return true ; } @Override public int hashCode() { return this.first^this.second ; } @Override public String toString() { String ans = "" ;ans += this.first ; ans += " "; ans += this.second ; return ans ; } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
10a9a5cf691fbf59498368b38ad85d5b
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.*; public class cf1545a { public static void main(String[] args) throws IOException { int t = ri(); next: while (t --> 0) { int n = ri(), a[] = ria(n); Map<p, Integer> cnt = new HashMap<>(); for (int i = 0; i < n; ++i) { p p = new p(a[i], i & 1); cnt.put(p, cnt.getOrDefault(p, 0) + 1); } rsort(a); for (int i = 0; i < n; ++i) { p p = new p(a[i], i & 1); if (!cnt.containsKey(p) || cnt.get(p) == 0) { prN(); continue next; } cnt.put(p, cnt.get(p) - 1); } prY(); } close(); } static class p { int a, b; p(int a_, int b_) { a = a_; b = b_; } @Override public String toString() { return "Pair{" + "a = " + a + ", b = " + b + '}'; } public boolean asymmetricEquals(Object o) { p p = (p) o; return a == p.a && b == p.b; } public boolean symmetricEquals(Object o) { p p = (p) o; return a == p.a && b == p.b || a == p.b && b == p.a; } @Override public boolean equals(Object o) { return asymmetricEquals(o); } public int asymmetricHashCode() { return Objects.hash(a, b); } public int symmetricHashCode() { return Objects.hash(a, b) + Objects.hash(b, a); } @Override public int hashCode() { return asymmetricHashCode(); } } static BufferedReader __i = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter __o = new PrintWriter(new OutputStreamWriter(System.out)); static StringTokenizer input; static Random __r = new Random(); // references // IBIG = 1e9 + 7 // IMAX ~= 2e9 // LMAX ~= 9e18 // constants static final int IBIG = 1000000007; static final int IMAX = 2147483647; static final long LMAX = 9223372036854775807L; // math util static int minof(int a, int b, int c) {return min(a, min(b, c));} static int minof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;} static long minof(long a, long b, long c) {return min(a, min(b, c));} static long minof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;} static int maxof(int a, int b, int c) {return max(a, max(b, c));} static int maxof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;} static long maxof(long a, long b, long c) {return max(a, max(b, c));} static long maxof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;} static int powi(int a, int b) {if (a == 0) return 0; int ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static long powl(long a, int b) {if (a == 0) return 0; long ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static int fli(double d) {return (int) d;} static int cei(double d) {return (int) ceil(d);} static long fll(double d) {return (long) d;} static long cel(double d) {return (long) ceil(d);} static int gcd(int a, int b) {return b == 0 ? a : gcd(b, a % b);} static long gcd(long a, long b) {return b == 0 ? a : gcd(b, a % b);} static int[] exgcd(int a, int b) {if (b == 0) return new int[] {1, 0}; int[] y = exgcd(b, a % b); return new int[] {y[1], y[0] - y[1] * (a / b)};} static long[] exgcd(long a, long b) {if (b == 0) return new long[] {1, 0}; long[] y = exgcd(b, a % b); return new long[] {y[1], y[0] - y[1] * (a / b)};} static int randInt(int min, int max) {return __r.nextInt(max - min + 1) + min;} static long mix(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);} // array util static void reverse(int[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void reverse(long[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void reverse(double[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void reverse(char[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void shuffle(int[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void shuffle(long[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void shuffle(double[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void rsort(int[] a) {shuffle(a); sort(a);} static void rsort(long[] a) {shuffle(a); sort(a);} static void rsort(double[] a) {shuffle(a); sort(a);} static int[] copy(int[] a) {int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static long[] copy(long[] a) {long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static double[] copy(double[] a) {double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static char[] copy(char[] a) {char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} // input static void r() throws IOException {input = new StringTokenizer(rline());} static int ri() throws IOException {return Integer.parseInt(rline());} static long rl() throws IOException {return Long.parseLong(rline());} static double rd() throws IOException {return Double.parseDouble(rline());} static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni(); return a;} static void ria(int[] a) throws IOException {int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = ni();} static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; return a;} static void riam1(int[] a) throws IOException {int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1;} static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for (int i = 0; i < n; ++i) a[i] = nl(); return a;} static void rla(long[] a) throws IOException {int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = nl();} static double[] rda(int n) throws IOException {double[] a = new double[n]; r(); for (int i = 0; i < n; ++i) a[i] = nd(); return a;} static void rda(double[] a) throws IOException {int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = nd();} static char[] rcha() throws IOException {return rline().toCharArray();} static void rcha(char[] a) throws IOException {int n = a.length, i = 0; for (char c : rline().toCharArray()) a[i++] = c;} static String rline() throws IOException {return __i.readLine();} static String n() {return input.nextToken();} static int rni() throws IOException {r(); return ni();} static int ni() {return Integer.parseInt(n());} static long rnl() throws IOException {r(); return nl();} static long nl() {return Long.parseLong(n());} static double rnd() throws IOException {r(); return nd();} static double nd() {return Double.parseDouble(n());} // output static void pr(int i) {__o.print(i);} static void prln(int i) {__o.println(i);} static void pr(long l) {__o.print(l);} static void prln(long l) {__o.println(l);} static void pr(double d) {__o.print(d);} static void prln(double d) {__o.println(d);} static void pr(char c) {__o.print(c);} static void prln(char c) {__o.println(c);} static void pr(char[] s) {__o.print(new String(s));} static void prln(char[] s) {__o.println(new String(s));} static void pr(String s) {__o.print(s);} static void prln(String s) {__o.println(s);} static void pr(Object o) {__o.print(o);} static void prln(Object o) {__o.println(o);} static void prln() {__o.println();} static void pryes() {prln("yes");} static void pry() {prln("Yes");} static void prY() {prln("YES");} static void prno() {prln("no");} static void prn() {prln("No");} static void prN() {prln("NO");} static boolean pryesno(boolean b) {prln(b ? "yes" : "no"); return b;}; static boolean pryn(boolean b) {prln(b ? "Yes" : "No"); return b;} static boolean prYN(boolean b) {prln(b ? "YES" : "NO"); return b;} static void prln(int... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();} static void prln(long... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();} static void prln(double... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();} static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i); if (n >= 0) prln(iter.next()); else prln();} static void h() {prln("hlfd"); flush();} static void flush() {__o.flush();} static void close() {__o.close();} }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
a8c4e144a9d37de23af168085e3ef773
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.util.*; import java.text.DecimalFormat; public class Main { static long mod=(long)1e9+7; static long mod1=998244353; public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); int t= in.nextInt(); while(t-->0) { int n = in.nextInt(); int[] arr = in.readArray(n); HashMap<Integer,int[]> hmap =new HashMap<>(); for(int i = 0;i<n;i++){ int [] count = new int[2]; if(hmap.containsKey(arr[i])) count = hmap.get(arr[i]); count[i%2]++; hmap.put(arr[i],count); } Main.ruffleSort(arr); boolean flag = true; for(int i = 0;i<n;i++){ int [] count = hmap.get(arr[i]); if(i%2==0){ if(count[0]>0) count[0]--; else{ flag = false; break; } }else{ if(count[1]>0){ count[1]--; }else{ flag = false; break; } } } if(flag) out.println("YES"); else out.println("NO"); } out.close(); } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static long gcd(long x, long y){ if(x==0) return y; if(y==0) return x; long r=0, a, b; a = Math.max(x, y); b = Math.min(x, y); r = b; while(a % b != 0){ r = a % b; a = b; b = r; } return r; } static long modulo(long a,long b,long c){ long x=1,y=a%c; while(b > 0){ if(b%2 == 1) x=(x*y)%c; y = (y*y)%c; b = b>>1; } return x%c; } public static void debug(Object... o){ System.err.println(Arrays.deepToString(o)); } static int upper_bound(int[] arr,int n,int x){ int mid; int low=0; int high=n; while(low<high){ mid=low+(high-low)/2; if(x>=arr[mid]) low=mid+1; else high=mid; } return low; } static int lower_bound(int[] arr,int n,int x){ int mid; int low=0; int high=n; while(low<high){ mid=low+(high-low)/2; if(x<=arr[mid]) high=mid; else low=mid+1; } return low; } static String printPrecision(double d){ DecimalFormat ft = new DecimalFormat("0.00000000000"); return String.valueOf(ft.format(d)); } static int countBit(long mask){ int ans=0; while(mask!=0){ mask&=(mask-1); ans++; } return ans; } 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()); } public int[] readArray(int n) { int[] arr=new int[n]; for(int i=0;i<n;i++) arr[i]=nextInt(); return arr; } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
d7e07c218af9db018d7ac6add0d43e6d
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class hello { static Scanner scan = new Scanner(System.in); public static void main(String[] args) { int t = scan.nextInt(); while(t-->0){ solve(); } } static void solve(){ int n = scan.nextInt(); int a[] = new int[n]; int evn[] = new int[100009]; int odd[] = new int[100009]; Arrays.fill(evn, 0);Arrays.fill(odd, 0); for(int i = 0;i < n;i++){ a[i] = scan.nextInt(); if(i%2 == 0) evn[a[i]]++; else odd[a[i]]++; } Arrays.sort(a); int flag = 0; for(int i = 0;i < n;i++){ if(i%2 == 0 && evn[a[i]] > 0) evn[a[i]]--; else if(i%2 == 1 && odd[a[i]] > 0) odd[a[i]]--; else{ flag = 1; break; } } if(flag == 0) System.out.println("YES"); else System.out.println("NO"); for(int i = 0;i < n;i++) { evn[a[i]] = 0;odd[a[i]] = 0; } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
d1b9f55b6e20c1660221cdc9c8559b3d
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
/* stream Butter! eggyHide eggyVengeance I need U xiao rerun when */ import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; public class x1545A { public static void main(String hi[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int T = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); while(T-->0) { st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); int[] arr = readArr(N, infile, st); int[] oddcnt = new int[100005]; int[] evencnt = new int[100005]; for(int i=0; i < N; i+=2) evencnt[arr[i]]++; for(int i=1; i < N; i++) oddcnt[arr[i]]++; sort(arr); String res = "YES"; for(int i=0; i < N; i+=2) { //process evens if(evencnt[arr[i]] == 0) res = "NO"; else evencnt[arr[i]]--; } for(int i=1; i < N; i+=2) { //process odds if(oddcnt[arr[i]] == 0) res = "NO"; else oddcnt[arr[i]]--; } sb.append(res+"\n"); } System.out.print(sb); } public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception { int[] arr = new int[N]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } public static void sort(int[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort 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); } } /* 1 2 2 4 3 */
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
df265091392488a85108d4f1fa518d59
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.PriorityQueue; public class Main { public static void main(String[] args) { new Main(); } public Main() { FastScanner fs = new FastScanner(); java.io.PrintWriter out = new java.io.PrintWriter(System.out); solve(fs, out); out.flush(); } public void solve(FastScanner fs, java.io.PrintWriter out) { int t = fs.nextInt(); cont: while(t --> 0) { int n = fs.nextInt(); int[] a = new int[n]; for (int i = 0;i < n;++ i) a[i] = fs.nextInt(); int[] check = new int[100001]; for (int i = 0;i < n;++ i) check[a[i]] += i % 2 == 0 ? 1 : -1; PriorityQueue<Integer> pq = new PriorityQueue<>((l, r) -> Integer.compare(a[l], a[r])); for (int i = 0;i < n;++ i) pq.add(i); for (int i = 0;i < n;++ i) check[a[pq.poll()]] -= i % 2 == 0 ? 1 : -1; for (int i : check) { if (i != 0) { out.println("NO"); continue cont; } } out.println("YES"); } } final int MOD = 998_244_353; int plus(int n, int m) { int sum = n + m; if (sum >= MOD) sum -= MOD; return sum; } int minus(int n, int m) { int sum = n - m; if (sum < 0) sum += MOD; return sum; } int times(int n, int m) { return (int)((long)n * m % MOD); } int divide(int n, int m) { return times(n, IntMath.pow(m, MOD - 2, MOD)); } int[] fact, invf; void calc(int len) { len += 2; fact = new int[len]; invf = new int[len]; fact[0] = fact[1] = invf[0] = invf[1] = 1; for (int i = 2;i < fact.length;++ i) fact[i] = times(fact[i - 1], i); invf[len - 1] = divide(1, fact[len - 1]); for (int i = len - 1;i > 1;-- i) invf[i - 1] = times(i, invf[i]); } int comb(int n, int m) { if (n < m) return 0; return times(fact[n], times(invf[n - m], invf[m])); } } class FastScanner { private final java.io.InputStream in = System.in; private final byte[] buffer = new byte[8192]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) return true; ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } return buflen > 0; } private byte readByte() { return hasNextByte() ? buffer[ptr++ ] : -1; } private static boolean isPrintableChar(byte c) { return 32 < c || c < 0; } private static boolean isNumber(int c) { return '0' <= c && c <= '9'; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++ ; return hasNextByte(); } public String next() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); byte b; while (isPrintableChar(b = readByte())) sb.appendCodePoint(b); return sb.toString(); } public final char nextChar() { if (!hasNext()) throw new java.util.NoSuchElementException(); return (char)readByte(); } public final long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; try { byte b = readByte(); if (b == '-') { while (isNumber(b = readByte())) n = n * 10 + '0' - b; return n; } else if (!isNumber(b)) throw new NumberFormatException(); do n = n * 10 + b - '0'; while (isNumber(b = readByte())); } catch (java.util.NoSuchElementException e) {} return n; } public final int nextInt() { if (!hasNext()) throw new java.util.NoSuchElementException(); int n = 0; try { byte b = readByte(); if (b == '-') { while (isNumber(b = readByte())) n = n * 10 + '0' - b; return n; } else if (!isNumber(b)) throw new NumberFormatException(); do n = n * 10 + b - '0'; while (isNumber(b = readByte())); } catch (java.util.NoSuchElementException e) {} return n; } public double nextDouble() { return Double.parseDouble(next()); } } class Arrays { public static void sort(final int[] array) { sort(array, 0, array.length); } public static void sort(final int[] array, int fromIndex, int toIndex) { if (toIndex - fromIndex <= 512) { java.util.Arrays.sort(array, fromIndex, toIndex); return; } sort(array, fromIndex, toIndex, 0, new int[array.length]); } private static final void sort(int[] a, final int from, final int to, final int l, final int[] bucket) { if (to - from <= 512) { java.util.Arrays.sort(a, from, to); return; } final int BUCKET_SIZE = 256; final int INT_RECURSION = 4; final int MASK = 0xff; final int shift = l << 3; final int[] cnt = new int[BUCKET_SIZE + 1]; final int[] put = new int[BUCKET_SIZE]; for (int i = from; i < to; i++) ++ cnt[(a[i] >>> shift & MASK) + 1]; for (int i = 0; i < BUCKET_SIZE; i++) cnt[i + 1] += cnt[i]; for (int i = from; i < to; i++) { int bi = a[i] >>> shift & MASK; bucket[cnt[bi] + put[bi]++] = a[i]; } for (int i = BUCKET_SIZE - 1, idx = from; i >= 0; i--) { int begin = cnt[i]; int len = cnt[i + 1] - begin; System.arraycopy(bucket, begin, a, idx, len); idx += len; } final int nxtL = l + 1; if (nxtL < INT_RECURSION) { sort(a, from, to, nxtL, bucket); if (l == 0) { int lft, rgt; lft = from - 1; rgt = to; while (rgt - lft > 1) { int mid = lft + rgt >> 1; if (a[mid] < 0) lft = mid; else rgt = mid; } reverse(a, from, rgt); reverse(a, rgt, to); } } } public static void sort(final long[] array) { sort(array, 0, array.length); } public static void sort(final long[] array, int fromIndex, int toIndex) { if (toIndex - fromIndex <= 512) { java.util.Arrays.sort(array, fromIndex, toIndex); return; } sort(array, fromIndex, toIndex, 0, new long[array.length]); } private static final void sort(long[] a, final int from, final int to, final int l, final long[] bucket) { final int BUCKET_SIZE = 256; final int LONG_RECURSION = 8; final int MASK = 0xff; final int shift = l << 3; final int[] cnt = new int[BUCKET_SIZE + 1]; final int[] put = new int[BUCKET_SIZE]; for (int i = from; i < to; i++) ++ cnt[(int) ((a[i] >>> shift & MASK) + 1)]; for (int i = 0; i < BUCKET_SIZE; i++) cnt[i + 1] += cnt[i]; for (int i = from; i < to; i++) { int bi = (int) (a[i] >>> shift & MASK); bucket[cnt[bi] + put[bi]++] = a[i]; } for (int i = BUCKET_SIZE - 1, idx = from; i >= 0; i--) { int begin = cnt[i]; int len = cnt[i + 1] - begin; System.arraycopy(bucket, begin, a, idx, len); idx += len; } final int nxtL = l + 1; if (nxtL < LONG_RECURSION) { sort(a, from, to, nxtL, bucket); if (l == 0) { int lft, rgt; lft = from - 1; rgt = to; while (rgt - lft > 1) { int mid = lft + rgt >> 1; if (a[mid] < 0) lft = mid; else rgt = mid; } reverse(a, from, rgt); reverse(a, rgt, to); } } } public static void reverse(int[] array) { reverse(array, 0, array.length); } public static void reverse(int[] array, int fromIndex, int toIndex) { for (-- toIndex;fromIndex < toIndex;++ fromIndex, -- toIndex) { int swap = array[fromIndex]; array[fromIndex] = array[toIndex]; array[toIndex] = swap; } } public static void reverse(long[] array) { reverse(array, 0, array.length); } public static void reverse(long[] array, int fromIndex, int toIndex) { for (-- toIndex;fromIndex < toIndex;++ fromIndex, -- toIndex) { long swap = array[fromIndex]; array[fromIndex] = array[toIndex]; array[toIndex] = swap; } } } class IntMath { public static int gcd(int a, int b) { while (a != 0) if ((b %= a) != 0) a %= b; else return a; return b; } public static int gcd(int... array) { int ret = array[0]; for (int i = 1; i < array.length; ++i) ret = gcd(ret, array[i]); return ret; } public static long gcd(long a, long b) { while (a != 0) if ((b %= a) != 0) a %= b; else return a; return b; } public static long gcd(long... array) { long ret = array[0]; for (int i = 1; i < array.length; ++i) ret = gcd(ret, array[i]); return ret; } public static long lcm(long a, long b) { return a / gcd(a, b) * b; } public static int pow(int a, int b) { int ans = 1; for (int mul = a; b > 0; b >>= 1, mul *= mul) if ((b & 1) != 0) ans *= mul; return ans; } public static long pow(long a, long b) { long ans = 1; for (long mul = a; b > 0; b >>= 1, mul *= mul) if ((b & 1) != 0) ans *= mul; return ans; } public static int pow(int a, long b, int mod) { if (b < 0) b = b % (mod - 1) + mod - 1; long ans = 1; for (long mul = a; b > 0; b >>= 1, mul = mul * mul % mod) if ((b & 1) != 0) ans = ans * mul % mod; return (int)ans; } public static int pow(long a, long b, int mod) { return pow((int)(a % mod), b, mod); } public static int floorsqrt(long n) { return (int)Math.sqrt(n + 0.1); } public static int ceilsqrt(long n) { return n <= 1 ? (int)n : (int)Math.sqrt(n - 0.1) + 1; } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
ac6d9b5a863c97624aad957f1a9f69a6
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import java.io.*; public class A { public static void main(String[] args) { FastScanner sc = new FastScanner(); int T = sc.nextInt(); int N = 100000; while(T-->0) { int n = sc.nextInt(); LinkedList<Integer>[] pos = new LinkedList[N+1]; for(int i = 0; i <= N; i++) { pos[i] = new LinkedList<>(); } for(int i = 0; i < n; i++) { int a = sc.nextInt(); pos[a].add(i); } int c = 0; boolean yes = true; for(int a = 0; a <= N; a++) { int od = 0, ev = 0; for(int p: pos[a]) { if(p % 2 == 0) ev++; else od++; } if(od > ev + 1 || ev > od + 1) yes = false; else { if(od > ev) { if(c % 2 == 0) yes = false; } else if(ev > od) { if(c % 2 == 1) yes = false; } else { ; } } if(!yes) break; c += pos[a].size(); } if(yes) System.out.println("YES"); else System.out.println("NO"); } } static class FastScanner { public BufferedReader reader; public StringTokenizer tokenizer; public FastScanner() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return reader.readLine(); } catch(IOException e) { throw new RuntimeException(e); } } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
b189de2791439824080e09cf19bf4d45
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
//package round732; import java.io.*; import java.util.ArrayDeque; import java.util.Arrays; import java.util.InputMismatchException; import java.util.Queue; public class A { InputStream is; FastWriter out; String INPUT = ""; void solve() { for(int T = ni();T > 0;T--)go(); } void go() { int n = ni(); int[] a = na(n); { int[] b = new int[(n + 1) / 2]; for (int i = 0; i < n; i += 2) b[i / 2] = a[i]; b = radixSort2(b); for (int i = 0; i < n; i += 2) a[i] = b[i / 2]; } { int[] b = new int[n / 2]; for (int i = 1; i < n; i += 2) b[i / 2] = a[i]; b = radixSort2(b); for (int i = 1; i < n; i += 2) a[i] = b[i / 2]; } for(int i = 0;i < n-1;i++){ if(a[i] > a[i+1]){ out.println("NO"); return; } } out.println("YES"); } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for(int v : a) { c0[(v&0xff)+1]++; c1[(v>>>8&0xff)+1]++; c2[(v>>>16&0xff)+1]++; c3[(v>>>24^0x80)+1]++; } for(int i = 0;i < 0xff;i++) { c0[i+1] += c0[i]; c1[i+1] += c1[i]; c2[i+1] += c2[i]; c3[i+1] += c3[i]; } int[] t = new int[n]; for(int v : a)t[c0[v&0xff]++] = v; for(int v : t)a[c1[v>>>8&0xff]++] = v; for(int v : a)t[c2[v>>>16&0xff]++] = v; for(int v : t)a[c3[v>>>24^0x80]++] = v; return a; } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new FastWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new A().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private long[] nal(int n) { long[] a = new long[n]; for(int i = 0;i < n;i++)a[i] = nl(); return a; } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[][] nmi(int n, int m) { int[][] map = new int[n][]; for(int i = 0;i < n;i++)map[i] = na(m); return map; } private int ni() { return (int)nl(); } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } public static class FastWriter { private static final int BUF_SIZE = 1<<13; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastWriter(){out = null;} public FastWriter(OutputStream os) { this.out = os; } public FastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public FastWriter write(byte b) { buf[ptr++] = b; if(ptr == BUF_SIZE)innerflush(); return this; } public FastWriter write(char c) { return write((byte)c); } public FastWriter write(char[] s) { for(char c : s){ buf[ptr++] = (byte)c; if(ptr == BUF_SIZE)innerflush(); } return this; } public FastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte)c; if(ptr == BUF_SIZE)innerflush(); }); return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } public FastWriter write(int x) { if(x == Integer.MIN_VALUE){ return write((long)x); } if(ptr + 12 >= BUF_SIZE)innerflush(); if(x < 0){ write((byte)'-'); x = -x; } int d = countDigits(x); for(int i = ptr + d - 1;i >= ptr;i--){ buf[i] = (byte)('0'+x%10); x /= 10; } ptr += d; return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastWriter write(long x) { if(x == Long.MIN_VALUE){ return write("" + x); } if(ptr + 21 >= BUF_SIZE)innerflush(); if(x < 0){ write((byte)'-'); x = -x; } int d = countDigits(x); for(int i = ptr + d - 1;i >= ptr;i--){ buf[i] = (byte)('0'+x%10); x /= 10; } ptr += d; return this; } public FastWriter write(double x, int precision) { if(x < 0){ write('-'); x = -x; } x += Math.pow(10, -precision)/2; // if(x < 0){ x = 0; } write((long)x).write("."); x -= (long)x; for(int i = 0;i < precision;i++){ x *= 10; write((char)('0'+(int)x)); x -= (int)x; } return this; } public FastWriter writeln(char c){ return write(c).writeln(); } public FastWriter writeln(int x){ return write(x).writeln(); } public FastWriter writeln(long x){ return write(x).writeln(); } public FastWriter writeln(double x, int precision){ return write(x, precision).writeln(); } public FastWriter write(int... xs) { boolean first = true; for(int x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter write(long... xs) { boolean first = true; for(long x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter writeln() { return write((byte)'\n'); } public FastWriter writeln(int... xs) { return write(xs).writeln(); } public FastWriter writeln(long... xs) { return write(xs).writeln(); } public FastWriter writeln(char[] line) { return write(line).writeln(); } public FastWriter writeln(char[]... map) { for(char[] line : map)write(line).writeln(); return this; } public FastWriter writeln(String s) { return write(s).writeln(); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } public FastWriter print(byte b) { return write(b); } public FastWriter print(char c) { return write(c); } public FastWriter print(char[] s) { return write(s); } public FastWriter print(String s) { return write(s); } public FastWriter print(int x) { return write(x); } public FastWriter print(long x) { return write(x); } public FastWriter print(double x, int precision) { return write(x, precision); } public FastWriter println(char c){ return writeln(c); } public FastWriter println(int x){ return writeln(x); } public FastWriter println(long x){ return writeln(x); } public FastWriter println(double x, int precision){ return writeln(x, precision); } public FastWriter print(int... xs) { return write(xs); } public FastWriter print(long... xs) { return write(xs); } public FastWriter println(int... xs) { return writeln(xs); } public FastWriter println(long... xs) { return writeln(xs); } public FastWriter println(char[] line) { return writeln(line); } public FastWriter println(char[]... map) { return writeln(map); } public FastWriter println(String s) { return writeln(s); } public FastWriter println() { return writeln(); } } public void trnz(int... o) { for(int i = 0;i < o.length;i++)if(o[i] != 0)System.out.print(i+":"+o[i]+" "); System.out.println(); } // print ids which are 1 public void trt(long... o) { Queue<Integer> stands = new ArrayDeque<>(); for(int i = 0;i < o.length;i++){ for(long x = o[i];x != 0;x &= x-1)stands.add(i<<6|Long.numberOfTrailingZeros(x)); } System.out.println(stands); } public void tf(boolean... r) { for(boolean x : r)System.out.print(x?'#':'.'); System.out.println(); } public void tf(boolean[]... b) { for(boolean[] r : b) { for(boolean x : r)System.out.print(x?'#':'.'); System.out.println(); } System.out.println(); } public void tf(long[]... b) { if(INPUT.length() != 0) { for (long[] r : b) { for (long x : r) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } System.out.println(); } } public void tf(long... b) { if(INPUT.length() != 0) { for (long x : b) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 11
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
62c91a338ff38a108a5feca09d173d14
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.util.*; public class A { public static void main(String[] args) { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); int T = in.nextInt(); for(int ttt = 1; ttt <= T; ttt++) { int n = in.nextInt(); int[] a = new int[n]; int[][] count = new int[100001][2]; for(int i = 0; i < n; i++) { a[i] = in.nextInt(); count[a[i]][i%2]++; } Arrays.sort(a); for(int i = 0; i < n; i++) { count[a[i]][i%2]--; } boolean p = true; for(int i = 0; i < n; i++) { if(count[a[i]][0]!=0 || count[a[i]][1]!=0) { p = false; break; } } if(p) out.println("YES"); else out.println("NO"); } out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } int[] readInt(int size) { int[] arr = new int[size]; for(int i = 0; i < size; i++) arr[i] = Integer.parseInt(next()); return arr; } long[] readLong(int size) { long[] arr = new long[size]; for(int i = 0; i < size; i++) arr[i] = Long.parseLong(next()); return arr; } int[][] read2dArray(int rows, int cols) { int[][] arr = new int[rows][cols]; for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) arr[i][j] = Integer.parseInt(next()); } return arr; } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
9af262b1a214b9dec06469e1ece71f98
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.util.*; public class A { public static void main(String[] args) { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); int T = in.nextInt(); for(int ttt = 1; ttt <= T; ttt++) { int n = in.nextInt(); Integer[] a = new Integer[n]; int[][] count = new int[100001][2]; for(int i = 0; i < n; i++) { a[i] = in.nextInt(); count[a[i]][i%2]++; } Arrays.sort(a); for(int i = 0; i < n; i++) { count[a[i]][i%2]--; } boolean p = true; for(int i = 0; i < n; i++) { if(count[a[i]][0]!=0 || count[a[i]][1]!=0) { p = false; break; } } if(p) out.println("YES"); else out.println("NO"); } out.close(); } private static void ruffleSort(int[] a) { // Shuffles elements and then sorts the array. Improves chances to avoid worst case of quicksort (O(n^2)). int n = a.length; for(int i = 0; i < n; i++) { int randInd = new Random().nextInt(n); int temp = a[randInd]; a[randInd] = a[i]; a[i]=temp; } Arrays.sort(a); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } int[] readInt(int size) { int[] arr = new int[size]; for(int i = 0; i < size; i++) arr[i] = Integer.parseInt(next()); return arr; } long[] readLong(int size) { long[] arr = new long[size]; for(int i = 0; i < size; i++) arr[i] = Long.parseLong(next()); return arr; } int[][] read2dArray(int rows, int cols) { int[][] arr = new int[rows][cols]; for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) arr[i][j] = Integer.parseInt(next()); } return arr; } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
616243bd5d6683e229bdbff489c71b2a
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class CF1545_D1_A { public static void main(String[] args) { FastScanner scanner = new FastScanner(); int t = scanner.nextInt(); for (int i = 0; i < t; i++) { solve(scanner); } } private static void solve(FastScanner scanner) { int n = scanner.nextInt(); int[] arr = scanner.nextArray(n); int[] sorted = Arrays.copyOf(arr, n); Arrays.sort(sorted); HashMap<Integer, Item> map = new HashMap<>(); for (int i = 0; i < n; i++) { Item item = map.getOrDefault(arr[i], new Item()); if (i % 2 == 0) { item.even++; } else { item.odd++; } map.put(arr[i], item); } HashMap<Integer, Item> mapSort = new HashMap<>(); for (int i = 0; i < n; i++) { Item item = mapSort.getOrDefault(sorted[i], new Item()); if (i % 2 == 0) { item.even++; } else { item.odd++; } mapSort.put(sorted[i], item); } for (Map.Entry<Integer, Item> e : map.entrySet()) { if(!e.getValue().equals(mapSort.get(e.getKey()))){ System.out.println("NO"); return; } } // for (int i = 0; i < n; i++) { // int x = arr[i]; // int lowest = lowest(sorted, x - 1); // int highest = highest(sorted, x + 1); // if (Math.abs(lowest - i) % 2 != 0) { // if (highest - lowest + 1 == 1) { // System.out.println("NO"); // return; // } // } // } // int start = 0; // for (int i = 0; i < n; i++) { // int x = arr[i]; // boolean found = false; // for (int j = -2; j < 3; j++) { // int index = i + j; // if (index < 0) { // index += n; // } // if (index >= n) { // index -= n; // } // if (sorted[index] == x && !used[index] && Math.abs(index - i) % 2 == 0) { // found = true; // used[index] = true; // break; // } // } // if (!found) { // System.out.println("NO"); // return; // } // if (index < start) { // index = start; // } // boolean found = false; // while (index < n && sorted[index] == x) { // if (!used[index] && Math.abs(index - i) % 2 == 0) { // used[index] = true; // found = true; // if (index == start) { // start++; // } // break; // } // index++; // } // if (!found) { // System.out.println("NO"); // return; // } // } System.out.println("YES"); } static class Item { int odd, even; public Item() { this.odd = 0; this.even = 0; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Item item = (Item) o; return odd == item.odd && even == item.even; } @Override public int hashCode() { return Objects.hash(odd, even); } } static int lowest(int[] arr, int x) { int start = 0, end = arr.length - 1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr[mid] <= x) { start = mid + 1; } else { ans = mid; end = mid - 1; } } return ans; } static int highest(int[] arr, int x) { int start = 0, end = arr.length - 1; int ans = -1; while (start <= end) { int mid = start + (end - start) / 2; if (arr[mid] >= x) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } 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()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] nextArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } Integer[] nextArray(int n, boolean object) { Integer[] arr = new Integer[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
4393cdcee863ec46d162a6bade0c1215
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.Random; import java.util.Stack; import java.util.concurrent.ThreadLocalRandom; public class AquaMoonAndStrangeSort { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pr=new PrintWriter(System.out); int t=Integer.parseInt(br.readLine()); while(t!=0){ solve(br,pr); t--; } pr.flush(); pr.close(); } public static void solve(BufferedReader br,PrintWriter pr) throws IOException{ int n=Integer.parseInt(br.readLine()); String[] temp=br.readLine().split(" "); int[] nums=new int[n]; int max=0; for(int i=0;i<n;i++){ nums[i]=Integer.parseInt(temp[i]); max=Math.max(max,nums[i]); } int[][] count=new int[max+1][2]; for(int i=0;i<n;i++){ int num=nums[i]; if(i%2==0){ count[num][0]++; } else{ count[num][1]++; } } shuffleArray(nums); Arrays.sort(nums); for(int i=0;i<n;i++){ int num=nums[i]; if(i%2==0){ count[num][0]--; } else{ count[num][1]--; } } for(int i=0;i<=max;i++){ if(count[i][0]!=0||count[i][1]!=0){ pr.println("NO"); return; } } pr.println("YES"); } public static void shuffleArray(int[] arr) { Random rnd = ThreadLocalRandom.current(); for (int i = arr.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); if(index!=i){ arr[index]^=arr[i]; arr[i]^=arr[index]; arr[index]^=arr[i]; } } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
edeb6a9bbc8ec166745caa13d0066a21
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.util.*; public class Main { static BufferedReader bf; static PrintWriter out; static Scanner sc; static StringTokenizer st; static long mod = (long)(1e9+7); static long mod2 = 998244353; public static void main (String[] args)throws IOException { bf = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new Scanner(System.in); // solve(); int t = nextInt(); while(t-->0){ solve(); } } public static void solve() throws IOException{ int n = nextInt(); Integer[]arr = new Integer[n]; Map<Integer,int[]>map = new HashMap<>(); for(int i =0 ;i<n;i++){ arr[i] = nextInt(); if(!map.containsKey(arr[i])){ int[]cur = new int[2]; cur[i%2]++; map.put(arr[i],cur); } else{ map.get(arr[i])[i%2]++; } } Arrays.sort(arr); Map<Integer,int[]>map2 = new HashMap<>(); for(int i =0;i<n;i++){ if(!map2.containsKey(arr[i])){ int[]cur = new int[2]; cur[i%2]++; map2.put(arr[i],cur); } else{ map2.get(arr[i])[i%2]++; } } for(Map.Entry<Integer,int[]>items:map.entrySet()){ int []cur = items.getValue(); int key = items.getKey(); if(cur[0] != map2.get(key)[0] || cur[1] != map.get(key)[1]){ println("NO"); return; } } println("YES"); } public static void help(int[]arr,char[]signs,int l,int r){ if( l < r){ int mid = (l+r)/2; help(arr,signs,l,mid); help(arr,signs,mid+1,r); merge(arr,signs,l,mid,r); } } public static void merge(int[]arr,char[]signs,int l,int mid,int r){ int n1 = mid - l + 1; int n2 = r - mid; int[]a = new int[n1]; int []b = new int[n2]; char[]asigns = new char[n1]; char[]bsigns = new char[n2]; for(int i = 0;i<n1;i++){ a[i] = arr[i+l]; asigns[i] = signs[i+l]; } for(int i = 0;i<n2;i++){ b[i] = arr[i+mid+1]; bsigns[i] = signs[i+mid+1]; } int i = 0; int j = 0; int k = l; boolean opp = false; while(i<n1 && j<n2){ if(a[i] <= b[j]){ arr[k] = a[i]; if(opp){ if(asigns[i] == 'R'){ signs[k] = 'L'; } else{ signs[k] = 'R'; } } else{ signs[k] = asigns[i]; } i++; k++; } else{ arr[k] = b[j]; int times = n1 - i; if(times%2 == 1){ if(bsigns[j] == 'R'){ signs[k] = 'L'; } else{ signs[k] = 'R'; } } else{ signs[k] = bsigns[j]; } j++; opp = !opp; k++; } } while(i<n1){ arr[k] = a[i]; if(opp){ if(asigns[i] == 'R'){ signs[k] = 'L'; } else{ signs[k] = 'R'; } } else{ signs[k] = asigns[i]; } i++; k++; } while(j<n2){ arr[k] = b[j]; signs[k] = bsigns[j]; j++; k++; } } public static long binaryExpo(long base,long expo){ if(expo == 0)return 1; long val; if(expo%2 == 1){ val = (binaryExpo(base, expo-1)*base)%mod; } else{ val = binaryExpo(base, expo/2); val = (val*val)%mod; } return (val%mod); } public static boolean isSorted(int[]arr){ for(int i =1;i<arr.length;i++){ if(arr[i] < arr[i-1]){ return false; } } return true; } //function to find the topological sort of the a DAG public static boolean hasCycle(int[]indegree,List<List<Integer>>list,int n,List<Integer>topo){ Queue<Integer>q = new LinkedList<>(); for(int i =1;i<indegree.length;i++){ if(indegree[i] == 0){ q.add(i); topo.add(i); } } while(!q.isEmpty()){ int cur = q.poll(); List<Integer>l = list.get(cur); for(int i = 0;i<l.size();i++){ indegree[l.get(i)]--; if(indegree[l.get(i)] == 0){ q.add(l.get(i)); topo.add(l.get(i)); } } } if(topo.size() == n)return false; return true; } // function to find the parent of any given node with path compression in DSU public static int find(int val,int[]parent){ if(val == parent[val])return val; return parent[val] = find(parent[val],parent); } // function to connect two components public static void union(int[]rank,int[]parent,int u,int v){ int a = find(u,parent); int b= find(v,parent); if(a == b)return; if(rank[a] == rank[b]){ parent[b] = a; rank[a]++; } else{ if(rank[a] > rank[b]){ parent[b] = a; } else{ parent[a] = b; } } } // public static int findN(int n){ int num = 1; while(num < n){ num *=2; } return num; } // code for input public static void print(String s ){ System.out.print(s); } public static void print(int num ){ System.out.print(num); } public static void print(long num ){ System.out.print(num); } public static void println(String s){ System.out.println(s); } public static void println(int num){ System.out.println(num); } public static void println(long num){ System.out.println(num); } public static void println(){ System.out.println(); } public static int Int(String s){ return Integer.parseInt(s); } public static long Long(String s){ return Long.parseLong(s); } public static String[] nextStringArray()throws IOException{ return bf.readLine().split(" "); } public static String nextString()throws IOException{ return bf.readLine(); } public static long[] nextLongArray(int n)throws IOException{ String[]str = bf.readLine().split(" "); long[]arr = new long[n]; for(int i =0;i<n;i++){ arr[i] = Long.parseLong(str[i]); } return arr; } public static int[][] newIntMatrix(int r,int c)throws IOException{ int[][]arr = new int[r][c]; for(int i =0;i<r;i++){ String[]str = bf.readLine().split(" "); for(int j =0;j<c;j++){ arr[i][j] = Integer.parseInt(str[j]); } } return arr; } public static long[][] newLongMatrix(int r,int c)throws IOException{ long[][]arr = new long[r][c]; for(int i =0;i<r;i++){ String[]str = bf.readLine().split(" "); for(int j =0;j<c;j++){ arr[i][j] = Long.parseLong(str[j]); } } return arr; } static class pair{ int one; int two; pair(int one,int two){ this.one = one ; this.two =two; } } public static long gcd(long a,long b){ if(b == 0)return a; return gcd(b,a%b); } public static long lcm(long a,long b){ return (a*b)/(gcd(a,b)); } public static boolean isPalindrome(String s){ int i = 0; int j = s.length()-1; while(i<=j){ if(s.charAt(i) != s.charAt(j)){ return false; } i++; j--; } return true; } // these functions are to calculate the number of smaller elements after self public static void sort(int[]arr,int l,int r){ if(l < r){ int mid = (l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); smallerNumberAfterSelf(arr, l, mid, r); } } public static void smallerNumberAfterSelf(int[]arr,int l,int mid,int r){ int n1 = mid - l +1; int n2 = r - mid; int []a = new int[n1]; int[]b = new int[n2]; for(int i = 0;i<n1;i++){ a[i] = arr[l+i]; } for(int i =0;i<n2;i++){ b[i] = arr[mid+i+1]; } int i = 0; int j =0; int k = l; while(i<n1 && j < n2){ if(a[i] < b[j]){ arr[k++] = a[i++]; } else{ arr[k++] = b[j++]; } } while(i<n1){ arr[k++] = a[i++]; } while(j<n2){ arr[k++] = b[j++]; } } public static String next(){ while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(bf.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } static int nextInt() { return Integer.parseInt(next()); } static long nextLong() { return Long.parseLong(next()); } static double nextDouble() { return Double.parseDouble(next()); } static String nextLine(){ String str = ""; try { str = bf.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // use some math tricks it might help // sometimes just try to think in straightforward plan in A and B problems don't always complecate the questions with thinking too much differently // always use long number to do 10^9+7 modulo // if a problem is related to binary string it could also be related to parenthesis // *****try to use binary search(it is a very beautiful thing it can work in some of the very unexpected problems ) in the question it might work****** // try sorting // try to think in opposite direction of question it might work in your way // if a problem is related to maths try to relate some of the continuous subarray with variables like - > a+b+c+d or a,b,c,d in general // if the question is to much related to left and/or right side of any element in an array then try monotonic stack it could work. // in range query sums try to do binary search it could work // analyse the time complexity of program thoroughly // anylyse the test cases properly // if we divide any number by 2 till it gets 1 then there will be (number - 1) operation required // try to do the opposite operation of what is given in the problem //think about the base cases properly //If a question is related to numbers try prime factorisation or something related to number theory // keep in mind unique strings //you can calculate the number of inversion in O(n log n) // in a matrix you could sometimes think about row and cols indenpendentaly. // Try to think in more constructive(means a way to look through various cases of a problem) way. // observe the problem carefully the answer could be hidden in the given test cases itself. (A, B , C); // when we have equations like (a+b = N) and we have to find the max of (a*b) then the values near to the N/2 must be chosen as (a and b);
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
9388ddc8daa6484cc781922b37968fd7
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import java.io.*; public class codeforces1545A { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int numCases = Integer.parseInt(br.readLine()); while (numCases-->0) { int length = Integer.parseInt(br.readLine()); int [][] parity = new int[100001][2]; int [] arr = new int[length]; StringTokenizer st = new StringTokenizer(br.readLine()); for (int i = 0; i<length;i++) { arr[i] = Integer.parseInt(st.nextToken()); } for (int i = 0; i<length;i++) { parity[arr[i]][i%2]++; } Arrays.sort(arr); for (int i = 0; i<length;i++) { parity[arr[i]][i%2]--; } boolean bo = true; for (int i = 1; i<100001;i++) { if (parity[i][0]!=0 || parity[i][1]!=0) { bo = false; break; } } if (bo) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
0128a9fb82920d5bf4954984550a9305
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.util.*; public class Solution{ public static int INF = 0x3f3f3f3f; public static int mod = 1000000007; public static int mod1 = 998244353; public static void main(String args[]){ try { int t = fReader.nextInt(), loop = 0; while (loop < t) { loop++; solve(); } } catch (Exception e){} } static void solve(){ try { int n = fReader.nextInt(); int[] arr = new int[n]; int[][] cnt = new int[(int)1e5+1][2]; for(int i=0;i<n;i++){ arr[i] = fReader.nextInt(); cnt[arr[i]][i%2]++; } Arrays.sort(arr); for(int i=0;i<n;i++){ cnt[arr[i]][i%2]--; } for(int i=0;i<n;i++){ if((cnt[arr[i]][0] | cnt[arr[i]][1]) != 0){ System.out.println("NO"); return; } } System.out.println("YES"); } catch (Exception e){ e.printStackTrace(); } } public static void reverse(int[] array) { if (array != null) { int i = 0; for(int j = array.length - 1; j > i; ++i) { int tmp = array[j]; array[j] = array[i]; array[i] = tmp; --j; } } } public static boolean doubleEqual(double d1, double d2){ if(Math.abs(d1-d2) < 1e-6){ return true; } return false; } public static long fac(int n){ long ret = 1; while(n > 0){ ret = ret * n % mod1; n--; } return ret; } public static long qpow(int n, int m){ long n_ = n, ret = 1; while(m > 0){ if((m&1) == 1){ ret = ret * n_ % mod; } m >>= 1; n_ = n_ * n_ % mod; } return ret; } public static class fReader { public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); public static StringTokenizer tokenizer = new StringTokenizer(""); public static String next() throws IOException{ while(!tokenizer.hasMoreTokens()){ tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } public static int nextInt() throws IOException{ return Integer.parseInt(next()); } public static double nextDouble() throws IOException{ return Double.parseDouble(next()); } public static String nextLine() throws IOException{ return reader.readLine(); } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
8210a8d3fedcf90c9a479dff42852a7f
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import java.io.*; public class Solution { static FastScanner scr=new FastScanner(); // static Scanner scr=new Scanner(System.in); static PrintStream out=new PrintStream(System.out); static StringBuilder sb=new StringBuilder(); static class pair{ int x;int y; pair(int x,int y){ this.x=x;this.y=y; } } static class triplet{ int x; long y; int z; triplet(int x,long y,int z){ this.x=x; this.y=y; this.z=z; } } 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(); } long gcd(long a,long b){ if(b==0) { return a; } return gcd(b,a%b); } int[] sort(int a[]) { int multiplier = 1, len = a.length, max = Integer.MIN_VALUE; int b[] = new int[len]; int bucket[]; for (int i = 0; i < len; i++) if (max < a[i]) max = a[i]; while (max / multiplier > 0) { bucket = new int[10]; for (int i = 0; i < len; i++) bucket[(a[i] / multiplier) % 10]++; for (int i = 1; i < 10; i++) bucket[i] += (bucket[i - 1]); for (int i = len - 1; i >= 0; i--) b[--bucket[(a[i] / multiplier) % 10]] = a[i]; for (int i = 0; i < len; i++) a[i] = b[i]; multiplier *= 10; } return a; } long modPow(long base,long exp) { if(exp==0) { return 1; } if(exp%2==0) { long res=(modPow(base,exp/2)); return (res*res); } return (base*modPow(base,exp-1)); } int []reverse(int a[]){ int b[]=new int[a.length]; int index=0; for(int i=a.length-1;i>=0;i--) { b[index]=a[i]; } return b; } 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()); } } static HashMap<Character,ArrayList<Integer>>hm=new HashMap<>(); static void solve() { int n = scr.nextInt(); HashMap<Integer,ArrayList<Integer>>hm=new HashMap<>(); int []a=scr.readArray(n); int []odd=new int[(int)1e5+2]; int []even=new int[(int)1e5+2]; for(int i=0;i<n;i+=2) { even[a[i]]++; } for(int i=1;i<n;i++) { odd[a[i]]++; } Arrays.sort(a); boolean good=true; for(int i=0;i<n;i+=2) { if(even[a[i]]==0) { good=false; break; }else { even[a[i]]--; } } for(int i=1;i<n;i+=2) { if(odd[a[i]]==0) { good=false; }else { odd[a[i]]--; } } String s=(good)?"YES":"NO"; sb.append(s).append('\n'); } static int MAX = Integer.MAX_VALUE; static int MIN = Integer.MIN_VALUE; public static void main(String []args) { int t=scr.nextInt(); while(t-->0) { solve(); } // solve(); out.println(sb); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
f861371b8564d8e7f6a5b910807a3d92
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws Exception { (new Main()).run(); } BufferedReader in; PrintWriter out; StringTokenizer st = new StringTokenizer(""); void run() throws Exception { if ((new File("input.txt").exists())) { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } else { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } solve(); in.close(); out.close(); } void solve() throws Exception { int t = nextInt(); int M = 100001; Pair[] a = new Pair[M]; boolean[] b = new boolean[M]; for (int i = 0; i < M; i++) { a[i] = new Pair(); } for (int i = 0; i < t; i++) { int n = nextInt(); for (int j = 0; j < n; j++) { a[j].a = nextInt(); a[j].b = j; } Arrays.sort(a, 0, n); int head = 0; for (int j = 0; j < n; j++) { // System.err.println(a[j].a + " " + a[j].b); boolean cur = (Math.abs(a[j].b - j) & 1) == 1; if (j > 0 && (a[j].a != a[j - 1].a)) { if (head != 0) { break; } } if (head == 0) { if (cur) { b[head++] = true; } } else { if (b[head - 1] == cur) { head--; } else { b[head++] = cur; } } } if (head != 0) { out.println("NO"); } else { out.println("YES"); } } } String nextToken() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } int nextInt() throws Exception { return Integer.parseInt(nextToken()); } class Pair implements Comparable<Pair> { int a; int b; @Override public int compareTo(Pair o) { if (this.a == o.a) { return this.b - o.b; } return this.a - o.a; } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
86753d848a73d62da01a965406bce3db
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.Arrays; public class BubbleSort { public static void main(String[] args) { FastScanner sc = new FastScanner(); int testCases = sc.nextInt(); for(int i = 0 ; i < testCases; i ++) { int len = sc.nextInt(); int[] arr = new int[len]; for(int j = 0 ; j < len; j++) { arr[j] = sc.nextInt(); } if(sortArray(arr)) { System.out.println("YES"); }else { System.out.println("NO"); } } } private static boolean sortArray(int[] arr) { int len = arr.length; int[] even = (len%2 == 0) ? new int[len/2] : new int[(len/2) + 1]; int[] odd = new int[len/2]; for(int i = 0; i < len; i++) { if(i%2 == 0) { even[i/2] = arr[i]; } else { odd[i/2] = arr[i]; } } Arrays.sort(arr); Arrays.sort(even);Arrays.sort(odd); int[] secondArr = new int[len]; for(int i = 0; i < len; i++) { if(i%2 == 0) { secondArr[i] = even[i/2]; } else { secondArr[i] = odd[i/2]; } } if(Arrays.equals(arr,secondArr)) return true; return false; } } class FastScanner { private final java.io.InputStream in = System.in; private final byte[] buffer = new byte[8192]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) return true; ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } return buflen > 0; } private byte readByte() { return hasNextByte() ? buffer[ptr++ ] : -1; } private static boolean isPrintableChar(byte c) { return 32 < c || c < 0; } private static boolean isNumber(int c) { return '0' <= c && c <= '9'; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++ ; return hasNextByte(); } public String next() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); byte b; while (isPrintableChar(b = readByte())) sb.appendCodePoint(b); return sb.toString(); } public final char nextChar() { if (!hasNext()) throw new java.util.NoSuchElementException(); return (char)readByte(); } public final long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; try { byte b = readByte(); if (b == '-') { while (isNumber(b = readByte())) n = n * 10 + '0' - b; return n; } else if (!isNumber(b)) throw new NumberFormatException(); do n = n * 10 + b - '0'; while (isNumber(b = readByte())); } catch (java.util.NoSuchElementException e) {} return n; } public final int nextInt() { if (!hasNext()) throw new java.util.NoSuchElementException(); int n = 0; try { byte b = readByte(); if (b == '-') { while (isNumber(b = readByte())) n = n * 10 + '0' - b; return n; } else if (!isNumber(b)) throw new NumberFormatException(); do n = n * 10 + b - '0'; while (isNumber(b = readByte())); } catch (java.util.NoSuchElementException e) {} return n; } public double nextDouble() { return Double.parseDouble(next()); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
729f9e93d949e9fbfba424213d96d011
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Arrays; import java.io.IOException; import java.lang.reflect.Field; import java.util.stream.Collectors; import java.nio.charset.StandardCharsets; import java.io.UncheckedIOException; import java.util.List; import java.util.stream.Stream; import java.util.Map; import java.io.Closeable; import java.util.Comparator; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 29); thread.start(); thread.join(); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInput in = new FastInput(inputStream); FastOutput out = new FastOutput(outputStream); AAquaMoonAndStrangeSort solver = new AAquaMoonAndStrangeSort(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } } static class AAquaMoonAndStrangeSort { int L = (int) 1e5; IntegerBIT bit = new IntegerBIT(L); public void solve(int testNumber, FastInput in, FastOutput out) { int n = in.ri(); Item[] items = new Item[n]; for (int i = 0; i < n; i++) { items[i] = new Item(); items[i].index = i; items[i].val = in.ri(); } bit.clear(); for (Item item : items) { item.flip += bit.query(item.val + 1, L); bit.update(item.val, 1); } SequenceUtils.reverse(items); bit.clear(); for (Item item : items) { item.flip += bit.query(item.val - 1); bit.update(item.val, 1); } for (Item item : items) { item.flip &= 1; } Map<Integer, List<Item>> groupByVal = Arrays.stream(items).collect(Collectors.groupingBy(x -> x.val)); int[] oe = new int[2]; for (List<Item> list : groupByVal.values()) { list.sort(Comparator.comparingInt(x -> x.index)); Arrays.fill(oe, 0); for (int i = 0; i < list.size(); i++) { oe[i & 1] += list.get(i).flip; } if (oe[0] != oe[1]) { out.println("NO"); return; } } out.println("YES"); } } static class SequenceUtils { public static <T> void swap(T[] data, int i, int j) { T tmp = data[i]; data[i] = data[j]; data[j] = tmp; } public static <T> void reverse(T[] data) { reverse(data, 0, data.length - 1); } public static <T> void reverse(T[] data, int l, int r) { while (l < r) { swap(data, l, r); l++; r--; } } } static class IntegerBIT { private int[] data; private int n; public IntegerBIT(int n) { this.n = n; data = new int[n + 1]; } public int query(int i) { i = Math.min(i, n); int sum = 0; for (; i > 0; i -= i & -i) { sum += data[i]; } return sum; } public int query(int l, int r) { return query(r) - query(l - 1); } public void update(int i, int mod) { if (i <= 0) { return; } for (; i <= n; i += i & -i) { data[i] += mod; } } public void clear(int n) { this.n = n; Arrays.fill(data, 1, n + 1, 0); } public void clear() { clear(n); } public String toString() { StringBuilder builder = new StringBuilder(); for (int i = 1; i <= n; i++) { builder.append(query(i) - query(i - 1)).append(' '); } return builder.toString(); } } static class Item { int index; int flip; int val; } static class FastInput { private final InputStream is; private StringBuilder defaultStringBuf = new StringBuilder(1 << 13); private byte[] buf = new byte[1 << 13]; private int bufLen; private int bufOffset; private int next; public FastInput(InputStream is) { this.is = is; } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { bufLen = -1; } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public String next() { return readString(); } public int ri() { return readInt(); } public int readInt() { boolean rev = false; skipBlank(); if (next == '+' || next == '-') { rev = next == '-'; next = read(); } int val = 0; while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } return rev ? val : -val; } public String readString(StringBuilder builder) { skipBlank(); while (next > 32) { builder.append((char) next); next = read(); } return builder.toString(); } public String readString() { defaultStringBuf.setLength(0); return readString(defaultStringBuf); } } static class FastOutput implements AutoCloseable, Closeable, Appendable { private static final int THRESHOLD = 32 << 10; private OutputStream writer; private StringBuilder cache = new StringBuilder(THRESHOLD * 2); private static Field stringBuilderValueField; static { try { stringBuilderValueField = StringBuilder.class.getSuperclass().getDeclaredField("value"); stringBuilderValueField.setAccessible(true); } catch (Exception e) { stringBuilderValueField = null; } } public FastOutput append(CharSequence csq) { cache.append(csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append(csq, start, end); return this; } private void afterWrite() { if (cache.length() < THRESHOLD) { return; } flush(); } public FastOutput(OutputStream writer) { this.writer = writer; } public FastOutput append(char c) { cache.append(c); afterWrite(); return this; } public FastOutput append(String c) { cache.append(c); afterWrite(); return this; } public FastOutput println(String c) { return append(c).println(); } public FastOutput println() { return append('\n'); } public FastOutput flush() { try { if (stringBuilderValueField != null) { try { byte[] value = (byte[]) stringBuilderValueField.get(cache); writer.write(value, 0, cache.length()); } catch (Exception e) { stringBuilderValueField = null; } } if (stringBuilderValueField == null) { //slow writer.write(cache.toString().getBytes(StandardCharsets.UTF_8)); // writer.append(cache); } writer.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { writer.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
67f0014b7bf5fc1526cc50fbafa2c9ec
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
/* stream Butter! eggyHide eggyVengeance I need U xiao rerun when */ import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; public class x1545A { public static void main(String hi[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int T = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); while(T-->0) { st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); int[] arr = readArr(N, infile, st); int[] oddcnt = new int[100005]; int[] evencnt = new int[100005]; for(int i=0; i < N; i+=2) evencnt[arr[i]]++; for(int i=1; i < N; i++) oddcnt[arr[i]]++; sort(arr); String res = "YES"; for(int i=0; i < N; i+=2) { //process evens if(evencnt[arr[i]] == 0) res = "NO"; else evencnt[arr[i]]--; } for(int i=1; i < N; i+=2) { //process odds if(oddcnt[arr[i]] == 0) res = "NO"; else oddcnt[arr[i]]--; } sb.append(res+"\n"); } System.out.print(sb); } public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception { int[] arr = new int[N]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } public static void sort(int[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort 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); } } /* 1 2 2 4 3 */
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output