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
ba7e4cad8f930ef108b565502cbdbfa3
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.*; public class Regular { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String s = scan.nextLine(); Stack<Character> list = new Stack<>(); int b = 0; for(int i = 0;i < s.length();i++) if(s.charAt(i) == '(') list.push(s.charAt(i)); else if(s.charAt(i) == ')' && list.size() != 0) { b++; list.pop(); } System.out.println(b * 2); scan.close(); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 11
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
fbaacce71931a5f2d6ed9ea618941e4f
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.Scanner; import java.io.BufferedReader; import java.io.*; import java.io.InputStream; public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); String str = input.nextLine(); int openningBraket=0,ans=0; for(int i=0;i<str.length();i++){ if(str.charAt(i)=='('){ openningBraket++; }else if(openningBraket>0){ ans+=2; openningBraket--; } } System.out.println(ans); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 11
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
ce85502885e581bcce2a4c44da9e88a0
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String [] args) { FastReader in=new FastReader(); String s=in.next(); int count=0; int total=0; for(int i=0;i<s.length();i++){ if(s.charAt(i) == '(') count++; else{ if(count!=0){ count--; total+=2; } } } System.out.println(total); } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 11
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
c66927d02a77db6fe4b12dc9da501bdb
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Main { public static void main (String[]args) throws IOException { Scanner in = new Scanner(System.in); try (PrintWriter or = new PrintWriter(System.out)) { char[]a=in.next().toCharArray(); Stack<Integer>stack=new Stack<>(); int ans=0; for (int i = 0; i < a.length; i++) { if (a[i]=='(')stack.push(1); else{ if (!stack.isEmpty()){ stack.pop(); ans+=2; } } } or.println(ans); } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) { if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) { f *= 10; } } } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } } class Pair implements Comparable<Pair> { int first, second; public Pair(int first, int second) { this.first = first; this.second = second; } @Override public int compareTo(Pair o) { if (first!=o.first)return first-o.first; return second-o.second; } } class Tempo { int first,second,third; public Tempo(int first, int second, int third) { this.first = first; this.second = second; this.third = third; } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 11
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
a8effa3570d951f38b8dfa18b0eb7b04
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.io.*; import java.util.*; public class MyClass { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String args[]) { FastReader fr = new FastReader(); char[] str = fr.nextLine().toCharArray(); int res = 0, cnt = 0; for (char ch : str) { if (ch == '(') cnt += 1; else if (cnt > 0) { cnt -= 1; res += 2; } } System.out.println(res); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 11
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
6708867283476f7448d122e4750570b9
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.*; public class Regular { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String s = scan.nextLine(); Stack<Character> list = new Stack<>(); int b = 0; for(int i = 0;i < s.length();i++) if(s.charAt(i) == '(') list.push(s.charAt(i)); else if(s.charAt(i) == ')' && list.size() != 0) { b++; list.pop(); } System.out.println(b * 2); scan.close(); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 11
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
c9cf6ecbb77e75b564db71ef870e4177
train_002.jsonl
1281970800
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?
256 megabytes
import java.util.Scanner; public class test { public static void main(String[] args) { int cot = 0; int total = 0; Scanner in = new Scanner(System.in); String t = in.next(); for (int i = 0; i < t.length(); i++) { if (t.charAt(i) == '(') { cot++; } else { if (cot !=0) { cot--; total += 2; } } } System.out.println(total); } }
Java
["(()))(", "((()())"]
5 seconds
["4", "6"]
null
Java 11
standard input
[ "greedy" ]
2ce2d0c4ac5e630bafad2127a1b589dd
Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
1,400
Output the maximum possible length of a regular bracket sequence.
standard output
PASSED
3336914947cf8ef53228c130f74345e8
train_002.jsonl
1301410800
Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n".You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.
256 megabytes
import java.util.*; import java.io.*; public class TakeExp { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while(t-->0) { String a= br.readLine(); int cnt=0,len=a.length(); String temp=""; if(a.length()>10) { for(int i=1;i<a.length()-1;i++) { cnt++; } temp=temp+cnt; StringBuilder sb=new StringBuilder(temp); sb.insert(0, a.charAt(0)); sb.append(a.charAt(len-1)); System.out.println(sb.toString()); } else { System.out.println(a); } } } // br.close(); }
Java
["4\nword\nlocalization\ninternationalization\npneumonoultramicroscopicsilicovolcanoconiosis"]
1 second
["word\nl10n\ni18n\np43s"]
null
Java 11
standard input
[ "strings" ]
6639d6c53951f8c1a8324fb24ef68f7a
The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.
800
Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.
standard output
PASSED
66f4da7458f4ac0d6ab2ca81f12c267f
train_002.jsonl
1301410800
Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n".You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int a = input.nextInt(); a++; for (int i=0;i<a;i++) { String b = input.nextLine(); if (b.length() > 10) { int len = b.substring(1, b.length() - 1).length(); System.out.println(b.substring(0, 1) + len + b.substring(b.length() - 1)); } else { System.out.println(b); } } } }
Java
["4\nword\nlocalization\ninternationalization\npneumonoultramicroscopicsilicovolcanoconiosis"]
1 second
["word\nl10n\ni18n\np43s"]
null
Java 11
standard input
[ "strings" ]
6639d6c53951f8c1a8324fb24ef68f7a
The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.
800
Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.
standard output
PASSED
707c5dab7e372b9d4ca7988c274e546d
train_002.jsonl
1301410800
Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n".You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.
256 megabytes
import java.util.Scanner; public class WayTooLongWords { public static void main(String[] args) { Scanner k = new Scanner(System.in); int counter = k.nextInt(); String x = k.nextLine(); for (int i = 0; i < counter; i++) { x = k.nextLine(); int n = x.length(); int l = x.length() - 2; if (x.length() > 10) { System.out.print(x.charAt(0)); System.out.print(l); System.out.println(x.charAt(n-1)); } else { System.out.println(x); } } } }
Java
["4\nword\nlocalization\ninternationalization\npneumonoultramicroscopicsilicovolcanoconiosis"]
1 second
["word\nl10n\ni18n\np43s"]
null
Java 11
standard input
[ "strings" ]
6639d6c53951f8c1a8324fb24ef68f7a
The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.
800
Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.
standard output
PASSED
5e88f0ec75006651c529c80da40c1fd2
train_002.jsonl
1301410800
Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n".You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.
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 c=0; String str=sc.next(); int n=str.length(); if(n<=10) { System.out.println(str); } else { char x=str.charAt(0); char y=str.charAt(n-1); for(int i=1;i<n-1;i++) { if(str.charAt(i)!='\0') { c++; } } //System.out.println(str.charAt(0) +c+ Str.charAt(n-1)); System.out.println(x+""+c+""+y); } } } }
Java
["4\nword\nlocalization\ninternationalization\npneumonoultramicroscopicsilicovolcanoconiosis"]
1 second
["word\nl10n\ni18n\np43s"]
null
Java 11
standard input
[ "strings" ]
6639d6c53951f8c1a8324fb24ef68f7a
The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.
800
Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.
standard output
PASSED
0b5ee689e103089d7b1219aeaa4389e9
train_002.jsonl
1301410800
Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n".You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); int testCase = Integer.parseInt(input.readLine()); String line; char first; char last; int n; String res; for(int i=0; i<testCase; i++) { line = input.readLine(); if(line.length() > 10){ n = line.length()-2; first = line.charAt(0); last = line.charAt(line.length() - 1); res = new StringBuilder().append(first).append(n).append(last).toString(); // n + // line.charAt(line.length() - 1)); System.out.println(res); }else{ System.out.println(line); } } } }
Java
["4\nword\nlocalization\ninternationalization\npneumonoultramicroscopicsilicovolcanoconiosis"]
1 second
["word\nl10n\ni18n\np43s"]
null
Java 11
standard input
[ "strings" ]
6639d6c53951f8c1a8324fb24ef68f7a
The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.
800
Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.
standard output
PASSED
6d41d6d09fb1b0949f53e9ae6468da12
train_002.jsonl
1301410800
Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n".You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.
256 megabytes
import java.util.*; import java.lang.*; public class Strings{ public static void main(String args[]){ String str; Scanner sc = new Scanner(System.in); int test_cases = sc.nextInt(); for(int i=0;i<test_cases;i++){ str = sc.next(); int n = str.length(); if(n<=10){ System.out.println(str); } else{ System.out.println(str.charAt(0)+""+(str.length()-2)+str.charAt(n-1)); } } } }
Java
["4\nword\nlocalization\ninternationalization\npneumonoultramicroscopicsilicovolcanoconiosis"]
1 second
["word\nl10n\ni18n\np43s"]
null
Java 11
standard input
[ "strings" ]
6639d6c53951f8c1a8324fb24ef68f7a
The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.
800
Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.
standard output
PASSED
0d3fa2bc83295614e6ff82f4263606ab
train_002.jsonl
1301410800
Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n".You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.
256 megabytes
import java.util.*; public class Solution{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int words= sc.nextInt(); sc.nextLine(); String[] arr = new String[words]; for(int i=0;i<words;i++){ arr[i]=sc.nextLine(); } for(int i=0;i<words;i++){ if(arr[i].length()>10){ StringBuilder sb = new StringBuilder(); sb.append(arr[i].charAt(0)); sb.append(arr[i].length()-2); sb.append(arr[i].charAt(arr[i].length()-1)); System.out.println(sb.toString()); } else{ System.out.println(arr[i]); } } } }
Java
["4\nword\nlocalization\ninternationalization\npneumonoultramicroscopicsilicovolcanoconiosis"]
1 second
["word\nl10n\ni18n\np43s"]
null
Java 11
standard input
[ "strings" ]
6639d6c53951f8c1a8324fb24ef68f7a
The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.
800
Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.
standard output
PASSED
be208f1a7a3a188ceb62579fb8cde922
train_002.jsonl
1301410800
Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n".You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.
256 megabytes
import java.util.Scanner; // Java solution for codeforces problem 71A. "Way too long words" // the program takes input of a series of words // if the word is "too long" it outputs the first and last letter with // the number of missing characters in between them. // otherwise it outputs just the word itself public class Problem71A { static Scanner sr = new Scanner(System.in); public static void main(String args[]) { double length = sr.nextInt(); String in = null; for(int i = 0; i <= length; i++) { in = sr.nextLine(); if(in.length() > 10) { System.out.print(in.charAt(0)); System.out.print(in.length() -2); System.out.println(in.charAt(in.length() -1)); }else System.out.println(in); } } }
Java
["4\nword\nlocalization\ninternationalization\npneumonoultramicroscopicsilicovolcanoconiosis"]
1 second
["word\nl10n\ni18n\np43s"]
null
Java 11
standard input
[ "strings" ]
6639d6c53951f8c1a8324fb24ef68f7a
The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.
800
Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.
standard output
PASSED
49e8f389b81d4c0c2341af6f36dcb972
train_002.jsonl
1301410800
Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n".You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.
256 megabytes
import java.util.*; public class java{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); while(n-->0){ String s = sc.nextLine(); int len = s.length(); if(len>10){ System.out.println(s.charAt(0)+Integer.toString(len-2)+s.charAt(len-1)); } else System.out.println(s); } } }
Java
["4\nword\nlocalization\ninternationalization\npneumonoultramicroscopicsilicovolcanoconiosis"]
1 second
["word\nl10n\ni18n\np43s"]
null
Java 11
standard input
[ "strings" ]
6639d6c53951f8c1a8324fb24ef68f7a
The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.
800
Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.
standard output
PASSED
812f1f44d36fb3fe4f79fe68431efedd
train_002.jsonl
1301410800
Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n".You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.
256 megabytes
import java.util.*; public class java{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); while(n-->0){ String s = sc.nextLine(); int l = s.length(); if(l>10){ String s1 = Integer.toString(l-2); System.out.print(s.charAt(0)+s1+s.charAt(l-1)); } else System.out.print(s); System.out.println(); } } }
Java
["4\nword\nlocalization\ninternationalization\npneumonoultramicroscopicsilicovolcanoconiosis"]
1 second
["word\nl10n\ni18n\np43s"]
null
Java 11
standard input
[ "strings" ]
6639d6c53951f8c1a8324fb24ef68f7a
The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.
800
Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.
standard output
PASSED
41509a4f9e570d156ce6d37cfa22f78a
train_002.jsonl
1301410800
Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n".You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); for(int i=0;i<n;i++){ String word = scn.next(); int l = word.length(); if(l>10) { word = word.charAt(0) +""+ (l-2) + word.charAt(l-1); System.out.println(word); }else { System.out.println(word); } } } }
Java
["4\nword\nlocalization\ninternationalization\npneumonoultramicroscopicsilicovolcanoconiosis"]
1 second
["word\nl10n\ni18n\np43s"]
null
Java 11
standard input
[ "strings" ]
6639d6c53951f8c1a8324fb24ef68f7a
The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.
800
Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.
standard output
PASSED
008376249ae0cf9607d9846199a34423
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.util.ArrayList; import java.util.Arrays; public class WeirdSortProblem { public static void main(String[] args) throws IOException { Problem p = new Problem() ; } } class Problem { ArrayList<ArrayList<Integer>> data = new ArrayList<>() ; Pares[] arrayStart ; boolean[] used; boolean helper = false ; Problem() throws IOException { QuickInput in = new QuickInput() ; int k = in.nextInt() ; for (int i = 0 ; i < k ; i ++) { int n = in.nextInt() ; int m = in.nextInt() ; data.clear(); for (int iiii = 0 ; iiii < n ; iiii++) { data.add(new ArrayList<Integer>()); } arrayStart = new Pares[n] ; for (int ii = 0 ; ii < arrayStart.length; ii ++) { int elem = in.nextInt() ; arrayStart[ii] = new Pares(elem, ii); } for (int iii = 0 ; iii < m ; iii ++) { int p = in.nextInt() ; data.get(p-1).add(p); data.get(p).add(p-1); } Arrays.sort(arrayStart); helper = false ; for (int j = 0 ; j < arrayStart.length ; j ++) { int start = arrayStart[j].position ; int finish = j ; used = new boolean[arrayStart.length]; dfs(start, finish); if (!helper) { System.out.println("NO"); break ; } if ((j== arrayStart.length-1)&&(helper)) { System.out.println("YES"); } helper = false ; } } } void dfs (int start, int finish) { if (start == finish ) { helper = true ; } used[start] = true ; for (int i = 0 ; i < arrayStart.length ; i ++) { if ((!used[i])&&(data.get(start).contains(i))) { dfs(i, finish); } } } static class Pares implements Comparable<Pares> { int elem ; int position ; Pares (int elem , int position) { this.elem = elem ; this.position = position ; } public String toString() { return "[elem : "+elem+", position : " + position + "]"; } @Override public int compareTo(Pares p) { if (elem < p.elem) return -1 ; if (elem > p.elem) return 1 ; return 0; } } } class QuickInput { QuickInput() throws IOException { stk = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); } int nextInt() throws IOException { stk.nextToken(); return (int)stk.nval; } double nextDouble() throws IOException { stk.nextToken(); return stk.nval; } String next() throws IOException { stk.nextToken(); return stk.sval; } StreamTokenizer stk; }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
7a48a3a84ab46a281fd9d0cf64e48e3c
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; /** * @author Nikita Gorokhov <wackloner@yandex-team.ru> */ public class B { private void solve() { int n = nextInt(), m = nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } int[] p = new int[m]; for (int i = 0; i < m; i++) { p[i] = nextInt() - 1; } for (int i = 0; i < n * n; i++) { for (int j = 0; j < m; j++) { int l = p[j], r = p[j] + 1; if (a[l] > a[r]) { int tmp = a[l]; a[l] = a[r]; a[r] = tmp; } } } boolean ok = true; for (int i = 0; i < n - 1; i++) { if (a[i] > a[i + 1]) { ok = false; } } out.println(ok ? "YES" : "NO"); } private final static String INPUT_FILENAME = "input.txt"; private final static boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; private InputStream inputStream; private PrintWriter out; private void run() throws FileNotFoundException { inputStream = ONLINE_JUDGE ? System.in : new FileInputStream(INPUT_FILENAME); out = new PrintWriter(System.out); int t = nextInt(); for (int testNum = 0; testNum < t; testNum++) { solve(); } out.flush(); } public static void main(String[] args) throws Exception { new B().run(); } private byte[] buffer = new byte[1024]; private int bufferLen = 0, bufferPtr = 0; private int readByte() { if (bufferLen == -1) { throw new InputMismatchException(); } if (bufferPtr >= bufferLen) { bufferPtr = 0; try { bufferLen = inputStream.read(buffer); } catch (IOException e) { throw new InputMismatchException(); } if (bufferLen <= 0) { return -1; } } return buffer[bufferPtr++]; } 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 nextDouble() { return Double.parseDouble(next()); } private char nextChar() { return (char) skip(); } private String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) { ; } if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) { ; } if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
cbb6000eba78699cc566db02f5d9160d
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.math.*; import java.io.*; import java.util.*; public class Codeforces{ static int binarySearch(int arr[],int value,int start,int end) { int mid=(start+end)/2; int result; if(start<=end) { if(arr[mid]==value) { return value; } else if(arr[mid]<value) { result=binarySearch(arr, value, mid+1, end); } else{ result=binarySearch(arr, value, start, mid-1); } } else{ return -1; } return result; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int gcd(Integer a,Integer b) { if(b==0) return a; else return gcd(b,a%b); } static boolean checkForPrime(int inputNumber) { boolean isItPrime = true; if(inputNumber <= 1) { isItPrime = false; return isItPrime; } else { if(inputNumber%2==0 && inputNumber==2) { return true; } else if(inputNumber%2==0) { return false; } else{ int check=(int)Math.sqrt(inputNumber); for (int i = 3; i<= check; i+=2) { if ((inputNumber % i) == 0) { isItPrime = false; break; } } } } return isItPrime; } public static void main(String args[]) throws Exception { FastReader sc=new FastReader(); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), "ASCII"), 512); int t=sc.nextInt(); for(int q=0;q<t;q++) { int n=sc.nextInt(); int m=sc.nextInt(); int arr[]=new int[n]; boolean b[]=new boolean[n]; int p[]=new int[m]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); b[i]=false; } for(int i=0;i<m;i++) { p[i]=sc.nextInt(); b[p[i]-1]=true; } Arrays.sort(p); boolean isAble=true; for(int i=0;i<n-1;i++) { for(int j=0;j<n-1-i;j++) { if(arr[j]>arr[j+1] && b[j]) { int temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } else if(arr[j]>arr[j+1] && !b[j]) { isAble=false; break; } } } if(isAble) { out.write("YES"); } else{ out.write("NO"); } out.write("\n"); out.flush(); } out.close(); } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
93f8c6b54dd6f7f079658eed224da600
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; public class A { static InputReader scn = new InputReader(System.in); static OutputWriter out = new OutputWriter(System.out); public static void main(String[] args) { int t = scn.nextInt(); while (t-- > 0) solve(); out.close(); } // 100 79 public static void solve() { int n = scn.nextInt(); int m = scn.nextInt(); int[] arr = readArray(n); int[] temparr = new int[n]; for (int i = 0; i < n; i++) { temparr[i] = arr[i]; } Arrays.sort(temparr); int[] p = readArray(m); int loop = 1000; while (loop > 0) { for (int i = 0; i < m; i++) { int a = p[i]; a--; if (arr[a] > arr[a + 1]) { int temp = arr[a]; arr[a] = arr[a + 1]; arr[a + 1] = temp; } } loop--; } // System.out.println(Arrays.toString(arr)); // System.out.println(Arrays.toString(temparr)); if (Arrays.equals(temparr, arr)) { out.println("YES"); } else { out.println("NO"); } } public static boolean check(int a, int b, int c, int d) { int[] arr = { a, b, c, d }; int odd = 0; int even = 0; for (int i = 0; i < 4; i++) { if (arr[i] % 2 == 0) { even++; } else { odd++; } } // System.out.println(even + " " + odd); if (even == 4 || even == 3 && odd == 1) { return true; } return false; } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } public static int lcm(int a, int b) { return (a * b) / gcd(a, b); } public static boolean isPrime(long n) { if (n < 2) return false; if (n == 2 || n == 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; long sqrtN = (long) Math.sqrt(n) + 1; for (long i = 6L; i <= sqrtN; i += 6) { if (n % (i - 1) == 0 || n % (i + 1) == 0) return false; } return true; } public static HashMap<Integer, Integer> CountFrequencies(int[] arr) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < arr.length; i++) { int a = arr[i]; if (map.containsKey(a)) { map.put(a, map.get(a) + 1); } else { map.put(a, 1); } } return map; } public static int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = scn.nextInt(); } return a; } public static int countPrimeFactors(int n) { int count = 0; while (n % 2 == 0) { n = n / 2; count++; } for (int i = 3; i <= Math.sqrt(n); i = i + 2) { while (n % i == 0) { n = n / i; count++; } } if (n > 2) count++; return (count); } public static ArrayList<Integer> printKAlmostPrimes(int n) { ArrayList<Integer> list = new ArrayList<>(); for (int i = 1, num = 2; i <= n; num++) { if (countPrimeFactors(num) == 2) { if (num > n) { break; } list.add(num); i++; } } return list; } public static int[] SieveOfEratosthenes(int[] arr) { int n = arr.length; for (int i = 3; i < n; i = i + 2) { arr[i] = 1; } for (int i = 3; i < n; i = i + 2) { if (arr[i] == 1) { for (int j = i * i; j < n; j = j + i) { arr[j] = 0; } } } arr[2] = 1; arr[0] = arr[1] = 0; return arr; } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } } public static ArrayList<String> printPermutn(String str, String ans, ArrayList<String> list) { if (str.length() == 0) { list.add(ans); } for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); String ros = str.substring(0, i) + str.substring(i + 1); printPermutn(ros, ans + ch, list); } return list; } public static ArrayList<String> GetSubSequences(String s) { if (s.length() == 0) { ArrayList<String> br = new ArrayList<>(); br.add(""); return br; } char ch = s.charAt(0); String ms = s.substring(1); ArrayList<String> rr = GetSubSequences(ms); ArrayList<String> mr = new ArrayList<>(); int t = rr.size(); for (int i = 0; i < t; i++) { mr.add(rr.get(i)); mr.add(ch + rr.get(i)); } return mr; } public static int binarySearch(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; if (arr[mid] == x) return mid; if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); return binarySearch(arr, mid + 1, r, x); } return -1; } public static int firstOccurence(int array1[], int low, int high, int x, int n) { if (low <= high) { int mid = low + (high - low) / 2; if ((mid == 0 || x > array1[mid - 1]) && array1[mid] == x) return mid; else if (x > array1[mid]) return firstOccurence(array1, (mid + 1), high, x, n); else return firstOccurence(array1, low, (mid - 1), x, n); } return -1; } public static int lastOccurence(int array2[], int low, int high, int x, int n) { if (low <= high) { int mid = low + (high - low) / 2; if ((mid == n - 1 || x < array2[mid + 1]) && array2[mid] == x) return mid; else if (x < array2[mid]) return lastOccurence(array2, low, (mid - 1), x, n); else return lastOccurence(array2, (mid + 1), high, x, n); } return -1; } public static void quickSort(int[] arr, int lo, int hi) { if (lo >= hi) { return; } int mid = (lo + hi) / 2; int pivot = arr[mid]; int left = lo; int right = hi; while (left <= right) { while (arr[left] < pivot) { left++; } while (arr[right] > pivot) { right--; } if (left <= right) { int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; left++; right--; } } quickSort(arr, lo, right); quickSort(arr, left, hi); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return nextLine(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); // writer.print(1); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
d718aa7f3f863486b3c970bce726acb2
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class WeirdSort{ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static boolean checkSorted(int[] a) { for(int i=0; i<a.length-1; i++) if(a[i] > a[i+1]) return false; return true; } public static void main(String[] args){ FastReader s = new FastReader(); int t = s.nextInt(); for(int u = 0;u<t;u++){ Boolean possible = true; int n = s.nextInt(); int m = s.nextInt(); int[] a = new int[n+1]; int[] p = new int[m+1]; for(int i = 1;i<=n;i++){ a[i] = s.nextInt(); } for(int i = 1;i<=m;i++){ p[i] = s.nextInt(); } while(true){ int swaps = 0; for(int i = 1;i<=m;i++){ if(a[p[i]]>a[p[i]+1]){ int temp = a[p[i]]; a[p[i]] = a[p[i]+1]; a[p[i]+1] = temp; swaps++; } } boolean isSorted = checkSorted(a); if(isSorted){ break; } if(swaps==0){ possible = false; break; } } if(!possible){ System.out.println("NO"); }else{ System.out.println("YES"); } } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
1481855973c264471888d82419abdbe2
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.io.BufferedReader; import java.util.stream.IntStream; public class 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) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } void readLine() throws IOException { br.readLine(); } int[] readIntegerArray(int n) { int [] arr = new int[n]; for (int i =0; i < n; ++i) { arr[i] = nextInt(); } return arr; } long[] readLongArray(int n) { long [] arr = new long[n]; for (int i =0; i < n; ++i) { arr[i] = nextLong(); } return arr; } String[] readStringArray(int n) { String [] arr = new String[n]; for (int i =0; i < n; ++i) { arr[i] = next(); } return arr; } } public static void printIntArr(int[] arr, int n) { for (int i = 0; i < n; ++i) { System.out.print(arr[i] + " "); } System.out.println(); } public static void printStringArr(String[] arr, int n) { for (int i = 0; i < n; ++i) { System.out.print(arr[i] + " "); } System.out.println(); } public static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static void fill(int n, HashSet<Integer> set) { for (int i = 1; i <=n; ++i) { set.add(i); } } public static void solve(int n, int m, int[] a, int[] pos) { boolean [] swapAble = new boolean[n]; for (int i = 0; i < m; ++i) { swapAble[pos[i]-1] = true; } boolean [] flag = new boolean[1]; flag[0] = true; qsort(a, 0, a.length-1, swapAble, flag); if (flag[0]) { System.out.println("YES"); } } private static void qsort(int[] a, int l, int r, boolean [] swapAble, boolean [] flag) { if (l >= r) { return; } int pivot = partition(l, r, a, swapAble); if (pivot < 0) { System.out.println("NO"); flag[0] = false; return; } qsort(a, pivot + 1, r, swapAble, flag); qsort(a, l, pivot, swapAble, flag); } private static int partition(int l, int r, int[] a, boolean[] swapAble) { int pivotVal = a[l]; int pos = l; for (int i = l + 1; i <= r; ++i) { if (a[i] < pivotVal) { for (int j = i-1; j >= pos; --j) { if (!swapAble[j]) { return -1; } } int temp = a[pos+1]; a[pos+1] = a[i]; a[i] = temp; a[pos] = a[pos+1]; a[pos+1] = pivotVal; ++pos; } } return pos; } public static void main(String [] args) throws IOException { FastScanner fs = new FastScanner(); int tests = fs.nextInt(); for (int t = 0; t < tests; ++t) { int n = fs.nextInt(); int m = fs.nextInt(); int [] a = fs.readIntegerArray(n); int [] pos = fs.readIntegerArray(m); solve(n , m , a, pos); } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
67479b43834d5bba45181fcf5d2c3fea
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
/* package codechef; // don't place package name! */ import java.math.BigInteger; import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { static void fun(boolean l,int n, int a[],int m,int p[]){ for(int i=1;i<=n-1;++i){ if(a[i]>a[i+1]){ boolean b=false; for(int j=1;j<=m;++j){ if(p[j]==i){ b=true; int tt=a[i]; a[i]=a[i+1]; a[i+1]=tt; i-=2; break; } } if(!b){ System.out.println("NO"); l=false; break; } } if(!l){ break; } } if(l) System.out.println("YES"); } public static void main (String[] args) throws java.lang.Exception { // your code goes here BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while(t-- >0){ //long n=Long.parseLong(br.readLine()); //int n=Integer.parseInt(br.readLine()); String ss[]=br.readLine().split(" "); int n=Integer.parseInt(ss[0]); int m=Integer.parseInt(ss[1]); String s[]=br.readLine().split(" "); int a[]=new int[n+1]; for(int i=1;i<=n;++i){ a[i]=Integer.parseInt(s[i-1]); } String sa[]=br.readLine().split(" "); int p[]=new int[m+1]; for(int i=1;i<=m;++i){ p[i]=Integer.parseInt(sa[i-1]); } boolean l=true; fun(l,n,a,m,p); } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
79715d0e8aeca0d1929896334e0bfbc1
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; public class B1311 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); sc.nextLine(); List<String> answers = new ArrayList<>(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); int m = sc.nextInt(); sc.nextLine(); List<Integer> list = Arrays.stream(sc.nextLine().trim().split(" ")) .map(Integer::valueOf).collect(Collectors.toList()); List<Integer> indexList = Arrays.stream(sc.nextLine().trim().split(" ")) .map(s -> Integer.valueOf(s) - 1).sorted().collect(Collectors.toList()); String answer = calc(0, 0, 0, list, indexList, 0); answers.add(answer); } for (String s : answers) { System.out.println(s); } } private static String calc(int unsortedVal, int sortedVal, int currentIndex, List<Integer> list, List<Integer> indexes, int lastSortedBlockMaxVal) { if (currentIndex >= list.size()) { return "YES"; } if (indexes.contains(currentIndex) || indexes.contains(currentIndex - 1)) { if (!indexes.contains(currentIndex - 1)) { lastSortedBlockMaxVal = sortedVal; sortedVal = list.get(currentIndex); if (sortedVal < lastSortedBlockMaxVal) { return "NO"; } } sortedVal = Math.max(sortedVal, list.get(currentIndex)); if (list.get(currentIndex) < unsortedVal || list.get(currentIndex) < lastSortedBlockMaxVal) { return "NO"; } if (sortedVal < unsortedVal) { return "NO"; } } else { int currentValue = list.get(currentIndex); if (currentValue < unsortedVal || currentValue < sortedVal) { return "NO"; } unsortedVal = currentValue; } return calc(unsortedVal, sortedVal, ++currentIndex, list, indexes, lastSortedBlockMaxVal); } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
bad70882ffff74b5470584c313f3cee7
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.HashSet; import java.util.Scanner; import java.util.Set; /** * Built using CHelper plug-in * Actual solution is at the top * * @author ky112233 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class TaskB { public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); Set<Integer> set = new HashSet<>(); for (int i = 0; i < m; i++) set.add(in.nextInt()); for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1 - i; j++) { if (a[j] > a[j + 1]) { if (set.contains(j + 1)) { int temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } else { out.println("NO"); return; } } } } out.println("YES"); } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
876a6486578417f611f59e9b856e672c
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; public class WierdSort { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); outer: while(t--!=0) { int n=sc.nextInt(),m=sc.nextInt(); int a[]=new int[n];int sor[]=new int[n]; int b[]=new int[100]; for(int i=0;i<n;i++) { a[i]=sc.nextInt();sor[i]=a[i]; } for(int i=0;i<m;i++) { int x=sc.nextInt(); b[x-1]=1; } for(int i=0; i<n-1;i++) { for(int j=0; j<n-i-1;j++) { if(a[j]>a[j+1]) { if(b[j]==1) { int temp =a[j]; a[j]= a[j+1]; a[j+1]= temp; } else { System.out.println("NO"); continue outer; } } } } System.out.println("YES"); } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
8e9168ec012907ee4638bab5f1dad486
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.*; public class recc1311b { static BufferedReader __in; static PrintWriter __out; static StringTokenizer input; public static void main(String[] args) throws IOException { __in = new BufferedReader(new InputStreamReader(System.in)); __out = new PrintWriter(new OutputStreamWriter(System.out)); int t = ri(); next: while(t --> 0) { r(); int n = ni(), m = ni(), a[] = ria(n), p[] = riam1(m); sort(p); for(int i = 0; i < n; ++i) { for(int j = 0; j < m; ++j) { if(a[p[j]] > a[p[j] + 1]) { int swap = a[p[j]]; a[p[j]] = a[p[j] + 1]; a[p[j] + 1] = swap; } } } for(int i = 0; i < n - 1; ++i) { if(a[i] > a[i + 1]) { prN(); continue next; } } prY(); } close(); } static class Node { List<Node> nei = new ArrayList<>(); int i; Node(int i_) { i = i_; } boolean connected(int x, Node par) { if(i == x) { return true; } for(Node n : nei) { if(n != par) { if(n.connected(x, this)) { return true; } } } return false; } } // references // IBIG = 1e9 + 7 // IRAND ~= 3e8 // IMAX ~= 2e10 // LMAX ~= 9e18 // constants static final int IBIG = 1000000007; static final int IRAND = 327859546; static final int IMAX = 2147483647; static final int IMIN = -2147483648; static final long LMAX = 9223372036854775807L; static final long LMIN = -9223372036854775808L; // util static int minof(int a, int b, int c) {return min(a, min(b, c));} static int minof(int... x) {return x.length == 1 ? x[0] : x.length == 2 ? min(x[0], x[1]) : min(x[0], minstarting(1, x));} static int minstarting(int offset, int... x) {assert x.length > 2; return offset == x.length - 2 ? min(x[offset], x[offset + 1]) : min(x[offset], minstarting(offset + 1, x));} static long minof(long a, long b, long c) {return min(a, min(b, c));} static long minof(long... x) {return x.length == 1 ? x[0] : x.length == 2 ? min(x[0], x[1]) : min(x[0], minstarting(1, x));} static long minstarting(int offset, long... x) {assert x.length > 2; return offset == x.length - 2 ? min(x[offset], x[offset + 1]) : min(x[offset], minstarting(offset + 1, x));} 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 floori(double d) {return (int)d;} static int ceili(double d) {return (int)ceil(d);} static long floorl(double d) {return (long)d;} static long ceill(double d) {return (long)ceil(d);} // input static void r() throws IOException {input = new StringTokenizer(__in.readLine());} static int ri() throws IOException {return Integer.parseInt(__in.readLine());} static long rl() throws IOException {return Long.parseLong(__in.readLine());} static int[] ria(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()); return a;} static int[] riam1(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()) - 1; return a;} static long[] rla(int n) throws IOException {long[] a = new long[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Long.parseLong(input.nextToken()); return a;} static char[] rcha() throws IOException {return __in.readLine().toCharArray();} static String rline() throws IOException {return __in.readLine();} static int ni() {return Integer.parseInt(input.nextToken());} static long nl() {return Long.parseLong(input.nextToken());} // output static void pr(int i) {__out.print(i);} static void prln(int i) {__out.println(i);} static void pr(long l) {__out.print(l);} static void prln(long l) {__out.println(l);} static void pr(double d) {__out.print(d);} static void prln(double d) {__out.println(d);} static void pr(char c) {__out.print(c);} static void prln(char c) {__out.println(c);} static void pr(char[] s) {__out.print(new String(s));} static void prln(char[] s) {__out.println(new String(s));} static void pr(String s) {__out.print(s);} static void prln(String s) {__out.println(s);} static void pr(Object o) {__out.print(o);} static void prln(Object o) {__out.println(o);} static void prln() {__out.println();} static void pryes() {__out.println("yes");} static void pry() {__out.println("Yes");} static void prY() {__out.println("YES");} static void prno() {__out.println("no");} static void prn() {__out.println("No");} static void prN() {__out.println("NO");} static void pryesno(boolean b) {__out.println(b ? "yes" : "no");}; static void pryn(boolean b) {__out.println(b ? "Yes" : "No");} static void prYN(boolean b) {__out.println(b ? "YES" : "NO");} static void prln(int... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), i++); __out.println(a[a.length - 1]);} static void prln(long... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), i++); __out.println(a[a.length - 1]);} static void flush() {__out.flush();} static void close() {__out.close();} // debug static void debug(String desc, int... a) {System.out.print(desc); System.out.print(": "); for(int i = 0, len = a.length - 1; i < len; System.out.print(a[i]), System.out.print(", "), i++); System.out.println(a[a.length - 1]);} static void debug(String desc, long... a) {System.out.print(desc); System.out.print(": "); for(int i = 0, len = a.length - 1; i < len; System.out.print(a[i]), System.out.print(", "), i++); System.out.println(a[a.length - 1]);} }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
798024c0a020106c899cce410a271846
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; /* Hepler Class Methods **only Few Imp Ones** * int[][] getIntMatrix(String s,int r,int c) //Works only for int and long * char getCharacterValue(int i) * int getIntegerValueOfCharacter(char c) * boolean isPerfectSquare(int n) // works only for int and long * int getLargestPrimeFactor(int n) //works only for int and long * boolean isPrime(int n) //works only for int and long * void printArray(int a[]), //works for all * void printMatrix(int a[][]), //works for all * void printNestedList(ArrayList<ArrayList<Integer>> a) //works for all * boolean isPalindrome //works for all except Character * T swap(T) //works only for int[],long[] and String * T getArraySum(T[]) //works only for int[],long[],double[] * T reverse(T) //works only for String,int and long * T[] getReverseArray(T[]) //works for all * HashSet<T> getHashSet(T[]) //works for int[],long[],char[],String[] * T setBit(T,k,side) //works only when n = int or long and k is always int **returns int or long** * String setBitString(T,k,side) //works only when n = int or long and k is always int **returns String** * int getSetBits(long n) //Gives Number of sets bits in a number * String cvtToBinary(T) * int cvtToDecimal(String n) //Always returns Long * boolean isPowerOf2(T) * boolean isSafe(T [][], row,col) //Returns whether current row and col are under bounds * T Log2(T num) //Returns (Math.log(num) / Math.log(2)) * long nCr(long n,long r) // Returns nCr value * long nCrMod(long n,long r) // Returns nCr % MOD value * */ public class A { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); static Helper sc = new Helper(br); static int MOD = 1000000007,INF = Integer.MAX_VALUE,NEG_INF = Integer.MIN_VALUE,SEM_INF = INF / 2; //BigInteger B = new BigInteger("1"); //Scanner scanner = new Scanner(System.in); public static void main(String args[]) { try { int t = sc.getInt(br.readLine()); while (t-- > 0) { testCase(); } out.flush(); }catch (Exception e){ System.out.println("Exception Occured: " + e.getMessage()); e.printStackTrace(); } } private static void testCase() throws Exception { int n = sc.nextInt(),m = sc.nextInt(); int a[] = sc.getIntArray(br.readLine()); int sortedArray[] = Arrays.copyOfRange(a,0,a.length);Arrays.parallelSort(sortedArray,0,n); int p[] = sc.getIntArray(br.readLine());Arrays.parallelSort(p,0,p.length); int vis[] = new int[n]; int ans = 0; int num = 1; int i = 0; for(int x = 0;x < m;x++) p[x]--; while(i < m) { ArrayList<Integer> buffer = new ArrayList<>(); while(i + 1 < m && p[i + 1] == p[i] + 1) { buffer.add(p[i]); i++; } buffer.add(p[i]);i++; for(int x = 0;x < buffer.size();x++) { vis[buffer.get(x)] = num; vis[buffer.get(x) + 1] = num; } num++; } i = 0; while(i < n) { if(vis[i] != 0) { int j = i; num = vis[j]; PriorityQueue<Integer> pq = new PriorityQueue<>(); while(j < n && vis[j] == num) { pq.offer(a[j]); j++; } int sz = pq.size(); while(i < n && !pq.isEmpty()) { if(sortedArray[i] != pq.poll()) { ans = -1; break; } i++; } if(ans == -1) break; }else { if(sortedArray[i] != a[i]) { ans = -1; break; } i++; } } writeln(ans == -1 ? "No" : "Yes"); } public static void writeln() throws Exception { out.write("\n"); } public static void write(Object o) throws Exception { out.write(String.valueOf(o)); } public static void writeln(Object o) throws Exception { out.write(String.valueOf(o) + "\n"); } public static void println() { System.out.println(); } public static void print(Object o) { System.out.print(String.valueOf(o)); } public static void println(Object o) { System.out.println(String.valueOf(o)); } static class Helper { FastReader fr; /*Constructor*/ public Helper(BufferedReader br) { try{ fr = new FastReader(br); }catch (Exception e){ System.out.println("Exception Occured: " + e.getMessage()); e.printStackTrace(); } } /* Inputs*/ public String next() throws Exception { return fr.next(); } public int nextInt() throws Exception { return fr.nextInt(); } public long nextLong() throws Exception { return fr.nextLong(); } public String trimLine() throws Exception { return fr.trimLine(); } public String rawLine() throws Exception { return fr.nextLine(); } public double nextDouble() throws Exception { return fr.nextDouble(); } public float nextFloat() throws Exception { return fr.nextFloat(); } public int [] getIntArray( String s) throws Exception { String input[] = s.split(" "); int res[] = new int[input.length]; for(int x = 0;x < res.length;x++) res[x] = getInt(input[x]); return res; } public long [] getLongArray( String s) throws Exception { String input[] = s.split(" "); long res[] = new long[input.length]; for(int x = 0;x < res.length;x++) res[x] = getLong(input[x]); return res; } public double [] getDoubleArray( String s) throws Exception { String input[] = s.split(" "); double res[] = new double[input.length]; for(int x = 0;x < res.length;x++) res[x] = getDouble(input[x]); return res; } public int[][] getIntMatrix(String s,int r,int c) { int i = 0;int mat[][] = new int[r][c]; String st[] = s.split(" "); for(int x = 0;x < r;x++) for(int y =0 ;y < c;y++) mat[x][y] = Integer.parseInt(st[i++]); return mat; } public long[][] getlongMatrix(String s,int r,int c) { int i = 0;long mat[][] = new long[r][c]; String st[] = s.split(" "); for(int x = 0;x < r;x++) for(int y =0 ;y < c;y++) mat[x][y] = Long.parseLong(st[i++]); return mat; } public int getInt(String s) { return Integer.parseInt(s); } public long getLong(String s) { return Long.parseLong(s); } public float getFloat(String s) { return Float.parseFloat(s); } public double getDouble(String s) { return Double.parseDouble(s); } /*Some basic hepler methods*/ public char getCharacterValue(int i) { return (char)i; } public int getIntegerValueOfCharacter(char c) { return (int)c; } public int Log2(int num) { return (int)(Math.log(num) / Math.log(2)); } public long Log2(long num) { return (long) (Math.log(num) / Math.log(2)); } public double Log2(double num) { return (double) (Math.log(num) / Math.log(2)); } public long nCr(long n,long r) { if(r > n - r) r = n - r; long res = 1; for(long x = 0;x < r;x++) { res *= (n - x); res /= (x + 1); } return res; } public long nCrMod(long n,long r,long md) { if(r > n - r) r = n - r; long res = 1; for(long x = 0;x < r;x++) { res = (res * (n - x)) % md; res /= (x + 1); } return res % md; } public int getGCD(int a,int b) { if(b == 0) return a; return getGCD(b,a % b); } public long getGCD(long a,long b) { if(b == 0) return a; return getGCD(b,a % b); } public double getGCD(double a,double b) { if(b == 0) return a; return getGCD(b,a % b); } public float getGCD(float a,float b) { if(b == 0) return a; return getGCD(b,a % b); } public int getLCM(int a,int b) { return ((a * b) / getGCD(a,b)); } public long getLCM(long a,long b) { return ((a * b) / getGCD(a,b)); } public double getLCM(double a,double b) { return ((a * b) / getGCD(a,b)); } public float getLCM(float a,float b) { return ((a * b) / getGCD(a,b)); } public boolean isSafe(int a[][],int x,int y) { if(x >=0 && y >= 0 && x < a.length && y < a[0].length) return true; return false; } public boolean isSafe(long a[][],int x,int y) { if(x >=0 && y >= 0 && x < a.length && y < a[0].length) return true; return false; } public boolean isSafe(double a[][],int x,int y) { if(x >=0 && y >= 0 && x < a.length && y < a[0].length) return true; return false; } public boolean isSafe(char a[][],int x,int y) { if(x >=0 && y >= 0 && x < a.length && y < a[0].length) return true; return false; } public boolean isPerfectSquare(int n) { if(n == 0 || n == 1) return true; if(n == 2 || n == 3) return false; double d = Math.sqrt(n); return (d - Math.floor(d) == 0); } public boolean isPerfectSquare(long n) { if(n == 0 || n == 1) return true; if(n == 2 || n == 3) return false; double d = Math.sqrt(n); return (d - Math.floor(d) == 0); } public boolean isPowerOf2(long n) { return n != 0 && (n & (n - 1)) == 0; } public long fastPow(long n,long p) { long res = 1; while(p > 0){ if(p % 2 != 0) res = res * n; p = p / 2; n = n * n; } return res; } public long modPow(long n,long p,long md) { long res = 1; n = n % md; if(n == 0) return 0; while(p > 0){ if(p % 2 != 0) res = ((res % md) * (n % md)) % md; p = p / 2; n = ((n % md) * (n % md)) % md; } return (res % md); } public boolean isPalindrome(int n) { StringBuilder sb = new StringBuilder(n + ""); return (Integer.parseInt(sb.reverse().toString()) == n); } public boolean isPalindrome(long n) { StringBuilder sb = new StringBuilder(n + ""); return (Long.parseLong(sb.reverse().toString()) == n); } public boolean isPalindrome(String s) { StringBuilder sb = new StringBuilder(s + ""); return (sb.reverse().toString().equals(s)); } public int getSmallestPrimeFactor(int n) { if(n == 1 || n == 0) return n; if(n % 2 == 0) return 2; else if(n % 3 == 0) return 3; int pf = -1; for(int x = 3;x <= Math.sqrt(n);x += 2) if(n % x == 0) return x; return n; } public int getLargestPrimeFactor(int n) { int pf = -1; if(n == 1 || n == 2 || n == 3 || n == 0) return n; while(n % 2 == 0){ pf = 2; n /= 2; } for(int x = 3;x <= Math.sqrt(n);x += 2) while (n % x == 0){ pf = x; n /= x; } if(n > 2) pf = n; return pf; } public long getSmallestPrimeFactor(long n) { if(n == 1 || n == 0) return n; if(n % 2 == 0) return 2; else if(n % 3 == 0) return 3; for(long x = 3;x <= Math.sqrt(n);x += 2) if(n % x == 0) return x; return n; } public long getLargestPrimeFactor(long n) { long pf = -1; if(n == 1 || n == 2 || n == 3 || n == 0) return n; while(n % 2 == 0){ pf = 2; n /= 2; } for(long x = 3;x <= Math.sqrt(n);x += 2) while (n % x == 0){ pf = x; n /= x; } if(n > 2) pf = n; return pf; } public boolean isPrime(int n) { if(n == 0 || n == 1) return false; if(getLargestPrimeFactor(n) == n) return true; return false; } public boolean isPrime(long n) { if(n == 0 || n == 1) return false; if(getLargestPrimeFactor(n) == n) return true; return false; } public int getSetBits(long n) { int count = 0; while (n > 0){ count += n % 2; n /= 2; } return count; } public int setBit(int n,int k,String side) throws Exception { if(side.equalsIgnoreCase("r") || side.equalsIgnoreCase("right")){ return ((1 << k) | n); }else if(side.equalsIgnoreCase("l") || side.equalsIgnoreCase("left")){ return (int)cvtToDecimal(setBitString(n,k,"l")); }else throw new Exception("Unknown Side of shift! side must be l,left,r,right"); } public long setBit(long n,int k,String side) throws Exception { if(side.equalsIgnoreCase("r") || side.equalsIgnoreCase("right")){ return ((1 << k) | n); }else if(side.equalsIgnoreCase("l") || side.equalsIgnoreCase("left")){ return cvtToDecimal(setBitString(n,k,"l")); }else throw new Exception("Unknown Side of shift! side must be l,left,r,right"); } public String setBitString(int n,int k,String side) throws Exception { StringBuilder sb = new StringBuilder(cvtToBinary(n) + ""); if(side.equalsIgnoreCase("r") || side.equalsIgnoreCase("right")){ sb.setCharAt(sb.length() - 1 - k,'1'); }else if(side.equalsIgnoreCase("l") || side.equalsIgnoreCase("left")){ sb.setCharAt(k,'1'); }else throw new Exception("Unknown Side of shift! side must be l,left,r,right"); return sb.toString(); } public String setBitString(long n, int k,String side) throws Exception { StringBuilder sb = new StringBuilder(cvtToBinary(n) + ""); if(side.equalsIgnoreCase("r") || side.equalsIgnoreCase("right")){ sb.setCharAt(sb.length() - 1 - k,'1'); }else if(side.equalsIgnoreCase("l") || side.equalsIgnoreCase("left")){ sb.setCharAt(k,'1'); }else throw new Exception("Unknown Side of shift! side must be l,left,r,right"); return sb.toString(); } public long cvtToDecimal(String n) { String num = n; long dec_value = 0; // Initializing base value to 1, // i.e 2^0 long base = 1; int len = num.length(); for (int i = len - 1; i >= 0; i--) { if (num.charAt(i) == '1') dec_value += base; base = base * 2; } return dec_value; } public String cvtToBinary(int n) { if(n == 0 || n == 1) return "" + n; StringBuilder sb = new StringBuilder(); while (n > 1){ if(n % 2 == 0) sb.append(0); else sb.append(1); n /= 2; } if(n == 1) sb.append(1); return sb.reverse().toString(); } public String cvtToBinary(long n) { if(n == 0 || n == 1) return "" + n; StringBuilder sb = new StringBuilder(); while (n > 1){ if(n % 2 == 0) sb.append(0); else sb.append(1); n /= 2; } if(n == 1) sb.append(1); return sb.reverse().toString(); } /*Printing Arena*/ public void print(BufferedWriter out,Object s) throws Exception { out.write(String.valueOf(s)); } public void println(BufferedWriter out,Object s) throws Exception { out.write(String.valueOf(s)); } public void printArray(int a[],int s,int e,boolean nextLine) { if(e >= a.length || s < 0) throw new ArrayIndexOutOfBoundsException("Array Index Out Of Bounds " + "[" + s + ", " + e + "] Array Size: " + a.length); for(int x = s;x <= e;x++) System.out.print(String.valueOf(a[x]) + " "); if(nextLine) System.out.println(); } public void printArray(long a[],int s,int e,boolean nextLine) { if(e >= a.length || s < 0) throw new ArrayIndexOutOfBoundsException("Array Index Out Of Bounds " + "[" + s + ", " + e + "] Array Size: " + a.length); for(int x = s;x <= e;x++) System.out.print(String.valueOf(a[x]) + " "); if(nextLine) System.out.println(); } public void printArray(char a[],int s,int e,boolean nextLine) { if(e >= a.length || s < 0) throw new ArrayIndexOutOfBoundsException("Array Index Out Of Bounds " + "[" + s + ", " + e + "] Array Size: " + a.length); for(int x = s;x <= e;x++) System.out.print(String.valueOf(a[x]) + " "); if(nextLine) System.out.println(); } public void printArray(double a[],int s,int e,boolean nextLine) { if(e >= a.length || s < 0) throw new ArrayIndexOutOfBoundsException("Array Index Out Of Bounds " + "[" + s + ", " + e + "] Array Size: " + a.length); for(int x = s;x <= e;x++) System.out.print(String.valueOf(a[x]) + " "); if(nextLine) System.out.println(); } public void printArray(String a[],int s,int e,boolean nextLine) { if(e >= a.length || s < 0) throw new ArrayIndexOutOfBoundsException("Array Index Out Of Bounds " + "[" + s + ", " + e + "] Array Size: " + a.length); for(int x = s;x <= e;x++) System.out.print(String.valueOf(a[x]) + " "); if(nextLine) System.out.println(); } public void printArray(Object a[],int s,int e,boolean nextLine) { if(e >= a.length || s < 0) throw new ArrayIndexOutOfBoundsException("Array Index Out Of Bounds " + "[" + s + ", " + e + "] Array Size: " + a.length); for(int x = s;x <= e;x++) System.out.print(a[x].toString() + " "); if(nextLine) System.out.println(); } public void printMatrix(int a[][]) { for(int x = 0;x < a.length;x++){ for (int y = 0;y < a[0].length;y++) System.out.print(a[x][y] + " "); System.out.println(); } } public void printMatrix(long a[][]) { for(int x = 0;x < a.length;x++){ for (int y = 0;y < a[0].length;y++) System.out.print(String.valueOf(a[x][y]) + " "); System.out.println(); } } public void printMatrix(char a[][]) { for(int x = 0;x < a.length;x++){ for (int y = 0;y < a[0].length;y++) System.out.print(String.valueOf(a[x][y]) + " "); System.out.println(); } } public void printMatrix(double a[][]) { for(int x = 0;x < a.length;x++){ for (int y = 0;y < a[0].length;y++) System.out.print(String.valueOf(a[x][y]) + " "); System.out.println(); } } public void printMatrix(String a[][]) { for(int x = 0;x < a.length;x++){ for (int y = 0;y < a[0].length;y++) System.out.print(String.valueOf(a[x][y]) + " "); System.out.println(); } } public void printMatrix(Object a[][]) { for(int x = 0;x < a.length;x++){ for (int y = 0;y < a[0].length;y++) System.out.print(a[x][y].toString() + " "); System.out.println(); } } public void printIntNestedList(ArrayList<ArrayList<Integer>> a) { for(int x = 0;x < a.size();x++){ ArrayList<Integer> al = a.get(x); for(int y = 0;y < al.size();y++) System.out.print(String.valueOf(al.get(y)) + " "); System.out.println(); } } public void printLongNestedList(ArrayList<ArrayList<Long>> a) { for(int x = 0;x < a.size();x++){ ArrayList<Long> al = a.get(x); for(int y = 0;y < al.size();y++) System.out.print(String.valueOf(al.get(y)) + " "); System.out.println(); } } public void printCharNestedList(ArrayList<ArrayList<Character>> a) { for(int x = 0;x < a.size();x++){ ArrayList<Character> al = a.get(x); for(int y = 0;y < al.size();y++) System.out.print(String.valueOf(al.get(y)) + " "); System.out.println(); } } public void printStringNestedList(ArrayList<ArrayList<String>> a) { for(int x = 0;x < a.size();x++){ ArrayList<String> al = a.get(x); for(int y = 0;y < al.size();y++) System.out.print(String.valueOf(al.get(y)) + " "); System.out.println(); } } public String swap(String st,int i,int j) { StringBuilder sb = new StringBuilder(st); sb.setCharAt(i,st.charAt(j)); sb.setCharAt(j,st.charAt(i)); return sb.toString(); } public int [] swap(int a[],int i,int j) { int t = a[i]; a[i] = a[j]; a[j] = t; return a; } public long[] swap(long a[],int i,int j) { long t = a[i]; a[i] = a[j]; a[j] = t; return a; } public long getArraySum(int a[],int s,int e) { long sum = 0; if(e >= a.length || s < 0) throw new ArrayIndexOutOfBoundsException("Array Index Out Of Bounds " + "[" + e + ", " + s + "]"); for(int x = s;x <= e;x++) sum += a[x]; return sum; } public long getArraySum(long a[],int s,int e) { long sum = 0; if(e >= a.length || s < 0) throw new ArrayIndexOutOfBoundsException("Array Index Out Of Bounds " + "[" + e + ", " + s + "]"); for(int x = s;x <= e;x++) sum += a[x]; return sum; } public double getArraySum(double a[],int s,int e) { double sum = 0; if(e >= a.length || s < 0) throw new ArrayIndexOutOfBoundsException("Array Index Out Of Bounds " + "[" + e + ", " + s + "]"); for(int x = s;x <= e;x++) sum += a[x]; return sum; } public int reverse(int n) { StringBuilder sb = new StringBuilder(n + ""); return Integer.parseInt(sb.reverse().toString()); } public long reverse(long n) { StringBuilder sb = new StringBuilder(n + ""); return Long.parseLong(sb.reverse().toString()); } public String reverse(String s) { StringBuilder sb = new StringBuilder(s + ""); return sb.reverse().toString(); } public Object[] getReverseArray(Object a[]) { int i = 0,j = a.length - 1; while (i <= j) { Object o = a[i]; a[i] = a[j]; a[j] = o; i++;j--; } return a; } public int[] getReverseArray(int a[]) { int i = 0,j = a.length - 1; while (i <= j) { int o = a[i]; a[i] = a[j]; a[j] = o; i++;j--; } return a; } public double[] getReverseArray(double a[]) { int i = 0,j = a.length - 1; while (i <= j) { double o = a[i]; a[i] = a[j]; a[j] = o; i++;j--; } return a; } public char[] getReverseArray(char a[]) { int i = 0,j = a.length - 1; while (i <= j) { char o = a[i]; a[i] = a[j]; a[j] = o; i++;j--; } return a; } public long[] getReverseArray(long a[]) { int i = 0,j = a.length - 1; while (i <= j) { long o = a[i]; a[i] = a[j]; a[j] = o; i++;j--; } return a; } public String[] getReverseArray(String a[]) { int i = 0,j = a.length - 1; while (i <= j) { String o = a[i]; a[i] = a[j]; a[j] = o; i++;j--; } return a; } public HashSet<Integer> getHashSet(int a[]) { HashSet<Integer> set = new HashSet<>(); for(int x = 0;x < a.length;x++) set.add(a[x]); return set; } public HashSet<Long> getHashSet(long a[]) { HashSet<Long> set = new HashSet<>(); for(int x = 0;x < a.length;x++) set.add(a[x]); return set; } public HashSet<Character> getHashSet(char a[]) { HashSet<Character> set = new HashSet<>(); for(int x = 0;x < a.length;x++) set.add(a[x]); return set; } public HashSet<String> getHashSet(String a[]) { HashSet<String> set = new HashSet<>(); for(int x = 0;x < a.length;x++) set.add(a[x]); return set; } public int getMax(int a[]) { int max = Integer.MIN_VALUE; for(int x = 0;x < a.length;x++) max = Math.max(max,a[x]); return max; } public long getMax(long a[]) { long max = Long.MIN_VALUE; for(int x = 0;x < a.length;x++) max = Math.max(max,a[x]); return max; } public double getMax(double a[]) { double max = Double.MIN_VALUE; for(int x = 0;x < a.length;x++) max = Math.max(max,a[x]); return max; } public int getMin(int a[]) { int min = Integer.MAX_VALUE; for(int x = 0;x < a.length;x++) min = Math.min(min,a[x]); return min; } public long getMin(long a[]) { long min = Long.MAX_VALUE; for(int x = 0;x < a.length;x++) min = Math.min(min,a[x]); return min; } public double getMin(double a[]) { double min = Double.MAX_VALUE; for(int x = 0;x < a.length;x++) min = Math.min(min,a[x]); return min; } private class FastReader { BufferedReader br; StringTokenizer st; public FastReader(BufferedReader br) throws Exception { this.br = br; } public String next() throws Exception { if(st == null || !st.hasMoreElements()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public String trimLine() throws Exception { try{ return br.readLine().trim(); }catch (Exception e){ System.out.println("Exception Occured: " + e.getMessage()); e.printStackTrace(); return null; } } public String nextLine() throws Exception { try{ return br.readLine(); }catch (Exception e){ System.out.println("Exception Occured: " + e.getMessage()); e.printStackTrace(); return null; } } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public float nextFloat() throws Exception { return Float.parseFloat(next()); } } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
e5ce8f3068495eca4203601379188c74
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main { //static int p = 1000000007; static int p = 998244353; public static void main(String args[]) throws IOException { Scanner sc = new Scanner(); PrintWriter out = new PrintWriter(System.out); //Mod mod = new Mod(); //mod.precalc(); int t = 1; t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(), m=sc.nextInt(), arr[]=new int[n], pos[]=new int[n], arr2[]=new int[n-m+1], arr3[]=new int[n-m+1], k=0; for(int i=0; i<n; i++) arr[i]=sc.nextInt(); for(int i=0; i<m; i++) pos[sc.nextInt()-1]=1; int max=arr[0], min=arr[0]; for(int i=0; i<n; i++) { max=max(max, arr[i]); min = min(min, arr[i]); if(pos[i]==0) { arr2[k]=max; arr3[k]=min; max=Integer.MIN_VALUE; min=Integer.MAX_VALUE; k++; } } arr2[n-m]=max; arr3[n-m]=min; for(int i=0; i<n-m; i++) { if(arr2[i]>arr3[i+1]) { out.println("NO"); break; } if(i==n-m-1) out.println("YES"); } } out.flush(); } static int count(int x, int[] arr) { int count = 0; for (int i = 0; i < arr.length; i++) if (arr[i] == x) count++; return count; } public static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); sb.reverse(); return sb.toString(); } static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) { if (str.charAt(i) != str.charAt(j)) return false; i++; j--; } return true; } static void print(int[] arr) { for (int i = 0; i < arr.length; i++) System.out.print(arr[i] + " "); System.out.println(); } public static int abs(int x) { return ((x > 0) ? x : -x); } public static long abs(long x) { return ((x > 0) ? x : -x); } public static int max(int a, int b) { return Math.max(a, b); } public static long max(long a, long b) { return Math.max(a, b); } public static int min(int a, int b) { return Math.min(a, b); } public static long min(long a, long b) { return Math.min(a, b); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static int modInverse(int a, int m) { int g = gcd(a, m); if (g != 1) return -1; else return power(a, m - 2, m); } // To compute x^y under modulo m static int power(int x, int y, int m) { if (y == 0) return 1; int p = power(x, y / 2, m) % m; p = (p * p) % m; if (y % 2 == 0) return p; else return (x * p) % m; } static int[] primeGenerator(int num) { int length = 0, arr[] = new int[num], a = num, factor = 1; if (num % 2 == 0) { while (num % 2 == 0) { num /= 2; factor *= 2; } arr[length++] = factor; } for (int i = 3; i * i <= a; i++) { factor = 1; if (num % i == 0) { while (num % i == 0) { num /= i; factor *= i; } arr[length++] = factor; } } if (num > 1) arr[length++] = num; return Arrays.copyOfRange(arr, 0, length); } static boolean isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static class Scanner { BufferedReader br; StringTokenizer st; Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } Scanner(String fileName) throws FileNotFoundException { br = new BufferedReader(new FileReader(fileName)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine() throws IOException { return br.readLine(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(next()); } boolean ready() throws IOException { return br.ready(); } } static void sort(int[] a) { shuffle(a); Arrays.sort(a); } static void shuffle(int[] a) { int n = a.length; Random rand = new Random(); for (int i = 0; i < n; i++) { int tmpIdx = rand.nextInt(n); int tmp = a[i]; a[i] = a[tmpIdx]; a[tmpIdx] = tmp; } } } class Pair { int x; int y; Pair(int a, int b) { x = a; y = b; } void print() { System.out.println(this.x + " " + this.y); } } class Mod{ static final int MOD = 998244353; int add(int x, int y){ x += y; while(x >= MOD) x -= MOD; while(x < 0) x += MOD; return x; } int subtract(int x, int y) { x -= y; while(x >= MOD) x -= MOD; while(x < 0) x += MOD; return x; } int mul(int x, int y) { int result = (int)((long)x*y % MOD); return result>=0?result:result+MOD; } int binpow(int x, int y) { int z = 1; while(y!=0) { if((y & 1)!=0) z = mul(z, x); x = mul(x, x); y >>= 1; } return z; } int inv(int x) { return binpow(x, MOD - 2); } int divide(int x, int y) { return mul(x, inv(y)); } static final int N =200043; int[] fact; Mod() { fact = new int[N]; } void precalc() { fact[0] = 1; for(int i = 1; i < N; i++) fact[i] = mul(fact[i - 1], i); } int C(int n, int k) { return divide(fact[n], mul(fact[k], fact[n - k])); } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
1d9ef181047808cd533c47c903d73c92
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; public class Try4 { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++){ int n= sc.nextInt(); int m= sc.nextInt(); int[] a = new int[n+1]; int[] p = new int[m+1]; for(int j=1;j<n+1;j++) a[j]=sc.nextInt(); for(int j=1;j<m+1;j++) p[j]=sc.nextInt(); Arrays.sort(p); int[] b = new int[m]; int[] c = new int[m]; b[0]=p[1]; c[0]=p[1]+1; int index = 0; for(int j=2;j<m+1;j++){ if(p[j]==p[j-1]+1) { c[index]=p[j]+1; } else { index++; b[index]=p[j]; c[index]=p[j]+1; } } int[] min= new int[index+1]; int[] max = new int[index+1]; for(int j=0; j<=index; j++){ min[j] = mins(a,b[j],c[j]); max[j] = maxs(a,b[j],c[j]); } // for(int j=0; j<=index; j++){ // System.out.printf("%d %d\n",min[j],max[j]); // } int flag = 0; for(int j=0; j<index;j++){ if(max[j]>min[j+1]) { flag = 1; break; } } if(flag==1){ System.out.println("NO"); continue; } int temp_index = index; index = 0; //System.out.println("1"); for(int j=0; index<=temp_index;j++) { if(j==b[index]||j==n){ j=c[index]; index++; continue; } if((j<n&&a[j]>a[j+1])||a[j]>min[index]||(index>=1&&a[j]<max[index-1])){ flag=1; break; } } if(flag==1){ System.out.println("NO"); continue; } for(int j=c[temp_index]+1;j<n+1;j++) if((j<n&&a[j]>a[j+1])||a[j]<max[temp_index]) { flag = 1; break; } if(flag==1){ System.out.println("NO"); continue; } else System.out.println("YES"); } } private static int mins(int[] a,int s, int e) { int min = a[s]; for(int i=s+1;i<=e;i++) if(min>a[i]) min=a[i]; return min; } private static int maxs(int[] a,int s, int e) { int max = a[s]; for(int i=s+1;i<=e;i++) if(max<a[i]) max=a[i]; return max; } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
f4cdb4d970ccaca40eb68885a19387c5
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class Cf182 implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 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() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Cf182(),"Main",1<<27).start(); } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } // array sorting by colm static void sortbycolomn(int arr[][], int col) { Arrays.sort(arr, new Comparator<int[]>() { public int compare(final int[] entry1, final int[] entry2) { if (entry1[col] > entry2[col]) return 1; else return -1; } }); } // gcd public static long findGCD(long arr[], int n) { long result = arr[0]; for (int i = 1; i < n; i++) result = gcd(arr[i], result); return result; } // fibonaci static int fib(int n) { int a = 0, b = 1, c; if (n == 0) return a; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } // sort a string public static String sortString(String inputString) { char tempArray[] = inputString.toCharArray(); Arrays.sort(tempArray); return new String(tempArray); } public void run() { InputReader sc = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int t = sc.nextInt(); while(t--!=0) { int n = sc.nextInt(); int m = sc.nextInt(); int arr[] = new int[n]; HashMap<Integer,Integer> map = new HashMap<Integer,Integer>(); for(int i=0;i<n;i++) arr[i] = sc.nextInt(); for(int i=0;i<m;i++) map.put(sc.nextInt(),0); int i=0,j=0,key,ans=0; for (i = 1; i < n; i++) { key = arr[i]; j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; if(!map.containsKey(j+1)) { ans=1; break; } j = j - 1; } arr[j + 1] = key; } if(ans==0) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
77376f0df47a268630bb50c413ce1141
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; public class Power { static long power(int a,int b){ if(b==0) return 1; long pow=power(a,b/2); if(b%2!=0){ return pow*pow*a; } else{ return pow*pow; } } static long gcd(long a,long b){ if(b==0) return a; else return gcd(b,b%a); } static long lcm(long a,long b){ return (a*b)/gcd(a,b); } static List<Integer> primes(int n){ List<Integer> list=new ArrayList<Integer>(); boolean[] prime=new boolean[n+1]; Arrays.fill(prime,true); prime[0]=false; prime[1]=false; int sqrt=(int)Math.sqrt(n); for(int i=2;i<=sqrt;i++){ if(prime[i]){ for(int j=i*i;j<=n;j=j+i){ prime[j]=false; } } } for(int i=0;i<prime.length;i++){ if(prime[i]) list.add(i); } return list; } static long catalan(long n){ long result=0; if(n<=1) return 1; for(long i=0;i<n;i++) result+=catalan(i)*catalan(n-i-1); return result; } static long totient(long n){ long res=0; for(long i=1;i<=n;i++){ if(gcd(i,n)==1) res++; } return res; } /* static double ternarysearch(long low,long high,long[] a,long[] b,long result){ int t=500; while(t-->0 ){ long m1=low+(high-low)/3; long m2=high-(high-low)/3; long v1=ft(m1,a,b); long v2=ft(m2,a,b); result=Math.min(result,Math.min(v1,v2)); if(v1<result) result=v1; if(v2<result) result=v2; if(v1<v2){ high=m2; } else low=m1; } for(long i=low;i<=high;i++){ // if(ft(i,a,b)<result) // result=ft(i,a,b); } return result; }*/ static int countone(Integer[] one,int low,int high){ if(low<=high){ int mid=(low+high)/2; if((mid==high || one[mid+1]==0) && one[mid]==1) return mid+1; if(one[mid]==1){ return countone(one,mid+1,high); } else{ return countone(one,low,mid-1); } } return 0; } public static void main(String[] args) { Scanner input=new Scanner(System.in); int test=input.nextInt(); input.nextLine(); while(test-->0){ int n=input.nextInt(); int m=input.nextInt(); int[] arr=new int[n]; ArrayList<Integer> list=new ArrayList<Integer>(); for(int i=0;i<n;i++){ arr[i]=input.nextInt(); } for(int i=0;i<m;i++){ int x=input.nextInt(); list.add(x); } boolean b=true; boolean bb=true; while(bb){ bb=false; for(int j=0;j<arr.length-1;j++){ if(arr[j]>arr[j+1] && list.contains(j+1)){ int temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; bb=true; } } if(bb) continue; break; } for(int i=0;i<n-1;i++){ if(arr[i]<=arr[i+1]){ continue; } System.out.println("NO"); b=false; break; } if(b){ System.out.println("YES"); } } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
404f29867f2ac0186d692c8f70a7abf6
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.StringTokenizer; import java.io.FileReader; import java.io.BufferedReader; import java.io.PrintWriter; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++){ int n= sc.nextInt(); int m= sc.nextInt(); int[] a = new int[n+1]; int[] p = new int[m+1]; for(int j=1;j<n+1;j++) a[j]=sc.nextInt(); for(int j=1;j<m+1;j++) p[j]=sc.nextInt(); Arrays.sort(p); int[] b = new int[m]; int[] c = new int[m]; b[0]=p[1]; c[0]=p[1]+1; int index = 0; for(int j=2;j<m+1;j++){ if(p[j]==p[j-1]+1) { c[index]=p[j]+1; } else { index++; b[index]=p[j]; c[index]=p[j]+1; } } int[] min= new int[index+1]; int[] max = new int[index+1]; for(int j=0; j<=index; j++){ min[j] = mins(a,b[j],c[j]); max[j] = maxs(a,b[j],c[j]); } // for(int j=0; j<=index; j++){ // System.out.printf("%d %d\n",min[j],max[j]); // } int flag = 0; for(int j=0; j<index;j++){ if(max[j]>min[j+1]) { flag = 1; break; } } if(flag==1){ System.out.println("NO"); continue; } int temp_index = index; index = 0; //System.out.println("1"); for(int j=0; index<=temp_index;j++) { if(j==b[index]||j==n){ j=c[index]; index++; continue; } if((j<n&&a[j]>a[j+1])||a[j]>min[index]||(index>=1&&a[j]<max[index-1])){ flag=1; break; } } if(flag==1){ System.out.println("NO"); continue; } for(int j=c[temp_index]+1;j<n+1;j++) if((j<n&&a[j]>a[j+1])||a[j]<max[temp_index]) { flag = 1; break; } if(flag==1){ System.out.println("NO"); continue; } else System.out.println("YES"); } } private static int mins(int[] a,int s, int e) { int min = a[s]; for(int i=s+1;i<=e;i++) if(min>a[i]) min=a[i]; return min; } private static int maxs(int[] a,int s, int e) { int max = a[s]; for(int i=s+1;i<=e;i++) if(max<a[i]) max=a[i]; return max; } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
83ec8f01a6c57f966ecb225a434a8bd5
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; public class WeirdSort { public static void main(String[] args) { Scanner s = new Scanner(System.in); int cases = s.nextInt(); for (int x = 0; x < cases; x++) { int n = s.nextInt(); int m = s.nextInt(); boolean yes = true; boolean[] b = new boolean[n]; int[] ar = new int[n]; for (int i = 0; i < n; i++) { ar[i] = s.nextInt(); } for (int i = 0; i < m; i++) { int num = s.nextInt(); b[num-1] = true; } for (int i = 0; i < n && yes; i++) { for (int j = 0; j < n-1; j++) { if (ar[j] > ar[j+1]) { if (b[j]) { int temp = ar[j]; ar[j] = ar[j + 1]; ar[j + 1] = temp; } else { yes = false; break; } } } } if (yes) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
c682989d2dcfd1c92bc05be2c1517d20
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; public class WeirdSort { public static void main(String[] args)throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { String[] s = br.readLine().split(" "); int n = Integer.parseInt(s[0]); int m = Integer.parseInt(s[1]); Integer[] a = new Integer[n]; int[] p = new int[m]; s = br.readLine().split(" "); for(int i=0;i<n;i++){a[i] = Integer.parseInt(s[i]);} s = br.readLine().split(" "); for(int i=0;i<m;i++){p[i] = Integer.parseInt(s[i]);} int[] as = new int[n]; for(int i=0;i<n;i++){as[i] = a[i];} Arrays.sort(as); //System.out.println("as: "); //for(int i=0;i<n;i++){ System.out.print(as[i]+" "); }System.out.println(); int[] ps = new int[101]; for(int i=0;i<ps.length;i++){ps[i] = 0;} for(int i=0;i<m;i++){ps[p[i]] = p[i];} int hobena = 0; List<Integer> al = Arrays.asList(a); for(int i=0;i<n;i++){ int q = as[i]; //System.out.println("for "+q); int ind = al.indexOf(q); al.set(ind, Integer.MAX_VALUE); for(int j=ind; j>i; j--){ if(ps[j] == j){int temp = a[j]; a[j] = a[j-1]; a[j-1]=temp; temp = al.get(j); al.set(j,al.get(j-1)); al.set((j-1),temp);} else{ hobena = 1;break; } //for(int ls:al)System.out.print(ls+" "); //System.out.println(); } if(hobena == 1){break;} } if(hobena == 1){ System.out.println("NO"); }else{ System.out.println("YES"); } } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
9c12de2f48467da4198ef65f2e021261
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; public class GFG { public boolean check (int[] arr) { boolean issorted = true; for (int i = 1; i < arr.length; i++) { if (arr[i - 1] > arr[i]) { return false; } } return true; } public static void main (String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); int m = sc.nextInt(); int[] arr = new int[n]; int[] pos = new int[m]; GFG obj = new GFG(); for (int j = 0; j < n; j++) { arr[j] = sc.nextInt(); } for (int j = 0; j < m; j++) { pos[j] = sc.nextInt(); } if (obj.check(arr)) { System.out.println("YES"); } else { Arrays.sort(pos); for (int z = 0; z < n; z++) { for (int j = 0; j < m; j++) { if (arr[pos[j]] < arr[pos[j] - 1]) { int temp = arr[pos[j] - 1]; arr[pos[j] - 1] = arr[pos[j]]; arr[pos[j]] = temp; } } } if (obj.check(arr)) { System.out.println("YES"); } else { System.out.println("NO"); } } } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
a56bfffad473e2aa8da80d9f3f3901b8
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; public class GFG { public boolean check (int[] arr) { boolean issorted = true; for (int i = 1; i < arr.length; i++) { if (arr[i - 1] > arr[i]) { return false; } } return true; } public static void main (String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); int m = sc.nextInt(); int[] arr = new int[n]; int[] pos = new int[m]; GFG obj = new GFG(); for (int j = 0; j < n; j++) { arr[j] = sc.nextInt(); } for (int j = 0; j < m; j++) { pos[j] = sc.nextInt(); } if (obj.check(arr)) { System.out.println("YES"); } else { //Arrays.sort(pos); for (int z = 0; z < n; z++) { for (int j = 0; j < m; j++) { if (arr[pos[j]] < arr[pos[j] - 1]) { int temp = arr[pos[j] - 1]; arr[pos[j] - 1] = arr[pos[j]]; arr[pos[j]] = temp; } } } if (obj.check(arr)) { System.out.println("YES"); } else { System.out.println("NO"); } } } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
678090677fff3b56bf2e08137834339a
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.*; import java.util.StringTokenizer; public class hello1 {static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static String sum (String s) { String s1 = ""; if(s.contains("a")) s1+="a"; if(s.contains("e")) s1+="e"; if(s.contains("i")) s1+="i"; if(s.contains("o")) s1+="o"; if(s.contains("u")) s1+="u"; return s1; } public static HashMap<String, Integer> sortByValue(HashMap<String, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<String, Integer> > list = new LinkedList<Map.Entry<String, Integer> >(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<String, Integer> >() { public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // put data from sorted list to hashmap HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>(); for (Map.Entry<String, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } public static void main(String args[]) { FastReader input =new FastReader(); int t=input.nextInt(); while(t-->0) { int n = input.nextInt(); int m = input.nextInt(); int a[] = new int[n]; int p[] = new int[m]; for(int i=0;i<n;i++) { a[i] = input.nextInt(); } HashSet<Integer> set = new HashSet<Integer>(); for(int j=0;j<m;j++) { p[j] = input.nextInt(); set.add(p[j]-1); } int temp =0; for(int i=0;i<(n-1);i++) { if(a[i]>a[i+1]) { if(set.contains(i)) { temp = a[i+1]; a[i+1] = a[i]; a[i] = temp; i=-1; // System.out.println(a[i] + " " + a[i+1]); } } } int flag=0; for(int i=0;i<n-1;i++) { if(a[i]<=a[i+1]) { } else flag=1; } if(flag==1) System.out.println("NO"); else System.out.println("YES"); } } static long gcd(long a,long b) { if (a == 0) return b; return gcd(b % a, a); } static boolean find(int a[],int A,int B) { if( root(a,A)==root(a,B) ) //if A and B have the same root, it means that they are connected. return true; else return false; } static void union(int Arr[],int size[],int A,int B) { int root_A = root(Arr,A); int root_B = root(Arr,B); if(root_A == root_B) { return; } if(size[root_A] < size[root_B] ) { Arr[ root_A ] = Arr[root_B]; size[root_B] += size[root_A]; size[root_A] = 0; } else { Arr[ root_B ] = Arr[root_A]; size[root_A] += size[root_B]; size[root_B] = 0; } } static int root (int Arr[ ] ,int i) { while(Arr[ i ] != i) { i = Arr[ i ]; } return i; } static long factorial(long n) { long fact = 1; for(int i=1;i<=n;i++) { fact*=i; } return fact; } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
104a743d37d09df1b0a8b2eb8e310913
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; public class Main{ public static void main(String[] args) { PrintWriter out=new PrintWriter(System.out); InputReader in=new InputReader(System.in); int t=in.nextInt(); while(t-->0) { int n=in.nextInt(); int m=in.nextInt(); int [] a=in.readArray(n); int [] p=in.readArray(m); boolean b=true; outer:for(int i=0; i<n-1; i++) { for(int j=0; j<n-i-1; j++) { if(a[j+1]<a[j]) { if(!contains(p,j+1)) { b=false; break outer; } else { int temp=a[j+1]; a[j+1]=a[j]; a[j]=temp; } } } } if(b) out.println("YES"); else out.println("NO"); } out.close(); } static boolean contains(int [] p, int x) { for(int i=0; i<p.length; i++) { if(p[i]==x) return true; } return false; } static class InputReader{ BufferedReader br; StringTokenizer to; InputReader(InputStream stream){ br=new BufferedReader(new InputStreamReader(stream)); to=new StringTokenizer(""); } String next() { while(!to.hasMoreTokens()) { try { to=new StringTokenizer(br.readLine()); }catch(IOException e) {} } return to.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int array[]=new int[n]; for(int i=0; i<array.length; i++) array[i]=nextInt(); return array; } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
07a9f94981a555a2bdeff3344c92b9cd
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; import java.lang.*; import java.math.*; import java.io.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); long x; int y=0; while (t-->0){ int n=sc.nextInt(); int m=sc.nextInt(); int[] a=new int[n]; int[] p=new int[m]; int mi=-1,flag=0; for(int i=0;i<n;i++) a[i]=sc.nextInt(); for(int i=0;i<m;i++){ p[i]=sc.nextInt(); mi=Math.min(p[i],mi); } if(m==n-1){ System.out.println("YES"); continue; } Arrays.sort(p); int l=p[0]; int[] b=a.clone(); for(int i=0;i<m-1;i++){ if(p[i+1]!=(p[i]+1)){ Arrays.sort(a,l-1,p[i]+1); l=p[i+1]; } } Arrays.sort(a,l-1,p[m-1]+1); Arrays.sort(b); if(Arrays.equals(a,b)) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
ebed8b37213638008fd0c276ef990bf1
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; public class Main { // private int V; // private LinkedList<Integer> adj[]; // // Main(int v) { // V = v; // adj = new LinkedList[v]; // for (int i = 0; i < v; ++i) { // adj[i] = new LinkedList(); // } // } // // void addEdge(int v, int w) { // adj[v].add(w); // } // // void DFSUtil(int v, boolean visited[]) { // visited[v] = true; // System.out.println(v + " "); // Iterator<Integer> i = adj[v].listIterator(); // while(i.hasNext()) { // int n = i.next(); // if(!visited[n]) { // DFSUtil(n, visited); // } // } // } // // void DFS(int v) { // boolean visited[] = new boolean[V]; // // DFSUtil(v, visited); // } private static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int t = sc.nextInt(); while(t-- != 0) { int n= sc.nextInt(), m = sc.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; ++i) { a[i] = sc.nextInt(); } int p[] = new int[n]; for (int i = 0; i < m; ++i) { p[sc.nextInt() - 1] = 1; } for(int i = 0; i < n; ++i) { for(int j = 0; j < n-1; ++j) { if(p[j] == 1 && a[j] > a[j+1]) { int temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } boolean flag = true; for(int i = 0; i < n-1; ++i) { flag &= a[i]<=a[i+1]; } if(flag) { System.out.println("YES"); } else System.out.println("NO"); } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
4c4ea7e1df27286522f2a457d13f2aac
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Class2 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int tt = Integer.parseInt(br.readLine()); StringBuffer res = new StringBuffer(); while (tt-- > 0) { StringTokenizer st = new StringTokenizer(br.readLine(), " "); int n = Integer.parseInt(st.nextToken()); int m=Integer.parseInt(st.nextToken()); int a[][]=new int[n][2]; int p[]=new int[m]; st = new StringTokenizer(br.readLine(), " "); for (int i = 0; i < n; i++) { a[i][0]=Integer.parseInt(st.nextToken()); } st = new StringTokenizer(br.readLine(), " "); for (int i = 0; i < m; i++) { int x=Integer.parseInt(st.nextToken())-1; a[x][1]=1; } for (int i = 0; i < n; i++) { for (int j = 0; j < n-1; j++) { if(a[j][1]==1){ if(a[j][0]>a[j+1][0]){ int t=a[j][0]; a[j][0]=a[j+1][0]; a[j+1][0]=t; } } } } int fl=0; for (int i = 1; i < n; i++) { if(a[i][0]<a[i-1][0]){ fl=1; } } if(fl==1){ res.append("NO\n"); } else{ res.append("YES\n"); } } System.out.println(res); } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
d57da5baafc331a75f844ca28f2d30dd
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.Scanner; public class Main { // static StreamTokenizer in; static Scanner sc; static PrintWriter out; int n; int[] a; int[] p; public static void main(String[] args) throws IOException { boolean oj = System.getProperty("ONLINE_JUDGE") != null; Reader reader = oj ? new InputStreamReader(System.in) : new FileReader("input.txt"); //Writer writer = oj ? new OutputStreamWriter(System.out) : new FileWriter("output.txt"); Writer writer = new OutputStreamWriter(System.out); //in = new StreamTokenizer(new BufferedReader(reader)); sc = new Scanner(reader); out = new PrintWriter(writer); new Main().run(); } void run() throws IOException { solve(); out.flush(); } void swap (int i, int j){ // out.println("sw: " + a[i] + " " + a[j]); int temp = a[i]; a[i] = a[j]; a[j] = temp; } boolean BubleSort () { for(int j = n - 1 ; j > 1 ; j-- ) { for(int i = 0; i < j ; i++ ) { if(a[i] > a[i+1]) { if (p[i] == 1) { swap(i, i+1); } else { return false; } } } } return true; } void solve() throws IOException { int t = sc.nextInt(); while ( t > 0) { n = sc.nextInt(); int m = sc.nextInt(); a = new int[n]; p = new int[n]; for(int i=0; i < n;i++){ a[i] = sc.nextInt(); } for(int i = 0; i < m;i++) { p[sc.nextInt() - 1] = 1 ; } /* for(int i = 0; i < n;i++) { out.print( a[i] + " " ); } out.println(""); for(int i = 0; i < n;i++) { out.print( p[i] + " " ); } out.println(""); */ out.println(BubleSort()? "YES" : "NO"); t--; } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
424ddb2d2bbc6a131f2ad516d93b8851
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
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 m = sc.nextInt(); int[] arr = new int[n]; Set<Integer> set = new HashSet<>(); for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } for (int i = 0; i < m; i++) { set.add(sc.nextInt()); } solve(arr, set); } } private static void solve(int[] a, Set<Integer> p) { int[][] sorted = new int[a.length][2]; for (int i = 0; i < a.length; i++) { sorted[i][0] = a[i]; sorted[i][1] = i; } Arrays.sort(sorted, ((o1, o2) -> o1[0] - o2[0])); for (int i = 0; i < a.length; i++) { if (a[i] != sorted[i][0]) { int l = Math.min(i, sorted[i][1]); int h = Math.max(i, sorted[i][1]); if (!check(l, h, p)) { System.out.println("No"); return; } } } System.out.println("Yes"); } private static boolean check(int i, int j, Set<Integer> set) { for (int k = i; k < j; k++) { if (!set.contains(k + 1)) return false; } return true; } } /** * * p = 1, 2 * * 3 2 1 * 0 1 2 * * 1 2 3 * 2 1 0 * * 3 2 1 * 0 1 2 **/
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
563e58a53c569dffaebbde73251474eb
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.HashSet; import java.util.Scanner; public class Main { public static boolean get(int[] a, HashSet<Integer> set) { for (int i = 0; i < a.length - 1; i++) { for (int j = 0; j < a.length - 1; j++) { if (a[j] > a[j + 1]) { if (!set.contains(j)) return false; int tmp = a[j]; a[j] = a[j + 1]; a[j + 1] = tmp; } } } return true; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); int m = sc.nextInt(); int[] a = new int[n]; for (int j = 0; j < n; j++) { a[j] = sc.nextInt(); } HashSet<Integer> set = new HashSet<>(); for (int j = 0; j < m; j++) { set.add(sc.nextInt() - 1); } if (get(a, set)) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
0bac60e93d42dd9bfa8ac72aa134c486
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { static int n; static int org[]; public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner input = new Scanner(System.in); int test = input.nextInt(); while(test-->0){ n = input.nextInt(); int pos = input.nextInt(); int arr[] = new int[n]; for(int i =0;i<n;i++)arr[i] = input.nextInt(); org = Arrays.copyOf(arr,n); int p[] = new int[pos]; for(int i =0;i<pos;i++){ p[i] = input.nextInt(); } if(sorted(arr)){ System.out.println("YES"); continue; } while(true){ boolean ok = false; for(int i =0;i<pos;i++){ int a = p[i]; a--; if(arr[a]>arr[a+1]){ swap(a,a+1,arr); ok = true; } } if(!ok)break; } boolean ok = true; for(int i =0;i<n-1;i++){ ok &= (arr[i]<=arr[i+1]?true:false); } if(ok)System.out.println("YES"); else System.out.println("NO"); } } public static boolean sorted(int arr[]){ for(int i =0;i<n-1;i++){ if(arr[i]>arr[i+1])return false; } return true; } public static boolean same(int arr[]){ for(int i =0;i<n;i++){ if(arr[i] != org[i]){ return false; } } return true; } public static void swap(int p1,int p2,int arr[]){ int temp = arr[p1]; arr[p1] = arr[p2]; arr[p2] = temp; } } class pair{ int a,b; public pair(int a,int b){ this.a = a; this.b= b; } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
d0ba5c9b36b208d0f9e6a211c08973f2
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Housni Abdellatif */ public class Main { public static void main(String[] args) throws IOException{ InputStream inputStream = System.in; OutputStream outputStream = System.out; Task.Reader in = new Task.Reader(); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(1, in, out); out.close(); } static class Task { public void solve(int testNumber, Reader reader, PrintWriter out) throws IOException { int t = reader.nextInt(); loop: while(t-- > 0) { int n = reader.nextInt(); int m = reader.nextInt(); int[] a = new int[n]; for (int i = 0; i < n ; ++i) { a[i] = reader.nextInt(); } List<Integer> p = new ArrayList<>(); for (int i = 0; i < m ; ++i) { p.add(reader.nextInt() - 1); } List<List<Integer>> arr = new ArrayList<>(); List<Integer> list = new ArrayList<>(); list.add(a[0]); for (int i = 1; i < n; ++i) { if (p.contains(i - 1)) { list.add(a[i]); }else { Collections.sort(list); arr.add(list); list = new ArrayList<>(); list.add(a[i]); } } if (list.size() > 0) { Collections.sort(list); arr.add(list); } int max = -1; for (List<Integer> li : arr) { for (int x : li) { if (x >= max) { max = x; }else { out.println("NO"); continue loop; } } } out.println("YES"); } } static class Edge { int from; int to; } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
fb1ffd25315fc2e5122d60e7a127495a
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class A { static int find_par(int n){ if(par[n]==n) return n; return (par[n]=find_par(par[n])); } static void union(int a,int b){ int root1=find_par(a),root2=find_par(b); if(root1==root2) return; if(sz[root1]<=sz[root2]){ sz[root2]+=sz[root1]; par[root1]=root2; } else{ sz[root1]+=sz[root2]; par[root2]=root1; } } public static void process(int test_number)throws IOException { int n = ni(), m = ni(), aux[] = new int[n + 1], arr[] = new int[n + 1], p[] = new int[m + 1]; par = new int[n + 1]; sz = new int[n + 1]; for(int i = 1; i <= n; i++){ arr[i] = (aux[i] = ni()); sz[i] = 1; par[i] = i; } boolean taken[] = new boolean[n + 1]; Arrays.sort(aux, 1, n + 1); for(int i = 1; i <= m; i++){ p[i] = ni(); union(p[i], p[i] + 1); } boolean flag = true; for(int i = 1; i <= n; i++){ boolean can = false; for(int j = 1; j <= n; j++){ if(taken[j]) continue; if(arr[i] == aux[j] && find_par(i) == find_par(j)){ can = true; taken[j] = true; break; } } flag = flag && can; } pn(flag ? "YES" : "NO"); } static final long mod = (long)1e9+7l; static int par[], sz[]; static FastReader sc; static PrintWriter out; public static void main(String[]args)throws IOException { out = new PrintWriter(System.out); sc = new FastReader(); long s = System.currentTimeMillis(); int t = 1; t = ni(); for(int i = 1; i <= t; i++) process(i); out.flush(); System.err.println(System.currentTimeMillis()-s+"ms"); } static void trace(Object... o){ System.err.println(Arrays.deepToString(o)); }; static void pn(Object o){ out.println(o); } static void p(Object o){ out.print(o); } static int ni()throws IOException{ return Integer.parseInt(sc.next()); } static long nl()throws IOException{ return Long.parseLong(sc.next()); } static double nd()throws IOException{ return Double.parseDouble(sc.next()); } static String nln()throws IOException{ return sc.nextLine(); } static long gcd(long a, long b)throws IOException{ return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{ return (b==0)?a:gcd(b,a%b); } static int bit(long n)throws IOException{ return (n==0)?0:(1+bit(n&(n-1))); } static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } String nextLine(){ String str = ""; try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
1fad93dde201978bcde54a6edfbca915
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
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 CF1311B { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class Node { long pp; long a, b; Node(long x, long y) { a = x; b = y; pp = a * b; } } static class Comp implements Comparator<Node> { public int compare(Node o1, Node o2) { if (o1.pp > o2.pp) { return 1; } else { return -1; } } } public static void main(String[] args) { FastReader sc=new FastReader(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int m=sc.nextInt(); int a[]=new int[n]; int p[]=new int[m]; int b[]=new int[n]; for(int i=0;i<n;i++) {a[i]=sc.nextInt(); b[i]=a[i];} for(int i=0;i<m;i++) {p[i]=sc.nextInt(); p[i]--;} Arrays.sort(p); Arrays.sort(b); boolean flag=false; for(int j=0;j<n;j++) { boolean stat=true; for(int i : p) { if(a[i]>a[i+1]) { a[i]=a[i]^a[i+1]; a[i+1]=a[i+1]^a[i]; a[i]=a[i]^a[i+1]; stat=false; } if(stat==true) { if(Arrays.equals(a,b)) break; } } } // int flag=1; // for(int i=0;i<n-1;i++) // { // if(a[i]>a[i+1]) {flag=0; break;} // } if(Arrays.equals(a,b)) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
b3b79dcfa78f06459290073c419d9be4
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; public class A { static InputReader scn = new InputReader(System.in); static OutputWriter out = new OutputWriter(System.out); public static void main(String[] args) { int t = scn.nextInt(); while (t-- > 0) solve(); out.close(); } // 100 79 public static void solve() { int n = scn.nextInt(); int m = scn.nextInt(); int[] arr = readArray(n); int[] temparr = new int[n]; for (int i = 0; i < n; i++) { temparr[i] = arr[i]; } Arrays.sort(temparr); int[] p = readArray(m); int loop = 1000; while (loop > 0) { for (int i = 0; i < m; i++) { int a = p[i]; a--; if (arr[a] > arr[a + 1]) { int temp = arr[a]; arr[a] = arr[a + 1]; arr[a + 1] = temp; } } loop--; } // System.out.println(Arrays.toString(arr)); // System.out.println(Arrays.toString(temparr)); if (Arrays.equals(temparr, arr)) { out.println("YES"); } else { out.println("NO"); } } public static boolean check(int a, int b, int c, int d) { int[] arr = { a, b, c, d }; int odd = 0; int even = 0; for (int i = 0; i < 4; i++) { if (arr[i] % 2 == 0) { even++; } else { odd++; } } // System.out.println(even + " " + odd); if (even == 4 || even == 3 && odd == 1) { return true; } return false; } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } public static int lcm(int a, int b) { return (a * b) / gcd(a, b); } public static boolean isPrime(long n) { if (n < 2) return false; if (n == 2 || n == 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; long sqrtN = (long) Math.sqrt(n) + 1; for (long i = 6L; i <= sqrtN; i += 6) { if (n % (i - 1) == 0 || n % (i + 1) == 0) return false; } return true; } public static HashMap<Integer, Integer> CountFrequencies(int[] arr) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < arr.length; i++) { int a = arr[i]; if (map.containsKey(a)) { map.put(a, map.get(a) + 1); } else { map.put(a, 1); } } return map; } public static int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = scn.nextInt(); } return a; } public static int countPrimeFactors(int n) { int count = 0; while (n % 2 == 0) { n = n / 2; count++; } for (int i = 3; i <= Math.sqrt(n); i = i + 2) { while (n % i == 0) { n = n / i; count++; } } if (n > 2) count++; return (count); } public static ArrayList<Integer> printKAlmostPrimes(int n) { ArrayList<Integer> list = new ArrayList<>(); for (int i = 1, num = 2; i <= n; num++) { if (countPrimeFactors(num) == 2) { if (num > n) { break; } list.add(num); i++; } } return list; } public static int[] SieveOfEratosthenes(int[] arr) { int n = arr.length; for (int i = 3; i < n; i = i + 2) { arr[i] = 1; } for (int i = 3; i < n; i = i + 2) { if (arr[i] == 1) { for (int j = i * i; j < n; j = j + i) { arr[j] = 0; } } } arr[2] = 1; arr[0] = arr[1] = 0; return arr; } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } } public static ArrayList<String> printPermutn(String str, String ans, ArrayList<String> list) { if (str.length() == 0) { list.add(ans); } for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); String ros = str.substring(0, i) + str.substring(i + 1); printPermutn(ros, ans + ch, list); } return list; } public static ArrayList<String> GetSubSequences(String s) { if (s.length() == 0) { ArrayList<String> br = new ArrayList<>(); br.add(""); return br; } char ch = s.charAt(0); String ms = s.substring(1); ArrayList<String> rr = GetSubSequences(ms); ArrayList<String> mr = new ArrayList<>(); int t = rr.size(); for (int i = 0; i < t; i++) { mr.add(rr.get(i)); mr.add(ch + rr.get(i)); } return mr; } public static int binarySearch(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; if (arr[mid] == x) return mid; if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); return binarySearch(arr, mid + 1, r, x); } return -1; } public static int firstOccurence(int array1[], int low, int high, int x, int n) { if (low <= high) { int mid = low + (high - low) / 2; if ((mid == 0 || x > array1[mid - 1]) && array1[mid] == x) return mid; else if (x > array1[mid]) return firstOccurence(array1, (mid + 1), high, x, n); else return firstOccurence(array1, low, (mid - 1), x, n); } return -1; } public static int lastOccurence(int array2[], int low, int high, int x, int n) { if (low <= high) { int mid = low + (high - low) / 2; if ((mid == n - 1 || x < array2[mid + 1]) && array2[mid] == x) return mid; else if (x < array2[mid]) return lastOccurence(array2, low, (mid - 1), x, n); else return lastOccurence(array2, (mid + 1), high, x, n); } return -1; } public static void quickSort(int[] arr, int lo, int hi) { if (lo >= hi) { return; } int mid = (lo + hi) / 2; int pivot = arr[mid]; int left = lo; int right = hi; while (left <= right) { while (arr[left] < pivot) { left++; } while (arr[right] > pivot) { right--; } if (left <= right) { int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; left++; right--; } } quickSort(arr, lo, right); quickSort(arr, left, hi); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return nextLine(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); // writer.print(1); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
55c7b8fca0bf82c2caf9eeeb732e6ee5
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution { private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) throws IOException { String l = scanner.nextLine().trim(); int t = Integer.parseInt(l); for(int i=0;i<t;i++){ String line = scanner.nextLine().trim(); int n = Integer.parseInt(line.split(" ")[0]); int m = Integer.parseInt(line.split(" ")[1]); String[] ai=scanner.nextLine().trim().split(" "); String[] sortedA = new String[ai.length]; System.arraycopy( ai, 0, sortedA, 0, ai.length ); Arrays.sort(sortedA); String[] sortedD = new String[ai.length]; System.arraycopy( ai, 0, sortedD, 0, ai.length ); Arrays.sort(sortedD, Collections.reverseOrder()); String[] pi = scanner.nextLine().trim().split(" "); boolean no = false; for(int k=0;k<ai.length-1;k++){ for(int j=0;j<ai.length-1;j++){ if(Integer.parseInt(ai[j])>Integer.parseInt(ai[j+1]) ){ int inc=j+1; if(Arrays.asList(pi).indexOf(inc+"")==-1){ no=true; } else { String tmp= ai[j]; ai[j]=ai[j+1]; ai[j+1]=tmp; } } } } if(no==false) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
ff674325d609945eabc6c605ec4148c2
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; import java.io.*; public class Wierd_Sort { 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()); } } public static void main(String[] args) { FastReader sc = new FastReader(); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); int m = sc.nextInt(); int a[] = new int[n]; for(int i = 0 ; i<n; i++) { a[i] = sc.nextInt(); } int p[] = new int[m]; for(int i = 0; i<m; i++) { p[i] = sc.nextInt(); } Arrays.sort(p); for(int k = 0; k<n-1; k++) { for(int i = 0; i<n-1; i++) { if(a[i] > a[i+1]) { for(int j = 0 ; j < m; j++) { if(p[j] == i+1){ int temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } } }else { continue; } } } boolean flag = false; for(int i = 0; i < n-1; i++) { if(a[i] > a[i+1]) { flag = true; break; } } if(flag) { System.out.println("NO"); }else { System.out.println("YES"); } } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
58e9cc79f5c8bbb8c688f963355881ee
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.Arrays; import java.util.Scanner; import java.util.concurrent.ThreadLocalRandom; public class Main { static void swap(int a[] , int l , int r){ int temp = a[l]; a[l] = a[r]; a[r] = temp; } static void shuffle( int arr[] , int start , int end) { int n = arr.length; for (int i = end; i > start; i--) { int randomNum = ThreadLocalRandom.current().nextInt(start, end + 1); int j = randomNum; swap(arr , i , j); } } public static void main(String [] commandLineArgument){ Scanner input = new Scanner(System.in); int tc = input.nextInt(); while(tc > 0){ --tc; int n = input.nextInt() , m = input.nextInt(); int [] a = new int[n]; int [] b = new int[m]; for(int i = 0; i < n; ++i){ a[i] = input.nextInt(); } for(int i = 0; i < m; ++i){ b[i] = input.nextInt(); } shuffle(b , 0 , m - 1); Arrays.sort(b); int i = 0; while(i < m){ int start = b[i] - 1, end = b[i]; int cur = b[i]; ++i; while(i < m){ if(cur != (b[i] - 1)){ break; } cur = b[i]; end = cur; ++i; } shuffle(a , start , end); Arrays.sort(a , start , end + 1); } boolean isSorted = true; for(int j = 0; j < n - 1; ++j){ if(a[j] > a[j + 1]){ isSorted = false; break; } } String answer = (isSorted) ? "YES" : "NO"; System.out.println(answer); } input.close(); } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
5a2fe0b7cf52c01a5c79ea95b8c3e8bb
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class Main { static void swap(int a[] , int l , int r){ int temp = a[l]; a[l] = a[r]; a[r] = temp; } static void shuffle( int arr[] , int start , int end) { int n = arr.length; Random r = new Random(); for (int i = end; i > start; i--) { int j = r.nextInt(i+1); swap(arr , i , j); } } public static void main(String [] commandLineArgument){ Scanner input = new Scanner(System.in); int tc = input.nextInt(); while(tc > 0){ --tc; int n = input.nextInt() , m = input.nextInt(); int [] a = new int[n]; int [] b = new int[m]; for(int i = 0; i < n; ++i){ a[i] = input.nextInt(); } for(int i = 0; i < m; ++i){ b[i] = input.nextInt(); } //shuffle(b , 0 , m - 1); Arrays.sort(b); int i = 0; while(i < m){ int start = b[i] - 1, end = b[i]; int cur = b[i]; ++i; while(i < m){ if(cur != (b[i] - 1)){ break; } cur = b[i]; end = cur; ++i; } // shuffle(a , start , end); Arrays.sort(a , start , end + 1); } boolean isSorted = true; for(int j = 0; j < n - 1; ++j){ if(a[j] > a[j + 1]){ isSorted = false; break; } } String answer = (isSorted) ? "YES" : "NO"; System.out.println(answer); } input.close(); } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
2fa03ae6ca3a3045f34db1280d19a66b
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; import java.io.*; public class Main { static int mod=(int)1e9+7; public static void main(String[] args) throws Exception { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while (t-->0){ int n=sc.nextInt(),m=sc.nextInt(); int arr[]=new int[n]; for (int i=0;i<n;i++){ arr[i]=sc.nextInt(); } int pro[]=new int[101]; for (int i=0;i<m;i++){ pro[sc.nextInt()-1]=1; } for (int i=0;i<n;i++){ for (int j=0;j<n;j++){ if (pro[j]==1){ if (arr[j]>arr[j+1]){ int temp2=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp2; } } } } if (isTrue(arr)) System.out.println("YES"); else System.out.println("NO"); } } static boolean isTrue(int arr[]){ int n=arr.length; for (int i=0;i<n-1;i++){ if (arr[i]>arr[i+1])return false; } return true; } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
bcbb866f05d34541ca00a5aa262164c5
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import javax.swing.*; import java.awt.desktop.SystemSleepEvent; import java.util.*; import java.io.*; public class Main { public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } } public class pair { int a; int b; pair(int p, int q) { a = p; b = q; } public int hashCode() { return new Integer(a).hashCode() * 31 + new Integer(b).hashCode(); } } public static int gcd(int a, int b) { if (a == 1 || b == 1) return 1; if (a == 0) return b; if (b == 0) return a; return gcd(b % a, a); } public static void main(String[] args) { FastReader s = new FastReader(); int t = s.nextInt(); while (t > 0) { int n = s.nextInt(); int m = s.nextInt(); int[] arr=new int[n]; int[] arr1=new int[m]; int[] arr2 = new int[n]; for(int i=0;i<n;i++) { arr[i]=s.nextInt(); arr2[i] = arr[i]; } Arrays.sort(arr2); for(int i=0;i<m;i++) { arr1[i]=s.nextInt(); } int flag=0; int temp; int count=0; int temp2; while(!same(arr,arr2)) { temp2 = count; for(int i=0;i<n-1;i++) { if(arr[i]>arr[i+1]) { if(find(arr1,i+1)) { temp = arr[i + 1]; arr[i + 1] = arr[i]; arr[i] = temp; count++; } } } if(temp2==count) break; } if(same(arr,arr2)) System.out.println("YES"); else System.out.println("NO"); t--; } } public static boolean same(int[] arr, int[] arr1) { int n=arr.length; for(int i=0;i<n;i++) { if(arr[i]!=arr1[i]) return false; } return true; } public static boolean find(int[] arr, int x) { int n=arr.length; for(int i=0;i<n;i++) { if(arr[i]==x) return true; } return false; } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
b5cbf20355bcc748cd7b51649c515d22
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int m = sc.nextInt(); int[] a = new int[n]; int[] p = new int[m]; for(int i = 0; i < n; i++){ a[i] = sc.nextInt(); } for(int i = 0; i < m; i++){ p[i] = sc.nextInt(); } boolean ok = true; Arrays.sort(p); while(true){ ok = false; for(int i = 0; i < n - 1; i++){ if(Arrays.binarySearch(p, i + 1) >= 0 && a[i] > a[i + 1]){ ok = true; int temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } } if(!ok){ break; } } ok = true; for(int i = 0; i < n - 1; i++){ if(a[i] > a[i + 1]){ ok = false; break; } } System.out.println(ok?"YES":"NO"); } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
9883e3bd0dbd9dc6b77236b42099b457
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; public class test { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Reader.init(System.in); int t=Reader.nextInt(); while(t-->0) { int n=Reader.nextInt(); int m=Reader.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=Reader.nextInt(); Map<Integer,Integer> mp=new HashMap<>(); for(int i=0;i<m;i++) { int data=Reader.nextInt(); mp.put(data-1, 1); } while(true) { boolean b=false; for(int i=0;i<n-1;i++) { if(arr[i]>arr[i+1] && mp.containsKey(i)) { b=true; int temp=arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } } if(b==false) break; } boolean b=true; for(int i=0;i<n-1;i++) { if(arr[i]<=arr[i+1]) continue; else b=false; } if(b==false) { System.out.println("NO"); } else System.out.println("YES"); } } } 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 int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
7cc455721c090e3a8751e046600f9b04
train_002.jsonl
1582554900
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; public class Main { 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()); } } public static void main(String[] args) { FastScanner s = new FastScanner(); int t = s.nextInt(); for (int vguifrjvf = 0; vguifrjvf < t; vguifrjvf++) { int n = s.nextInt(); int m = s.nextInt(); int []a = new int [n]; int []p = new int [m]; TreeSet<Integer> set = new TreeSet<>(); for (int i = 0; i < n; i++) { a[i] = s.nextInt(); } for (int i = 0; i < m; i++) { set.add(s.nextInt()-1); } for (int i = 0; i < n * n; i++) { for (int x: set) { if (x < n-1 && a[x] > a[x+1]) { int ft = a[x]; a[x] = a[x+1]; a[x+1] = ft; } } } boolean gf = false; for (int i = 0; i < n-1; i++) { if (a[i] > a[i+1]){ gf = true; break; } } if (gf) System.out.println("NO"); else System.out.println("YES"); } } }
Java
["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"]
2 seconds
["YES\nNO\nYES\nYES\nNO\nYES"]
null
Java 11
standard input
[ "sortings", "dfs and similar" ]
e1481b9d940407da75e11355b580f459
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m &lt; n \le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i &lt; n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.
1,200
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$) using only allowed swaps. Otherwise, print "NO".
standard output
PASSED
dbeac22a6e9f0546555069f4dfcc1e8a
train_002.jsonl
1549208100
Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.Initially, there are $$$n$$$ superheroes in avengers team having powers $$$a_1, a_2, \ldots, a_n$$$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $$$1$$$. They can do at most $$$m$$$ operations. Also, on a particular superhero at most $$$k$$$ operations can be done.Can you help the avengers team to maximize the average power of their crew?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Kraken7 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(), k = in.nextInt(), m = in.nextInt(); double[] a = new double[n]; for (int i = 0; i < n; i++) { a[i] = in.nextDouble(); } Arrays.sort(a); double tot = 0, res = 0, sum = 0; for (int i = n - 1; i >= 0; i--) { tot += a[i]; sum += k; if (i <= m) res = Math.max(res, (tot + Math.min(sum, m - i)) / (n - i)); } out.printf("%.20f", res); } } }
Java
["2 4 6\n4 7", "4 2 6\n1 3 2 3"]
1 second
["11.00000000000000000000", "5.00000000000000000000"]
NoteIn the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $$$2$$$ each.
Java 11
standard input
[ "implementation", "brute force", "math" ]
d6e44bd8ac03876cb03be0731f7dda3d
The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \le n \le 10^{5}$$$, $$$1 \le k \le 10^{5}$$$, $$$1 \le m \le 10^{7}$$$) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^{6}$$$) — the initial powers of the superheroes in the cast of avengers.
1,700
Output a single number — the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$$$.
standard output
PASSED
7539aeb569a55ad5bf95038caf0eb064
train_002.jsonl
1549208100
Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.Initially, there are $$$n$$$ superheroes in avengers team having powers $$$a_1, a_2, \ldots, a_n$$$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $$$1$$$. They can do at most $$$m$$$ operations. Also, on a particular superhero at most $$$k$$$ operations can be done.Can you help the avengers team to maximize the average power of their crew?
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.StringTokenizer; public class Solution{ public static void main(String[] args) { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int tt = 1; while(tt-->0) { int n = fs.nextInt(), k = fs.nextInt(), m = fs.nextInt(); int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = fs.nextInt(); ruffleSort(a); long[] suf = new long[n]; suf[n-1] = a[n-1]; for(int i=n-2;i>=0;i--) suf[i] += suf[i+1] + a[i]; double ans = 0; for(int num=0;num<=Math.min(n-1, m); num++){ int rem = m - num; double avg = ((double)suf[num] + Math.min((long)rem, (long)(n-num)*k))/(n-num); ans = Math.max(ans, avg); } out.println(ans); } 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 class FastScanner{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next(){ while(!st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch(IOException e){ e.printStackTrace(); } } return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt(){ return Integer.parseInt(next()); } public int[] readArray(int n){ int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } public long nextLong() { return Long.parseLong(next()); } public char nextChar() { return next().toCharArray()[0]; } } }
Java
["2 4 6\n4 7", "4 2 6\n1 3 2 3"]
1 second
["11.00000000000000000000", "5.00000000000000000000"]
NoteIn the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $$$2$$$ each.
Java 11
standard input
[ "implementation", "brute force", "math" ]
d6e44bd8ac03876cb03be0731f7dda3d
The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \le n \le 10^{5}$$$, $$$1 \le k \le 10^{5}$$$, $$$1 \le m \le 10^{7}$$$) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^{6}$$$) — the initial powers of the superheroes in the cast of avengers.
1,700
Output a single number — the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$$$.
standard output
PASSED
6ef99ef05df1931c3efab7b089f3cb3a
train_002.jsonl
1549208100
Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.Initially, there are $$$n$$$ superheroes in avengers team having powers $$$a_1, a_2, \ldots, a_n$$$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $$$1$$$. They can do at most $$$m$$$ operations. Also, on a particular superhero at most $$$k$$$ operations can be done.Can you help the avengers team to maximize the average power of their crew?
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Unstoppable solver = new Unstoppable(); // int t=in.nextInt(); // while(t-->0) solver.solve(in, out); out.close(); } static class Unstoppable { public void solve(InputReader in, PrintWriter out) { int n=in.nextInt(); long k=in.nextLong(); long m=in.nextLong(); double sum=0; ArrayList<Long> a=new ArrayList<>(); for(int i=0;i<n;i++){ a.add(in.nextLong()); sum+=a.get(i); } double max=sum/n; max=Math.max(max,(Math.min(m,k*n)+sum)/n); Collections.sort(a); for(int i=0;i<n-1;i++){ if(m>0){ m--; sum-=a.get(i); } if(m>=0) max=Math.max(max,(Math.min(m,k*(n-1-i))+sum)/(n-1-i)); if(m==0) break; } out.println(max); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public long nextLong(){ return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["2 4 6\n4 7", "4 2 6\n1 3 2 3"]
1 second
["11.00000000000000000000", "5.00000000000000000000"]
NoteIn the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $$$2$$$ each.
Java 11
standard input
[ "implementation", "brute force", "math" ]
d6e44bd8ac03876cb03be0731f7dda3d
The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \le n \le 10^{5}$$$, $$$1 \le k \le 10^{5}$$$, $$$1 \le m \le 10^{7}$$$) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^{6}$$$) — the initial powers of the superheroes in the cast of avengers.
1,700
Output a single number — the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$$$.
standard output
PASSED
ac61c3291624334759c276d2e044af65
train_002.jsonl
1549208100
Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.Initially, there are $$$n$$$ superheroes in avengers team having powers $$$a_1, a_2, \ldots, a_n$$$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $$$1$$$. They can do at most $$$m$$$ operations. Also, on a particular superhero at most $$$k$$$ operations can be done.Can you help the avengers team to maximize the average power of their crew?
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int k=in.nextInt(); int m=in.nextInt(); double[] a=new double[n]; for (int i=0;i<n;++i) a[i]=in.nextDouble(); Arrays.sort(a); double res=0,sum=0,tot=0; for (int i=n-1;i>=0;--i) { tot+=a[i]; sum+=k; if (i<=m) res=Math.max(res,(tot+Math.min(sum,m-i))/(n-i)); } System.out.printf("%.12f\n",res); } }
Java
["2 4 6\n4 7", "4 2 6\n1 3 2 3"]
1 second
["11.00000000000000000000", "5.00000000000000000000"]
NoteIn the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $$$2$$$ each.
Java 11
standard input
[ "implementation", "brute force", "math" ]
d6e44bd8ac03876cb03be0731f7dda3d
The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \le n \le 10^{5}$$$, $$$1 \le k \le 10^{5}$$$, $$$1 \le m \le 10^{7}$$$) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^{6}$$$) — the initial powers of the superheroes in the cast of avengers.
1,700
Output a single number — the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$$$.
standard output
PASSED
9254d78798a8467e701fb0d58cd69bfd
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; public class Building_Permutation { public static void main(String[] args)throws IOException { BufferedReader ob=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(ob.readLine()); String s[]=ob.readLine().split(" "); ArrayList<Integer> a=new ArrayList<Integer>(); HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>(); for(int i=0;i<n;i++) { int temp=Integer.parseInt(s[i]); if(temp>=1 && temp<=n) { if(!hm.containsKey(temp)) { hm.put(temp,1); } else { a.add(temp); } } else { a.add(temp); } } Collections.sort(a); // System.out.println(" a = "+a); int prev=1; long ans=0; for(int j=0;j<a.size();j++) { for(int i=prev;i<=n;i++) { if(hm.containsKey(i)) continue; // System.out.println("i = "+i); hm.put(i,1); ans+=(long)Math.abs(a.get(j) - i); // System.out.println("ans = "+ans); prev=i; break; } } System.out.println(ans); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
d8ca8ddb5517046fc6abf20634ccac74
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.io.BufferedInputStream; import java.util.Arrays; public class Solution { static BufferedInputStream in = new BufferedInputStream(System.in); public static void main(String[] args) throws Exception { int n = readInt(); Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = readInt(); Arrays.parallelSort(a); long count = 0; for (int i = 0; i < n; i++) count += Math.abs(a[i] - i - 1); System.out.println(count); } static int readInt() throws Exception { int x = 0, c = in.read(); while (!(c == '-' || ('0' <= c && c <= '9'))) c = in.read(); boolean negative = c == '-'; if (negative) c = in.read(); while (c == '-' || ('0' <= c && c <= '9')) { x = 10 * x + c - '0'; c = in.read(); } return negative ? -x : x; } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
51f0f2c5b9b76c454af0b0316f94451d
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws Throwable { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); int[] a = new int[n]; for (int i = 0; i < a.length; i++) { a[i] = Integer.parseInt(st.nextToken()); } System.out.println(solve(a)); } static long solve(int[] a) { a = Arrays.stream(a).boxed().sorted().mapToInt(x -> x).toArray(); long result = 0; for (int i = 0; i < a.length; i++) { result += Math.abs(a[i] - (i + 1)); } return result; } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
4c74a7468ef2c92b76e2da37080dc182
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws Throwable { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); int[] a = new int[n]; for (int i = 0; i < a.length; i++) { a[i] = Integer.parseInt(st.nextToken()); } System.out.println(solve(a)); } static long solve(int[] a) { int[] sorted = Arrays.stream(a).boxed().sorted().mapToInt(x -> x).toArray(); long result = 0; for (int i = 0; i < sorted.length; i++) { result += Math.abs(sorted[i] - (i + 1)); } return result; } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
b4f51587e90c367c8692b36da4ee6348
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
//package com.company; import java.io.*; import java.util.*; public class Main{ public static void main(String[] args)throws IOException { FastReader sc = new FastReader(); OutputStream output = System.out; PrintWriter out = new PrintWriter(output); //InputStream input = System.in; //Scanner sc = new Scanner(input); int n=sc.nextInt(); Integer[] arr=new Integer[n]; long count=0; for(int i=0;i<n;i++) arr[i]=sc.nextInt(); Arrays.sort(arr); for(int i=1;i<n+1;i++) { count+=Math.abs(arr[i-1]-i); } out.println(count); 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; } } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
b06e0f8a78ca6adfa8ccdaea51c051f4
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class C285 { public static void main(String args[])throws Exception { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); Long a[]=new Long[n]; for(int i=0;i<n;i++) a[i]=sc.nextLong(); Arrays.sort(a); long sum=0; for(int i=0;i<n;i++) { sum+= Math.abs(a[i]-i-1); } System.out.print(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
000af59c17865560c133c8fe194f8c5e
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { InputStream input = System.in; OutputStream output = System.out; InputReader in = new InputReader(input); PrintWriter out = new PrintWriter(output); Solution s = new Solution(); s.solve(1, in, out); out.close(); } static class Solution { double EPS = 0.0000001; public void solve(int cs, InputReader in, PrintWriter out) { int n = in.nextInt(); Integer[] arr = new Integer[n]; for (int i = 0; i < n; ++i) arr[i] = in.nextInt(); Arrays.sort(arr); long count = 0; if (arr[0] <= 0) { count += Math.abs(1-arr[0]); arr[0] = Math.abs(1); } for (int i = 1; i < n; ++i) { count += Math.abs((arr[i-1]+1)-arr[i]); arr[i] = arr[i-1]+1; } out.println(count); } } static class InputReader { BufferedReader br; StringTokenizer st; public InputReader(InputStream i) { br = new BufferedReader(new InputStreamReader(i), 32768); st = null; } public InputReader(FileReader s) { br = new BufferedReader(s); st = null; } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader(){ din=new DataInputStream(System.in); buffer=new byte[BUFFER_SIZE]; bufferPointer=bytesRead=0; } public Reader(String file_name) throws IOException{ din=new DataInputStream(new FileInputStream(file_name)); buffer=new byte[BUFFER_SIZE]; bufferPointer=bytesRead=0; } public String readLine() throws IOException{ byte[] buf=new byte[64]; // line length int cnt=0,c; while((c=read())!=-1){ if(c=='\n')break; buf[cnt++]=(byte)c; } return new String(buf,0,cnt); } public int nextInt() throws IOException{ int ret=0;byte c=read(); while(c<=' ')c=read(); boolean neg=(c=='-'); if(neg)c=read(); do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); if(neg)return -ret; return ret; } public long nextLong() throws IOException{ long ret=0;byte c=read(); while(c<=' ')c=read(); boolean neg=(c=='-'); if(neg)c=read(); do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); if(neg)return -ret; return ret; } public double nextDouble() throws IOException{ double ret=0,div=1;byte c=read(); while(c<=' ')c=read(); boolean neg=(c=='-'); if(neg)c = read(); do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); if(c=='.')while((c=read())>='0'&&c<='9') ret+=(c-'0')/(div*=10); if(neg)return -ret; return ret; } private void fillBuffer() throws IOException{ bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE); if(bytesRead==-1)buffer[0]=-1; } private byte read() throws IOException{ if(bufferPointer==bytesRead)fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException{ if(din==null) return; din.close(); } } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
10406f8fb6c624e01c4a9711c9f93c9c
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.Collections; //import java.util.LinkedList; //import java.util.List; import java.util.Scanner; import java.util.Vector; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; //Long ar[]=new Long[n]; Vector<Integer> ar=new Vector<Integer>(300005); for(int i=0;i<n;++i) { x=sc.nextInt(); ar.add(x); } long sum=0; Collections.sort(ar); for(int i=0;i<ar.size();++i) { sum+=Math.abs((i+1)-(Integer)ar.get(i)); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
2fe72ce21acd412ec40f334ff7a1f756
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.ArrayList; import java.util.Collections; //import java.util.LinkedList; //import java.util.List; import java.util.Scanner; import java.util.Vector; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; //Long ar[]=new Long[n]; Vector<Integer> ar=new Vector<Integer>(); int a=1; for(int i=0;i<n;++i) { ar.add(sc.nextInt()); } long sum=0; Collections.sort(ar); for(int i=0;i<ar.size();++i) { sum+=Math.abs((i+1)-(Integer)ar.get(i)); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
ef82b714415415d3df01e66a5a48dc38
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.ArrayList; import java.util.Collections; //import java.util.LinkedList; //import java.util.List; import java.util.Scanner; import java.util.Vector; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; //Long ar[]=new Long[n]; ArrayList<Integer> ar=new ArrayList<Integer>(10000); int a=6; for(int i=0;i<n;++i) { ar.add(sc.nextInt()); } long sum=0; Collections.sort(ar); for(int i=0;i<ar.size();++i) { sum+=Math.abs((i+1)-(Integer)ar.get(i)); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
3440c51d8688430cada6b6f54b8509e2
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Scanner; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; //Long ar[]=new Long[n]; ArrayList<Integer> ar=new ArrayList<Integer>(n); for(int i=0;i<n;++i) { x=sc.nextInt(); ar.add(x); } long sum=0; Collections.sort(ar); for(int i=0;i<ar.size();++i) { sum+=Math.abs((i+1)-(Integer)ar.get(i)); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
d6be7e07ddddc3785dd860b4f4987512
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Scanner; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; //Long ar[]=new Long[n]; ArrayList<Integer> ar=new ArrayList<Integer>(300005); for(int i=0;i<n;++i) { x=sc.nextInt(); ar.add(x); } long sum=0; Collections.sort(ar); for(int i=0;i<ar.size();++i) { sum+=Math.abs((i+1)-(Integer)ar.get(i)); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
327c6942e185c214404dab1472bf2daf
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.ArrayList; import java.util.Collections; //import java.util.LinkedList; //import java.util.List; import java.util.Scanner; import java.util.Vector; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; //Long ar[]=new Long[n]; ArrayList<Integer> ar=new ArrayList<Integer>(); int a=1; for(int i=0;i<n;++i) { ar.add(sc.nextInt()); } long sum=0; Collections.sort(ar); for(int i=0;i<ar.size();++i) { sum+=Math.abs((i+1)-(Integer)ar.get(i)); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
26c0d6f21fca508108478e92c3638195
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.ArrayList; import java.util.Collections; //import java.util.LinkedList; //import java.util.List; import java.util.Scanner; import java.util.Vector; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; //Long ar[]=new Long[n]; ArrayList<Integer> ar=new ArrayList<Integer>(100000); for(int i=0;i<n;++i) { ar.add(sc.nextInt()); } long sum=0; Collections.sort(ar); for(int i=0;i<ar.size();++i) { sum+=Math.abs((i+1)-(Integer)ar.get(i)); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
e602b477537eaef6aeeaa59c8497bab3
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Scanner; import java.util.Vector; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; //Long ar[]=new Long[n]; Vector<Integer> ar=new Vector<Integer>(); for(int i=0;i<n;++i) { x=sc.nextInt(); ar.add(x); } long sum=0; Collections.sort(ar); for(int i=0;i<ar.size();++i) { sum+=Math.abs((i+1)-(Integer)ar.get(i)); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
28d0c61f9cdf95c3aebf868a0d3092e0
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.Collections; //import java.util.LinkedList; //import java.util.List; import java.util.Scanner; import java.util.Vector; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; //Long ar[]=new Long[n]; Vector<Integer> ar=new Vector<Integer>(); for(int i=0;i<n;++i) { ar.add(sc.nextInt()); } long sum=0; Collections.sort(ar); for(int i=0;i<ar.size();++i) { sum+=Math.abs((i+1)-(Integer)ar.get(i)); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
ac8948650af37b0af30687958ae53eeb
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.ArrayList; import java.util.Collections; //import java.util.LinkedList; //import java.util.List; import java.util.Scanner; import java.util.Vector; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; //Long ar[]=new Long[n]; ArrayList<Integer> ar=new ArrayList<Integer>(); int a=9; for(int i=0;i<n;++i) { ar.add(sc.nextInt()); } long sum=0; Collections.sort(ar); for(int i=0;i<ar.size();++i) { sum+=Math.abs((i+1)-(Integer)ar.get(i)); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
5915f0e34f1ad39143aa8707601a005e
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.ArrayList; import java.util.Collections; //import java.util.LinkedList; //import java.util.List; import java.util.Scanner; import java.util.Vector; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; //Long ar[]=new Long[n]; ArrayList<Integer> ar=new ArrayList<Integer>(100000); int a=6; for(int i=0;i<n;++i) { ar.add(sc.nextInt()); } long sum=0; Collections.sort(ar); for(int i=0;i<ar.size();++i) { sum+=Math.abs((i+1)-(Integer)ar.get(i)); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
5f491769e795cf32083fe75619f1a656
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.ArrayList; import java.util.Collections; //import java.util.LinkedList; //import java.util.List; import java.util.Scanner; //import java.util.Vector; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; //Long ar[]=new Long[n]; ArrayList<Integer> ar=new ArrayList<Integer>(); int a=9; for(int i=0;i<n;++i) { ar.add(sc.nextInt()); } long sum=0; Collections.sort(ar); for(int i=0;i<ar.size();++i) { sum+=Math.abs((i+1)-(Integer)ar.get(i)); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
b237bfb353736e5020b77185c340463a
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.ArrayList; import java.util.Collections; //import java.util.LinkedList; //import java.util.List; import java.util.Scanner; import java.util.Vector; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; //Long ar[]=new Long[n]; ArrayList<Integer> ar=new ArrayList<Integer>(); for(int i=0;i<n;++i) { ar.add(sc.nextInt()); } long sum=0; Collections.sort(ar); for(int i=0;i<ar.size();++i) { sum+=Math.abs((i+1)-(Integer)ar.get(i)); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
29a41df85e8c31fa57b69b8ebb169644
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Scanner; import java.util.Vector; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; //Long ar[]=new Long[n]; Vector<Integer> ar=new Vector<Integer>(300005); for(int i=0;i<n;++i) { x=sc.nextInt(); ar.add(x); } long sum=0; Collections.sort(ar); for(int i=0;i<ar.size();++i) { sum+=Math.abs((i+1)-(Integer)ar.get(i)); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
318856e9f1e6c4880e60df3b78749c08
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.ArrayList; import java.util.Collections; //import java.util.LinkedList; //import java.util.List; import java.util.Scanner; import java.util.Vector; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; //Long ar[]=new Long[n]; ArrayList<Integer> ar=new ArrayList<Integer>(); int a=2; for(int i=0;i<n;++i) { ar.add(sc.nextInt()); } long sum=0; Collections.sort(ar); for(int i=0;i<ar.size();++i) { sum+=Math.abs((i+1)-(Integer)ar.get(i)); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
4a57ef165a338393326971cbdfa8c203
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Scanner; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; //Long ar[]=new Long[n]; ArrayList ar=new ArrayList(); for(int i=0;i<n;++i) { x=sc.nextInt(); ar.add(x); } long sum=0; Collections.sort(ar); for(int i=0;i<ar.size();++i) { sum+=Math.abs((i+1)-(Integer)ar.get(i)); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
256083097e920bd228456d374010b55e
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Scanner; import java.util.Vector; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; //Long ar[]=new Long[n]; Vector<Integer> ar=new Vector<Integer>(300005); for(int i=0;i<n;++i) { x=sc.nextInt(); ar.add(x); } long sum=0; Collections.sort(ar); for(int i=0;i<ar.size();++i) { sum+=Math.abs((i+1)-(Integer)ar.get(i)); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
80ecf5738b7ce5e4e5a1caf1ef6ea8ea
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.Arrays; import java.util.Scanner; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; Long ar[]=new Long[n]; for(int i=0;i<n;++i) { ar[i]=sc.nextLong(); } long sum=0; Arrays.sort(ar); for(int i=0;i<n;++i) { sum+=Math.abs((i+1)-ar[i]); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
68bc0f98752565377dfca11d7c66d82c
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.Collections; //import java.util.LinkedList; //import java.util.List; import java.util.Scanner; import java.util.Vector; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; //Long ar[]=new Long[n]; Vector<Integer> ar=new Vector<Integer>(100000); for(int i=0;i<n;++i) { x=sc.nextInt(); ar.add(x); } long sum=0; Collections.sort(ar); for(int i=0;i<ar.size();++i) { sum+=Math.abs((i+1)-(Integer)ar.get(i)); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
9ae46a012d27e29287f6f5940a2e0ef9
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.ArrayList; import java.util.Collections; //import java.util.LinkedList; //import java.util.List; import java.util.Scanner; import java.util.Vector; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; //Long ar[]=new Long[n]; ArrayList<Integer> ar=new ArrayList<Integer>(5000); int a=9; for(int i=0;i<n;++i) { ar.add(sc.nextInt()); } long sum=0; Collections.sort(ar); for(int i=0;i<ar.size();++i) { sum+=Math.abs((i+1)-(Integer)ar.get(i)); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
f3b77fc6f38272ccc2362d26faf70a14
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Scanner; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; //Long ar[]=new Long[n]; ArrayList ar=new ArrayList(300005); for(int i=0;i<n;++i) { x=sc.nextInt(); ar.add(x); } long sum=0; Collections.sort(ar); for(int i=0;i<ar.size();++i) { sum+=Math.abs((i+1)-(Integer)ar.get(i)); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
4de80a5cf4966fa491d1fe7428c992ba
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.ArrayList; import java.util.Collections; //import java.util.LinkedList; //import java.util.List; import java.util.Scanner; import java.util.Vector; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; //Long ar[]=new Long[n]; ArrayList<Integer> ar=new ArrayList<Integer>(300005); for(int i=0;i<n;++i) { ar.add(sc.nextInt()); } long sum=0; Collections.sort(ar); for(int i=0;i<ar.size();++i) { sum+=Math.abs((i+1)-(Integer)ar.get(i)); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
46a047da68e4e8c4a2956af58543530f
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.Collections; //import java.util.LinkedList; //import java.util.List; import java.util.Scanner; import java.util.Vector; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author workzone */ public class permut_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; n=sc.nextInt(); int x=1; //Long ar[]=new Long[n]; Vector<Integer> ar=new Vector<Integer>(); for(int i=0;i<n;++i) { x=sc.nextInt(); ar.add(x); } long sum=0; Collections.sort(ar); for(int i=0;i<ar.size();++i) { sum+=Math.abs((i+1)-(Integer)ar.get(i)); } System.out.println(sum); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
294e5e75615e10904d95d62a1b2cc2cf
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.*; public class Main{ static Scanner in = new Scanner(System.in); public static void main(String args[]){ int x = in.nextInt(); Integer a[] = new Integer[x]; for(int i=0;i<x;i++)a[i] = in.nextInt(); Arrays.sort(a); long ans = 0; for(int i=0;i<x;i++){ int yt = i + 1; // 3 0 // 0 3 // -1 1 if(a[i] < 0 && a[i] < yt){ ans = ans + (Math.abs(a[i]) + yt); } // 1 2 if(a[i] >= 0 && a[i] < yt){ ans = ans + (yt - a[i]); } // 3 1 if(a[i] >= 0 && a[i] > yt){ ans = ans + (a[i] - yt); } } System.out.println(ans); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
81c6b21278c2606112a8f3eb836402ce
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.util.*; public class Main{ static Scanner in = new Scanner(System.in); public static void main(String args[]){ int x = in.nextInt(); Integer a[] = new Integer[x]; for(int i=0;i<x;i++)a[i] = in.nextInt(); Arrays.sort(a); long ans = 0; int t = 1; for(int i=0;i<x;i++){ ans = ans + Math.abs(a[i] - t++); } System.out.println(ans); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
b7bbe377cc21fd5f297584c774f720ed
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; import java.util.TreeSet; public class l025 { public static void main(String[] args) throws Exception { // StringTokenizer stok = new StringTokenizer(new Scanner(new File("F:/books/input.txt")).useDelimiter("\\A").next()); StringTokenizer stok = new StringTokenizer(new Scanner(System.in).useDelimiter("\\A").next()); StringBuilder sb = new StringBuilder(); Integer n = Integer.parseInt(stok.nextToken()); Integer[] a = new Integer[n]; for(int i=0;i<n;i++) a[i] = Integer.parseInt(stok.nextToken()); Arrays.sort(a); long tot = 0; for(int i=1;i<=n;i++) { int v = a[i-1]; tot += Math.abs(i-v); } System.out.println(tot); } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
b3d102de2e1127758d152899d7579baa
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class BuildingPermutation { static boolean[] b; static int max; static int min; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); b = new boolean[n+1]; Integer[] input = new Integer[n]; for (int i = 0; i < input.length; i++) input[i] = sc.nextInt(); Arrays.sort(input); min = 1; max = input.length; long steps = 0; for (int i = 0; i < input.length; i++) { steps += Math.abs(input[i] - min); b[min] = true; nextMin(); } System.out.println(steps); } public static void nextMin() { while(min < b.length && b[min] == true) min ++; } public static void nextMax() { while(max > 0 && b[max] == true) max --; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if(x.charAt(0) == '-') { neg = true; start++; } for(int i = start; i < x.length(); i++) if(x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if(dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg?-1:1); } public boolean ready() throws IOException {return br.ready();} } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output
PASSED
bae32bdd2d179ec665dbe3de4ac6fda1
train_002.jsonl
1363879800
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int[] a = new int[n]; long min = 0; StringTokenizer st = new StringTokenizer(br.readLine()); for(int i = 0; i < n; i++) a[i] = Integer.parseInt(st.nextToken()); shuffle(a); Arrays.sort(a); for(int i = 1; i <= n; i++) if(a[i-1] > i) min += a[i-1] - i; else min += i - a[i-1]; System.out.println(min); } public static void shuffle(int[] a) { int n = a.length; for (int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n-i)); int tmp = a[i]; a[i] = a[r]; a[r] = tmp; } } }
Java
["2\n3 0", "3\n-1 -1 2"]
1 second
["2", "6"]
NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).
Java 8
standard input
[ "implementation", "sortings", "greedy" ]
86d5da999415fa75b3ee754a2a28605c
The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
1,200
Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
standard output