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
0cae3f20cb4d2704f60b84e115428412
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k > 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; import static java.util.Arrays.sort; import javafx.util.Pair; public class Main { public static void main(String[] args) { FastScanner input = new FastScanner(); int tc = input.nextInt(); while (tc-- > 0) { int n = input.nextInt(); int a[] = new int[n + 1]; int index[] = new int[n + 1]; for (int i = 1; i <= n; i++) { a[i] = input.nextInt(); index[a[i]] = i; } StringBuilder ans = new StringBuilder(); int lastbarier = n + 1; for (int i = n; i >= 1; i--) { if (index[i] < lastbarier) { for (int j = index[i]; j < lastbarier; j++) { ans.append(a[j] + " "); } lastbarier = index[i]; } } System.out.println(ans); } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
0f7e2f072f6ea8cb62caaeff9c579634
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.io.*; public class Main2 { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static FastReader sc = new FastReader(); public static void main (String[] args) { PrintWriter out = new PrintWriter(System.out); int t = 1; t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int a[] = new int[n]; int pos[] = new int[n+1]; for(int i=0;i<n;i++) { a[i] = sc.nextInt(); pos[a[i]] = i; } int start = 0,end = n; for(int i=n;i>=1;i--) { start = pos[i]; if(start<end) { for(int j=start;j<end;j++){ out.write(a[j]+" "); } end = start; } } out.write("\n"); } out.close(); } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
8dcb3b2d975261fe943a9292783e0936
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 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[] p=new int[n]; for(int i=0;i<n;i++) p[i]=sc.nextInt(); HashSet<Integer> set=new HashSet<>(); ArrayList<Integer> l=new ArrayList<>(); int n1=n; int c=0; for(int i=n1-1;i>=0;i--){ if(p[i]!=n){ set.add(p[i]); c++; } else if(p[i]==n){ for(int j=i;j<=i+c;j++) l.add(p[j]); c=0; n--; while(set.contains(n)) n--; } } for(int i=0;i<l.size();i++) System.out.print(l.get(i)+" "); System.out.println(); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
ecb58f8d25e12aba55882769012d7e83
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.*; import java.util.*; public class CardDeck{ static long mod = 1000000007L; static MyScanner sc = new MyScanner(); static void print(int arr[],int i,int j){ for(int k = i;k<=j;k++){ out.print(arr[k]+" "); } } static void solve() { int n = sc.nextInt(); int arr[] = sc.readIntArray(n); int mx[] = new int[n]; mx[0] = arr[0]; for(int i= 1;i<n;i++){ mx[i] = Math.max(mx[i-1],arr[i]); } int last = n-1; int j = mx[n-1]; for(int i = n-1;i>=0;i--){ if(arr[i]==j){ print(arr,i,last); last = i-1; if(i!=0) j = mx[i-1]; } } out.println(); } static class Pair implements Comparable<Pair>{ int num; int fre; Pair(int n,int f){ num = n; fre = f; } public int compareTo(Pair p){ return this.num - p.num; } } static void reverse(int arr[]){ int i = 0;int j = arr.length-1; while(i<j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } static long pow(long a, long b) { if (b == 0) return 1; long res = pow(a, b / 2); res = (res * res) % 1_000_000_007; if (b % 2 == 1) { res = (res * a) % 1_000_000_007; } return res; } static int lis(int arr[],int n){ int lis[] = new int[n]; lis[0] = 1; for(int i = 1;i<n;i++){ lis[i] = 1; for(int j = 0;j<i;j++){ if(arr[i]>arr[j]){ lis[i] = Math.max(lis[i],lis[j]+1); } } } int max = Integer.MIN_VALUE; for(int i = 0;i<n;i++){ max = Math.max(lis[i],max); } return max; } static boolean isPali(String str){ int i = 0; int j = str.length()-1; while(i<j){ if(str.charAt(i)!=str.charAt(j)){ return false; } i++; j--; } return true; } static long gcd(long a,long b){ if(b==0) return a; return gcd(b,a%b); } static String reverse(String str){ char arr[] = str.toCharArray(); int i = 0; int j = arr.length-1; while(i<j){ char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } String st = new String(arr); return st; } static boolean isprime(int n){ if(n==1) return false; if(n==3 || n==2) return true; if(n%2==0 || n%3==0) return false; for(int i = 5;i*i<=n;i+= 6){ if(n%i== 0 || n%(i+2)==0){ return false; } } return true; } public static void main(String[] args) { out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); // int t= 1; while(t-- >0){ solve(); // solve2(); // solve3(); } // Stop writing your solution here. ------------------------------------- out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int[] readIntArray(int n){ int arr[] = new int[n]; for(int i = 0;i<n;i++){ arr[i] = Integer.parseInt(next()); } return arr; } int[] reverse(int arr[]){ int n= arr.length; int i = 0; int j = n-1; while(i<j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; j--;i++; } return arr; } long[] readLongArray(int n){ long arr[] = new long[n]; for(int i = 0;i<n;i++){ arr[i] = Long.parseLong(next()); } return arr; } 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; } } private static void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(i); } } private static void sort(long[] arr) { List<Long> list = new ArrayList<>(); for (int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(i); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
6e9abefdae0cc1dc76f1904a25864d89
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
//package MyPackage; import java.io.*; import java.util.*; public class MyClass { final static int mod = 998_244_353; final static int mini = Integer.MIN_VALUE; final static int maxi = Integer.MAX_VALUE; static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long ncr(long n, long r) { if (r > n - r) r = n - r; long[] C = new long[(int) r + 1]; C[0] = 1; for (int i = 1; i <= n; i++) { for (long j = Math.min(i, r); j > 0; j--) C[(int) j] = (C[(int) j] + C[(int) j - 1]) % mod; } return C[(int) r]; } static boolean isPrime(long n) { if (n <= 1) return false; else if (n == 2) return true; else if (n % 2 == 0) return false; for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static boolean isPalindrome(String s) { int i = 0, j = s.length() - 1; while(i < j) { if(s.charAt(i) != s.charAt(j)) return false; i++; j--; } return true; } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long value : a) { l.add(value); } Collections.sort(l); for (int i = 0; i < l.size(); i++) a[i] = l.get(i); } static int ceil(int a, int b) { return a % b == 0 ? a / b : a / b + 1; } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int value : a) { l.add(value); } Collections.sort(l); for (int i = 0; i < l.size(); i++) a[i] = l.get(i); } static void reverse(int[] arr) { int l = 0; int h = arr.length - 1; while (l < h) { swap(arr, l, h); l++; h--; } } static String reverse(String s) { StringBuilder res = new StringBuilder(s); return res.reverse().toString(); } static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String n() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nint() { return Integer.parseInt(n()); } long nlong() { return Long.parseLong(n()); } double ndouble() { return Double.parseDouble(n()); } int[] narr(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nint(); return a; } int[][] narr(int n, int m) { int[][] a = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = nint(); return a; } String nline() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); // int t = 1; int t = sc.nint(); while(t-- > 0) { int n = sc.nint(); int[] arr = sc.narr(n); TreeSet<Integer> set = new TreeSet<>(); for(int i : arr) set.add(i); int x = n; int j = n - 1; int k = n; while(j >= 0) { while(j >= 0) { set.remove(arr[j]); if(arr[j] == x) break; j--; } if(j < 0) j = 0; for(int i = j ; i < k; i++) out.print(arr[i] + " "); k = j; j--; if(set.size() > 0) x = set.last(); //out.println(list); } out.println(); } out.flush(); out.close(); } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
cf994ea455c0a6a57f3dc58094bb888e
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.*; import java.util.*; public final class Solution { static PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static long mod = (long) 1e9 + 7; static Pair[] moves = new Pair[]{new Pair(-1, 0), new Pair(1, 0), new Pair(0, -1), new Pair(0, 1)}; public static void main(String[] args) { int t = i(); while (t-- > 0) { int n = i(); int[] arr = input(n); PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { return o2[1] - o1[1]; } }); for (int i = 0; i < n; i++) { pq.offer(new int[]{i, arr[i]}); } int r = n; while (true) { while (!pq.isEmpty() && pq.peek()[0] >= r) { pq.poll(); } if (pq.isEmpty()) { break; } int[] max = pq.poll(); for (int j = max[0]; j < r; j++) { out.print(arr[j] + " "); } r = max[0]; } out.println(); } out.flush(); } static boolean isPS(double number) { double sqrt = Math.sqrt(number); return ((sqrt - Math.floor(sqrt)) == 0); } static int sd(long i) { int d = 0; while (i > 0) { d += i % 10; i = i / 10; } return d; } static int[] leastPrime; static void sieveLinear(int N) { int[] primes = new int[N]; int idx = 0; leastPrime = new int[N + 1]; for (int i = 2; i <= N; i++) { if (leastPrime[i] == 0) { primes[idx++] = i; leastPrime[i] = i; } int curLP = leastPrime[i]; for (int j = 0; j < idx; j++) { int p = primes[j]; if (p > curLP || (long) p * i > N) { break; } else { leastPrime[p * i] = p; } } } } static int lower(long A[], long x) { int l = -1, r = A.length; while (r - l > 1) { int m = (l + r) / 2; if (A[m] >= x) { r = m; } else { l = m; } } return r; } static int upper(long A[], long x) { int l = -1, r = A.length; while (r - l > 1) { int m = (l + r) / 2; if (A[m] <= x) { l = m; } else { r = m; } } return l; } static void swap(int A[], int a, int b) { int t = A[a]; A[a] = A[b]; A[b] = t; } static int lowerBound(int A[], int low, int high, int x) { if (low > high) { if (x >= A[high]) { return A[high]; } } int mid = (low + high) / 2; if (A[mid] == x) { return A[mid]; } if (mid > 0 && A[mid - 1] <= x && x < A[mid]) { return A[mid - 1]; } if (x < A[mid]) { return lowerBound(A, low, mid - 1, x); } return lowerBound(A, mid + 1, high, x); } static long pow(long a, long b) { long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) { pow = (pow * x) % mod; } x = (x * x) % mod; b /= 2; } return pow; } static boolean isPrime(long N) { if (N <= 1) { return false; } if (N <= 3) { return true; } if (N % 2 == 0 || N % 3 == 0) { return false; } for (int i = 5; i * i <= N; i = i + 6) { if (N % i == 0 || N % (i + 2) == 0) { return false; } } return true; } static void print(char A[]) { for (char c : A) { out.print(c); } out.println(); } static void print(boolean A[]) { for (boolean c : A) { out.print(c + " "); } out.println(); } static void print(int A[]) { for (int c : A) { out.print(c + " "); } out.println(); } static void print(long A[]) { for (long i : A) { out.print(i + " "); } out.println(); } static void print(List<Integer> A) { for (int a : A) { out.print(a + " "); } } static void printYes() { out.println("YES"); } static void printNo() { out.println("NO"); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static String s() { return in.nextLine(); } static int[] input(int N) { int A[] = new int[N]; for (int i = 0; i < N; i++) { A[i] = in.nextInt(); } return A; } static long[] inputLong(int N) { long A[] = new long[N]; for (int i = 0; i < A.length; i++) { A[i] = in.nextLong(); } return A; } static int GCD(int a, int b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } static long GCD(long a, long b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } } class BIT { int n; int[] tree; public BIT(int n) { this.n = n; this.tree = new int[n + 1]; } public static int lowbit(int x) { return x & (-x); } public void update(int x) { while (x <= n) { ++tree[x]; x += lowbit(x); } } public int query(int x) { int ans = 0; while (x > 0) { ans += tree[x]; x -= lowbit(x); } return ans; } public int query(int x, int y) { return query(y) - query(x - 1); } } class SegmentTree { long[] t; public SegmentTree(int n) { t = new long[n + n]; Arrays.fill(t, Long.MIN_VALUE); } public long get(int i) { return t[i + t.length / 2]; } public void add(int i, long value) { i += t.length / 2; t[i] = value; for (; i > 1; i >>= 1) { t[i >> 1] = Math.max(t[i], t[i ^ 1]); } } // max[a, b] public long max(int a, int b) { long res = Long.MIN_VALUE; for (a += t.length / 2, b += t.length / 2; a <= b; a = (a + 1) >> 1, b = (b - 1) >> 1) { if ((a & 1) != 0) { res = Math.max(res, t[a]); } if ((b & 1) == 0) { res = Math.max(res, t[b]); } } return res; } } class Pair { int i, j; Pair(int i, int j) { this.i = i; this.j = j; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
dd86d5ee88f59a45a67b7247e478d13e
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.io.*; public class Solution{ public static void main(String arg[]){ Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while (t > 0) { int i, j = 0, k = 0, n = scan.nextInt(); int [] arr1 = new int[n]; int [] arr2 = new int[n]; for (i = 0; i < n; ++i) { arr1[i] = scan.nextInt(); if (arr1[i] > j) { j = arr1[i]; k = i; } arr2[i] = k; } while (n > 0) { for (i = arr2[n - 1]; i < n; ++i) { System.out.print(arr1[i] + " "); } n = arr2[n - 1]; } System.out.println(""); t--; } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
d67ee713217e1035e2d52c6540d9ae1b
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class solve { static Scanner sc; static solve obj; public static void main (String[] args) throws java.lang.Exception { sc = new Scanner(System.in); // testCases int testCases = Integer.parseInt(sc.nextLine()); // scan first-word obj = new solve(); while (testCases > 0) { obj.solve(); testCases--; } } void solve () { int n = Integer.parseInt(sc.nextLine()); ArrayList<Integer> nums = new ArrayList<Integer>(n); int [] indexes = new int[n+1]; int temp = n, num = 0, j = 0; while (temp-- > 0){ num = sc.nextInt(); nums.add(num); indexes[num] = j++; } sc.nextLine(); // solve int index = 0, prevIndex = n; for (int i = n; i >= 0; i--) { index = indexes[i]; if (index < prevIndex) { for (int k = index; k < prevIndex; k++) System.out.print(nums.get(k) + " "); prevIndex = index; } } System.out.println(); } } // greedy solution
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
19c4b61624858a4b508571030193375a
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
//package codeforces.problem.feb; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; /* https://codeforces.com/problemset/problem/1492/B */ public class Task1492_B { static void solve() { int n = FS.nextInt(); int[] cards = new int[n]; int[] map = new int[n + 1]; for (int i = 0; i < n; i++) { cards[i] = FS.nextInt(); map[cards[i]] = i; } StringBuilder sb = new StringBuilder(); int end = n; for (int i = n; i > 0; i--) { int index = map[i];//max index if (map[i] != -1) { for (int j = index; j < end; j++) { map[cards[j]] = -1; sb.append("" + cards[j]); sb.append(' '); } end = index; } } System.out.println(sb.toString()); } public static void main(String[] args) { int T = FS.nextInt(); for (int tt = 0; tt < T; tt++) { solve(); } FS.pt.close(); } static class FS { private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private static StringTokenizer st = new StringTokenizer(""); static PrintWriter pt = new PrintWriter(System.out); static String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } static int nextInt() { return Integer.parseInt(next()); } static int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } static double nextDouble() { return Double.parseDouble(next()); } static int[][] read2Array(int n, int m) { int[][] a = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = nextInt(); } } return a; } static long[][] read2ArrayL(int n, int m) { long[][] a = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = nextLong(); } } return a; } void printArr(long[] arr) { for (long value : arr) { pt.print(value); pt.print(" "); } pt.println(); } static long[] readArrayL(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } static void printArr(int[] arr) { for (int value : arr) { pt.print(value); pt.print(" "); } pt.println(); } static void printArrL(int[] arr) { for (int value : arr) { pt.print(value); pt.print(" "); } pt.println(); } static void print2Arr(int[][] arr, int n, int m) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { pt.print(arr[i][j]); pt.print(" "); } pt.println(); } pt.println(); } static void print2Arr(long[][] arr, int n, int m) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { pt.print(arr[i][j]); pt.print(" "); } pt.println(); } pt.println(); } static List<Long> readListLong(int n) { List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) { list.add(nextLong()); } return list; } static List<Integer> readListInt(int n) { List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) { list.add(nextInt()); } return list; } static <T> void printList(List<T> list) { for (T value : list) { pt.print(value); pt.print(" "); } pt.println(); } static void close() { pt.close(); } static long nextLong() { return Long.parseLong(next()); } } static class Mat { public static long pow(long a, long exp) { return (long) Math.pow(a, exp); } } static class NumberTheory { public static boolean isPrime(long n) { if (n <= 1) return false; if (n < 4) return true; if (n % 2 == 0) return false; if (n % 3 == 0) return false; for (int i = 5, next = 2; i * i <= n; i += next, next = 6 - next) if (n % i == 0) return false; return true; } public static long gcd(long a, long b) { while (b != 0) { long tmp = a % b; a = b; b = tmp; } return a; } public static long lcm(long a, long b) { return a * b / gcd(a, b); } public static List<Long> primeFactors(long n) { List<Long> ans = new ArrayList<>(); while (n % 2 == 0) { ans.add(2L); n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i + 2) for (long i = 3; i <= Math.sqrt(n); i += 2) { // While i divides n, print i and divide n while (n % i == 0) { ans.add(i); n /= i; } } // This condition is to handle the case when // n is a prime number greater than 2 if (n > 2) { ans.add(n); } return ans; } static long fact(long n) { if (n == 0 || n == 1) return 1L; long r = 1; for (int i = 1; i <= n; i++) { r *= i; } return r; } static long fact(long n, long mod) { if (n <= 1) return 1; long ans = 1; for (int i = 1; i <= n; i++) { ans = (ans * i) % mod; } return ans; } static long fastExp(long x, long n, long mod) { long ans = 1; while (n > 0) { if (n % 2 == 1) ans = (ans * x) % mod; x = (x * x) % mod; n /= 2; } return ans; } } static class BinarySearch { public static int lowerBound(int[] arr, int key) { int low = 0; int high = arr.length - 1; while (low < high) { int mid = low + (high - low) / 2; if (arr[mid] >= key) { high = mid; } else { low = mid + 1; } } return low; } public static int lowerBound(long[] arr, long key) { int low = 0; int high = arr.length - 1; while (low < high) { int mid = low + (high - low) / 2; if (arr[mid] >= key) { high = mid; } else { low = mid + 1; } } return low; } public static <T extends Comparable<T>> int lowerBound(List<T> arr, T key) { int low = 0; int high = arr.size() - 1; while (low < high) { int mid = low + (high - low) / 2; if (arr.get(mid).compareTo(key) >= 0) { high = mid; } else { low = mid + 1; } } return low; } public static int upperBound(int[] arr, int key) { int low = 0; int high = arr.length - 1; while (low < high) { int mid = low + (high - low + 1) / 2; if (arr[mid] <= key) { low = mid; } else { high = mid - 1; } } return low; } public static int upperBound(long[] arr, long key) { int low = 0; int high = arr.length - 1; while (low < high) { int mid = low + (high - low + 1) / 2; if (arr[mid] <= key) { low = mid; } else { high = mid - 1; } } return low; } public static <T extends Comparable<T>> int upperBound(List<T> arr, T key) { int low = 0; int high = arr.size() - 1; while (low < high) { int mid = low + (high - low + 1) / 2; if (arr.get(mid).compareTo(key) >= 0) { low = mid; } else { high = mid - 1; } } return low; } } static class Pair<T, K> { T first; K second; public Pair(T first, K second) { this.first = first; this.second = second; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return Objects.equals(first, pair.first) && Objects.equals(second, pair.second); } @Override public int hashCode() { return Objects.hash(first, second); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
568090dde1c31e95091594fabc1ce443
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; public class Question2 { public static void main(String args[]) { Scanner in=new Scanner(System.in); int t=in.nextInt(); int i,j; while(t-->0) { int n=in.nextInt(); //int k=in.nextInt(); int a[]=new int[n]; int sa[]=new int[n]; Map<Integer,Integer> mp=new HashMap<>(); for(i=0;i<n;i++) { a[i]=in.nextInt(); sa[i]=a[i]; mp.put(a[i],0); } Arrays.sort(sa); int c=0,d=n,x=n-1; for(i=n-1;i>=0;i--) { //System.out.println(i+" "+x+" "+d); if(mp.get(sa[x])==1) { x--; i++; continue; } if(sa[x]==a[i]) { x--; for(j=i;j<=d-1;j++) { System.out.print(a[j]+" "); mp.put(a[j],1); if(x==-1) break; if(sa[x]==a[j]) x--; } d=i; } } //func(a,n); System.out.println(); } } } // static void func(int arr[],int n) // { // int i,p=0; // int max=0; // for(i=n-1;i>=0;i--) // { // if(arr[i]>max) // { // max=arr[i]; // p=i; // } // } // for(i=p;i<=n-1;i++) // { // System.out.print(arr[i]+" "); // } // if(p!=0) // func(arr,p); // } // }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
63fb6ed2b555e9604f0e5b6ce25c3766
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; public class Main{ static int[] a=new int[100005]; static Stack<Integer> q=new Stack<>(); public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t--!=0){ int n=sc.nextInt(); for(int i=1;i<=n;i++){ a[i]=sc.nextInt(); } a[0]=a[1]; int start=1; for(int i=1;i<=n;i++){ if(a[0]<a[i]){ a[0]=a[i]; for(int j=i-1;j>=start;j--){ q.push(a[j]); } start=i; } } for(int i=n;i>=start;i--){ q.push(a[i]); } Iterator it=q.iterator(); while (it.hasNext()){ System.out.print(q.pop()+" "); } System.out.println(); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
3ae394fb6e4472d2d72ec10287dc88dc
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.*; import java.util.*; public class CardDeck{ public static void main (String [] args) throws IOException { // Use BufferedReader rather than RandomAccessFile; it's much faster //BufferedReader f = new BufferedReader(new FileReader("carddeck.in")); BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); // input file name goes above // Use StringTokenizer vs. readLine/split -- lots faster StringTokenizer st = new StringTokenizer(f.readLine()); // Get line, break into tokens int numberOfCases = Integer.parseInt(st.nextToken()); for(int i = 0; i < numberOfCases; i ++){ StringTokenizer st1 = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st1.nextToken()); StringTokenizer st2 = new StringTokenizer(f.readLine()); ArrayList<Long> sortedDeck = new ArrayList<Long>(); int m = -1; int [] deck = new int[n]; for (int j=0; j < n; j++) { deck[j] = Integer.parseInt(st2.nextToken()); // pj * n + j if (deck[j] > m) { m = deck[j]; sortedDeck.add((long)deck[j] * n + j); } } //Collections.sort(sortedDeck); // output System.out.println(reorder(deck, sortedDeck)); } } public static String reorder(int [] deck, ArrayList<Long> sorted) { int length = deck.length; if (length == 1) return "" + deck[0]; StringBuffer sb = new StringBuffer(); int dLength = length; while (sorted.size() > 0) { // largest long l = sorted.get(sorted.size() -1); int index = (int) (l % length); // index of the largest int cIndex = index; while (cIndex < dLength) { sb.append(deck[cIndex]); cIndex ++; sb.append(" "); } // end of while // trim the array sorted.remove(sorted.size() -1); dLength = index; } return sb.toString().trim(); } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
9caafb33be55ed7242ed3644d8fa6e5f
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.awt.*; import java.io.*; import java.util.*; import java.util.List; public class Solution { public static void main(String[] args) throws IOException { FastReader sc = new FastReader(); FastWriter out = new FastWriter(); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); LinkedList<Integer> list = new LinkedList<>(); int[] arr = new int[n+1]; for (int j = 0; j < n; j++) { int temp = sc.nextInt(); list.add(temp); arr[temp] = j; } for (int j = n; j >= 1; j--) { int index = arr[j]; if ( index < list.size()) { List<Integer> temp = list.subList(index, list.size()); for (int e:temp) { out.write(e+" "); } int e = temp.size(); while (e > 0) { list.removeLast(); e--; } } } out.writeln(""); } out.flush(); } // FastReader public 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(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // FastWriter public static class FastWriter { BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); public void writeln(Object i) throws IOException { log.write(i + "\n"); } public void write(Object i) throws IOException { log.write((String) i); } public void flush() throws IOException { log.flush(); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
dbbd7d817da1ec5fa88ea2fd6fe0dc2e
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.*; import java.util.*; public class p4 { public static void main(String[] args) throws IOException { Scanner sc= new Scanner(System.in); Long t= sc.nextLong(); for(int k=0;k<t;k++) { long n=sc.nextLong(); Stack<Long> s = new Stack<Long>(); TreeSet<Long> x = new TreeSet<Long>(); Stack<Long> tmp = new Stack<Long>(); long j=1; for (int i=0;i<n;i++) { s.push(sc.nextLong()); x.add(j++); } StringBuilder ans= new StringBuilder(); while(!s.isEmpty()) { long curr= s.pop(); tmp.push(curr); while(!s.isEmpty() && curr!=x.last() ) { curr= s.pop(); tmp.push(curr); } x.pollLast(); while(!tmp.isEmpty()) { if(x.contains(tmp.peek())) { x.remove(tmp.peek()); } ans.append(tmp.pop()+" "); } } System.out.println(ans); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
eb19371659f7e84a1bf73f93b9ab47cd
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.*; import java.util.*; public class GFG { public static class Pair{ int x; int y; Pair(int x,int y){ this.x = x; this.y = y; } } public static void main (String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0){ int n = sc.nextInt(); int[] arr = new int[n]; int[] res = new int[n]; PriorityQueue<Pair> pq = new PriorityQueue<Pair>((a,b) -> b.x-a.x); Set<Integer> set = new HashSet<Integer>(); for(int i=0;i<n;i++) { arr[i] = sc.nextInt(); pq.add(new Pair(arr[i],i)); set.add(arr[i]); } int c = 0,prev = n-1,flag = 0; while(prev>=0 && c<n){ while(!set.contains(pq.peek().x)) pq.poll(); Pair p = pq.poll(); for(int i=p.y;i<=prev;i++){ res[c++] = arr[i]; set.remove(arr[i]); if(c==n){ flag = 1; break; } if(pq.peek().x==arr[i]) pq.poll(); } if(flag==1) break; prev = p.y-1; } for(int i=0;i<n;i++) System.out.print(res[i]+" "); System.out.println(); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
c1c004438bf2ec620eaf461e2072f817
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.io.*; public class Solution { private static ArrayList<Integer> prime = new ArrayList<>(); public static void main(String[] args) throws IOException { Scanner in=new Scanner(System.in); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringBuffer out = new StringBuffer(); int T = in.nextInt(); OUTER: while(T-->0) { int n=in.nextInt(); int p[]=new int[n+1]; int index[]=new int[n+1]; for(int i=1; i<=n; i++) { p[i]=in.nextInt(); index[p[i]]=i; } for(int i=n, top=n; i>=1; i--) { if(index[i]==-1) continue; int newTop=top; for(int j=index[i]; j<=top; j++) { out.append(p[j]+" "); index[p[j]]=-1; newTop-=1; } top=newTop; } out.append("\n"); } System.out.print(out); } private static int gcd(int a, int b) { if (a==0) return b; return gcd(b%a, a); } private static int toInt(String s) { return Integer.parseInt(s); } private static long toLong(String s) { return Long.parseLong(s); } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
cf1985b7b00a27ca9c92f74e7678087a
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.io.*; public class Solution { private static ArrayList<Integer> prime = new ArrayList<>(); public static void main(String[] args) throws IOException { Scanner in=new Scanner(System.in); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringBuffer out = new StringBuffer(); int T = in.nextInt(); OUTER: while(T-->0) { int n=in.nextInt(); int p[]=new int[n]; TreeSet<Integer> set = new TreeSet<>((i1, i2)->(p[i2]-p[i1])); for(int i=0; i<n; i++) { p[i]=in.nextInt(); set.add(i); } int top=n; while(!set.isEmpty()) { int maxIndex=set.first(); for(int i=maxIndex; i<top; i++) { out.append(p[i]+" "); set.remove(i); } top=maxIndex; } out.append("\n"); } System.out.print(out); } private static int gcd(int a, int b) { if (a==0) return b; return gcd(b%a, a); } private static int toInt(String s) { return Integer.parseInt(s); } private static long toLong(String s) { return Long.parseLong(s); } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
66c601f485dc0efef0a51333f9011f64
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.*; import java.util.*; import java.lang.Math; public class Main{ static BufferedReader reader; static StringTokenizer tokenizer; static PrintWriter writer; static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } static long nextLong() throws IOException { return Long.parseLong(nextToken()); } static double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } static String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static String nexts() throws IOException { tokenizer = new StringTokenizer(reader.readLine()); String s=""; while (tokenizer.hasMoreTokens()) { s+=tokenizer.nextElement()+" "; } return s; } public static int gcd(int x, int y){ if (y == 0) return x; else return gcd(y, x % y); } public static boolean isPrime(int nn) { if(nn<=1){ return false; } if(nn<=3){ return true; } if (nn%2==0||nn%3==0){ return false; } for (int i=5;i*i<=nn;i=i+6){ if(nn%i==0||nn%(i+2)==0){ return false; } } return true; } public static void shuffle(int[] A) { for (int i = 0; i < A.length; i++) { int j = (int)(i * Math.random()); int tmp = A[i]; A[i] = A[j]; A[j] = tmp; } } public static void shufflel(Long[] A) { for (int i = 0; i < A.length; i++) { int j = (int)(i * Math.random()); long tmp = A[i]; A[i] = A[j]; A[j] = tmp; } } public static String reverse(String input) { StringBuilder input2=new StringBuilder(); input2.append(input); input2 = input2.reverse(); return input2.toString(); } public static long power(int x, long n) { // long mod = 1000000007; if (n == 0) { return 1; } long pow = power(x, n / 2); if ((n & 1) == 1) { return (x * pow * pow); } return (pow * pow); } public static long power(long x, long y, long p) //x^y%p { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) { res = (res * x) % p; } y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long ncr(int n, int k) { long res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) { k = n - k; } // Calculate value of [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1] for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } public static int inv(int a, int m) //Returns modulo inverse of a with respect to m using extended euclid algorithm { int m0=m,t,q; int x0=0,x1=1; if(m==1){ return 0; } while(a>1) { q=a/m; t=m; m=a%m;a=t; t=x0; x0=x1-q*x0; x1=t; } if(x1<0){ x1+=m0; } return x1; } static int modInverse(int n, int p) { return (int)power((long)n, (long)(p - 2),(long)p); } static int ncrmod(int n, int r, int p) { if (r == 0) // Base case return 1; int[] fac = new int[n + 1]; // Fill factorial array so that we can find all factorial of r, n and n-r fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; //IMPORTANT } static int upper(int a[], int x) //Returns rightest index of x in sorted arr else -1 { //If not present returns index of just smaller element int n=a.length; int l=0; int r=n-1; int ans=-1; while(l<=r){ int m=(r-l)/2+l; if(a[m]<=x){ ans=m; l=m+1; } else{ r=m-1; } } return ans; } static int lower(int a[], int x) //Returns leftest index of x in sorted arr else n { //If not present returns index of just greater element int n=a.length; int l=0; int r=n-1; int ans=n; while(l<=r){ int m=(r-l)/2+l; if(a[m]>=x){ ans=m; r=m-1; } else{ l=m+1; } } return ans; } public static long stringtoint(String s){ // Convert bit string to number long a = 1; long ans = 0; StringBuilder sb = new StringBuilder(); sb.append(s); sb.reverse(); s=sb.toString(); for(int i=0;i<s.length();i++){ ans = ans + a*(s.charAt(i)-'0'); a = a*2; } return ans; } String inttostring(long a,long m){ // a is number and m is length of string required String res = ""; // Convert a number in bit string representation for(int i=0;i<(int)m;i++){ if((a&(1<<i))==1){ res+='1'; }else res+='0'; } StringBuilder sb = new StringBuilder(); sb.append(res); sb.reverse(); res=sb.toString(); return res; } static class R implements Comparable<R>{ int x, y; public R(int x, int y) { //a[i]=new R(x,y); this.x = x; this.y = y; } public int compareTo(R o) { return x-o.x; //Increasing order(Which is usually required) } } /* FUNCTION TO SORT HASHMAP ACCORDING TO VALUE //USE IT AS FOLLOW //Make a new list List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(h.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); //This is used to sort string stored as VALUE, use below function here accordingly } }); // put data from sorted list to new hashmap HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>(); for (Map.Entry<String, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; //Now temp is our new sorted hashmap OR for(int i = 0;i<h.size();i++) //accessing elements from the list made { int count = list.get(i).getValue(); //Value while(count >= 0) { int key=list.get(i).getKey(); //key count -- ; } } //////////////////////////////////////////////////////////////////////////////////////// public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) { if(o1.getValue() != o2.getValue()) return o1.getValue() > o2.getValue()?1:-1; //Sorts Value in increasing order else if(o1.getValue() == o2.getValue()){ return o1.getKey() > o2.getKey() ? 1:-1; //If value equal, then sorts key in increasing order } return Integer.MIN_VALUE; } */ //Check if palindrome string - System.out.print(A.equals(new StringBuilder(A).reverse().toString())?"Yes":"No"); public static void main(String[] args) throws IOException { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } private static void solve() throws IOException { int t = nextInt(); while(t-->0){ //String s= nextToken(); //String[] s = str.split("\\s+"); //ArrayList<Integer> ar=new ArrayList<Integer>(); //HashSet<Integer> set=new HashSet<Integer>(); //HashMap<Integer,String> h=new HashMap<Integer,String>(); //R[] a1=new R[n]; //char[] c=nextToken().toCharArray(); //PriorityQueue<Integer> pq=new PriorityQueue<Integer>(Collections.reverseOrder()); NavigableSet<Integer> pq=new TreeSet<>(); int n = nextInt(); //long n = nextLong(); Integer[] a=new Integer[n]; //long[] a=new long[n]; for(int i=0;i<n;i++){ a[i]=nextInt(); pq.add(a[i]); } //shuffle(a); //shufflel(a); //Arrays.sort(a); int j=-1; int p=n; for(int i=n-1;i>=0;i--){ while(i>=0&&a[i]!=pq.last()){ i--; } j=i; for(int k=j;k<p;k++) { pq.remove(a[k]); writer.print(a[k]+" "); } p=i; } writer.println(); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
c87eafd666adc2d759c722ffbadeae8f
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.*; import java.util.*; import java.lang.Math; public class Main{ static BufferedReader reader; static StringTokenizer tokenizer; static PrintWriter writer; static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } static long nextLong() throws IOException { return Long.parseLong(nextToken()); } static double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } static String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static String nexts() throws IOException { tokenizer = new StringTokenizer(reader.readLine()); String s=""; while (tokenizer.hasMoreTokens()) { s+=tokenizer.nextElement()+" "; } return s; } public static int gcd(int x, int y){ if (y == 0) return x; else return gcd(y, x % y); } public static boolean isPrime(int nn) { if(nn<=1){ return false; } if(nn<=3){ return true; } if (nn%2==0||nn%3==0){ return false; } for (int i=5;i*i<=nn;i=i+6){ if(nn%i==0||nn%(i+2)==0){ return false; } } return true; } public static void shuffle(int[] A) { for (int i = 0; i < A.length; i++) { int j = (int)(i * Math.random()); int tmp = A[i]; A[i] = A[j]; A[j] = tmp; } } public static void shufflel(Long[] A) { for (int i = 0; i < A.length; i++) { int j = (int)(i * Math.random()); long tmp = A[i]; A[i] = A[j]; A[j] = tmp; } } public static String reverse(String input) { StringBuilder input2=new StringBuilder(); input2.append(input); input2 = input2.reverse(); return input2.toString(); } public static long power(int x, long n) { // long mod = 1000000007; if (n == 0) { return 1; } long pow = power(x, n / 2); if ((n & 1) == 1) { return (x * pow * pow); } return (pow * pow); } public static long power(long x, long y, long p) //x^y%p { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) { res = (res * x) % p; } y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long ncr(int n, int k) { long res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) { k = n - k; } // Calculate value of [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1] for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } public static int inv(int a, int m) //Returns modulo inverse of a with respect to m using extended euclid algorithm { int m0=m,t,q; int x0=0,x1=1; if(m==1){ return 0; } while(a>1) { q=a/m; t=m; m=a%m;a=t; t=x0; x0=x1-q*x0; x1=t; } if(x1<0){ x1+=m0; } return x1; } static int modInverse(int n, int p) { return (int)power((long)n, (long)(p - 2),(long)p); } static int ncrmod(int n, int r, int p) { if (r == 0) // Base case return 1; int[] fac = new int[n + 1]; // Fill factorial array so that we can find all factorial of r, n and n-r fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; //IMPORTANT } static int upper(int a[], int x) //Returns rightest index of x in sorted arr else -1 { //If not present returns index of just smaller element int n=a.length; int l=0; int r=n-1; int ans=-1; while(l<=r){ int m=(r-l)/2+l; if(a[m]<=x){ ans=m; l=m+1; } else{ r=m-1; } } return ans; } static int lower(int a[], int x) //Returns leftest index of x in sorted arr else n { //If not present returns index of just greater element int n=a.length; int l=0; int r=n-1; int ans=n; while(l<=r){ int m=(r-l)/2+l; if(a[m]>=x){ ans=m; r=m-1; } else{ l=m+1; } } return ans; } public static long stringtoint(String s){ // Convert bit string to number long a = 1; long ans = 0; StringBuilder sb = new StringBuilder(); sb.append(s); sb.reverse(); s=sb.toString(); for(int i=0;i<s.length();i++){ ans = ans + a*(s.charAt(i)-'0'); a = a*2; } return ans; } String inttostring(long a,long m){ // a is number and m is length of string required String res = ""; // Convert a number in bit string representation for(int i=0;i<(int)m;i++){ if((a&(1<<i))==1){ res+='1'; }else res+='0'; } StringBuilder sb = new StringBuilder(); sb.append(res); sb.reverse(); res=sb.toString(); return res; } static class R implements Comparable<R>{ int x, y; public R(int x, int y) { //a[i]=new R(x,y); this.x = x; this.y = y; } public int compareTo(R o) { return x-o.x; //Increasing order(Which is usually required) } } /* FUNCTION TO SORT HASHMAP ACCORDING TO VALUE //USE IT AS FOLLOW //Make a new list List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(h.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); //This is used to sort string stored as VALUE, use below function here accordingly } }); // put data from sorted list to new hashmap HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>(); for (Map.Entry<String, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; //Now temp is our new sorted hashmap OR for(int i = 0;i<h.size();i++) //accessing elements from the list made { int count = list.get(i).getValue(); //Value while(count >= 0) { int key=list.get(i).getKey(); //key count -- ; } } //////////////////////////////////////////////////////////////////////////////////////// public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) { if(o1.getValue() != o2.getValue()) return o1.getValue() > o2.getValue()?1:-1; //Sorts Value in increasing order else if(o1.getValue() == o2.getValue()){ return o1.getKey() > o2.getKey() ? 1:-1; //If value equal, then sorts key in increasing order } return Integer.MIN_VALUE; } */ //Check if palindrome string - System.out.print(A.equals(new StringBuilder(A).reverse().toString())?"Yes":"No"); public static void main(String[] args) throws IOException { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } private static void solve() throws IOException { int t = nextInt(); while(t-->0){ //String s= nextToken(); //String[] s = str.split("\\s+"); // ArrayList<Integer> ar=new ArrayList<Integer>(); ArrayList<Integer> ans=new ArrayList<Integer>(); HashSet<Integer> ss=new HashSet<Integer>(); //HashMap<Integer,String> h=new HashMap<Integer,String>(); //R[] a1=new R[n]; //char[] c=nextToken().toCharArray(); // PriorityQueue<Integer> pq=new PriorityQueue<Integer>(Collections.reverseOrder()); int n = nextInt(); //long n = nextLong(); int[] arr=new int[n]; //long[] a=new long[n]; for(int i=0;i<n;i++){ arr[i]=nextInt(); // pq.add(a[i]); } //shuffle(a); //shufflel(a); //Arrays.sort(a); // Collections.sort(pq); Stack<Integer> s=new Stack<>(); int i=n-1; int maxi=n; while(i>=0){ while(i>=0 && arr[i]!=maxi){ ss.add(arr[i]); s.push(arr[i]); i--; } if(i>=0){ ans.add(arr[i]); while(s.empty()==false){ ans.add(s.peek()); s.pop(); } maxi--; while(ss.contains(maxi)!=false){maxi--;} i--; } } for(int b:ans){ writer.print(b+" "); } writer.println(); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
f72aa7fe9d65316b0a3e425123b335d1
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.*; import java.lang.*; import java.util.*; import java.io.*; import java.util.*; public class Main{ static Scanner scanner=new Scanner(System.in); public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int[] p=new int[n]; for(int i=0;i<n;i++)p[i]=sc.nextInt(); HashSet<Integer> set=new HashSet<>(); ArrayList<Integer> l=new ArrayList<>(); int n1=n; int c=0; for(int i=n1-1;i>=0;i--){ if(p[i]!=n){ set.add(p[i]); c++; }else if(p[i]==n){ for(int j=i;j<=i+c;j++)l.add(p[j]); c=0; n--; while(set.contains(n))n--;//找到剩下的最大值 } } for(int i=0;i<l.size();i++)System.out.print(l.get(i)+" "); System.out.println(); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
f2f99427f10c67d8f2052d8a18c75c34
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Scanner; import java.util.StringTokenizer; import static java.lang.Math.abs; import java.util.Collections; import java.util.ArrayList; import java.util.LinkedList; import java.util.Comparator; public class Test { //=============================Solution===================================// public static void main(String[] args) { Reader input = new Reader(); PrintWriter out = new PrintWriter(System.out); int t = input.nextInt(); while (t-->0) { int n = input.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = input.nextInt(); } boolean[] used = new boolean[n]; LinkedList<Integer> res = new LinkedList<>(); ArrayList<int[]> max = toList(a); Collections.sort(max, new CP()); int currentMax = 0; while (!used[0]) { int imax = max.get(currentMax++)[1]; for (int i = imax; i < n; i++) { if (!used[i]) { res.add(a[i]); used[i] = true; } else { break; } } } for (Integer x : res) { out.print(x + " "); } out.println(); } out.close(); } public static ArrayList<int[]> toList(int[] a) { ArrayList<int[]> list = new ArrayList<>(a.length); for (int i = 0; i < a.length; i++) { list.add(new int[]{a[i], i}); } return list; } static class CP implements Comparator<int[]> { @Override public int compare(int[] a, int[] b) { if (a[0] > b[0]) { return -1; } else if (a[0] < b[0]) { return 1; } return 0; } } //========================================================================// static class Reader { BufferedReader br; StringTokenizer st; public Reader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException ex) { ex.printStackTrace(); } } return st.nextToken(); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException ex) { ex.printStackTrace(); } return str; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
fec4ef029b31994e256e612739f0e96f
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import static java.lang.Math.ceil; import static java.lang.Math.min; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); int t = input.nextInt(); while (t-->0) { int n = input.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = input.nextInt(); } boolean[] used = new boolean[n]; LinkedList<Integer> res = new LinkedList<>(); ArrayList<int[]> max = toList(a); Collections.sort(max, new CP()); int currentMax = 0; while (!used[0]) { int imax = max.get(currentMax++)[1]; for (int i = imax; i < n; i++) { if (!used[i]) { res.add(a[i]); used[i] = true; } else { break; } } } for (Integer x : res) { System.out.print(x + " "); } System.out.println(); } } public static ArrayList<int[]> toList(int[] a) { ArrayList<int[]> list = new ArrayList<>(a.length); for (int i = 0; i < a.length; i++) { list.add(new int[]{a[i], i}); } return list; } static class CP implements Comparator<int[]> { @Override public int compare(int[] a, int[] b) { if (a[0] > b[0]) { return -1; } else if (a[0] < b[0]) { return 1; } return 0; } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
2652dfe380f1838458e88b25511a7811
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.io.IOException; import java.util.Comparator; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Sandip Jana */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); 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, InputReader in, PrintWriter out) { int n = in.readInt(); int a[][] = new int[n][2]; int b[][] = new int[n][2]; for (int i = 0; i < n; i++) { a[i][0] = b[i][0] = in.readInt(); a[i][1] = b[i][1] = i; } Arrays.sort(a, new Comparator<int[]>() { public int compare(int a[], int b[]) { return Integer.compare(b[0], a[0]); } }); int currentMaxIndex = n; StringBuilder ans = new StringBuilder(); for (int i = 0; i < n; i++) { int maxIndex = a[i][1]; if (maxIndex < currentMaxIndex) { for (int j = maxIndex; j < currentMaxIndex; j++) ans.append(b[j][0] + " "); currentMaxIndex = maxIndex; } } out.println(ans); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
3f20923fd6a833b3ba26a3e2c768ed17
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.Scanner; public class T1492B { static Scanner in = new Scanner(System.in); static int deck[]; static int foundedMaxs[]; static int lastIndexMaximum; public static void main(String[] args) { int t = in.nextInt(); for (int i = 0; i < t; i++) { int n = in.nextInt(); reset(n); readDeck(n); int maximum; int start = n - 1; int finish = n - 1; while (start >= 0) { maximum = nextMaximum(); while (finish - 1 >= 0) { foundedMaxs[deck[finish] - 1]++; if (deck[finish] != maximum) { finish--; } else { break; } } printPartDeck(start, finish); start = finish - 1; finish = start; } System.out.println(); } } private static void printPartDeck(int start, int finish) { for (int i = finish; i <= start; i++) { System.out.print(deck[i] + " "); } } static void readDeck(int n) { for (int i = 0; i < n; i++) { deck[i] = in.nextInt(); } } static int nextMaximum() { int indexMaximum = lastIndexMaximum; while (foundedMaxs[indexMaximum] != 0) { indexMaximum--; } lastIndexMaximum = indexMaximum; return indexMaximum + 1; } static void reset(int n) { deck = new int[n]; foundedMaxs = new int[n]; lastIndexMaximum = n - 1; } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
3e9dd64c5de817eaa69d7bf7d181b93d
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
//WHEN IN DOUBT , USE BRUTE FORCE !!!!!!!!! import java.lang.String; import java.io.*; import java.util.*; import java.util.StringTokenizer; //class Main //AtCoder() //class Solution // Codechef public class Solution2 //Codeforces { public static void main(String args[]) { try { FastReader sc = new FastReader(); int TT = sc.nextInt(); for(int hehe=1 ; hehe <= TT ; hehe++){ int N=sc.nextInt(); long p[]=new long[N]; int largestIndex[]=new int[N]; largestIndex[0]=0; long max=0;int ind=0; for(int i=0;i<N;i++) { p[i] = sc.nextLong(); if(p[i] > max){ max=p[i]; ind=i; largestIndex[i]=i; } else if(i != 0){ largestIndex[i]=largestIndex[i-1]; } } int i=0; Map<Integer,List<Long>> ll = new TreeMap<>(new Comparator<Integer>() { @Override public int compare(Integer integer, Integer t1) { return t1-integer; } }); while(i<largestIndex.length){ int A=largestIndex[i]; List<Long> ll2 = new ArrayList<>(); while (i<N && largestIndex[i]==A){ ll2.add(p[i]); i++; } ll.put(A, ll2); } //sopln(ll); for(Integer iii : ll.keySet()){ List<Long> l=ll.get(iii); for(Long lll:l){ sop(lll+" "); } } sopln(""); //printArray(ans); } //out.flush(); }catch (Exception e) { sopln(e.toString()); } } public final static int d = 256; static int MOD = 1000000007; static final double PI = Math.PI; private static BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); private static PrintWriter out = new PrintWriter (new OutputStreamWriter (System.out)); static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } char nextChar() { try { return (char) (br.read()); }catch (IOException e){ return '~'; } } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void sop(Object o){System.out.print(o);} static double ceil(double d){ return Math.ceil(d); } static double floor(double d){ return Math.floor(d); } static double round(double d){ return Math.round(d); } static void sopln(Object o){System.out.println(o);} static void printArray(long[] arr){ for(int i=0;i<arr.length;i++){ sop(arr[i]+" "); } sopln(""); } static void printArray(int[] arr){ for(int i=0;i<arr.length;i++){ sop(arr[i]+" "); } sopln(""); } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
bda14e576674c01671a5b2dc578e4ec0
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
//WHEN IN DOUBT , USE BRUTE FORCE !!!!!!!!! import java.lang.String; import java.io.*; import java.util.*; import java.util.StringTokenizer; //class Main //AtCoder() //class Solution // Codechef public class Solution2 //Codeforces { public static void main(String args[]) { try { FastReader sc = new FastReader(); int TT = sc.nextInt(); for(int hehe=1 ; hehe <= TT ; hehe++){ int N=sc.nextInt(); long p[]=new long[N]; int largestIndex[]=new int[N]; largestIndex[0]=0; long max=0;int ind=0; for(int i=0;i<N;i++) { p[i] = sc.nextLong(); if(p[i] > max){ max=p[i]; ind=i; largestIndex[i]=i; } else if(i != 0){ largestIndex[i]=largestIndex[i-1]; } } int i=0; Map<Integer,List<Long>> ll = new TreeMap<>(new Comparator<Integer>() { @Override public int compare(Integer integer, Integer t1) { return t1-integer; } }); while(i<largestIndex.length){ int A=largestIndex[i]; List<Long> ll2 = new ArrayList<>(); while (i<N && largestIndex[i]==A){ ll2.add(p[i]); i++; } ll.put(A, ll2); } //sopln(ll); for(Integer iii : ll.keySet()){ List<Long> l=ll.get(iii); for(Long lll:l){ sop(lll+" "); } } sopln(""); //printArray(ans); } //out.flush(); }catch (Exception e) { sopln(e.toString()); } } public final static int d = 256; static int MOD = 1000000007; static final double PI = Math.PI; private static BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); private static PrintWriter out = new PrintWriter (new OutputStreamWriter (System.out)); static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } char nextChar() { try { return (char) (br.read()); }catch (IOException e){ return '~'; } } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void sop(Object o){System.out.print(o);} static double ceil(double d){ return Math.ceil(d); } static double floor(double d){ return Math.floor(d); } static double round(double d){ return Math.round(d); } static void sopln(Object o){System.out.println(o);} static void printArray(long[] arr){ for(int i=0;i<arr.length;i++){ sop(arr[i]+" "); } sopln(""); } static void printArray(int[] arr){ for(int i=0;i<arr.length;i++){ sop(arr[i]+" "); } sopln(""); } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
24ffbb7d4f4942939f142564670dd645
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.LinkedList; import java.util.StringTokenizer; public class TaskB { private final static String YES = "YES"; private final static String NO = "NO"; public static void main(String[] arg) { final FastScanner in = new FastScanner(System.in); final PrintWriter out = new PrintWriter(System.out); final int T = in.nextInt(); StringBuilder res = new StringBuilder(); for(int t=0;t<T;t++) { final int n = in.nextInt(); final int []p = new int[n]; final int []a = new int[n+1]; for(int i=0;i<n;i++){ p[i]=in.nextInt(); a[p[i]]=i; } int to = n+1; for(int i=n;i>0;i--) { if(a[i]>=to) { continue; } for(int j=a[i];j<to&&j<n;j++){ res.append(p[j]).append(' '); } to=a[i]; } res.append('\n'); } out.print(res); out.close(); in.close(); } public static int nextSimpleNum(final int a){ int i=a; while (!isSimpleNumber(i)){ i++; } return i; } public static boolean isSimpleNumber(final int n){ for(int i=2;i*i<=n;i++){ if(n%i==0) return false; } return true; } private static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream)); } catch (Exception e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readIntArr(int n) { int[] result = new int[n]; for (int i = 0; i < n; i++) { result[i] = Integer.parseInt(next()); } return result; } void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
9a6a58f07c0c3f6778c0789db920d2e8
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.LinkedList; import java.util.StringTokenizer; public class TaskB { private final static String YES = "YES"; private final static String NO = "NO"; public static void main(String[] arg) { final FastScanner in = new FastScanner(System.in); final PrintWriter out = new PrintWriter(System.out); final int T = in.nextInt(); StringBuilder res = new StringBuilder(); for(int t=0;t<T;t++) { final int n = in.nextInt(); final int []p = in.readIntArr(n); final int []a = new int[n+1]; for(int i=0;i<n;i++){ a[p[i]]=i; } int to = n+1; for(int i=n;i>0;i--) { if(a[i]>=to) { continue; } for(int j=a[i];j<to&&j<n;j++){ res.append(p[j]).append(' '); } to=a[i]; } res.append('\n'); } out.print(res); out.close(); in.close(); } public static int nextSimpleNum(final int a){ int i=a; while (!isSimpleNumber(i)){ i++; } return i; } public static boolean isSimpleNumber(final int n){ for(int i=2;i*i<=n;i++){ if(n%i==0) return false; } return true; } private static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream)); } catch (Exception e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readIntArr(int n) { int[] result = new int[n]; for (int i = 0; i < n; i++) { result[i] = Integer.parseInt(next()); } return result; } void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
329cc68410b554e0c53abdd786ae74de
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.LinkedList; import java.util.StringTokenizer; public class TaskB { private final static String YES = "YES"; private final static String NO = "NO"; public static void main(String[] arg) { final FastScanner in = new FastScanner(System.in); final PrintWriter out = new PrintWriter(System.out); final int T = in.nextInt(); StringBuilder res = new StringBuilder(); for(int t=0;t<T;t++) { final int n = in.nextInt(); final int []p = new int[n]; final int [][]a = new int[n][2]; for(int i=0;i<n;i++){ a[i][0]=in.nextInt(); a[i][1]=i; p[i]=a[i][0]; } Arrays.sort(a, (e1, e2)->Integer.compare(e2[0], e1[0])); int to = n; LinkedList<Integer> result = new LinkedList<>(); for(int i=0;i<n;i++) { if(a[i][1]>=to) { continue; } for(int j=a[i][1];j<to;j++){ result.add(p[j]); } to=a[i][1]; } for(Integer pn:result){ res.append(pn).append(' '); } res.append('\n'); } out.print(res); out.close(); in.close(); } public static int nextSimpleNum(final int a){ int i=a; while (!isSimpleNumber(i)){ i++; } return i; } public static boolean isSimpleNumber(final int n){ for(int i=2;i*i<=n;i++){ if(n%i==0) return false; } return true; } private static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream)); } catch (Exception e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readIntArr(int n) { int[] result = new int[n]; for (int i = 0; i < n; i++) { result[i] = Integer.parseInt(next()); } return result; } void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
5ce05fec08d3c0408a15a95de22b674c
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; public class MyClass { public static void main(String args[]) { Scanner kb = new Scanner(System.in); int t=kb.nextInt(); while(t-->0){ int n=kb.nextInt(); int []a = new int[n]; int []p = new int[n+1]; for(int i=0; i<n; i++){ a[i]=kb.nextInt(); p[a[i]]=i; } int s; int e = n-1; while(n>0){ s=p[n]; if(s==-1) {n--;continue;} if(s<=e){ for(int i=s;i<=e;i++){ System.out.print(a[i]+" "); p[a[i]]=-1; } } e=s-1; n--; } System.out.println(); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
efce9146eb45e12570bfba2cfeaa2517
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
// package contest; import java.io.*; import java.util.*; public class B_Card_Deck { // static int mod=1000000007; public static void main(String[] args) throws Exception { try { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); int test = in.nextInt(); while(test-->0){ int n = in.nextInt(); String[] str = in.nextLine().split(" "); int arr[] = new int[n]; for(int i=0;i<n;i++){ arr[i] = Integer.parseInt(str[i]); } SparseMatrix mat = new SparseMatrix(arr); String ans=""; StringBuilder sb = new StringBuilder(); int max = n; int left = 0; int right = n-1; while(left<=right){ int maxIdx = mat.getMaxIndex(left, right); for(int i=maxIdx;i<max;i++){ sb.append(arr[i]+" "); } max = maxIdx; right = maxIdx-1; } ans += sb.toString(); out.println(ans); } out.close(); } catch (Exception e) { System.out.println(e); return; } } 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; } String[] strArray() { String str[] = nextLine().split(" "); return str; } int[] intArray(int n) { String[] str = strArray(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(str[i]); } return arr; } long[] longArray(int n) { String[] str = strArray(); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = Long.parseLong(str[i]); } return arr; } int[][] two_d_Array(int n, int m){ int arr[][] = new int[n][m]; for(int i=0;i<n;i++){ String[] str = strArray(); for(int j=0;j<m;j++){ arr[i][j] = Integer.parseInt(str[j]); } } return arr; } Integer[] IntArray(int n) { String[] str = strArray(); Integer[] arr = new Integer[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(str[i]); } return arr; } private static void swap (int arr[], int a, int b){ int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } } static class SparseMatrix { private int[] arr; private int m; private int[][] minSparse; private int[][] minIndex; private int[][] maxSparse; private int[][] maxIndex; private int n; public SparseMatrix(int[] arr) { this.arr = arr; this.m=arr.length; this.n=Integer.toBinaryString(m).length(); minSparse=new int[n][m]; minIndex=new int[n][m]; maxSparse=new int[n][m]; maxIndex=new int[n][m]; // for(int i=0;i<n;i++) // { // Arrays.fill(minSparse[i],-1); // } // for(int i=0;i<n;i++) // { // Arrays.fill(minIndex[i],-1); // } createMinSparse(); createMaxSparse(); } private void createMaxSparse() { for(int j=0;j<m;j++) { maxSparse[0][j]=arr[j]; maxIndex[0][j]=j; } for(int i=1;i<n;i++) { for(int j=0;j+(1<<(i-1))<m;j++) { int left=maxSparse[i-1][j]; int right=maxSparse[i-1][j+(1<<(i-1))]; maxSparse[i][j]=Math.max(left,right); if(left>=right) { maxIndex[i][j]=maxIndex[i-1][j]; } else { maxIndex[i][j]=maxIndex[i-1][j+(1<<(i-1))]; } } } } private void createMinSparse() { //filling the first row of sparse matrix with the values of the input array for(int j=0;j<m;j++) { minSparse[0][j]=arr[j]; minIndex[0][j]=j; } //filling other rows of the sparse matrix for(int i=1;i<n;i++) { for(int j=0;j+(1<<(i-1))<m;j++) { int left=minSparse[i-1][j]; int right=minSparse[i-1][j+(1<<(i-1))]; //change to min-> max to create MaxSparseMatrix minSparse[i][j]=Math.min(left,right); //filling index if(left<=right) { minIndex[i][j]=minIndex[i-1][j]; } else { minIndex[i][j]=minIndex[i-1][j+(1<<(i-1))]; } } } } //get minimum value in range l->r inclusive /* for any range [l, r] we can find the two values and find their minimum. These values are defined below: len: length of the required range, i.e., r-l+1 p: maximum power of 2 that can fit in len. E.g, [1,11] , len=11, thus p=3 k: 2^p find the minimum between sparse[p][l] and sparse[p][r-k+1] */ public int getMin(int l,int r) { int len=r-l+1; int p=Integer.toBinaryString(len).length()-1; int k=1<<p; int left=minSparse[p][l]; int right=minSparse[p][r-k+1]; return Math.min(right,left); } public int getMinIndex(int l,int r) { int len=r-l+1; int p=Integer.toBinaryString(len).length()-1; int k=1<<p; int left=minSparse[p][l]; int right=minSparse[p][r-k+1]; if (left <= right) { return minIndex[p][l]; } else { return minIndex[p][r - k + 1]; } } public int getMax(int l,int r) { int len=r-l+1; int p=Integer.toBinaryString(len).length()-1; int k=1<<p; int left=maxSparse[p][l]; int right=maxSparse[p][r-k+1]; return Math.max(right,left); } public int getMaxIndex(int l,int r) { int len=r-l+1; int p=Integer.toBinaryString(len).length()-1; int k=1<<p; int left=maxSparse[p][l]; int right=maxSparse[p][r-k+1]; if(left>=right) { return maxIndex[p][l]; } return maxIndex[p][r-k+1]; } void print() { for(int i=0;i<minSparse.length;i++) { for(int j=0;j<minSparse[i].length;j++) { System.out.print(minSparse[i][j]+" "); } System.out.println(); } System.out.println(); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { System.out.print(minIndex[i][j]+" "); } System.out.println(); } } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
2a8f6dd9a077c062149c0a24eaf1f84d
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
//created by Akshay Mathur import java.util.*; import java.io.*; import java.lang.*; public class Main { /*public static void func() { }*/ public static void main (String[] args) throws java.lang.Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out)); int t=Integer.parseInt(br.readLine()); while(t-->0) { int n=Integer.parseInt(br.readLine()); String[] str = br.readLine().split(" "); int[] a1=new int[n]; int[] indxarr=new int[n+1]; for(int j=0;j<n;j++) { a1[j]=Integer.parseInt(str[j]); indxarr[a1[j]]=j; } int end=n; for(int i=n;i>0;i--) { int start=indxarr[i]; if(end>start) { for(int j=start;j<end;j++) { bw.write(String.valueOf(a1[j])); bw.write(" "); } end=start; } } bw.newLine(); } br.close(); bw.close(); } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
64ddfdc018a93c9c9b9cff245be915e7
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; public class B_Card_Deck { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int q = sc.nextInt(); while (q-- > 0) { int n = sc.nextInt(); int ar[]=new int[n]; int ind[]=new int[n+1]; int i=0; StringBuilder sb=new StringBuilder(); for(i=0;i<n;i++) { ar[i]=sc.nextInt(); ind[ar[i]]=i; } int max=n; for(i=n;i>=1;i--) { int pos=ind[i]; if(pos<max) { for(int j=pos;j<max;j++) { sb.append(ar[j]+" "); } max=pos; } } System.out.println(sb); // int j=n-1; // StringBuilder ans=new StringBuilder(); // for(i=n;i>=1;i--) // { // StringBuilder sb=new StringBuilder(); // if(f[i]==false) // { // while(j>=0) // { // sb.insert(0,ar[j]+" "); // f[ar[j]]=true; // j--; // if(j==-1||ar[j+1]==i) // { // break; // } // } // } // ans.append(sb); // } // System.out.println(ans); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
3c763205b6534c61d2275a7457bb94ed
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.Scanner; public class p1492B { public static void main(String[] args) { Scanner sc=new Scanner(System.in); for(int t=sc.nextInt();t-->0;) { int n=sc.nextInt(),a[]=new int[n],c[]=new int[n+1]; boolean vis[]=new boolean[n+1]; for(int i=0;i<n;i++) c[a[i]=sc.nextInt()]=i; c[0]=-1; int pre=n; StringBuilder sb=new StringBuilder(); for(int i=n;i>0;i--) { if(!vis[i]) { for(int j=c[i];j<pre;j++) { sb.append(a[j]+" "); vis[a[j]]=true; } pre=c[i]; } } System.out.println(sb.toString()); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
e938cbff9728ba56717eb94e0701a9bb
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; public class Contest { private static void sport(int[] p) { List<Integer> ans = new ArrayList<>(); int n = p.length; boolean[] m = new boolean[n+1]; int f = n; int r = n; for (int i = n - 1; i >= 0; i--) { if (f == p[i]) { for (int j = i; j < r; j++) { ans.add(p[j]); m[p[j]] = true; } int next = find(m, f); if (next == -1) { break; } f = next; r = i; } } for (Integer integer : ans) { System.out.print(integer + " "); } System.out.println(); } private static int find(boolean[] m, int n) { for (int i = n; i >= 0; i--) { if (!m[i]) { return i; } } return -1; } 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[] p = new int[n]; for (int j = 0; j < n; j++) { p[j] = sc.nextInt(); } sport(p); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
03c9507e44d39028b6520e527e0a4924
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import java.util.LinkedList; import java.util.Queue; import static java.lang.Math.*; public class Cf294 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[] nextArray(int n) { int arr[] = new int[n]; int i; for(i=0;i<n;i++) { arr[i] = nextInt(); } return arr; } 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 int[] nextIntArray(int n) { int i; int arr[] = new int[n]; for(i=0;i<n;i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { int i; long arr[] = new long[n]; for(i=0;i<n;i++) { arr[i] = nextLong(); } return arr; } public double[] nextDoubleArray(int n) { int i; double arr[] = new double[n]; for(i=0;i<n;i++) { arr[i] = nextDouble(); } return arr; } public Integer[] IntegerArray(int n) { int i; Integer arr[] = new Integer[n]; for(i=0;i<n;i++) { arr[i] = nextInt(); } return arr; } public Long[] LongArray(int n) { int i; Long arr[] = new Long[n]; for(i=0;i<n;i++) { arr[i] = nextLong(); } return arr; } public Double[] DoubleArray(int n) { int i; Double arr[] = new Double[n]; for(i=0;i<n;i++) { arr[i] = nextDouble(); } return arr; } public ArrayList<ArrayList<Integer>> getGraph(int n,int m) { ArrayList<ArrayList<Integer>> g =new ArrayList<>(); int i; for(i=0;i<=n;i++) { g.add(new ArrayList<>()); } for(i=0;i<m;i++) { int x = nextInt(); int y = nextInt(); g.get(x).add(y); g.get(y).add(x); } return g; } public ArrayList<ArrayList<Integer>> getTree(int n) { ArrayList<ArrayList<Integer>> g =new ArrayList<>(); int i; for(i=0;i<=n;i++) { g.add(new ArrayList<>()); } for(i=0;i<n-1;i++) { int x = nextInt(); int y = nextInt(); g.get(x).add(y); g.get(y).add(x); } return g; } } public static int maxInt(int arr[]) { int max = Integer.MIN_VALUE; for(int i : arr) { max = Math.max(i,max); } return max; } public static int minInt(int arr[]) { int min = Integer.MAX_VALUE; for(int i : arr) { min = Math.min(i,min); } return min; } public static long maxLong(long arr[]) { long max = Long.MIN_VALUE; for(long i : arr) { max = Math.max(i,max); } return max; } public static long minLong(long arr[]) { long min = Long.MAX_VALUE; for(long i : arr) { min = Math.min(i,min); } return min; } public static double maxDouble(double arr[]) { double max = Double.MIN_VALUE; for(double i : arr) { max = Math.max(i,max); } return max; } public static double minDouble(double arr[]) { double min = Double.MAX_VALUE; for(double i : arr) { min = Math.min(i,min); } return min; } void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[] = new int [n1]; int R[] = new int [n2]; for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } void sort(int arr[], int l, int r) { if (l < r) { int m = (l+r)/2; sort(arr, l, m); sort(arr , m+1, r); merge(arr, l, m, r); } } static class Pair { int x,y; public Pair(int x,int y) { this.x = x; this.y = y; } public static Comparator<Pair> sortX = new Comparator<Pair>(){ public int compare(Pair p1,Pair p2) { if(p1.x==p2.x) { return (int)(p1.y-p2.y); } return (int)(p1.x-p2.x); } }; public static Comparator<Pair> sortY = new Comparator<Pair>(){ public int compare(Pair p1,Pair p2) { if(p1.y==p2.y) { return (int)(p1.x-p2.x); } return (int)(p1.y-p2.y); } }; public static Comparator<Pair> custom = new Comparator<Pair>(){ public int compare(Pair p1,Pair p2) { if(p1.x-p2.x==0) { return -1*(p1.y-p2.y); } return -1*(p1.x-p2.x); } }; public boolean equals(Object obj) { if (obj == null) return false; if (!(obj instanceof Pair)) return false; return (this.x == ((Pair) obj).x && this.y == ((Pair)obj).y); } public int hashCode() { if(x<0 || y<0) { return (int)(-5*x+-32*y); } return (int)(5*(x+y)); } } static class DPair { Pair s,d; public DPair(Pair s,Pair d) { this.s = s; this.d = d; } public boolean equals(Object obj) { if (obj == null) return false; if (!(obj instanceof DPair)) return false; return (this.s.x == ((DPair) obj).s.x && this.s.y == ((DPair) obj).s.y && this.d.x == ((DPair)obj).d.x && this.d.y == ((DPair) obj).d.y); } public int hashCode() { return 5*(s.x+s.y+d.x+d.y); } } static class DSU { public static int rank[],parent[]; int n; HashMap<Integer,Integer> map = new HashMap<>(); public DSU(int n) { rank = new int[n+1]; parent = new int[n+1]; this.n = n; makeSet(n); } public void makeSet(int n) { for(int i=1;i<=n;i++) { parent[i] = i; map.put(i,1); } } public static int find(int x) { if(parent[x]!=x) { parent[x] = find(parent[x]); } return parent[x]; } public void union(int x,int y) { int xRoot = find(x); int yRoot = find(y); if(xRoot==yRoot) { return; } if(rank[xRoot] < rank[yRoot]) { parent[xRoot] = yRoot; rank[yRoot] = rank[xRoot] + 1; map.put(yRoot,map.getOrDefault(yRoot,1)+map.getOrDefault(xRoot,1)); if(map.getOrDefault(xRoot,0)!=0) { map.remove(xRoot); } } else if(rank[yRoot] < rank[xRoot]) { parent[yRoot] = xRoot; rank[xRoot] = rank[yRoot] + 1; map.put(xRoot,map.getOrDefault(xRoot,1) + map.getOrDefault(yRoot,1)); if(map.getOrDefault(yRoot,0)!=0) { map.remove(yRoot); } } else { parent[yRoot] = xRoot; rank[xRoot] = rank[yRoot] + 1; map.put(xRoot,map.getOrDefault(xRoot,1)+map.getOrDefault(yRoot,1)); if(map.getOrDefault(yRoot,0)!=0) { map.remove(yRoot); } } } } static class SegmentTree { public static int tree[]; public SegmentTree(int arr[], int n) { int x = (int) (Math.ceil(Math.log(n) / Math.log(2))); int size = 2 * (int) Math.pow(2, x) - 1; tree = new int[size]; constructTree(arr, 0, n - 1, 0); } public int getMid(int s, int e) { return s + (e - s) / 2; } public int minVal(int x, int y) { return (x < y) ? x : y; } public int constructTree(int arr[], int s, int e, int ind) { if (s == e) { tree[ind] = arr[s]; return arr[s]; } int mid = getMid(s, e); tree[ind] = constructTree(arr, s, mid, ind * 2 + 1) + constructTree(arr, mid + 1, e, ind * 2 + 2); return tree[ind]; } public long getSum(int n, int l, int r) { if (l < 0 || r > n - 1 || l > r) { return -1; } return getSumR(0, n - 1, l, r, 0); } public long getSumR(int s, int e, int l, int r, int ind) { if (l <= s && r >= e) { return tree[ind]; } if (e < l || s > r) { return 0; } int mid = getMid(s, e); return getSumR(s, mid, l, r, 2 * ind + 1) + getSumR(mid + 1, e, l, r, 2 * ind + 2); } public void updateValue(int arr[], int n, int i, int val) { if (i < 0 || i > n - 1) { return; } int diff = val - arr[i]; arr[i] = val; updateValueR(0, n - 1, i, diff, 0); } public void updateValueR(int s, int e, int i, int diff, int ind) { if (i < s || i > e) { return; } tree[ind] = tree[ind] + diff; if (e != s) { int mid = getMid(s, e); updateValueR(s, mid, i, diff, 2 * ind + 1); updateValueR(mid + 1, e, i, diff, 2 * ind + 2); } } ////FOR MIN APLLICATION public int constructMinTree(int arr[], int s, int e, int ind) { if (s == e) { tree[ind] = arr[s]; return arr[s]; } int mid = getMid(s, e); tree[ind] = minVal(constructMinTree(arr, s, mid, ind * 2 + 1), constructMinTree(arr, mid + 1, e, ind * 2 + 2)); return tree[ind]; } public int getMin(int n,int l,int r) { if (l < 0 || r > n - 1 || l > r) { return -1; } return getMinR(0, n - 1, l, r, 0); } public void updateValueMin(int arr[],int s, int e, int index, int value, int node) { if (index < s || index > e) { return; } if (s == e) { arr[index] = value; tree[node] = value; } else { int mid = getMid(s, e); if (index >= s && index <= mid) { updateValueMin(arr,s, mid, index, value, 2 * node + 1); } else { updateValueMin(arr,mid + 1, e, index, value, 2 * node + 2); } tree[node] = Math.min(tree[2 * node + 1], tree[2 * node + 2]); } return; } public int getMinR(int s, int e, int l, int r, int index) { if (l <= s && r >= e) { return tree[index]; } if (e < l || s > r) { return Integer.MAX_VALUE; } int mid = getMid(s, e); return minVal(getMinR(s, mid, l, r, 2 * index + 1) , getMinR(mid + 1, e, l, r, 2 * index + 2)); } } 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 / gcd(a, b)) * b; } public static void main(String args[]) throws Exception { new Thread(null, new Cf294(),"Main",1<<27).start(); } public void run() { InputReader in = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int TESTCASES = in.nextInt(); while(TESTCASES-->0) { int n = in.nextInt(); int arr[] = in.nextIntArray(n); boolean visited[] = new boolean[n]; HashMap<Integer, Integer> map = new HashMap<>(); int i,j,k; PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); for(i=0;i<n;i++) { map.put(arr[i],i); pq.add(arr[i]); } ArrayList<Integer> res = new ArrayList<>(); while(pq.size()!=0) { int x = pq.poll(); if(!visited[map.get(x)]) { int ind = map.get(x); for(i=ind;i<n;i++) { if(!visited[i]) { visited[i] = true; res.add(arr[i]); } else { break; } } } } for(int x : res) { w.print(x + " "); } w.println(); } w.flush(); w.close(); } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
b141c0bc16b1d01051f6ab85de3a9c23
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.io.*; public class Main { static FastScanner sc = new FastScanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(); public static void main(String[] args) throws Exception { int n = sc.nextInt(); for(int i = 0; i < n; i++) solve(); pw.flush(); } public static void solve(){ int n = sc.nextInt(); ArrayList<int[]> arr = new ArrayList<>(); int[] a = sc.nextIntArray(n); for(int i = 0; i < n; i++){ arr.add(new int[]{a[i], i}); } Collections.sort(arr,new ArrayComparator()); HashSet<Integer> set = new HashSet<>(); int top = 0; for(int[] b : arr){ int index = b[1]; int value = b[0]; if(set.contains(index)) continue; for(int i = index; i < n; i++){ if(set.contains(i)) break; set.add(i); sb.append(a[i]).append(" "); } } pw.println(sb.toString().trim()); sb.setLength(0); } static class ArrayComparator implements Comparator<int[]> { @Override public int compare(int[] a1, int[] a2) { for (int i = 0; i < a1.length; i++) { if (a1[i] > a2[i]) { return -1; } else if (a1[i] < a2[i]) { return 1; } } if (a1.length > a2.length) { return -1; } else if (a1.length < a2.length) { return 1; } else { return 0; } } } static class GeekInteger { public static void save_sort(int[] array) { shuffle(array); Arrays.sort(array); } public static int[] shuffle(int[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); int randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } public static void save_sort(long[] array) { shuffle(array); Arrays.sort(array); } public static long[] shuffle(long[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); long randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } } } class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String[] nextArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) a[i] = next(); return a; } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
7d8bfeee3b9c28b6d2a3c97841f83ed4
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.PrintWriter; import java.util.*; public class wqwq { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); while (t-- > 0) { ArrayList<Integer> arr = new ArrayList<>(); Map<Integer, Integer> hm = new HashMap<>(); int n = in.nextInt(); int x; for (int i = 0; i < n; i++) { x = in.nextInt(); arr.add(x); hm.put(x, i); } int last = n; int ind = 0; while (arr.size() > 0) { if (hm.get(last) != null) { ind = hm.get(last); if (ind > arr.size()) { last--; continue; } for (int i = ind; i < arr.size(); i++) { out.print(arr.get(i) + " "); } for (int i = arr.size() - 1; i >= ind; i--) { arr.remove(i); } last--; } } out.println(); } out.close(); } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
f2606bd2e12da293ca304dba69d97e57
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.PrintWriter; import java.util.*; public class wqwq { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); while (t-- > 0) { ArrayList<Integer> arr = new ArrayList<>(); Map<Integer, Integer> hm = new HashMap<>(); int n = in.nextInt(); int x; for (int i = 0; i < n; i++) { x = in.nextInt(); arr.add(x); hm.put(x, i); } int last = n; int ind = 0; while (arr.size() > 0) { if (hm.get(last) != null) { ind = hm.get(last); if (ind > arr.size()) { last--; continue; } for (int i = ind; i < arr.size(); i++) { System.out.print(arr.get(i) + " "); } for (int i = arr.size() - 1; i >= ind; i--) { arr.remove(i); } last--; } } System.out.println(); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
3542cc3650418ff69e500c1a45d527f3
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; import java.util.TreeSet; import static java.lang.System.out; public class pre111 { 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 obj = new FastReader(); int tc = obj.nextInt(); while(tc--!=0){ int n = obj.nextInt(),arr[] = new int[n]; for(int i=0;i<n;i++) arr[i] = obj.nextInt(); Map<Integer,Integer> mp = new HashMap<>(); TreeSet<Integer> set = new TreeSet<>(); for(int i=0;i<n;i++) { mp.put(arr[i],i); set.add(arr[i]); } while(set.size()>0){ int l = set.last(),idx = mp.get(l); for(int i=idx;i<n;i++){ if(arr[i]!=-1){ out.print(arr[i]+" "); set.remove(arr[i]); arr[i] = -1; }else break; } } out.println(); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
c92332450e12ee9b6fcb544187822eda
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.*; import java.util.*; //import javafx.util.*; public class Main { static PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); public static void main(String args[])throws IOException { int t = i(); while(t-- > 0){ int n = i(); int[] arr = input(n); int[] position = new int[n+1]; for(int i = 0;i < n;i++){ position[arr[i]] = i; } int l = 0; int h = n; for(int i = n;i >= 1;i--){ l = position[i]; if(h > l){ for(int j = l;j < h;j++){ out.print(arr[j] + " "); } h = l; } } out.println(); } out.close(); } public static void sort(int[] arr){ ArrayList<Integer> ls = new ArrayList<>(); for(int x : arr){ ls.add(x); } Collections.sort(ls); for(int i = 0;i < arr.length;i++){ arr[i] = ls.get(i); } } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] inputLong(int N) { long A[]=new long[N]; for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } } class Pair implements Comparable<Pair>{ int x; int y; Pair(int x, int y){ this.x = x; this.y = y; } @Override public int compareTo(Pair obj) { // we sort objects on the basis of Student Id return (this.x - obj.x); } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()) { try { st=new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
0272e886653517f5fff436b2f0342347
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
//package cp; import java.util.*; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.Math.max; import java.io.*; public class Codechef { static long[] mods = {1000000007, 998244353, 1000000009}; static long mod = mods[0]; public static MyScanner in; public static PrintWriter out; static int[][] dp; public static void main( String[] args ) throws IOException{ in = new MyScanner(); out = new PrintWriter(System.out); StringBuilder ans1=new StringBuilder(); int test=in.nextInt(); while(test-->0) { StringBuilder ans=new StringBuilder(); int n=in.nextInt(); int arr[]=readArrayInt(n); boolean max[]=new boolean[n+1]; Arrays.fill(max, false); int max_value=n, end=n-1; while(max_value>0) { for(int i=end;i>=0;i--) { if(arr[i]==max_value) { for(int j=i;j<=end;j++) { ans.append(arr[j]+" "); max[arr[j]]=true; } end=i-1; break; } } for(int i=max_value;i>=0;i--) { if(max[i]==false) { max_value=i; break; } } } ans1.append(ans).append(System.getProperty("line.separator")); } out.println(ans1); out.close(); } public static void reArrange(long[] arr, int i) { while(i>0) { long temp=arr[i]; arr[i]=arr[i-1]; arr[i-1]=temp; i--; } } static long lcm_comp(long a, long b) { return (a / gcd_comp(a, b)) * b; } static long gcd_comp(long a, long b){ if (a == 0) return b; if (b == 0) return a; long k; for (k = 0; ((a | b) & 1) == 0; ++k){ a >>= 1; b >>= 1; } while ((a & 1) == 0) a >>= 1; do{ while ((b & 1) == 0) b >>= 1; if (a > b){ long temp = a; a = b; b = temp; } b = (b - a); } while (b != 0); return a << k; } //comparator to solve according to b in descending order static class Pair implements Comparable<Pair>{ private long a; private long b; public Pair(long a, long b) { this.a = a; this.b = b; } public int compareTo(Pair other) { return Long.compare(this.b, other.b ); //to sort in ascending order swap the arguments in Long.compare //return Long.compare(other.b ,this.b); //to sort in descending order swap the arguments in Long.compare } } static void printArray(long[] a) { StringBuilder stringBuilder=new StringBuilder(); for(long i: a) stringBuilder.append(i).append(" "); out.println(stringBuilder); } public static void sort(long[] array){ ArrayList<Long> copy = new ArrayList<>(); for (long i : array) copy.add(i); Collections.sort(copy); for(int i = 0;i<array.length;i++) array[i] = copy.get(i); } public static void sortDes(long[] array){ ArrayList<Long> copy = new ArrayList<>(); for (long i : array) copy.add(i); Collections.sort(copy); int j=array.length-1; for(int i = 0;i<array.length;i++) array[j--] = copy.get(i); } static String[] readArrayString(int n){ String[] array = new String[n]; for(int j =0 ;j<n;j++) array[j] = in.next(); return array; } static int[] readArrayInt(int n){ int[] array = new int[n]; for(int j = 0;j<n;j++) array[j] = in.nextInt(); return array; } static int[] readArrayInt1(int n){ int[] array = new int[n+1]; for(int j = 1;j<=n;j++){ array[j] = in.nextInt(); } return array; } static long[] readArrayLong(int n){ long[] array = new long[n]; for(int j =0 ;j<n;j++) array[j] = in.nextLong(); return array; } static double[] readArrayDouble(int n){ double[] array = new double[n]; for(int j =0 ;j<n;j++) array[j] = in.nextDouble(); return array; } static int minIndex(int[] array){ int minValue = Integer.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static int minIndex(long[] array){ long minValue = Long.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static int minIndex(double[] array){ double minValue = Double.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static int maxIndex(int[] array){ int maxValue = Integer.MIN_VALUE; int maxIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] > maxValue){ maxValue = array[j]; maxIndex = j; } } return maxIndex; } static int maxIndex(long[] array){ long maxValue = Long.MIN_VALUE; int maxIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] > maxValue){ maxValue = array[j]; maxIndex = j; } } return maxIndex; } static long power(long x, long y){ if (y == 0) return 1; if (y%2 == 1) return (x*power(x, y-1))%mod; return power((x*x)%mod, y/2)%mod; } static void verdict(boolean a){ out.println(a ? "YES" : "NO"); } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
65b8739f7af102087ce3318dae211d75
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
// package codeforces; import java.io.*; import java.util.*; public class Round724_div2 { static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } private static String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } static BufferedReader br; static StringTokenizer st; static BufferedWriter bw ; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); bw = new BufferedWriter(new OutputStreamWriter(System.out)); int[]nums= {1,1,2,2,3,3}; int t = nextInt(); while (t-- > 0) { card_Deck(); } bw.close(); } public static void threeswimmers() throws IOException{ long p=nextLong(); long a=nextLong(); long b=nextLong(); long c=nextLong(); if(p%a==0 || p%b==0 || p%c==0) { bw.append(0+"\n"); return; } long min=(long)1e18; long x=p/a; min=Math.min(min, a*(x+1)-p); long y=p/b; min=Math.min(min, b*(y+1)-p); long z=p/c; min=Math.min(min, c*(z+1)-p); bw.append(min+"\n"); } public static void card_Deck() throws IOException{ int n=nextInt(); int[]arr=new int[n]; for(int i=n-1;i>=0;i--) { arr[i]=nextInt(); } Stack<Integer> s=new Stack<>(); int max=n; HashMap<Integer,Integer> map=new HashMap<>(); ArrayList<Integer> l=new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]==max) { s.push(arr[i]); map.put(arr[i], 1); while(!s.isEmpty()) { l.add(s.pop()); } max--; while(map.containsKey(max)) { max--; } }else{ s.push(arr[i]); map.put(arr[i], 1); } } while(!s.isEmpty()) { l.add(s.pop()); } for(int x:l) { bw.append(x+" "); } bw.append("\n"); } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
1347ac285f8f7cb0b1780e3e3dd73845
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
//package Codeforces; import java.io.*; import java.util.*; public class CardDeck { public static void main(String[] args)throws Exception{ new CardDeck().run();} long mod=1000000000+7; // int[][] ar; void solve() throws Exception { int t =ni(); // ar = new int[1001][1001]; // buildMatrix(); for(int tt=1;tt<=t;tt++) { int n=ni(); int[] ar = new int[n]; int[][] tmp = new int[n][2]; for(int i=0;i<n;i++){ ar[i]=ni(); tmp[i][0]=ar[i]; tmp[i][1]=i; } Arrays.sort(tmp,new Comparator<int[]>(){ public int compare(int[] a,int[] b){ return b[0]-a[0]; } }); int[] mark = new int[n]; for(int i=0;i<n;i++){ for(int j=tmp[i][1];j<n;j++){ if(mark[j]==1) break; out.print(ar[j]+" "); mark[j]=1; } } out.println(); } } // void buildMatrix(){ // // for(int i=1;i<=1000;i++){ // // ar[i][1] = (i*(i+1))/2; // // for(int j=2;j<=1000;j++){ // ar[i][j] = ar[i][j-1]+(j-1)+i-1; // } // } // } /*FAST INPUT OUTPUT & METHODS BELOW*/ private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; private SpaceCharFilter filter; PrintWriter out; int min(int... ar){int min=Integer.MAX_VALUE;for(int i:ar)min=Math.min(min, i);return min;} long min(long... ar){long min=Long.MAX_VALUE;for(long i:ar)min=Math.min(min, i);return min;} int max(int... ar) {int max=Integer.MIN_VALUE;for(int i:ar)max=Math.max(max, i);return max;} long max(long... ar) {long max=Long.MIN_VALUE;for(long i:ar)max=Math.max(max, i);return max;} void shuffle(int a[]) { ArrayList<Integer> al = new ArrayList<>(); for(int i=0;i<a.length;i++) al.add(a[i]); Collections.sort(al); for(int i=0;i<a.length;i++) a[i]=al.get(i); } long lcm(long a,long b) { return (a*b)/(gcd(a,b)); } int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } /* for (1/a)%mod = ( a^(mod-2) )%mod ----> use expo to calc -->(a^(mod-2)) */ long expo(long p,long q) /* (p^q)%mod */ { long z = 1; while (q>0) { if (q%2 == 1) { z = (z * p)%mod; } p = (p*p)%mod; q >>= 1; } return z; } void run()throws Exception { in=System.in; out = new PrintWriter(System.out); solve(); out.flush(); } private int scan()throws IOException { if(total<0) throw new InputMismatchException(); if(index>=total) { index=0; total=in.read(buf); if(total<=0) return -1; } return buf[index++]; } private int ni() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); int sgn = 1; if (c == '-') { sgn = -1; c = scan(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = scan(); } while (!isSpaceChar(c)); return res * sgn; } private long nl() throws IOException { long num = 0; int b; boolean minus = false; while ((b = scan()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = scan(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = scan(); } } private double nd() throws IOException{ return Double.parseDouble(ns()); } private String ns() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) res.appendCodePoint(c); c = scan(); } while (!isSpaceChar(c)); return res.toString(); } private String nss() throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); return br.readLine(); } private char nc() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); return (char) c; } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } private boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhiteSpace(c); } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
a708ae555da8516e3a2e9ca92f55a8d8
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.math.*; public class CardDeck { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int runs = sc.nextInt(); while(runs-->0) { int size = sc.nextInt(); int[] arr = new int[size]; int[] hold = new int[size]; for(int i = 0;i<size;i++) { arr[i] = sc.nextInt(); hold[arr[i]-1] = i; } int temp; int num = size; for(int i = size-1;i>-1;--i) { temp = hold[i]; if(temp<num) { for(int j = temp;j<num;j++) { System.out.print(arr[j]+" "); } num = temp; } } System.out.println(); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
58e14613b47831f0a21a3f5b42a380d5
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Scanner; import java.util.Set; import java.util.Vector; public class NewClass1 { public static void main(String[] args) { Scanner ob = new Scanner(System.in); int t = ob.nextInt(); while (t-- > 0) { int n = ob.nextInt(); int a[] = new int[n]; int b[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ob.nextInt(); b[a[i] - 1] = i; } int s; int e = n; for (int i = a.length - 1; i >= 0; i--) { s = b[i]; if (s < e) { for (int j = s; j < e; j++) { System.out.print(a[j] + " "); } e = s; } } System.out.println(); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
4442a781d702cba88224963d360c5527
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.io.*; public class B_CardDeck { public static void main(String args[]) throws NumberFormatException, IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t =Integer.parseInt(br.readLine()); StringBuilder sb=new StringBuilder(); while(t-->0){ int n =Integer.parseInt(br.readLine()); String[] str=br.readLine().split(" "); int[] arr=new int[n+1]; for(int i=0;i<n;i++) { arr[i+1]=Integer.parseInt(str[i]); } int x=n; Stack<Integer> st=new Stack<>(); for(int i=n;i>0;i--) { int idx=(int)Math.abs(arr[i]);; arr[idx]=-1*arr[idx]; if(x==Math.abs(arr[i])) { sb.append(x+" "); while(st.size()>0) { sb.append(st.pop()+" "); } while(arr[x]<0) { x--; } }else { st.push(Math.abs(arr[i])); } } while(st.size()>0) { sb.append(st.pop()+" "); } sb.append("\n"); } System.out.println(sb); } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
c0da4205f3c6b392178c293e8e75117a
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.BufferedReader; 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; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeSet; import static java.lang.Math.*; public class B { static FastScanner sc; static PrintWriter pw; public static void main(String[] args) { sc = new FastScanner(); pw = new PrintWriter(System.out); int T = sc.nextInt(); while (T-- > 0) { solve(); } pw.close(); } private static void solve() { int n = sc.nextInt(); int[] a = sc.readArray(n); TreeSet<Integer> max = new TreeSet<>(Collections.reverseOrder()); for(int i = 1; i <= n; i++) { max.add(i); } Stack<Integer> st = new Stack<>(); for(int i = n-1; i >= 0; i--) { st.push(a[i]); if(a[i] == max.first()) { while(!st.isEmpty()) { pw.print(st.pop() + " "); } } max.remove(a[i]); } pw.println(); } static void debug(Object... O) { System.out.println(Arrays.deepToString(O)); } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
64312001216835fb7ad776d012c93264
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class demo { public static void main(String[] args) { FastScanner sc=new FastScanner(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int[] a=new int[n]; SortedSet<Integer> q=new TreeSet<>(Collections.reverseOrder()); for (int i = 0; i < n; i++) { a[i]=sc.nextInt(); q.add(a[i]); } int max=n; int idx=n-1; while(!q.isEmpty()) { max=q.first(); List<Integer> ans=new ArrayList<>(); while(a[idx]!=max) { q.remove(a[idx]); ans.add(a[idx]); idx--; } q.remove(a[idx]); ans.add(a[idx]); for (int i = ans.size()-1; i >=0; i--) { System.out.print(ans.get(i)+" "); } idx--; } System.out.println(); } } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
7f6b3e0b9514c02251638b4167079d37
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.io.*; 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[] cards= new int[n]; int[] deck= new int[n]; int[] map =new int[n+1]; for(int i=0;i<n;i++){ cards[i]=sc.nextInt(); map[cards[i]]=i; } StringBuilder sb=new StringBuilder(); int end=n; for(int i=n;i>0;i--){ int index=map[i];//max index if(map[i]!=-1){ for(int j=index;j<end;j++){ map[cards[j]]=-1; sb.append(""+cards[j]); sb.append(' '); } end=index; } } System.out.println(sb.toString()); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
be8d5cf8baacf55b49fcf9140f650464
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.Scanner; import java.util.Stack; public class B { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); for (int i = 0; i < t; i++) { int n = s.nextInt(); int[] ps = new int[n]; boolean[] printedB = new boolean[n]; int printed = 0; for (int j = 0; j < n; j++) { ps[j] = s.nextInt(); } int next = n - 1; while (printed < n) { int actualNext = -1; for (; next >= 0; next--) { if (!printedB[next]) { actualNext = next + 1; break; } } // System.err.println("next: " + next); Stack<Integer> stack = new Stack<>(); for (int j = n - 1 - printed; j >= 0; j--) { stack.push(ps[j]); // System.err.println("pushed " + ps[j]); printedB[ps[j] - 1] = true; printed++; if (ps[j] == actualNext) { break; } } while (!stack.empty()) { // System.err.println("popped " + stack.peek()); System.out.print(stack.pop() + " "); } } System.out.print("\n"); } s.close(); } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
84ebe8438278741c4c7c2465fde16642
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.*; import java.util.*; import java.math.*; public class Main { static FastReader in; static PrintWriter o; public static void solve() { int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int[] arr = new int[n]; int[] max = new int[n]; arr[0] = in.nextInt(); max[0] = arr[0]; for (int i = 1; i < n; i++) { arr[i] = in.nextInt(); max[i] = Math.max(arr[i], max[i-1]); } List<Integer> ans = new ArrayList<>(); int k = n; int i = n - 1; while (k > 0) { for (int j = i; j >= 0; j--) { if (max[j] == arr[j]) { for (int l = j; l < k; l++) { ans.add(arr[l]); } k = j; i = k - 1; break; } } } for (i = 0; i < ans.size(); i++) { o.print(ans.get(i) + " "); } o.println(); } o.close(); return; } public static void main(String[] args) { in = new FastReader(); o = new PrintWriter(System.out); solve(); return; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } boolean isReady() throws Throwable{ return br.ready(); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
83d5110aaaf3e3a070682797d8bcfb51
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.*; import java.util.*; public class Prb2 { static PrintWriter pw; static Scanner sc; static long mod = 1000000000+7; static void print(final String arg) { pw.write(arg); pw.flush(); } static int ni(){ return sc.nextInt(); } static long nl(){ return sc.nextLong(); } static void runFile() throws Exception { sc = new Scanner(new FileReader("input.txt")); pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); } static void runIo() throws Exception { pw =new PrintWriter(System.out); sc = new Scanner(System.in); } static boolean sv[] = new boolean[1000002]; static void seive() { //true -> not prime // false->prime sv[0] = sv[1] = true; sv[2] = false; for(int i = 0; i< sv.length; i++) { if( !sv[i] && (long)i*(long)i < sv.length ) { for ( int j = i*i; j<sv.length ; j += i ) { sv[j] = true; } } } } static class Pair { int a ,b; Pair(int a, int b){ this.a = a; this.b = b; } } public static void main(String[] args) throws Exception { // runFile(); runIo(); int t = sc.nextInt(); //int t = 1; while( t-- > 0 ) { int n = ni(); int ar[] = new int[n+1]; int map[] = new int[n+1]; int ans[] = new int[n]; int pos = 0; for(int i = 1; i<=n; i++){ ar[i] = ni(); map[ar[i]] = i; } for( int i = n ; i > 0; i--) { int idx = map[i]; for(int j = idx; j<=n && ar[j] != -1; j++){ ans[pos++] = ar[j]; ar[j] = -1; } } for(int i: ans) print(i+" "); print("\n"); } } } // 0 1 2 3 4 5 // 4 2 5 3 -1 -1
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
d05159422dc038702e5c2e1653db8a68
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.io.*; public class HelloWorld { static class Pair implements Comparable<Pair> { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair p) { if(x < p.x) return -1; else if(x > p.x) return 1; else return 0; } } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(System.out); int T = Integer.parseInt(br.readLine()); while(T-- > 0) { int n = Integer.parseInt(br.readLine()); int[] a = new int[n]; Pair[] p = new Pair[n]; StringTokenizer st = new StringTokenizer(br.readLine()); for(int i = 0; i < n; i++) { a[i] = Integer.parseInt(st.nextToken()); Pair pair = new Pair(a[i], i); p[i] = pair; } Arrays.sort(p); boolean[] vis = new boolean[n]; for(int i = n-1; i >= 0; i--) { for(int j = p[i].y; j < n; j++) { if(!vis[j]) { writer.print(a[j]+" "); vis[j] = true; } else break; } } writer.println(); } writer.close(); } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
19ae6224db7461ba198dabda976b8850
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; public class SwingControlDemo { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); HashMap<Integer,Integer> h=new HashMap<>(n); Integer a[]=new Integer[n]; Integer temp[]=new Integer[n]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); temp[i]=a[i]; } Arrays.sort(temp, Collections.reverseOrder()); for(int i=0;i<n;i++) h.put(a[i], i); //anwer.. int prev=n,j=0,loop; for(int i=0;i<n;i++) { loop=0; j=h.get(temp[i]); for(int k=j;k<prev;k++) { System.out.print(a[k]+" "); loop=1; } if(loop==1) prev=j; if(h.get(temp[i])==0) break; } System.out.println(); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
cbd0dbca53d4f946b1650b970301cf41
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.awt.*; import java.awt.event.*; import java.util.Scanner; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SwingControlDemo { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int a[]=new int[n]; int temp[]=new int[n+1]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); temp[a[i]]=i; } //anwer.. long start=0,end=n; for(int i=n;i>0;i--) { start=temp[i]; if(end>start) { for(int j=(int)start;j<end;j++) { System.out.print(a[j]+" "); } end=start; } } System.out.println(); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
33875c3aff86c4f227641718511ce50b
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; public class ConditionalStatements { /* int a[]=new int[n]; String line=br.readLine(); String str[]= line.trim().split(" "); for(int i=0;i<n;i++) { a[i]=Integer.parseInt(str[i]); } */ // Integer Input // int n=Integer.parseInt(br.readLine()); // static ArrayList<Integer> al=new ArrayList<>(); // public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while(t--!=0) { int n=Integer.parseInt(br.readLine()); int a[]=new int[n]; String line=br.readLine(); String str[]= line.trim().split(" "); for(int i=0;i<n;i++) { a[i]=Integer.parseInt(str[i]); } int res[]=new int[n]; HashMap<Integer,Integer> map=new HashMap<>(); int temp[]=new int[n]; for(int i=0;i<n;i++) { map.put(a[i], i); } int fill=0,ind=n; boolean bool=false; for(int i=n;i>0;i--) { bool=false; int tempind=map.get(i); for(int j=tempind;j<ind;j++) { bool=true; if(j<n && fill<n) res[fill++]=a[j]; } if(bool) ind=tempind; } for(int i=0;i<n;i++) { System.out.print(res[i]+" "); }System.out.println(); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
fa97cf7a1233a34474341fc5aab24166
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.*; import java.util.*; public class B { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static FastReader s = new FastReader(); static PrintWriter out = new PrintWriter(System.out); private static int[] rai(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextInt(); } return arr; } private static int[][] rai(int n, int m) { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = s.nextInt(); } } return arr; } private static long[] ral(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextLong(); } return arr; } private static long[][] ral(int n, int m) { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = s.nextLong(); } } return arr; } private static int ri() { return s.nextInt(); } private static long rl() { return s.nextLong(); } private static String rs() { return s.next(); } static int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } static long gcd(long a,long b) { if(b==0) { return a; } return gcd(b,a%b); } static boolean isPrime(int n) { //check if n is a multiple of 2 if(n==1) { return false; } if(n==2) { return true; } if (n % 2 == 0) return false; //if not, then just check the odds for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static boolean[] sieveOfEratosthenes(int n) { // Create a boolean array "prime[0..n]" and initialize // all entries it as true. A value in prime[i] will // finally be false if i is Not a prime, else true. boolean prime[] = new boolean[n+1]; for(int i=0;i<n;i++) prime[i] = true; for(int p = 2; p*p <=n; p++) { // If prime[p] is not changed, then it is a prime if(prime[p] == true) { // Update all multiples of p for(int i = p*p; i <= n; i += p) prime[i] = false; } } return prime; } public static void main(String[] args) { StringBuilder ans = new StringBuilder(); int t = ri(); // int t=1; while (t-- > 0) { int n=ri(); int[] arr=rai(n); SparseMatrix sp=new SparseMatrix(arr); int prev=n; int l=0,r=n-1; while(l<=r) { int ind=sp.getMaxIndex(l,r); for(int i=ind;i<prev;i++) { ans.append(arr[i]).append(" "); } prev=ind; r=ind-1; } ans.append("\n"); } out.print(ans.toString()); out.flush(); } static class SparseMatrix { private int[] arr; private int m; private int[][] minSparse; private int[][] minIndex; private int[][] maxSparse; private int[][] maxIndex; private int n; public SparseMatrix(int[] arr) { this.arr = arr; this.m=arr.length; this.n=Integer.toBinaryString(m).length(); minSparse=new int[n][m]; minIndex=new int[n][m]; maxSparse=new int[n][m]; maxIndex=new int[n][m]; // for(int i=0;i<n;i++) // { // Arrays.fill(minSparse[i],-1); // } // for(int i=0;i<n;i++) // { // Arrays.fill(minIndex[i],-1); // } createMinSparse(); createMaxSparse(); } private void createMaxSparse() { for(int j=0;j<m;j++) { maxSparse[0][j]=arr[j]; maxIndex[0][j]=j; } for(int i=1;i<n;i++) { for(int j=0;j+(1<<(i-1))<m;j++) { int left=maxSparse[i-1][j]; int right=maxSparse[i-1][j+(1<<(i-1))]; maxSparse[i][j]=Math.max(left,right); if(left>=right) { maxIndex[i][j]=maxIndex[i-1][j]; } else { maxIndex[i][j]=maxIndex[i-1][j+(1<<(i-1))]; } } } } private void createMinSparse() { //filling the first row of sparse matrix with the values of the input array for(int j=0;j<m;j++) { minSparse[0][j]=arr[j]; minIndex[0][j]=j; } //filling other rows of the sparse matrix for(int i=1;i<n;i++) { for(int j=0;j+(1<<(i-1))<m;j++) { int left=minSparse[i-1][j]; int right=minSparse[i-1][j+(1<<(i-1))]; //change to min-> max to create MaxSparseMatrix minSparse[i][j]=Math.min(left,right); //filling index if(left<=right) { minIndex[i][j]=minIndex[i-1][j]; } else { minIndex[i][j]=minIndex[i-1][j+(1<<(i-1))]; } } } } //get minimum value in range l->r inclusive /* for any range [l, r] we can find the two values and find their minimum. These values are defined below: len: length of the required range, i.e., r-l+1 p: maximum power of 2 that can fit in len. E.g, [1,11] , len=11, thus p=3 k: 2^p find the minimum between sparse[p][l] and sparse[p][r-k+1] */ public int getMin(int l,int r) { int len=r-l+1; int p=Integer.toBinaryString(len).length()-1; int k=1<<p; int left=minSparse[p][l]; int right=minSparse[p][r-k+1]; return Math.min(right,left); } public int getMinIndex(int l,int r) { int len=r-l+1; int p=Integer.toBinaryString(len).length()-1; int k=1<<p; int left=minSparse[p][l]; int right=minSparse[p][r-k+1]; if (left <= right) { return minIndex[p][l]; } else { return minIndex[p][r - k + 1]; } } public int getMax(int l,int r) { int len=r-l+1; int p=Integer.toBinaryString(len).length()-1; int k=1<<p; int left=maxSparse[p][l]; int right=maxSparse[p][r-k+1]; return Math.max(right,left); } public int getMaxIndex(int l,int r) { int len=r-l+1; int p=Integer.toBinaryString(len).length()-1; int k=1<<p; int left=maxSparse[p][l]; int right=maxSparse[p][r-k+1]; if(left>=right) { return maxIndex[p][l]; } return maxIndex[p][r-k+1]; } void print() { for(int i=0;i<minSparse.length;i++) { for(int j=0;j<minSparse[i].length;j++) { System.out.print(minSparse[i][j]+" "); } System.out.println(); } System.out.println(); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { System.out.print(minIndex[i][j]+" "); } System.out.println(); } } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
827563ff217668dcbea2dacaaae642f0
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.Scanner; public class B_Card_Deck { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int q = sc.nextInt(); while (q-- > 0) { int n = sc.nextInt(); int ar[]=new int[n]; int ind[]=new int[n+1]; int i=0; StringBuilder sb=new StringBuilder(); for(i=0;i<n;i++) { ar[i]=sc.nextInt(); ind[ar[i]]=i; } int max=n; for(i=n;i>=1;i--) { int pos=ind[i]; if(pos<max) { for(int j=pos;j<max;j++) { sb.append(ar[j]+" "); } max=pos; } } System.out.println(sb); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
8d337e391da4b0f3aaadaf62665beb4f
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.util.Scanner; import java.io.*; import javax.lang.model.util.ElementScanner6; import static java.lang.System.out; import java.util.Stack; import java.util.Queue; import java.util.LinkedList; public class B1492 { static int mod=(int)(1e9+7); static long MOD=(long)(1e9+7); static FastReader in=new FastReader(); static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public static void main(String args[]) { int tc=1; tc=in.nextInt(); tcloop: while(tc-->0) { int n=in.nextInt(); int ind=-1; int arr[]=new int[n]; for(int i=0;i<n;i++) { arr[i]=in.nextInt(); if(arr[i]==n) { ind=i; } } LinkedHashSet<Integer> hs =new LinkedHashSet<Integer>(); for(int i=n-1;i>=1;i--) { hs.add(i); } Deque<Integer> dq=new ArrayDeque<Integer>(); int req=n; for(int i=n-1;i>=0;i--) { if(arr[i]==req) { dq.addFirst(arr[i]); for(int j : hs) { req = j; break; } hs.remove(req); for(int j : dq) { pr.print(j+" "); } dq.clear(); } else { dq.addFirst(arr[i]); hs.remove(arr[i]); } } for(int j : dq) { pr.print(j+" "); } pr.println(); } pr.flush(); } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readIntArray(int n) { int a[]=new int[n]; for(int i=0;i<n;i++)a[i]=nextInt(); return a; } long[] readLongArray(int n) { long a[]=new long[n]; for(int i=0;i<n;i++)a[i]=nextLong(); return a; } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
0ca00fd47d1c83c84d47c716b6aaa943
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.awt.Point; public class CFTemplate { static final long MOD = 1000000007L; //static final long MOD2 = 1000000009L; //static final long MOD = 998244353L; //static final long INF = 500000000000L; static final int INF = 1100000000; static final int NINF = -100000; static FastScanner sc; static PrintWriter pw; static final int[][] dirs = {{-1,0},{1,0},{0,-1},{0,1}}; public static void main(String[] args) { sc = new FastScanner(); pw = new PrintWriter(System.out); int Q = sc.ni(); for (int q = 0; q < Q; q++) { int N = sc.ni(); int[] nums = sc.intArray(N, -1); boolean[] hit = new boolean[N]; ArrayList<Integer> ans = new ArrayList<Integer>(); ArrayList<Integer> cur = new ArrayList<Integer>(); int target = N-1; for (int i = N-1; i >= 0; i--) { cur.add(nums[i]+1); hit[nums[i]] = true; if (nums[i]==target) { Collections.reverse(cur); ans.addAll(cur); cur.clear(); } while (target >= 0 && hit[target]) target--; } for (int a: ans) pw.print(a + " "); pw.println(); } pw.close(); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in), 32768); st = null; } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } int[] intArray(int N, int mod) { int[] ret = new int[N]; for (int i = 0; i < N; i++) ret[i] = ni()+mod; return ret; } long nl() { return Long.parseLong(next()); } long[] longArray(int N, long mod) { long[] ret = new long[N]; for (int i = 0; i < N; i++) ret[i] = nl()+mod; return ret; } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
b3ee3c97c07a467e960112a65e883065
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; public class pcarp{ static int max_value; public static void main(String[] args) { FastScanner sc = new FastScanner(); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int[] arr = new int[n]; int[] pos = new int[n+1]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); pos[arr[i]]=i; } int start=0; int end = n; for(int i=n;i>0;i--){ start=pos[i]; if(end>start){ for(int j=start;j<end;j++){ System.out.print(arr[j]+" "); } end=start; } } System.out.println(); } } public static long inc(String s){ char[] arr = s.toCharArray(); Arrays.sort(arr); String ans=""; for(int i=0;i<s.length();i++){ ans+=arr[i]; } return Long.parseLong(ans); } public static long dec(String s){ char[] arr = s.toCharArray(); Arrays.sort(arr); String ans=""; for(int i=s.length()-1;i>=0;i--){ ans+=arr[i]; } return Long.parseLong(ans); } public static long max(String n){ long max=0; for(int i=0;i<n.length();i++){ if(n.charAt(i)-'0'>max){ max=n.charAt(i)-'0'; } } return max; } public static long base(String n, long b){ long ans=0; for(int i=0;i<n.length();i++){ ans+=power(b,(long)(n.length()-1-i))*(n.charAt(i)-'0'); } return ans; } public static long power(long a, long b){ long res=1; while(b>0){ if((b&1)==1){ res=res*a; } b=b>>1; a=a*a; } return res; } public static long value(int[] arr){ Arrays.sort(arr); return arr[arr.length/2]-arr[(arr.length-1)/2]+1; } static void function(int l, int r, int[] arr, int[] depth, int d){ if(r<l){ return; } if(r==l){ depth[l]=d; return; } int m = l; for(int i=l+1;i<=r;i++){ if(arr[m]<arr[i]){ m=i; } } depth[m]=d; function(l,m-1,arr,depth,d+1); function(m+1,r,arr,depth,d+1); } static boolean perfectCube(long N) { long cube_root; cube_root = (long)Math.round(Math.cbrt(N)); // If cube of cube_root is equals to N, // then print Yes Else print No if (cube_root * cube_root * cube_root == N) { return true; } return false; } public static int value(int n){ if(n==0){ return 0; } int cnt=0; while(n>0){ cnt++; n/=2; } return cnt; } public static int value1(int n){ int cnt=0; while(n>1){ cnt++; n/=2; } return cnt; } public static String function(String s){ if(check(s)){ return s; } if(s.length()==2 && s.charAt(0)==s.charAt(1)){ return ""; } if(s.charAt(0)==s.charAt(1)){ return function(s.substring(2,s.length())); } else{ return function(s.charAt(0)+function(s.substring(1,s.length()))); } } static boolean isPowerOfTwo(int n){ if(n==0) return false; return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2))))); } static boolean isPerfectSquare(double x) { if (x >= 0) { double sr = Math.sqrt(x); return ((sr * sr) == x); } return false; } public static boolean isPerfect(int n){ int a = (int)Math.sqrt(n); if(a*a==n){ return true; } return false; } public static boolean check(String s){ if(s.length()==1){ return true; } for(int i=1;i<s.length();i++){ if(s.charAt(i)==s.charAt(i-1)){ return false; } } return true; } public static boolean isPrime(int n){ boolean flag=true; while(n%2==0){ n=n/2; flag=false; } for(int i=3;i<=Math.sqrt(n);i+=2){ if(n%i==0){ flag=false; while(n%i==0){ n=n/i; } } } return flag; } public static void dfst(ArrayList<ArrayList<Integer>> graph,int src, int deg,boolean ef, boolean of, boolean[] vis, boolean[] flip, int[] init, int[] goal){ if(vis[src]){ return; } vis[src]=true; if((deg%2==0 && ef) || (deg%2==1 && of)){ init[src]=1-init[src]; } if(init[src]!=goal[src]){ flip[src]=true; if(deg%2==0){ ef=!ef; } else{ of=!of; } } for(int i=0;i<graph.get(src).size();i++){ if(!vis[graph.get(src).get(i)]){ dfst(graph,graph.get(src).get(i),deg+1,ef,of,vis,flip,init,goal); } } } public static void dfs(ArrayList<ArrayList<Integer>> graph, int src, int val, boolean[] vis){ vis[src]=true; int cur_val =0; for(int i=0;i<graph.get(src).size();i++){ if(!vis[graph.get(src).get(i)]){ cur_val=val+1; dfs(graph,graph.get(src).get(i),cur_val,vis); } if(max_value<cur_val){ max_value=cur_val; } cur_val=0; } } public static ArrayList<Integer> pf(int n){ ArrayList<Integer> arr = new ArrayList<>(); boolean flag=false; while(n%2==0){ flag=true; n/=2; } if(flag){ arr.add(2); } for(int i=3;i<=Math.sqrt(n);i++){ if(n%i==0){ arr.add(i); while(n%i==0){ n/=i; } } } if(n>1){ arr.add(n); } return arr; } static int gcd(int a, int b){ if(b==0){ return a; } else{ return gcd(b,a%b); } } static boolean function(int n, int i, int[] arr, int sum){ if(sum==0){ return true; } if(i==n && sum!=0){ return false; } if(sum<arr[i]){ return function(n,i+1,arr,sum); } else{ return function(n,i+1,arr,sum-arr[i]) || function(n,i+1,arr,sum); } } public static long fact( long n, long mod){ long res =1; for(int i=1;i<=n;i++){ res%=mod; i%=mod; res=(res*i)%mod; } return res; } public static long nCk(long n,long k, long mod){ return (fact(n,mod)%mod*modular(fact(k,mod),mod-2,mod)%mod*modular(fact(n-k,mod),mod-2,mod)%mod)%mod; } public static long modular(long n, long e, long mod){ long res = 1; n%=mod; if (n == 0) return 0; while(e>0){ if((e&1)==1){ res=(res*n)%mod; } e=e>>1; n=(n*n)%mod; } return res; } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
4a10e07e8cc7d76ca8d5f599136a4cb3
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { FastReader input=new FastReader(); PrintWriter out=new PrintWriter(System.out); int T=input.nextInt(); while(T-->0) { int n=input.nextInt(); int p[]=new int[n]; for(int i=0;i<n;i++) { p[i]=input.nextInt(); } int i=n-1; HashSet<Integer> set=new HashSet<>(); int x=n; ArrayList<Integer> list=new ArrayList<>(); while(i>=0) { for(int j=x;j>=1;j--) { if(!set.contains(j)) { x=j; break; } } while(i>=0) { set.add(p[i]); if(p[i]==x) { list.add(i); i--; break; } i--; } } int pre=n; for(int j=0;j<list.size();j++) { if(j!=0) { pre=list.get(j-1); } for(int k=list.get(j);k<pre;k++) { out.print(p[k]+" "); } } out.println(); } 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
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
7993e30ffa4e569c1226d912fb9ab91e
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.Scanner; import java.util.Stack; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t>0){ int n=sc.nextInt(); int[] arr=new int[n+1]; arr[0]=0; Stack<Pair> s=new Stack<>(); int arr1=sc.nextInt(); arr[1]=arr1; s.push(new Pair(arr1,1)); for(int i=2;i<n+1;i++){ arr[i]=sc.nextInt(); if(arr[i]>s.peek().val){ s.push(new Pair(arr[i],i)); } } int end=n; while(s.size()>0){ Pair p=s.pop(); for(int i=p.idx;i<=end;i++){ System.out.print(arr[i]+" "); } end=p.idx-1; } System.out.println(); t--; } } public static class Pair{ int val; int idx; Pair(int val,int idx){ this.val=val; this.idx=idx; } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
de85ae4085527b924fa1fbd4485aa7ec
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.StringTokenizer; public class B { public static void main(String[] args) { FastReader fs = new FastReader(); StringBuilder sb = new StringBuilder(); int t = fs.nextInt(); for(int tt = 0; tt < t; tt++) { int n = fs.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++) { arr[i] = fs.nextInt(); } int index = 0; List<Integer> list = new LinkedList<>(); while(index < arr.length) { List<Integer> currList = new ArrayList<>(); currList.add(arr[index]); int curr = index + 1; while(curr < arr.length && arr[curr] < arr[index]) { currList.add(arr[curr]); curr++; } list.addAll(0, currList); index = curr; } for(Integer l : list) { sb.append(l); sb.append(" "); } sb.append("\n"); } System.out.println(sb.toString()); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
31a6bef191417de201b5f018eb87f6ea
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class SevenZeroFourB { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int testCases = Integer.parseInt(sc.nextLine()); for (int i = 0; i < testCases; i++) { int arrLen = Integer.parseInt(sc.nextLine()); String arr = sc.nextLine(); String[] arrElements = arr.trim().split("\\s+"); int[] arrInt = new int[arrLen]; for (int j = 0; j < arrLen; j++) { arrInt[j] = Integer.parseInt(arrElements[j]); } int[] maxInt = new int[arrLen]; maxInt[0] = arrInt[0]; for (int j = 1; j < arrLen; j++) { maxInt[j] = Math.max(maxInt[j-1], arrInt[j]); } int startPointer = arrLen-1; int endPointer = arrLen-1; List<Integer> resultSet = new ArrayList<>(); for(int j = arrLen-1 ; j>0 ; j--){ if(maxInt[j] == maxInt[j-1]){ startPointer--; } else { printData(startPointer, endPointer, arrInt, resultSet); endPointer = startPointer-1; startPointer = startPointer-1; } } printData(startPointer, endPointer, arrInt, resultSet); for (int j = 0; j < arrLen; j++) { if(j!=arrLen-1){ System.out.print(resultSet.get(j) + " "); } else { System.out.println(resultSet.get(j)); } } } } private static void printData(int startPointer, int endPointer, int[] arrInt, List<Integer> resultSet) { for(int i = startPointer; i <= endPointer; i++){ resultSet.add(arrInt[i]); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
3a61ad291986e6d09b930de280f6d4b0
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; public class P1492A { static FastReaderP1492A in = new FastReaderP1492A(); public static void main(String... x) { int test = i(); while (test-- > 0) { int n = i(); int ar[] = inputIntgerArray(n); int i, j; int temp[] = new int[n]; temp[0]=ar[0]; for (i = 1; i < n; i++) { temp[i]=Math.max(ar[i],temp[i-1]); } List<List<Integer>> ans=new ArrayList<List<Integer>>(); List<Integer> list=new ArrayList<>(); for(i=n-1;i>=0;i--){ if(ar[i]!=temp[i]){ list.add(ar[i]); } else { list.add(ar[i]); ans.add(list); list=new ArrayList<>(); } } for(List<Integer> al: ans){ for(i=al.size()-1;i>=0;i--){ System.out.print(al.get(i)+" "); } } } } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static String string() { return in.nextLine(); } static int[] inputIntgerArray(int N) { int A[] = new int[N]; for (int i = 0; i < N; i++) A[i] = in.nextInt(); return A; } static long[] inputLongArray(int N) { long A[] = new long[N]; for (int i = 0; i < A.length; i++) A[i] = in.nextLong(); return A; } } class FastReaderP1492A { BufferedReader br; StringTokenizer st; public FastReaderP1492A() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
1862af56d72edbac667fb0ca37eb2b2d
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import javax.print.DocFlavor; import javax.swing.text.html.parser.Entity; import java.awt.*; import java.io.*; import java.lang.reflect.Array; import java.util.*; import java.util.List; import java.util.stream.Collectors; public class Main { static FastScanner sc; static PrintWriter pw; static final long INF = 2000000000000000l; static final int MOD = 1000000007; static final int N = 1005; static long mul(long a, long b, long mod) { return (a * b) % mod; } public static long pow(long a, long n, long mod) { long res = 1; while(n != 0) { if((n & 1) == 1) { res = mul(res, a, mod); } a = mul(a, a, mod); n >>= 1; } return res; } /*public static int inv(int a) { return pow(a, MOD-2, mod); }*/ public static List<Integer> getPrimes() { List<Integer> ans = new ArrayList<>(); boolean[] prime = new boolean[N]; Arrays.fill(prime, true); prime[0] = prime[1] = false; for(int i = 2; i < N; ++i) { if(prime[i]) { ans.add(i); if (i * 1l * i <= N) for (int j = i * i; j < N; j += i) prime[j] = false; } } return ans; } public static long gcd(long a, long b) { if(b == 0) return a; else return gcd(b, a % b); } public static class A { long x; long y; A(long a, long b) { this.x = a; this.y = b; } } public static void solve() throws IOException { int n = sc.nextInt(); int[] a = new int[n]; List<Integer> b = new ArrayList<>(); for(int i = 0; i < n; ++i) { a[i] = sc.nextInt(); b.add(a[i]); } Collections.sort(b); Set<Integer> s = new HashSet<>(); int i = n - 1; int j = n - 1; List<Integer> tmp = new ArrayList<>(); List<Integer> ans = new ArrayList<>(); while(i >= 0) { while(i >= 0 && a[i] < b.get(j)) { tmp.add(a[i]); s.add(a[i]); i--; } if(i >= 0) { tmp.add(a[i]); s.add(a[i]); i--; } j--; while(j >= 0 && s.contains(b.get(j))) j--; Collections.reverse(tmp); ans.addAll(tmp); tmp.clear(); } for(Integer num : ans) { pw.print(num + " "); } pw.println(); } public static void main(String[] args) throws IOException { sc = new FastScanner(System.in); pw = new PrintWriter(System.out); int xx = sc.nextInt(); while(xx > 0) { solve(); xx--; } pw.close(); } } class FastScanner { static StringTokenizer st = new StringTokenizer(""); static BufferedReader br; public FastScanner(InputStream inputStream) { br = new BufferedReader(new InputStreamReader(inputStream)); } public String next() throws IOException { while (!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 double nextDouble() throws IOException { return Double.parseDouble(next()); } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
f77c423a6d7c0b3e11c9d43718cb997b
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.ArrayList; import java.util.Scanner; public class wow { // static long count = 0; public static void main(String[] args) { Scanner scn=new Scanner(System.in); int t=scn.nextInt(); while(t-->0) { int n=scn.nextInt(); int[] arr=new int[n]; for(int i=0;i<n;i++) { arr[i]=scn.nextInt(); } // int s=0; int[] find=new int[n]; find[0]=arr[0]; for(int i=1;i<n;i++) { find[i]=Math.max(find[i-1], arr[i]); } int e=n-1; int k=0; int[] ans =new int[n]; while(k<n) { int max=find[e]; int index=0; for(int i=e;i>=0;i--) { if(find[i]!=max) { index=i+1; break; } } for(int i=index;i<=e;i++) { ans[k]=arr[i]; ++k; } e=index-1; } for(int v:ans) { System.out.print(v+" "); } System.out.println(); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
c1eddc12116d906ec4b4bec84a32dc47
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.HashMap; import java.util.Map; import java.util.Scanner; import static java.lang.Math.min; /** * Accomplished using the EduTools plugin by JetBrains https://plugins.jetbrains.com/plugin/10081-edutools */ 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[] p = new int[n]; int pre = sc.nextInt(); p[0] = pre; Map<Integer, Integer> mp = new HashMap<>(); for (int i = 1; i < n; i++) { p[i] = sc.nextInt(); mp.put(pre, p[i]); pre = p[i]; } int[] visit = new int[n+1]; StringBuilder s = new StringBuilder(); for (int i = n; i > 0; i--) { if (visit[i]==0) { s.append(i).append(" "); visit[i] = 1; Integer x = mp.get(i); while (x!=null && visit[x]==0) { s.append(x).append(" "); visit[x] = 1; x = mp.get(x); } } } System.out.println(s); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
ec22684e90c1ef5307dca15c7366615d
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.io.*; import java.math.*; /** * * @Har_Har_Mahadev */ public class B { private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353; private static int N = 0; public static void process() throws IOException { int n = sc.nextInt(); int arr[] = sc.readArray(n); Deque<Integer> q = new LinkedList<Integer>(); Deque<Integer> ans = new LinkedList<Integer>(); HashSet<Integer> set = new HashSet<Integer>(); for(int e : arr)q.add(e); int curr = n; ArrayList<Integer> res = new ArrayList<Integer>(); while(!q.isEmpty()) { ans.clear(); while(q.peekLast() != curr) { ans.addFirst(q.pollLast()); set.add(ans.peekFirst()); } ans.addFirst(q.pollLast()); set.add(ans.peekFirst()); while(set.contains(curr))curr--; for(int e : ans)res.add(e); ans.clear(); } for(int e : res)print(e+" "); println(); } //============================================================================= //--------------------------The End--------------------------------- //============================================================================= static FastScanner sc; static PrintWriter out; public static void main(String[] args) throws IOException { boolean oj = true; if (oj) { sc = new FastScanner(); out = new PrintWriter(System.out); } else { sc = new FastScanner(100); out = new PrintWriter("output.txt"); } int t = 1; t = sc.nextInt(); while (t-- > 0) { process(); } out.flush(); out.close(); } static class Pair implements Comparable<Pair> { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { return Integer.compare(this.x, o.x); } // @Override // public boolean equals(Object o) { // if (this == o) return true; // if (!(o instanceof Pair)) return false; // Pair key = (Pair) o; // return x == key.x && y == key.y; // } // // @Override // public int hashCode() { // int result = x; // result = 31 * result + y; // return result; // } } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static void println(Object o) { out.println(o); } static void println() { out.println(); } static void print(Object o) { out.print(o); } static void pflush(Object o) { out.println(o); out.flush(); } static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } static int max(int x, int y) { return Math.max(x, y); } static int min(int x, int y) { return Math.min(x, y); } static int abs(int x) { return Math.abs(x); } static long abs(long x) { return Math.abs(x); } static int log2(int N) { int result = (int) (Math.log(N) / Math.log(2)); return result; } static long max(long x, long y) { return Math.max(x, y); } static long min(long x, long y) { return Math.min(x, y); } public static int gcd(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } public static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } public static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static int lcm(int a, int b) { return (a * b) / gcd(a, b); } public static int lower_bound(int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) high = mid; else low = mid + 1; } return low; } public static int upper_bound(int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > x) high = mid; else low = mid + 1; } return low; } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() throws FileNotFoundException { br = new BufferedReader(new InputStreamReader(System.in)); } FastScanner(int a) throws FileNotFoundException { br = new BufferedReader(new FileReader("input.txt")); } String next() throws IOException { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) throws IOException { int[] A = new int[n]; for (int i = 0; i != n; i++) { A[i] = sc.nextInt(); } return A; } long[] readArrayLong(int n) throws IOException { long[] A = new long[n]; for (int i = 0; i != n; i++) { A[i] = sc.nextLong(); } return A; } } static void ruffleSort(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
8215c08310d0be2e192fd034de6ba9bd
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; import java.text.*; public class Main { static long mod = 1000_000_007; static long mod1 = 998244353; static boolean fileIO = false; static boolean memory = true; static FastScanner f; static PrintWriter pw; static double eps = (double)1e-6; static int oo = (int)1e7; // N = 1 or N = max ? // longs vs. ints ? // max / min ? public static int[] build(int arr[] , int n) { int segtree[] = new int[2 * n]; //Assign value to leaves of segment tree for(int i = 0 ; i < n ; ++i) segtree[n + i] = arr[i]; //Assign value to internal node to compute minimum in a given range for(int i = n - 1; i >= 1; --i) { segtree[i] = Math.max(segtree[2 * i], segtree[2 * i + 1]); } return segtree; } public static int query(int segtree[] , int l , int r , int n) { if(l == r) return segtree[l + 1]; //Change indices to leaf node l += n; r += n; int ans = Integer.MIN_VALUE; while(l < r) { if((l & 1) == 1) ans = Math.max(ans , segtree[l++]); if((r & 1) == 1) ans = Math.max(ans , segtree[--r]); l /= 2; r /= 2; } return ans; } public static void solve() throws Exception { int n = f.ni(); int arr[] = new int[n]; int pos[] = new int[n + 1]; for (int i = 0; i < n; ++i) { arr[i] = f.ni(); pos[arr[i]] = i; } int seg[] = build(arr , n); int idx = n - 1 , r = n - 1; ArrayList<Integer> ans = new ArrayList<>(); HashSet<Integer> h = new HashSet<>(); while (idx >= 0) { int max = query(seg , 0 , idx + 1 , n); for (int j = pos[max]; j <= r; ++j) { if (!h.contains(arr[j])) { ans.add(arr[j]); } h.add(arr[j]); } idx = pos[max] - 1; r = idx; //pn("Mx = " + max + " Poss = " + pos[max] + " New idx = " + idx); } for (Integer x : ans) p(x + " "); pn(""); } public static void main(String[] args)throws Exception { if(memory) new Thread(null, new Runnable() {public void run(){try{new Main().run();}catch(Exception e){e.printStackTrace();System.exit(1);}}}, "", 1 << 28).start(); else new Main().run(); } /******************************END OF MAIN PROGRAM*******************************************/ void run()throws Exception { if (System.getProperty("ONLINE_JUDGE") == null) { f = new FastScanner(""); pw = new PrintWriter(System.out); } else { f = new FastScanner(); pw = new PrintWriter(System.out); //fw = new FileWriter("!out.txt"); } //pre(); int t = f.ni(); int tt = 1; while(t --> 0) { //fw.write("Case #" + (tt++) + ": "); //fw.write("\n"); solve(); } pw.flush(); pw.close(); //fw.close(); } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String str) throws Exception { try { br = new BufferedReader(new FileReader("!a.txt")); } catch (Exception e) { e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next()throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int ni() throws IOException {return Integer.parseInt(next());} public long nl() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public double nd() throws IOException {return Double.parseDouble(next());} } public static void pn(Object... o) {for(int i = 0; i < o.length; ++i) pw.print(o[i] + (i + 1 < o.length ? " ": "\n"));} public static void p(Object... o) {for(int i = 0; i < o.length; ++i) pw.print(o[i] + (i + 1 < o.length ? " " : ""));} public static void pni(Object... o) {for(Object obj : o) pw.print(oo + " "); pw.println(); pw.flush();} public static int gcd(int a,int b){if(b==0)return a;else{return gcd(b,a%b);}} public static long gcd(long a,long b){if(b==0l)return a;else{return gcd(b,a%b);}} public static long lcm(long a,long b){return (a*b/gcd(a,b));} public static long pow(long a,long b){long res=1;while(b>0){if((b&1)==1)res=res*a;b>>=1;a=a*a;}return res;} public static int pow(int a,int b){int res=1;while(b>0){if((b&1)==1)res=res*a;b>>=1;a=a*a;}return res;} public static long mpow(long a,long b, long m){long res=1;while(b>0){if((b&1)==1)res=((res%m)*(a%m))%m;b>>=1;a=((a%m)*(a%m))%m;}return res;} public static long mul(long a , long b , long mod){return ((a%mod)*(b%mod)%mod);} public static long adp(long a , long b){return ((a%mod)+(b%mod)%mod);} public static int dig(long a){int cnt=0;while(a>0){a/=10;++cnt;}return Math.max(1,cnt);} public static int dig(int a){int cnt=0;while(a>0){a/=10;++cnt;}return Math.max(1,cnt);} public static boolean isPrime(long n){if(n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(long i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;} public static boolean isPrime(int n){if(n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(int i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;} public static HashSet<Long> factors(long n){HashSet<Long> hs=new HashSet<Long>();for(long i=1;i<=(long)Math.sqrt(n);i++){if(n%i==0){hs.add(i);hs.add(n/i);}}return hs;} public static HashSet<Integer> factors(int n){HashSet<Integer> hs=new HashSet<Integer>();for(int i=1;i<=(int)Math.sqrt(n);i++){if(n%i==0){hs.add(i);hs.add(n/i);}}return hs;} public static HashSet<Long> pf(long n){HashSet<Long> ff=factors(n);HashSet<Long> ans=new HashSet<Long>();for(Long i:ff)if(isPrime(i))ans.add(i);return ans;} public static HashSet<Integer> pf(int n){HashSet<Integer> ff=factors(n);HashSet<Integer> ans=new HashSet<Integer>();for(Integer i:ff)if(isPrime(i))ans.add(i);return ans;} public static int gnv(char c){return Character.getNumericValue(c);} public static void sort(int[] a){ArrayList<Integer> l=new ArrayList<>();for(int i:a)l.add(i);Collections.sort(l);for(int i=0;i<a.length;++i)a[i]=l.get(i);} public static void sort(long[] a){ArrayList<Long> l=new ArrayList<>();for(long i:a)l.add(i);Collections.sort(l);for(int i=0;i<a.length;++i)a[i]=l.get(i);} public static void sort(ArrayList<Integer> a){Collections.sort(a);} }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
c56ef64f84212f8a1b39c2d3391f1f69
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.awt.*; import java.io.*; import java.lang.reflect.Array; import java.util.*; import java.util.List; import java.util.concurrent.LinkedBlockingQueue; public class Coding { private static BufferedReader bi = new BufferedReader(new InputStreamReader(System.in)); private static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out)); private static int[] po = new int[20]; public static void main(String[] args) { try { run(); } catch (Exception e) { e.printStackTrace(); } } public static void run() throws Exception { InputModule inp = new InputModule(); OutputModule out = new OutputModule(); int t = inp.cinInt(); while (t > 0) { int n = inp.cinInt(); int[] ar = inp.cinIntArray(n); int[] ans = new int[n]; int cur = 0; int[] vis = new int[n+1]; for(int i=1;i<=n;++i) { vis[i] = 0; } int end = n-1; int maxVal = n; while (true) { while ((maxVal>=1) && (vis[maxVal]==1)) { maxVal--; } if (maxVal == 0) break; int mIndex = end; while (ar[mIndex]!=maxVal) { mIndex--; } for(int i=mIndex;i<=end;++i) { ans[cur++] = ar[i]; vis[ar[i]] = 1; } end = mIndex-1; } out.printIntArray(ans); t--; } } private static class InputModule { private int cinInt() throws Exception { return Integer.parseInt(bi.readLine().split(" ")[0].trim()); } private long cinLong() throws Exception { return Long.parseLong(bi.readLine().split(" ")[0].trim()); } private Double cinDouble() throws Exception { return Double.parseDouble(bi.readLine().split(" ")[0].trim()); } private String cinString() throws Exception { return bi.readLine(); } private int[] cinIntArray(int n) throws Exception { String input = bi.readLine(); String[] values = input.split(" "); int[] ar = new int[n]; for (int i = 0; i < n; ++i) { ar[i] = Integer.parseInt(values[i]); } return ar; } private int[] cinIntArray() throws Exception { String input = bi.readLine(); String[] values = input.split(" "); int[] ar = new int[values.length]; for (int i = 0; i < values.length; ++i) { ar[i] = Integer.parseInt(values[i]); } return ar; } private long[] cinLongArray(int n) throws Exception { String input = bi.readLine(); String[] values = input.split(" "); long[] ar = new long[n]; for (int i = 0; i < n; ++i) { ar[i] = Long.parseLong(values[i]); } return ar; } private String[] cinStringArray(int n) throws Exception { return bi.readLine().split(" "); } } private static class OutputModule { private void printInt(int ans) throws Exception { writer.append(ans + "\n"); writer.flush(); } private void printLong(long ans) throws Exception { writer.append(ans + "\n"); writer.flush(); } private void printDouble(Double ans) throws Exception { writer.append(String.format("%.10f", ans)); writer.append("\n"); writer.flush(); } private void printString(String ans) throws Exception { writer.append(ans + "\n"); writer.flush(); } private void printIntArray(int[] ans) throws Exception { for (int i = 0; i < ans.length; ++i) { writer.append(ans[i] + " "); } writer.append("\n"); writer.flush(); } private void printLongArray(long[] ans) throws Exception { for (int i = 0; i < ans.length; ++i) { writer.append(ans[i] + " "); } writer.append("\n"); writer.flush(); } private void printIntList(List<Integer> list) throws Exception { for (int i = 0; i < list.size(); ++i) { writer.append(list.get(i) + " "); } writer.append("\n"); writer.flush(); } private void printLongList(List<Long> list) throws Exception { for (int i = 0; i < list.size(); ++i) { writer.append(list.get(i) + " "); } writer.append("\n"); writer.flush(); } private void printIntMatrix(int[][] mat, int n, int m) throws Exception { for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { writer.append(mat[i][j] + " "); } writer.append("\n"); } writer.flush(); } private void printLongMatrix(long[][] mat, int n, int m) throws Exception { for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { writer.append(mat[i][j] + " "); } writer.append("\n"); } writer.flush(); } private void printPoint(Point p) throws Exception { writer.append(p.x + " " + p.y + "\n"); writer.flush(); } private void printPoints(List<Point> p) throws Exception { for (Point pp : p) { writer.append(pp.x + " " + pp.y + "\n"); } writer.flush(); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
3716abe3b98d54ba545bc8fc0c19fb08
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.*; import java.util.*; import java.lang.*; 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); solve(in, out); out.close(); } static int L,R,top,bottom; static int cnt,edge; public static void solve(InputReader sc, PrintWriter pw) { int t=sc.nextInt(); // int t=1; u:while(t-->0){ int n=sc.nextInt(); int []arr=new int[n]; feedArr(arr,sc); List<Integer> list=new ArrayList<>(); for(int i=0;i<n;i++) list.add(i); Collections.sort(list,(a,b)->arr[b]-arr[a]); int i=0; HashSet<Integer>hset=new HashSet<>(); int k=0,v; int []ans=new int [n]; while(i<n){ if(hset.contains(list.get(i))){ i++; continue; } v=list.get(i); while(v<n&&!hset.contains(v)){ ans[k++]=arr[v]; hset.add(v++); } i++; } for(int j:ans) pw.print(j+" "); pw.println(); } } public static long find_val(long n){ char[] chrr=(""+n).toCharArray(); Arrays.sort(chrr); return Long.parseLong(new StringBuilder(new String(chrr)).reverse().toString())-Long.parseLong(new String(chrr)); } public static int ask(int l, int r, InputReader sc){ System.out.println("? "+l+" "+r); System.out.flush(); return sc.nextInt(); } public static void sort(long []arr){ ArrayList<Long> list=new ArrayList<>(); for(int i=0;i<arr.length;i++) list.add(arr[i]); Collections.sort(list); for(int i=0;i<arr.length;i++) arr[i]=list.get(i); } public static void swap(char []chrr, int i, int j){ char temp=chrr[i]; chrr[i]=chrr[j]; chrr[j]=temp; } public static int num(int n){ int a=0; while(n>0){ a+=(n&1); n>>=1; } return a; } static class Pair{ int a, b; Pair(int a,int b){ this.a=a; this.b=b; } //* } /*/ public int compareTo(Pair p){ return (b-p.b); } public int hashCode(){ int hashcode = (a+" "+b).hashCode(); return hashcode; } public boolean equals(Object obj){ if (obj instanceof Pair) { Pair p = (Pair) obj; return (p.a==this.a && p.b == this.b); } return false; } } //*/ static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int gcd(int a, int b) { if (b == 0) return a; return a>b?gcd(b, a % b):gcd(a, b % a); } static long fast_pow(long base,long n,long M){ if(n==0) return 1; if(n==1) return base; long halfn=fast_pow(base,n/2,M); if(n%2==0) return ( halfn * halfn ) % M; else return ( ( ( halfn * halfn ) % M ) * base ) % M; } static long modInverse(long n,long M){ return fast_pow(n,M-2,M); } public static void feedArr(long []arr,InputReader sc){ for(int i=0;i<arr.length;i++) arr[i]=sc.nextLong(); } public static void feedArr(double []arr,InputReader sc){ for(int i=0;i<arr.length;i++) arr[i]=sc.nextDouble(); } public static void feedArr(int []arr,InputReader sc){ for(int i=0;i<arr.length;i++) arr[i]=sc.nextInt(); } public static void feedArr(String []arr,InputReader sc){ for(int i=0;i<arr.length;i++) arr[i]=sc.next(); } public static String printArr(int []arr){ StringBuilder sbr=new StringBuilder(); for(int i:arr) sbr.append(i+" "); return sbr.toString(); } public static String printArr(long []arr){ StringBuilder sbr=new StringBuilder(); for(long i:arr) sbr.append(i+" "); return sbr.toString(); } public static String printArr(String []arr){ StringBuilder sbr=new StringBuilder(); for(String i:arr) sbr.append(i+" "); return sbr.toString(); } public static String printArr(double []arr){ StringBuilder sbr=new StringBuilder(); for(double i:arr) sbr.append(i+" "); return sbr.toString(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
b5b41a48fc1bfdac34837e87573a300e
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.*; import java.util.*; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { new Thread(null, () -> new Main().run(), "1", 1 << 23).start(); } private void run() { FastReader scan = new FastReader(); PrintWriter out = new PrintWriter(System.out); Solution solve = new Solution(); int t = scan.nextInt(); // int t = 1; for (int qq = 0; qq < t; qq++) { solve.solve(scan, out); out.println(); } out.close(); } } class Solution { /* * think and coding */ static long MOD = (long) (1e9); double EPS = 0.000_0001; public void solve(FastReader scan, PrintWriter out) { int n = scan.nextInt(); int[] arr = new int[n]; ArrayList<Integer> ans = new ArrayList<>(); TreeMap<Integer, Integer> map = new TreeMap<>(); for (int i = 0; i < n; i++) { arr[i] = scan.nextInt(); map.put(arr[i], i); } while (!map.isEmpty()) { int index = map.lastEntry().getValue(); for (int i = index; i < n; i++) { out.print(arr[i] + " "); map.remove(arr[i]); } n = index; } } void swap(int i, int j, int[] arr) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } boolean check(int[] a, int[] b) { for (int i = 0; i < a.length; i++) { if (a[i] != b[i]) { return false; } } return true; } static class Pair implements Comparable<Pair> { int a, b; public Pair(int a, int b) { this.a = a; this.b = b; } public Pair(Pair p) { this.a = p.a; this.b = p.b; } @Override public int compareTo(Pair p) { return Integer.compare(a, p.a); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; return a == pair.a && b == pair.b; } @Override public int hashCode() { return Objects.hash(a, b); } @Override public String toString() { return "Pair{" + "a=" + a + ", b=" + b + '}'; } } } class FastReader { private final BufferedReader br; private StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] initInt(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } long[] initLong(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
10d16625977bf3063b75bf71fd459d9a
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
// OM NAMAH SHIVAY // 22 days remaining(2609) import java.math.BigInteger; import java.util.*; import java.io.*; public class Vaibhav { static boolean prime[]; static class Pair implements Comparable<Pair> {int a;int b;Pair(int a, int b) {this.a = a;this.b = b;}public boolean equals(Object o) {if (o instanceof Pair) {Pair p = (Pair) o;return p.a == a && p.b == b;}return false;}public int hashCode() {int hash = 5;hash = 17 * hash + this.a;return hash;}public int compareTo(Pair o){if(this.a!=o.a)return this.a-o.a;else return this.b-o.b;}} static long power(long x, long y, long p) {if (y == 0) return 1;if (x == 0) return 0;long res = 1l;x = x % p;while (y > 0) {if (y % 2 == 1) res = (res * x) % p;y = y >> 1;x = (x * x) % p;}return res;} static void sort(long[] a) {ArrayList<Long> l = new ArrayList<>();for (long i : a) l.add(i);Collections.sort(l);for (int i = 0; i < a.length; i++) a[i] = l.get(i);} static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer("");String next() {while (!st.hasMoreTokens()) try {st = new StringTokenizer(br.readLine());} catch (IOException e) {e.printStackTrace();}return st.nextToken();}int nextInt() {return Integer.parseInt(next());}double nextDouble() {return Double.parseDouble(next());}int[] readArray(int n) {int[] a = new int[n];for (int i = 0; i < n; i++) a[i] = nextInt();return a;}long[] readlongArray(int n) {long[] a = new long[n];for (int i = 0; i < n; i++) a[i] = nextLong();return a;}long nextLong() {return Long.parseLong(next());}} static void sieveOfEratosthenes(int n) {prime = new boolean[n + 1];for (int i = 0; i <= n; i++) prime[i] = true;for (int p = 2; p * p <= n; p++) {if (prime[p] == true) {for (int i = p * p; i <= n; i += p) prime[i] = false;}}} public static int log2(int x) { return (int) (Math.log(x) / Math.log(2)); } static long binomialCoeff(long n, long r) {if (r > n) return 0l;long m = 998244353l;long inv[] = new long[(int) r + 1];inv[0] = 1;if (r + 1 >= 2)inv[1] = 1;for (int i = 2; i <= r; i++) {inv[i] = m - (m / i) * inv[(int) (m % i)] % m;}long ans = 1l;for (int i = 2; i <= r; i++) {ans = (int) (((ans % m) * (inv[i] % m)) % m);}for (int i = (int) n; i >= (n - r + 1); i--) {ans = (int) (((ans % m) * (i % m)) % m);}return ans;} public static long gcd(long a, long b) {if (b == 0) {return a;}return gcd(b, a % b);} static BigInteger bi(String str) { return new BigInteger(str); } // Author - vaibhav_1710 static FastScanner fs = null; static long mod = 1_000_000_007; static int[][] dirs = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}, }; static int k; static int ans[]; static String s; static int prev; static ArrayList<Integer> al[]; static long m = 998244353l; static int j; public static void main(String[] args) { fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = fs.nextInt(); outer: while (t-- > 0) { int n = fs.nextInt(); int a[] = fs.readArray(n); int pre[] = new int[n]; pre[0] = a[0]; prev=n; for(int i=1;i<n;i++){ pre[i] = Math.max(a[i],pre[i-1]); } j=0; ans = new int[n]; for(int i=n-1;i>=0;i--){ if(pre[i]==a[i]){ fill(i,a); } } for(int v:ans){ out.print(v+" "); } out.println(); } out.close(); } public static void fill(int i,int a[]){ int tmp=i; while(i<prev) { ans[j] = a[i]; j++; i++; } prev = tmp; } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
3a64c05d2b78bc97faa4b6ecbe6beb85
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.util.Arrays; import java.util.StringTokenizer; import java.util.*; import static java.lang.System.out; import static java.lang.Math.*; public class pre322 { 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 MultiSet<K> { TreeMap<K, Integer> map; MultiSet() { map = new TreeMap<>(); } void add(K a) { map.put(a, map.getOrDefault(a, 0) + 1); } boolean contains(K a) { return map.containsKey(a); } void remove(K a) { map.put(a, map.get(a) - 1); if (map.get(a) == 0) map.remove(a); } int occrence(K a) { return map.get(a); } K floor(K a) { return map.floorKey(a); } K ceil(K a) { return map.ceilingKey(a); } @Override public String toString() { ArrayList<K> set = new ArrayList<>(); for (Map.Entry<K, Integer> i : map.entrySet()) { for (int j = 1; j <= i.getValue(); j++) set.add(i.getKey()); } return set.toString(); } } static class Pair<K, V> { K value1; V value2; Pair(K a, V b) { value1 = a; value2 = b; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Pair)) return false; Pair<?, ?> p = (Pair<?, ?>) o; return Objects.equals(this.value1, p.value1) && Objects.equals(this.value2, p.value2); } @Override public int hashCode() { int result = this.value1.hashCode(); result = 31 * result + this.value2.hashCode(); return result; } @Override public String toString() { return ("[" + value1 + " <=> " + value2 + "]"); } } static ArrayList<Integer> primes; static void setPrimes(int n) { boolean p[] = new boolean[n]; Arrays.fill(p, true); for (int i = 2; i < p.length; i++) { if (p[i]) { primes.add(i); for (int j = i * 2; j < p.length; j += i) p[j] = false; } } } static int mod = (int) (1e9 + 7); static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } public static void main(String args[]) { FastReader obj = new FastReader(); int tc = obj.nextInt(); while(tc--!=0){ int n = obj.nextInt(),arr[] = new int[n]; for(int i=0;i<n;i++) arr[i] = obj.nextInt(); TreeMap<Integer,Integer> set = new TreeMap<>(); for(int i=0;i<n;i++) set.put(arr[i],i); boolean flag[] = new boolean[n]; Arrays.fill(flag,true); while(set.size()>0){ int idx = set.get(set.lastKey()); for(int j=idx;j<n && flag[j];j++){ out.print(arr[j]+" "); flag[j] = false; set.remove(arr[j]); } } out.println(); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
cebab70c81a8395f55eba1d8f87474f0
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.util.stream.LongStream; import java.io.*; import java.lang.reflect.Array; import java.math.*; public class Main{ public static void main(String[] args) { FastReader fr = new FastReader(); PrintWriter out = new PrintWriter(System.out); Scanner sc= new Scanner (System.in); //Code From Here---- StringBuilder answer = new StringBuilder(); int t =fr.nextInt(); for (int tt = 1; tt <=t; tt++) { int n = fr.nextInt(); int [] arr = fr.readArray(n); TreeSet <Integer> set = new TreeSet<>(); for (int i = 0; i < arr.length; i++) { set.add(arr[i]); } int end = n-1; for (int i = arr.length-1; i >=0; i--) { if(set.last()==arr[i]) { for (int j = i; j <=end; j++) { answer.append(arr[j]+" "); set.remove(arr[j]); } end = i-1; } } answer.append("\n"); } out.println(answer.toString()); out.flush(); sc.close(); } public static boolean isSorted(long [] arr ) { for (int i = 1; i < arr.length; i++) { if(arr[i]<arr[i-1]) return false; } return true; } public static TreeSet<Long> maxPrimeFactors(long n) { TreeSet <Long> set = new TreeSet<>(); long maxPrime = -1; while (n % 2 == 0) { maxPrime=2; n >>= 1; } while (n % 3 == 0) { maxPrime=3; n = n / 3; } for (long i = 5; i <= Math.sqrt(n); i += 6) { while (n % i == 0) { maxPrime=i; n = n / i; } while (n % (i + 2) == 0) { set.add((long)(i+2)); n = n / (i + 2); } } if (n > 4) { set.add((long)n); } return set; } public static int nSquaresFor(int n) { //Sum of Perfect Square Minimum ....... //Legendre's three-square theorem states that a natural number can be represented as the sum of three squares of integers //if and only if n is not of the form n = 4^a * (8b+7) int sqrt = (int)Math.sqrt(n); if(sqrt*sqrt==n) return 1; while ((n&3)==0) n>>=2; if((n&7)==7) return 4; int x = (int)Math.sqrt(n); for(int i =1;i<=x;i++) { int temp = n-(i*i); int tempsqrt = (int)Math.sqrt(temp); if(tempsqrt*tempsqrt == temp) return 2; } return 3; } public static BigInteger Factorial(BigInteger number){ if(number.compareTo(BigInteger.ONE)!=1) { return BigInteger.ONE; } return number.multiply(Factorial(number.subtract(BigInteger.ONE))); } private static boolean isPrime(int count) { if(count==1) return false; if(count==2 || count ==3) return true; if(count%2 == 0) return false; if(count%3 ==0) return false; for (int i = 5; i*i<=count; i+=6) { if(count%(i)==0 || count%(i+2)==0) return false;} return true; } static ArrayList<Integer> sieveOfEratosthenes(int n) { // Create a boolean array // "prime[0..n]" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } ArrayList <Integer> list = new ArrayList<>(); // Print all prime numbers for (int i = 2; i <= n; i++) { if (prime[i] == true) list.add(i); } return list; } //This RadixSort() is for long method public static long[] radixSort(long[] f){ return radixSort(f, f.length); } public static long[] radixSort(long[] f, int n) { long[] to = new long[n]; { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]&0xffff)]++] = f[i]; long[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>16&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>16&0xffff)]++] = f[i]; long[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>32&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>32&0xffff)]++] = f[i]; long[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>48&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>48&0xffff)]++] = f[i]; long[] d = f; f = to; to = d; } return f; } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for(int v : a) { c0[(v&0xff)+1]++; c1[(v>>>8&0xff)+1]++; c2[(v>>>16&0xff)+1]++; c3[(v>>>24^0x80)+1]++; } for(int i = 0;i < 0xff;i++) { c0[i+1] += c0[i]; c1[i+1] += c1[i]; c2[i+1] += c2[i]; c3[i+1] += c3[i]; } int[] t = new int[n]; for(int v : a)t[c0[v&0xff]++] = v; for(int v : t)a[c1[v>>>8&0xff]++] = v; for(int v : a)t[c2[v>>>16&0xff]++] = v; for(int v : t)a[c3[v>>>24^0x80]++] = v; return a; } static int lowerBound(long a[], long x) { // x is the target value or key int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] >= x) r = m; else l = m; } return r; } static int upperBound(long a[], long x) {// x is the key or target value int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1L; if (a[m] <= x) l = m; else r = m; } return l + 1; } static int upperBound(long[] arr, int start, int end, long k) { int N = end; while (start < end) { int mid = start + (end - start) / 2; if (k >= arr[mid]) { start = mid + 1; } else { end = mid; } } if (start < N && arr[start] <= k) { start++; } return start; } static long lowerBound(long[] arr, int start, int end, long k) { int N = end; while (start < end) { int mid = start + (end - start) / 2; if (k <= arr[mid]) { end = mid; } else { start = mid + 1; } } if (start < N && arr[start] < k) { start++; } return start; } // H.C.F. // For Fast Factorial public static long factorialStreams( long n ) { return LongStream.rangeClosed( 1, n ) .reduce(1, ( long a, long b ) -> a * b); } // Binary Exponentiation public static long FastExp(long base, long exp) { long ans=1; long mod=998244353; while (exp>0) { if (exp%2==1) ans*=base; exp/=2; base*=base; base%=mod; ans%=mod; } return ans; } // H.C.F. public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } // H.C.F. by Binary Method(Eucladian Algorithm) private static int gcdBinary(int x, int y) { int shift; /* GCD(0, y) == y; GCD(x, 0) == x, GCD(0, 0) == 0 */ if (x == 0) return y; if (y == 0) return x; int gcdX = Math.abs(x); int gcdY = Math.abs(y); if (gcdX == 1 || gcdY == 1) return 1; /* Let shift := lg K, where K is the greatest power of 2 dividing both x and y. */ for (shift = 0; ((gcdX | gcdY) & 1) == 0; ++shift) { gcdX >>= 1; gcdY >>= 1; } while ((gcdX & 1) == 0) gcdX >>= 1; /* From here on, gcdX is always odd. */ do { /* Remove all factors of 2 in gcdY -- they are not common */ /* Note: gcdY is not zero, so while will terminate */ while ((gcdY & 1) == 0) /* Loop X */ gcdY >>= 1; /* * Now gcdX and gcdY are both odd. Swap if necessary so gcdX <= gcdY, * then set gcdY = gcdY - gcdX (which is even). For bignums, the * swapping is just pointer movement, and the subtraction * can be done in-place. */ if (gcdX > gcdY) { final int t = gcdY; gcdY = gcdX; gcdX = t; } // Swap gcdX and gcdY. gcdY = gcdY - gcdX; // Here gcdY >= gcdX. }while (gcdY != 0); /* Restore common factors of 2 */ return gcdX << shift; } // Check For Palindrome public static boolean isPalindrome(String s) { return s.equals( new StringBuilder(s).reverse().toString()); } // For Fast Input ---- 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()); } int[] readArray(int n) { int [] arr = new int [n]; for (int i = 0; i < arr.length; i++) { arr[i]=nextInt(); } return arr; } long[] readArray(long []arr) { for (int i = 0; i < arr.length; i++) { arr[i]=nextLong(); } return arr; } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
679d06ea5d429b49943cb212f10ac548
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.io.*; // cd C:\Users\Lenovo\Desktop\New //ArrayList<Integer> a=new ArrayList<>(); //List<Integer> lis=new ArrayList<>(); //StringBuilder ans = new StringBuilder(); //HashMap<Integer,Integer> map=new HashMap<>(); public class cf { static FastReader in=new FastReader(); static final Random random=new Random(); static int mod=1000000007; //static long dp[]=new long[200002]; static int dp[][]; static int d; static int res; public static void main(String args[]) throws IOException { FastReader sc=new FastReader(); //Scanner s=new Scanner(System.in); int tt=sc.nextInt(); //int tt=1; while(tt-->0){ int n=sc.nextInt(); int arr[]=new int[n]; int pos[]=new int[n+1]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); pos[arr[i]]=i; } StringBuilder ans=new StringBuilder(); int last=n; int st=0; for(int i=n;i>=1;i--){ st=pos[i]; if(last>st){ for(int j=st;j<last;j++){ ans.append(arr[j]+" "); } last=st; } } System.out.println(ans); } } static boolean prime(int n){ for(int i=2;i<=Math.sqrt(n);i++){ if(n%i==0){ return false; } } return true; } static void factorial(int n){ long ret = 1; while(n > 0){ ret = ret * n % mod; n--; } System.out.println(ret); } static long find_min(int a,int arr[]){ long ans=Integer.MAX_VALUE; for(int i=0;i<arr.length;i++){ if(Math.abs(a-arr[i])<ans){ ans=Math.abs(a-arr[i]); } } return ans; } static long find(ArrayList<Long> arr,long n){ int l=0; int r=arr.size(); while(l+1<r){ int mid=(l+r)/2; if(arr.get(mid)<n){ l=mid; } else{ r=mid; } } return arr.get(l); } static void factorial(long []fact){ for(int i=2;i<14;i++){ fact[i]=1l*(i+1)*fact[i-1]; } } static void rotate(int ans[]){ int last=ans[0]; for(int i=0;i<ans.length-1;i++){ ans[i]=ans[i+1]; } ans[ans.length-1]=last; } static int reduce(int n){ while(n>1){ if(n%2==1){ n--; n=n/2; } else{ if(n%4==0){ n=n/4; } else{ break; } } } return n; } static long count(long n,int p,HashSet<Long> set){ if(n<Math.pow(2,p)){ set.add(n); return count(2*n+1,p,set)+count(4*n,p,set)+1; } return 0; } static int countprimefactors(int n){ int ans=0; int z=(int)Math.sqrt(n); for(int i=2;i<=z;i++){ while(n%i==0){ ans++; n=n/i; } } if(n>1){ ans++; } return ans; } /*static int count(int arr[],int idx,long sum,int []dp){ if(idx>=arr.length){ return 0; } if(dp[idx]!=-1){ return dp[idx]; } if(arr[idx]<0){ return dp[idx]=Math.max(1+count(arr,idx+1,sum+arr[idx],dp),count(arr,idx+1,sum,dp)); } else{ return dp[idx]=1+count(arr,idx+1,sum+arr[idx],dp); } }*/ static String reverse(String s){ String ans=""; for(int i=s.length()-1;i>=0;i--){ ans+=s.charAt(i); } return ans; } static int find_max(int []check,int y){ int max=0; for(int i=y;i>=0;i--){ if(check[i]!=0){ max=i; break; } } return max; } static int msb(int x){ int ans=0; while(x!=0){ x=x/2; ans++; } return ans; } static void ruffleSort(int[] a) { int n=a.length; for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } /* static boolean checkprime(int n1) { if(n1%2==0||n1%3==0) return false; else { for(int i=5;i*i<=n1;i+=6) { if(n1%i==0||n1%(i+2)==0) return false; } return true; } } */ /* Iterator<Map.Entry<Integer, Integer>> iterator = map.entrySet().iterator(); while(iterator.hasNext()){ Map.Entry<Integer, Integer> entry = iterator.next(); int value = entry.getValue(); if(value==1){ iterator.remove(); } else{ entry.setValue(value-1); } } */ static class Pair implements Comparable { int a,b; public String toString() { return a+" " + b; } public Pair(int x , int y) { a=x;b=y; } @Override public int compareTo(Object o) { Pair p = (Pair)o; if((Math.abs(a)+Math.abs(b))!=(Math.abs(p.a)+Math.abs(p.b))){ return ((Math.abs(a)+Math.abs(b))-(Math.abs(p.a)+Math.abs(p.b))); } else{ return a-p.a; } /*if(p.a!=a){ return a-p.a;//in } else{ return b-p.b;// }*/ } } /* public static boolean checkAP(List<Integer> lis){ for(int i=1;i<lis.size()-1;i++){ if(2*lis.get(i)!=lis.get(i-1)+lis.get(i+1)){ return false; } } return true; } public static int minBS(int[]arr,int val){ int l=-1; int r=arr.length; while(r>l+1){ int mid=(l+r)/2; if(arr[mid]>=val){ r=mid; } else{ l=mid; } } return r; } public static int maxBS(int[]arr,int val){ int l=-1; int r=arr.length; while(r>l+1){ int mid=(l+r)/2; if(arr[mid]<=val){ l=mid; } else{ r=mid; } } return l; } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; }*/ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
673fd92d664854bfdc8d5a81388d85dc
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
// package faltu; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.Map.Entry; public class Main { static long LowerBound(long[] a2, long x) { // x is the target value or key int l=-1,r=a2.length; while(l+1<r) { int m=(l+r)>>>1; if(a2[m]>=x) r=m; else l=m; } return r; } static int UpperBound(int a[], int x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } public static long getClosest(long val1, long val2,long target) { if (target - val1 >= val2 - target) return val2; else return val1; } static void ruffleSort(long[] a) { int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { long oi=r.nextInt(n), temp=a[i]; a[i]=a[(int)oi]; a[(int)oi]=temp; } Arrays.sort(a); } static void ruffleSort(int[] a){ int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n), temp=a[i]; a[i]=a[oi]; a[oi]=temp; } Arrays.sort(a); } int ceilIndex(int input[], int T[], int end, int s){ int start = 0; int middle; int len = end; while(start <= end){ middle = (start + end)/2; if(middle < len && input[T[middle]] < s && s <= input[T[middle+1]]){ return middle+1; }else if(input[T[middle]] < s){ start = middle+1; }else{ end = middle-1; } } return -1; } public static int findIndex(long arr[], long t) { if (arr == null) { return -1; } int len = arr.length; int i = 0; while (i < len) { if (arr[i] == t) { return i; } else { i = i + 1; } } return -1; } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } public static int[] swap(int a[], int left, int right) { int temp = a[left]; a[left] = a[right]; a[right] = temp; return a; } public static void swap(long x,long max1) { long temp=x; x=max1; max1=temp; } public static int[] reverse(int a[], int left, int right) { // Reverse the sub-array while (left < right) { int temp = a[left]; a[left++] = a[right]; a[right--] = temp; } return a; } static int lowerLimitBinarySearch(ArrayList<Integer> A,int B) { int n =A.size(); int first = 0,second = n; while(first <second) { int mid = first + (second-first)/2; if(A.get(mid) > B) { second = mid; }else { first = mid+1; } } if(first < n && A.get(first) < B) { first++; } return first; //1 index } 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; } } // *******----segement tree implement---***** // -------------START-------------------------- void buildTree (int[] arr,int[] tree,int start,int end,int treeNode) { if(start==end) { tree[treeNode]=arr[start]; return; } buildTree(arr,tree,start,end,2*treeNode); buildTree(arr,tree,start,end,2*treeNode+1); tree[treeNode]=tree[treeNode*2]+tree[2*treeNode+1]; } void updateTree(int[] arr,int[] tree,int start,int end,int treeNode,int idx,int value) { if(start==end) { arr[idx]=value; tree[treeNode]=value; return; } int mid=(start+end)/2; if(idx>mid) { updateTree(arr,tree,mid+1,end,2*treeNode+1,idx,value); } else { updateTree(arr,tree,start,mid,2*treeNode,idx,value); } tree[treeNode]=tree[2*treeNode]+tree[2*treeNode+1]; } // disjoint set implementation --start static void makeSet(int n) { parent=new int[n]; rank=new int[n]; for(int i=0;i<n;i++) { parent[i]=i; rank[i]=0; } } static void union(int u,int v) { u=findpar(u); v=findpar(v); if(rank[u]<rank[v])parent[u]=v; else if(rank[v]<rank[u])parent[v]=u; else { parent[v]=u; rank[u]++; } } private static int findpar(int node) { if(node==parent[node])return node; return parent[node]=findpar(parent[node]); } static int parent[]; static int rank[]; // *************end static Long MOD=(long) (1e9+7); static boolean coprime(int a, long l){ return (gcd(a, l) == 1); } static long[][] dp; static long[]dpp; static ArrayList<Integer>arr; static boolean[] vis; static ArrayList<ArrayList<Integer>>adj; static int maxsum(int a[]) { int size = a.length; int max_so_far = Integer.MIN_VALUE, max_ending_here = 0; for (int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } public static void main(String[] args) throws IOException { // sieve(); FastReader s = new FastReader(); long tt = s.nextLong(); while(tt-->0) { int n=s.nextInt(); int[]a=new int[n]; for(int i=0;i<n;i++)a[i]=s.nextInt(); int[]max=new int[n]; int x=0; for(int i=0;i<n;i++) { x=Math.max(x,a[i]); max[i]=x; } int end=n-1; for(int i=end;i>=0;i--) { if(x==max[i])continue; else { x=max[i]; for(int k=i+1;k<=end;k++) { System.out.print(a[k]+" "); } end=i; } } for(int i=0;i<=end;i++)System.out.print(a[i]+" "); System.out.println(); } } static void DFSUtil(int v, boolean[] visited) { visited[v] = true; Iterator<Integer> it = adj.get(v).iterator(); while (it.hasNext()) { int n = it.next(); if (!visited[n]) DFSUtil(n, visited); } } static long DFS(int n) { boolean[] visited = new boolean[n+1]; long cnt=0; for (int i = 1; i <= n; i++) { if (!visited[i]) { DFSUtil(i, visited); cnt++; } } return cnt; } public static String revStr(String str){ String input = str; StringBuilder input1 = new StringBuilder(); input1.append(input); input1.reverse(); return input1.toString(); } public static String sortString(String inputString){ char tempArray[] = inputString.toCharArray(); Arrays.sort(tempArray); return new String(tempArray); } static long myPow(long n, long i){ if(i==0) return 1; if(i%2==0) return (myPow(n,i/2)%MOD * myPow(n,i/2)%MOD)%MOD; return (n%MOD* myPow(n,i-i)%MOD)%MOD; } static void palindromeSubStrs(String str) { HashSet<String>set=new HashSet<>(); char[]a =str.toCharArray(); int n=str.length(); int[][]dp=new int[n][n]; for(int g=0;g<n;g++){ for(int i=0,j=g;j<n;j++,i++){ if(!set.contains(str.substring(i,i+1))&&g==0) { dp[i][j]=1; set.add(str.substring(i,i+1)); } else { if(!set.contains(str.substring(i,j+1))&&isPalindrome(str,i,j)) { dp[i][j]=1; set.add(str.substring(i,j+1)); } } } } int ans=0; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { System.out.print(dp[i][j]+" "); if(dp[i][j]==1)ans++; } System.out.println(); } System.out.println(ans); } static boolean isPalindrome(String str,int i,int j) { while (i < j) { if (str.charAt(i) != str.charAt(j)) return false; i++; j--; } return true; } static boolean sign(long num) { return num>0; } static boolean isSquare(long x){ if(x==1)return true; long y=(long) Math.sqrt(x); return y*y==x; } static long power1(long a,long b) { if(b == 0){ return 1; } long ans = power(a,b/2); ans *= ans; if(b % 2!=0){ ans *= a; } return ans; } static void swap(StringBuilder sb,int l,int r) { char temp = sb.charAt(l); sb.setCharAt(l,sb.charAt(r)); sb.setCharAt(r,temp); } // function to reverse the string between index l and r static void reverse(StringBuilder sb,int l,int r) { while(l < r) { swap(sb,l,r); l++; r--; } } // function to search a character lying between index l and r // which is closest greater (just greater) than val // and return it's index static int binarySearch(StringBuilder sb,int l,int r,char val) { int index = -1; while (l <= r) { int mid = (l+r)/2; if (sb.charAt(mid) <= val) { r = mid - 1; } else { l = mid + 1; if (index == -1 || sb.charAt(index) >= sb.charAt(mid)) index = mid; } } return index; } // this function generates next permutation (if there exists any such permutation) from the given string // and returns True // Else returns false static boolean nextPermutation(StringBuilder sb) { int len = sb.length(); int i = len-2; while (i >= 0 && sb.charAt(i) >= sb.charAt(i+1)) i--; if (i < 0) return false; else { int index = binarySearch(sb,i+1,len-1,sb.charAt(i)); swap(sb,i,index); reverse(sb,i+1,len-1); return true; } } private static int lps(int m ,int n,String s1,String s2,int[][]mat) { for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) { if(s1.charAt(i-1)==s2.charAt(j-1))mat[i][j]=1+mat[i-1][j-1]; else mat[i][j]=Math.max(mat[i-1][j],mat[i][j-1]); } } return mat[m][n]; } static String lcs(String X, String Y, int m, int n) { int[][] L = new int[m+1][n+1]; // Following steps build L[m+1][n+1] in bottom up fashion. Note // that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] for (int i=0; i<=m; i++) { for (int j=0; j<=n; j++) { if (i == 0 || j == 0) L[i][j] = 0; else if (X.charAt(i-1) == Y.charAt(j-1)) L[i][j] = L[i-1][j-1] + 1; else L[i][j] = Math.max(L[i-1][j], L[i][j-1]); } } // Following code is used to print LCS int index = L[m][n]; int temp = index; // Create a character array to store the lcs string char[] lcs = new char[index+1]; lcs[index] = '\u0000'; // Set the terminating character // Start from the right-most-bottom-most corner and // one by one store characters in lcs[] int i = m; int j = n; while (i > 0 && j > 0) { // If current character in X[] and Y are same, then // current character is part of LCS if (X.charAt(i-1) == Y.charAt(j-1)) { // Put current character in result lcs[index-1] = X.charAt(i-1); // reduce values of i, j and index i--; j--; index--; } // If not same, then find the larger of two and // go in the direction of larger value else if (L[i-1][j] > L[i][j-1]) i--; else j--; } return String.valueOf(lcs); // Print the lcs // System.out.print("LCS of "+X+" and "+Y+" is "); // for(int k=0;k<=temp;k++) // System.out.print(lcs[k]); } static long lis(long[] aa2, int n) { long lis[] = new long[n]; int i, j; long max = 0; for (i = 0; i < n; i++) lis[i] = 1; for (i = 1; i < n; i++) for (j = 0; j < i; j++) if (aa2[i] >= aa2[j] && lis[i] <= lis[j] + 1) lis[i] = lis[j] + 1; for (i = 0; i < n; i++) if (max < lis[i]) max = lis[i]; return max; } 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 boolean issafe(int i, int j, int r,int c, char ch) { if (i < 0 || j < 0 || i >= r || j >= c|| ch!= '1')return false; else return true; } static long power(long a, long b) { a %=MOD; long out = 1; while (b > 0) { if((b&1)!=0)out = out * a % MOD; a = a * a % MOD; b >>= 1; a*=a; } return out; } static long[] sieve; public static void sieve() { int nnn=(int) 1e6+1; long nn=(int) 1e6; sieve=new long[(int) nnn]; int[] freq=new int[(int) nnn]; sieve[0]=0; sieve[1]=1; for(int i=2;i<=nn;i++) { sieve[i]=1; freq[i]=1; } for(int i=2;i*i<=nn;i++) { if(sieve[i]==1) { for(int j=i*i;j<=nn;j+=i) { if(sieve[j]==1) { sieve[j]=0; } } } } } } class decrease implements Comparator<Long> { // Used for sorting in ascending order of // roll number public int compare(long a, long b) { return (int) (b - a); } @Override public int compare(Long o1, Long o2) { // TODO Auto-generated method stub return (int) (o2-o1); } } class pair{ long x; long y; long c; public pair(long x,long y) { this.x=x; this.y=y; } public pair(long x,long y,long c) { this.x=x; this.y=y; this.c=c; } } class Pair { int idx; String str; public Pair(String str,int idx) { this.str=str; this.idx=idx; } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
bf60a7c423361acd18f22fefd521b17e
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; public class CardDeck { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int a[]=new int [n]; int poi[]=new int[n+1]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); poi[a[i]]=i; } int l=0,h=n; for(int i=n;i>0;i--) { l=poi[i]; if(h>l) { for(int j=l;j<h;j++) { System.out.print(a[j]+" "); } h=l; } } System.out.println(); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
9f042e002448d4d1a0e0c2c065bf38af
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Random; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.io.*; public class TestClass { BufferedReader br; StringTokenizer st; public TestClass(){ // constructor br = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str = ""; try{ str = br.readLine(); } catch (IOException e){ e.printStackTrace(); } return str; } static void sort(int[] arr){ int n = arr.length; Random rnd = new Random(); for(int i = 0; i < n; ++i){ int tmp = arr[i]; int randomPos = i + rnd.nextInt(n - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } Arrays.sort(arr); } static void sort(long[] arr){ int n = arr.length; Random rnd = new Random(); for(int i = 0; i < n; ++i){ long tmp = arr[i]; int randomPos = i + rnd.nextInt(n - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } Arrays.sort(arr); } static long gcd(long a, long b) { if(b == 0) { return a; } return gcd(b, a % b); } static boolean isSafe(int r, int c, int n, int m){ if(r >= 0 && r < n && c >= 0 && c < m){ return true; } return false; } static long mod = (long)1e9 + 7; static int dir[][] = {{1, 0}, {-1, 0}, {0 ,1}, {0, -1}}; static long fact[]; public static void main(String[] args) throws Exception{ TestClass in = new TestClass(); PrintWriter out = new PrintWriter(System.out); // 000...1000 -> 2^k (kth bit i.e. kth index 0 based) // how to find msb of n -> simply for(int i = 0; i < 32; i++) if(n & mask > 0) last_mask = mask---- mask = 1 << i int test = in.nextInt(); while(test-- > 0) { int n = in.nextInt(); int arr[] = new int[n]; for(int i = 0; i < n; i++) { arr[i] = in.nextInt(); } int max[] = new int[n]; int m = 0; for(int i = 0; i < n; i++) { m = Math.max(m, arr[i]); max[i] = m; } int l = n - 1; for(int i = n - 1; i >= 0; i--) { if(max[i] == m) { continue; } else { m = max[i]; for(int j = i + 1; j <= l; j++) { out.print(arr[j] + " "); } l = i; } } for(int i = 0; i <= l; i++) { out.print(arr[i] + " "); } out.println(); } out.flush(); out.close(); } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
55d2f0ac486da2abe1dc004db64ce242
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for (int i = 0; i < T; i++) { int n = in.nextInt(),index = n - 1; int[] res = new int[n]; LinkedList<Integer> list = new LinkedList<>(); for (int j = 0; j < n; j++) { int temp = in.nextInt(); if (!list.isEmpty() && list.peekFirst() > temp) { list.offer(temp); } else { while (!list.isEmpty()) { res[index--] = list.pollLast(); } list.offer(temp); } } while (!list.isEmpty()) { res[index--] = list.pollLast(); } String s = Arrays.toString(res); System.out.println(s.replace("[","").replace("]","").replace(",","")); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
4232213285157ed53b4a8e13dd7df34f
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.io.*; public class B { static StringBuilder sb; static long mod = (long) (1e9 + 7); static void solve(int n,int[]arr) { Stack<Integer> st = new Stack<>(); int[] pmax = new int[n]; pmax[0] = arr[0]; for(int i = 1;i < n;i++){ pmax[i] = Math.max(pmax[i - 1],arr[i]); } for(int i = n - 1;i >= 0;i--){ st.push(arr[i]); while(arr[i] == pmax[i] && st.size() != 0){ sb.append(st.pop() + " "); } } sb.append("\n"); } public static void main(String[] args) { sb = new StringBuilder(); int test = i(); while (test-- > 0) { int n = i(); int[] arr = readArray(n); solve(n, arr); } out.printLine(sb); out.flush(); out.close(); } private static double log(long a,long b){ double ans = Math.log10(a) / Math.log10(b); return ans; } 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 Int() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String String() { 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 String(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } static InputReader in = new InputReader(System.in); static OutputWriter out = new OutputWriter(System.out); public static long[] sortlong(long[] a2) { int n = a2.length; ArrayList<Long> l = new ArrayList<>(); for (long i : a2) l.add(i); Collections.sort(l); for (int i = 0; i < l.size(); i++) a2[i] = l.get(i); return a2; } public static int[] sortint(int[] a2) { int n = a2.length; ArrayList<Integer> l = new ArrayList<>(); for (int i : a2) l.add(i); Collections.sort(l); for (int i = 0; i < l.size(); i++) a2[i] = l.get(i); return a2; } public static int i() { return in.Int(); } public static long l() { String s = in.String(); return Long.parseLong(s); } public static String s() { return in.String(); } public static int[] readArray(int n) { int A[] = new int[n]; for (int i = 0; i < n; i++) { A[i] = i(); } return A; } public static long[] readLongArray(int n) { long A[] = new long[(int) n]; for (int i = 0; i < n; i++) { A[i] = l(); } return A; } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
b7c6dba2f3590455ab8ea152a210c761
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class B_Card_Deck { static Scanner scanner=new Scanner(); static int testCases,n; static long a[]; static long ans[]; static class position{ long element; int position; public position() { } public position(long element, int position) { this.element = element; this.position = position; } } static void merge(position a[],int left,int right,int mid){ int n1=mid-left+1,n2=right-mid; position []L=new position[n1]; position []R=new position[n2]; for(int i=0;i<n1;i++){ L[i]=new position(-1,-1); } for(int i=0;i<n2;i++){ R[i]=new position(-1,-1); } for(int i=0;i<n1;i++){ L[i].element=a[left+i].element; L[i].position=a[left+i].position; } for(int i=0;i<n2;i++){ R[i].element=a[mid+1+i].element; R[i].position=a[mid+1+i].position; } int i=0,j=0,k=left; while( i<n1 && j<n2 ){ if( L[i].element>=R[j].element ){ a[k].element=L[i].element; a[k].position=L[i].position; i++; }else{ a[k].element=R[j].element; a[k].position=R[j].position; j++; } k++; } while( i<n1 ){ a[k].element=L[i].element; a[k].position=L[i].position; k++; i++; } while( j<n2 ){ a[k].element=R[j].element; a[k].position=R[j].position; j++; k++; } } static void mergeSort(position a[],int left,int right){ if( left>=right ){ return; } int mid=(left+right)/2; mergeSort( a,left,mid ); mergeSort( a,mid+1,right ); merge( a,left,right,mid ); } static void solve(position p[]){ position b[]=new position[p.length]; //System.arraycopy(p, 0, b, 0, n); for(int i=0;i<n;i++){ b[i]=new position( p[i].element,p[i].position ); } mergeSort( b,0,b.length-1 ); /* for( position i: b ){ System.out.println(i.element+" "+i.position); } System.out.println("orginal: "); for( position i: p ){ System.out.println(i.element+" "+i.position); }*/ int in=0,x=0; // System.out.println("n before: "+n); while( n>=0 ){ if( b[in].position<n ){ for(int i=b[in].position;i<n;i++){ if( i>=0 ){ ans[x++]=a[i]; } } n=(n-( (n-1)-b[in].position+1 )); } // System.out.println("n: "+n); in++; if( in>=p.length ){ break; } } for(int i=0;i<ans.length-1;i++){ System.out.print(ans[i]+" "); } System.out.println(); } public static void main(String[] args) throws IOException { testCases=scanner.nextInt(); position p[]; for(int i=0;i<testCases;i++){ n=scanner.nextInt(); ans=new long[n+1]; a=new long[n]; p=new position[n]; for(int j=0;j<n;j++){ p[j]=new position(); } for(int j=0;j<n;j++){ a[j]=scanner.nextLong(); p[j]=new position(a[j],j); } solve(p); } } static class Scanner{ BufferedReader in; StringTokenizer st; public Scanner() { in=new BufferedReader( new InputStreamReader(System.in) ); } String next() throws IOException{ while( st==null || !st.hasMoreElements() ){ st=new StringTokenizer( in.readLine() ); } return st.nextToken(); } int nextInt() throws IOException{ return Integer.parseInt( next() ); } long nextLong() throws IOException{ return Long.parseLong( next() ); } String nextLine() throws IOException{ return in.readLine(); } } } /* 1 5 1 5 2 4 3 */ /* 1 6 4 2 5 3 6 1 */ /* 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 */
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
b4c48ef63fe4bbf77f4211a9399705b8
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; public class abhishek { public static int find(int arr[],int ele) { for(int i=0;i<arr.length;i++) { if(arr[i]==ele)return i; } return -1; } public static void reverse(int arr[],int i,int j) { while(i<=j) { int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; i++; j--; } } public static void print(int[]arr,int[]ans,int i) { while(true) { System.out.print(arr[i]+" "); i++; if(i>=ans.length ||arr[i]==ans[i])break; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++)arr[i]=sc.nextInt(); int ans[]=new int[n]; int max=arr[0]; for(int i=0;i<n;i++) { if(max<arr[i])max=arr[i]; ans[i]=max; } for(int i=n-1;i>=0;i--) { if(arr[i]==ans[i])print(arr,ans,i); } System.out.println(); }}}
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
e7b9bb0f41b46bea7f34fa99a55fa7bc
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.lang.reflect.Array; import java.text.DecimalFormat; import java.util.*; import java.lang.*; import java.io.*; public class cf2 { static PrintWriter out; static int MOD = 1000000007; static FastReader scan; /*-------- I/O using short named function ---------*/ public static String ns() { return scan.next(); } public static int ni() { return scan.nextInt(); } public static long nl() { return scan.nextLong(); } public static double nd() { return scan.nextDouble(); } public static String nln() { return scan.nextLine(); } public static void p(Object o) { out.print(o); } public static void ps(Object o) { out.print(o + " "); } public static void pn(Object o) { out.println(o); } /*-------- for output of an array ---------------------*/ static void iPA(int arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + " "); out.println(output); } static void lPA(long arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + " "); out.println(output); } static void sPA(String arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + " "); out.println(output); } static void dPA(double arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + " "); out.println(output); } /*-------------- for input in an array ---------------------*/ static void iIA(int arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = ni(); } static void lIA(long arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = nl(); } static void sIA(String arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = ns(); } static void dIA(double arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = nd(); } /*------------ for taking input faster ----------------*/ 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 ArrayList<Integer> sieveOfEratosthenes(int n) { // Create a boolean array // "prime[0..n]" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } ArrayList<Integer> arr = new ArrayList<>(); // store all prime numbers for (int i = 2; i <= n; i++) { if (prime[i] == true) arr.add(i); } return arr; } // Method to check if x is power of 2 static boolean isPowerOfTwo(int x) { return x != 0 && ((x & (x - 1)) == 0); } //Method to return lcm of two numbers static int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); } //Method to count digit of a number static int countDigit(long n) { String sex = Long.toString(n); return sex.length(); } static void reverse(int a[]) { int i, k, t; int n = a.length; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } static long nCr(long n, long r) { long p = 1, k = 1; // C(n, r) == C(n, n-r), // choosing the smaller value if (n - r < r) { r = n - r; } if (r != 0) { while (r > 0) { p *= n; k *= r; // gcd of p, k long m = gcdlong(p, k); // dividing by gcd, to simplify // product division by their gcd // saves from the overflow p /= m; k /= m; n--; r--; } // k should be simplified to 1 // as C(n, r) is a natural number // (denominator should be 1 ) . } else { p = 1; } // if our approach is correct p = ans and k =1 return p; } static long gcdlong(long n1, long n2) { long gcd = 1; for (int i = 1; i <= n1 && i <= n2; ++i) { // Checks if i is factor of both integers if (n1 % i == 0 && n2 % i == 0) { gcd = i; } } return gcd; } // Returns factorial of n static long fact(long n) { long res = 1; for (long i = 2; i <= n; i++) res = res * i; return res; } // Get all prime numbers till N using seive of Eratosthenes public static List<Integer> getPrimesTill(int n){ boolean[] arr = new boolean[n+1]; List<Integer> primes = new LinkedList<>(); for(int i=2; i<=n; i++){ if(!arr[i]){ primes.add(i); for(long j=(i*i); j<=n; j+=i){ arr[(int)j] = true; } } } return primes; } static final Random random = new Random(); //Method for sorting static void ruffleSort(int[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int j = random.nextInt(n); int temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static void sortadv(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long value : a) { l.add(value); } Collections.sort(l); for (int i = 0; i < l.size(); i++) a[i] = l.get(i); } //Method for checking if a number is prime or not static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static void main(String[] args) throws java.lang.Exception { OutputStream outputStream = System.out; out = new PrintWriter(outputStream); scan = new FastReader(); //for fast output sometimes StringBuilder sb1 = new StringBuilder(); // prefix sum, *dp , odd-even segregate , *brute force(n=100)(jo bola wo kar) , 2 pointer , pattern , see contraints // but first observe! maybe just watch i-o like ans-x-1,y for input x,y; // divide wale ques me always check whether divisor 1? || probability mei mostly (1-p)... // Deque<String> deque = new LinkedList<String>(); // in mod ques take everything as long. int t =ni(); while (t-- != 0) { int n=ni(); int a[]=new int[n]; iIA(a); int[] lmax=new int[n]; lmax[0]=a[0]; for(int i=1;i<n;i++){ lmax[i]=Math.max(lmax[i-1],a[i]); } Stack<Integer> st=new Stack<Integer>(); //st.push(a[n-1]); for(int i=n-1;i>=0;i--){ st.push(a[i]); if(a[i]==lmax[i]){ //ps(a[i]); while (st.size()>0) ps(st.pop()); } } pn(""); } out.flush(); out.close(); } static long binpow(long a,long b) { a %= MOD; long res = 1; while (b > 0) { if ((b & 1)==1) res = (res%MOD * a % MOD)%MOD; a = (a%MOD * a % MOD)%MOD; b >>= 1; } return res; } public static long findpow(int n,int i){ long ans=1; for(int in=1;in<=i;in++) ans=(ans%MOD*n%MOD)%MOD; return ans; } public static String rev(String s,int m){ char ch[]=new char[m]; int j=m-1; for(int i=0;i<m;i++){ ch[j--]=s.charAt(i); } return new String(ch); } public static boolean palin(String s,int i,int j){ while(i<=j){ if(s.charAt(i++)!=s.charAt(j--)) return false; } return true; } public static int modInverse(int a, int m) { int m0 = m; int y = 0, x = 1; if (m == 1) return 0; while (a > 1) { // q is quotient int q = a / m; int t = m; // m is remainder now, process // same as Euclid's algo m = a % m; a = t; t = y; // Update x and y y = x - q * y; x = t; } // Make x positive if (x < 0) x += m0; return x; } public static int countodd(int[] a){ int count=0; for(int i:a){ if(i%2==1) count++; } return count; } static int minOps(String s, int si, int ei, char ch) { if (si == ei) { return ch == s.charAt(si) ? 0 : 1; } int mid =( si+ei) >>1; int leftC = minOps(s, si, mid, (char) (ch + 1)); int rightC = minOps(s, mid + 1, ei, (char) (ch + 1)); for (int i = si; i <= mid; i++) { if (s.charAt(i) != ch) rightC++; } for (int i = mid + 1; i <= ei; i++) { if (s.charAt(i) != ch) leftC++; } return Math.min(leftC, rightC); } public static int sumofdigit(long n){ int sum=0; while(n!=0){ sum+=n%10; n/=10; } return sum; } public static int computeXOR(int n) { // If n is a multiple of 4 if (n % 4 == 0) return n; // If n%4 gives remainder 1 if (n % 4 == 1) return 1; // If n%4 gives remainder 2 if (n % 4 == 2) return n + 1; // If n%4 gives remainder 3 return 0; } public static class Pair implements Comparable<Pair>{ int a, b; public Pair(int x,int y){ a=x;b=y; } @Override public int compareTo(Pair o) { if(this.a!=o.a) return this.a-o.a; else return o.b-this.b; } } private static boolean checkCycle(int node, ArrayList<ArrayList<Integer>> adj, int vis[], int dfsVis[]) { vis[node] = 1; dfsVis[node] = 1; for(Integer it: adj.get(node)) { if(vis[it] == 0) { if(checkCycle(it, adj, vis, dfsVis) == true) { return true; } } else if(dfsVis[it] == 1) { return true; } } dfsVis[node] = 0; return false; } public static boolean isCyclic(int N, ArrayList<ArrayList<Integer>> adj) { int vis[] = new int[N]; int dfsVis[] = new int[N]; for(int i = 0;i<N;i++) { if(vis[i] == 0) { if(checkCycle(i, adj, vis, dfsVis) == true) return true; } } return false; } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
33b963db78f92f5a9e02232f7811a014
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import static java.lang.Math.*; import java.io.*; import java.math.*; import java.util.*; public class cf1 { static FastReader x = new FastReader(); static OutputStream outputStream = System.out; static PrintWriter out = new PrintWriter(outputStream); /*---------------------------------------CODE STARTS HERE-------------------------*/ public static void main(String[] args) { int t = x.nextInt(); StringBuilder str = new StringBuilder(); while (t > 0) { int n =x.nextInt(); int a[] = new int[n]; ArrayList<Integer> ar = new ArrayList<>(); int max=0; for(int i=0;i<n;i++) { a[i] = x.nextInt(); if(a[i]>max) { ar.add(i); max=a[i]; } } int j = ar.size()-1; for(;j>=0;j--) { if(j==ar.size()-1) { for(int i=ar.get(j);i<n;i++) { str.append(a[i]+" "); } }else { for(int i=ar.get(j);i<ar.get(j+1);i++) { str.append(a[i]+" "); } } } str.append("\n"); t--; } out.println(str); out.flush(); } /*--------------------------------------------FAST I/O--------------------------------*/ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } char nextchar() { char ch = ' '; try { ch = (char) br.read(); } catch (IOException e) { e.printStackTrace(); } return ch; } } /*--------------------------------------------BOILER PLATE---------------------------*/ static int[] readarr(int n) { int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = x.nextInt(); } return arr; } static int[] sortint(int a[]) { ArrayList<Integer> al = new ArrayList<>(); for (int i : a) { al.add(i); } Collections.sort(al); for (int i = 0; i < a.length; i++) { a[i] = al.get(i); } return a; } static long[] sortlong(long a[]) { ArrayList<Long> al = new ArrayList<>(); for (long i : a) { al.add(i); } Collections.sort(al); for (int i = 0; i < al.size(); i++) { a[i] = al.get(i); } return a; } static int pow(int x, int y) { int result = 1; while (y > 0) { if (y % 2 == 0) { x = x * x; y = y / 2; } else { result = result * x; y = y - 1; } } return result; } static int[] revsort(int a[]) { ArrayList<Integer> al = new ArrayList<>(); for (int i : a) { al.add(i); } Collections.sort(al, Comparator.reverseOrder()); for (int i = 0; i < a.length; i++) { a[i] = al.get(i); } return a; } static int[] gcd(int a, int b, int ar[]) { if (b == 0) { ar[0] = a; ar[1] = 1; ar[2] = 0; return ar; } ar = gcd(b, a % b, ar); int t = ar[1]; ar[1] = ar[2]; ar[2] = t - (a / b) * ar[2]; return ar; } static boolean[] esieve(int n) { boolean p[] = new boolean[n + 1]; Arrays.fill(p, true); for (int i = 2; i * i <= n; i++) { if (p[i] == true) { for (int j = i * i; j <= n; j += i) { p[j] = false; } } } return p; } static ArrayList<Integer> primes(int n) { boolean p[] = new boolean[n + 1]; ArrayList<Integer> al = new ArrayList<>(); Arrays.fill(p, true); int i = 0; for (i = 2; i * i <= n; i++) { if (p[i] == true) { al.add(i); for (int j = i * i; j <= n; j += i) { p[j] = false; } } } for (i = i; i <= n; i++) { if (p[i] == true) { al.add(i); } } return al; } static int etf(int n) { int res = n; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { res /= i; res *= (i - 1); while (n % i == 0) { n /= i; } } } if (n > 1) { res /= n; res *= (n - 1); } return res; } static int gcd(int a, int b) { if (a == 0) { return b; } return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) { return b; } return gcd(b % a, a); } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
4f97888edb09cf0be46edfe98ca2ebab
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.io.*; public class Main { static StringBuilder sb; static dsu dsu; static long fact[]; static int mod = (int) (1e9 + 7); static void solve() { int n=i(); int[] arr=new int[n]; PriorityQueue<Pair> pq=new PriorityQueue<>(); for(int i=0;i<n;i++){ arr[i]=i(); pq.add(new Pair(i,arr[i])); } HashSet<Integer>hs =new HashSet<>(); int e=n-1; while(pq.size()>0){ Pair p=pq.remove(); if(hs.contains(p.x)) continue; for(int i=p.x;i<=e;i++){ sb.append(arr[i]+" "); hs.add(i); } e=p.x-1; } sb.append("\n"); } public static void main(String[] args) { sb = new StringBuilder(); int test = i(); while (test-- > 0) { solve(); } System.out.println(sb); } /* * fact=new long[(int)1e6+10]; fact[0]=fact[1]=1; for(int i=2;i<fact.length;i++) * { fact[i]=((long)(i%mod)1L(long)(fact[i-1]%mod))%mod; } */ //**************NCR%P****************** static long ncr(int n, int r) { if (r > n) return (long) 0; long res = fact[n] % mod; // System.out.println(res); res = ((long) (res % mod) * (long) (p(fact[r], mod - 2) % mod)) % mod; res = ((long) (res % mod) * (long) (p(fact[n - r], mod - 2) % mod)) % mod; // System.out.println(res); return res; } static long p(long x, long y)// POWER FXN // { if (y == 0) return 1; long res = 1; while (y > 0) { if (y % 2 == 1) { res = (res * x) % mod; y--; } x = (x * x) % mod; y = y / 2; } return res; } //**************END****************** // *************Disjoint set // union*********// static class dsu { int parent[]; dsu(int n) { parent = new int[n]; for (int i = 0; i < n; i++) parent[i] = -1; } int find(int a) { if (parent[a] < 0) return a; else { int x = find(parent[a]); parent[a] = x; return x; } } void merge(int a, int b) { a = find(a); b = find(b); if (a == b) return; parent[b] = a; } } //**************PRIME FACTORIZE **********************************// static TreeMap<Integer, Integer> prime(long n) { TreeMap<Integer, Integer> h = new TreeMap<>(); long num = n; for (int i = 2; i <= Math.sqrt(num); i++) { if (n % i == 0) { int nt = 0; while (n % i == 0) { n = n / i; nt++; } h.put(i, nt); } } if (n != 1) h.put((int) n, 1); return h; } //****CLASS PAIR ************************************************ static class Pair implements Comparable<Pair> { int x; long y; Pair(int x, long y) { this.x = x; this.y = y; } public int compareTo(Pair o) { return (int) (o.y - this.y); } } //****CLASS PAIR ************************************************** 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 Int() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String String() { 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 String(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } static InputReader in = new InputReader(System.in); static OutputWriter out = new OutputWriter(System.out); public static long[] sort(long[] a2) { int n = a2.length; ArrayList<Long> l = new ArrayList<>(); for (long i : a2) l.add(i); Collections.sort(l); for (int i = 0; i < l.size(); i++) a2[i] = l.get(i); return a2; } public static char[] sort(char[] a2) { int n = a2.length; ArrayList<Character> l = new ArrayList<>(); for (char i : a2) l.add(i); Collections.sort(l); for (int i = 0; i < l.size(); i++) a2[i] = l.get(i); return a2; } public static long pow(long x, long y) { long res = 1; while (y > 0) { if (y % 2 != 0) { res = (res * x);// % modulus; y--; } x = (x * x);// % modulus; y = y / 2; } return res; } //GCD___+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public static long gcd(long x, long y) { if (x == 0) return y; else return gcd(y % x, x); } // ******LOWEST COMMON MULTIPLE // ********************************************* public static long lcm(long x, long y) { return (x * (y / gcd(x, y))); } //INPUT PATTERN******************************************************** public static int i() { return in.Int(); } public static long l() { String s = in.String(); return Long.parseLong(s); } public static String s() { return in.String(); } public static int[] readArrayi(int n) { int A[] = new int[n]; for (int i = 0; i < n; i++) { A[i] = i(); } return A; } public static long[] readArray(long n) { long A[] = new long[(int) n]; for (int i = 0; i < n; i++) { A[i] = l(); } return A; } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
bb00de46beab49f78adcbec5c7512055
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import javax.sound.midi.SysexMessage; import java.io.*; import java.util.*; public class Code { 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(); } } static Reader sc = new Reader(); static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String args[]) throws IOException { Scanner sc = new Scanner(System.in); int t = inputInt(); //int t = sc.nextInt(); while(t-->0){ int n = inputInt(); int[] a = new int[n]; for(int i= 0;i < n;i++){ a[i] = inputInt(); } Stack<Integer> stack =new Stack<>(); int[] pge = new int[n]; for(int i = 0;i < n;i++){ if(stack.isEmpty()){ pge[i] = -1; } else if(stack.peek() > a[i]){ pge[i] = stack.peek(); } else{ while (!stack.isEmpty() && stack.peek() <= a[i]) stack.pop(); if(stack.isEmpty()) pge[i] = -1; else pge[i] = stack.peek(); } stack.add(a[i]); } //println(Arrays.toString(pge)); stack.clear(); for(int i = n-1;i >= 0;i--){ if(pge[i] != -1){ stack.add(a[i]); } else{ printSp(a[i]+""); while (!stack.isEmpty()) { printSp(stack.pop() + ""); } } } println(""); } bw.flush(); bw.close(); } public static boolean isSquare(int x){ int sq = (int)Math.sqrt(x); return sq * sq == x; } public static int inputInt() throws IOException { return sc.nextInt(); } public static long inputLong() throws IOException { return sc.nextLong(); } public static double inputDouble() throws IOException { return sc.nextDouble(); } public static String inputString() throws IOException { return sc.readLine(); } public static void print(String a) throws IOException { bw.write(a); } public static void printSp(String a) throws IOException { bw.write(a + " "); } public static void println(String a) throws IOException { bw.write(a + "\n"); } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 8
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
0ce4b77f0a50891cfa37e9f16492c5ff
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.LinkedList; import java.util.*; public class LInkedList { public static void main(String[] args) { Scanner scan=new Scanner(System.in); int t=scan.nextInt(); while(t-->0) { int size=scan.nextInt(); int [] arr=new int [size]; int []ind=new int[size+1]; for(int i=0;i<size;i++) { arr[i]=scan.nextInt(); ind[arr[i]]=i; } int n=size; while(n>0) { if(ind[size]<n) { for(int i=ind[size];i<n;i++) { System.out.print(arr[i]+" "); }n=ind[size]; } size--; } System.out.println(); } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 11
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
5f3baad4efc706e12b57d744f9e7316a
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; public class A { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); TreeSet<Integer> ts = new TreeSet<>(); for (int i = 1; i <= n; i++) ts.add(i); int ans[] = new int[n]; ArrayList<Integer> temp; int i = n - 1, k = 0; while (i >= 0) { temp = new ArrayList<>(); while (i >= 0 && a[i] != ts.last()) { temp.add(a[i]); ts.remove(a[i]); i--; } temp.add(a[i]); i--; ts.pollLast(); Collections.reverse(temp); for (int j : temp) { ans[k] = j; k++; } } for (int j : ans) System.out.print(j+" "); System.out.println(" "); } sc.close(); } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 11
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
faa589c36d1c57cbdcdad79bbade2f38
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.io.*; import java.math.*; import java.awt.geom.*; import static java.lang.Math.*; public class Solution implements Runnable { long mod1 = (long) 1e9 + 7; int mod2 = 998244353; public void solve() throws Exception { int t = sc.nextInt(); while (t-- > 0) { int n=sc.nextInt(); int arr[]=sc.readArray(n); int max=n; TreeSet<Integer> set=new TreeSet<>((a,b)->(b-a)); for(int i:arr) set.add(i); Stack<Integer> s=new Stack<>(); for(int i=n-1;i>=0;i--) { if(arr[i]!=max) { s.push(arr[i]); set.remove(arr[i]); } else { s.push(arr[i]); set.remove(arr[i]); while(!s.isEmpty()) { out.print(s.pop()+" "); } if(set.isEmpty()) break; max=set.first(); } } out.println(); } } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static long ncr(int n, int r, long p) { if (r > n) return 0l; if (r > n - r) r = n - r; long C[] = new long[r + 1]; C[0] = 1; for (int i = 1; i <= n; i++) { for (int j = Math.min(i, r); j > 0; j--) C[j] = (C[j] + C[j - 1]) % p; } return C[r] % p; } void sieveOfEratosthenes(boolean prime[], int size) { for (int i = 0; i < size; i++) prime[i] = true; for (int p = 2; p * p < size; p++) { if (prime[p] == true) { for (int i = p * p; i < size; i += p) prime[i] = false; } } } static int LowerBound(int a[], int x) { // smallest index having value >= x int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] >= x) r = m; else l = m; } return r; } static int UpperBound(int a[], int x) {// biggest index having value <= x int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] <= x) l = m; else r = m; } return l + 1; } public long power(long x, long y, long p) { long res = 1; // out.println(x+" "+y); x = x % p; if (x == 0) return 0; while (y > 0) { if ((y & 1) == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static Throwable uncaught; BufferedReader in; FastScanner sc; PrintWriter out; @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new FastScanner(in); solve(); } catch (Throwable uncaught) { Solution.uncaught = uncaught; } finally { out.close(); } } public static void main(String[] args) throws Throwable { Thread thread = new Thread(null, new Solution(), "", (1 << 26)); thread.start(); thread.join(); if (Solution.uncaught != null) { throw Solution.uncaught; } } } class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public int[] readArray(int n) throws Exception { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 11
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
6a0a35615b07a875f7f3d30b5c5411b4
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
//package credit; import java.io.*; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main{ static boolean v[]; static int ans[]; int size[]; static int count=0; static int dsu=0; static int c=0; static int e9=1000000007; int min=Integer.MAX_VALUE; int max=Integer.MIN_VALUE; long max1=Long.MIN_VALUE; long min1=Long.MAX_VALUE; boolean aBoolean=true; boolean y=false; long m=0; static boolean t1=false; static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } int parent[]; int rank[]; int n=0; ArrayList<ArrayList<Integer>> arrayLists; boolean v1[]; static boolean t2=false; boolean r=false; int fib[]; int fib1[]; int ind[]; int min3=Integer.MAX_VALUE; public static void main(String[] args) throws IOException { Main g = new Main(); g.go(); } public void go() throws IOException { FastReader scanner = new FastReader(); // Scanner scanner = new Scanner(System.in); // BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in)); // String s; PrintWriter printWriter = new PrintWriter(System.out); int t =scanner.nextInt(); for (int i = 0; i < t; i++) { int n=scanner.nextInt(); int m=n; int arr[]=new int[n]; int ans[]=new int[n]; int in[]=new int[n+1]; int y=0; boolean v[]=new boolean[n+1]; for (int j = 0; j < n; j++) { arr[j]=scanner.nextInt(); in[arr[j]]=j; } for (int j = n; j>0; j--) { if(!v[j]){ int o=in[j]; for (int k =in[j]; k<n; k++){ v[arr[k]]=true; ans[y]=arr[k]; y++; } n=o; } } for (int k = 0; k <m ; k++) { printWriter.print(ans[k]+" "); } printWriter.println(); } printWriter.flush(); } static final int mod=1_000_000_007; public long mul(long a, long b) { return a*b; } public long fact(int x) { long ans=1; for (int i=2; i<=x; i++) ans=mul(ans, i); return ans; } public long fastPow(long base, long exp) { if (exp==0) return 1; long half=fastPow(base, exp/2); if (exp%2==0) return mul(half, half); return mul(half, mul(half, base)); } public long modInv(long x) { return fastPow(x, mod-2); } public long nCk(int n, int k) { return mul(fact(n), mul(modInv(fact(k)), modInv(fact(n-k)))); } // public void sieve(int n){ // fib=new int[n+1]; // fib[1]=-1; // fib[0]=-1; // for (int i =2; i*i<=n; i++) { // if(fib[i]==0) // for (int j =i*i; j <=n; j+=i){ // fib[j]=-1; //// System.out.println("l"); // } // } // } public void parent(int n){ for (int i = 0; i < n; i++) { parent[i]=i; rank[i]=1; size[i]=1; } } public void union(int i,int j){ int root1=find(i); int root2=find(j); // if(root1 != root2) { // parent[root2] = root1; //// sz[a] += sz[b]; // } if(root1==root2){ return; } if(rank[root1]>rank[root2]){ parent[root2]=root1; rank[root1]++; size[root1]+=size[root2]; } else if(rank[root1]<rank[root2]){ parent[root1]=root2; size[root2]+=size[root1]; rank[root2]++; } else{ parent[root2]=root1; rank[root1]+=1; size[root1]+=size[root2]; } } public int find(int p){ if(parent[p]!=p){ if(parent[p]==-1){ return parent[p]; } parent[p]=find(parent[p]); } return parent[p]; // if(parent[p]==-1){ // return -1; // } // else if(parent[p]==p){ // return p; // } // else { // parent[p]=find(parent[p]); // return parent[p]; // } } public double dist(double x1,double y1,double x2,double y2){ double e=(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1); double e1=Math.sqrt(e); return e1; } public void make(int p){ parent[p]=p; rank[p]=1; } Random rand = new Random(); public void sort(int[] a, int n) { for (int i = 0; i < n; i++) { int j = rand.nextInt(i + 1); int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } Arrays.sort(a, 0, n); } public long gcd(long a,long b){ if(b==0){ return a; } return gcd(b,a%b); } public void dfs(ArrayList<Integer> arrayLists1){ for (int i = 0; i < arrayLists1.size(); i++) { if(v1[arrayLists1.get(i)]==false){ System.out.println(arrayLists1.get(i)); v1[arrayLists1.get(i)]=true; count++; dfs(arrayLists.get(arrayLists1.get(i))); } } } private void dfs2(ArrayList<Integer>[]arrayList,int j,int count){ ans[j]=count; for(int i:arrayList[j]){ if(ans[i]==-1){ dfs2(arrayList,i,count); } } } private void dfs3(ArrayList<Integer>[] arrayList, int j) { v[j]=true; count++; for(int i:arrayList[j]){ if(v[i]==false) { dfs3(arrayList, i); } } } public double fact(double h){ double sum=1; while(h>=1){ sum=(sum%e9)*(h%e9); h--; } return sum%e9; } public long primef(double r){ long c=0; long ans=1; while(r%2==0){ c++; r=r/2; } if(c>0){ ans*=2; } c=0; // System.out.println(ans+" "+r); for (int i = 3; i <=Math.sqrt(r) ;i+=2) { while(r%i==0){ // System.out.println(i); c++; r=r/i; } if(c>0){ ans*=i; } c=0; } if(r>2){ ans*=r; } return ans; } public long divisor(double r){ long c=0; for (int i = 1; i <=Math.sqrt(r); i++) { if(r%i==0){ if(r/i==i){ c++; } else{ c+=2; } } } return c; } } class Pair{ int x; int y; double z; public Pair(int x,int y){ this.x=x; this.y=y; } @Override public int hashCode() { int hash =37; return this.x * hash + this.y; } @Override public boolean equals(Object o1){ if(o1==null||o1.getClass()!=this.getClass()){ return false; } Pair o=(Pair)o1; if(o.x==this.x&&o.y==this.y){ return true; } return false; } } class Sorting implements Comparator<Pair> { public int compare(Pair p1,Pair p2){ // if(p1.x==p2.x){ // return -1*Double.compare(p1.y,p2.y); // } // else { return Double.compare(p1.x, p2.x); // } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 11
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
84d47eb643852406f0c0c8120bf1fba8
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.io.DataInputStream; import java.io.IOException; import java.io.PrintWriter; public class B_div2_704 { public static void main(String[] args) throws IOException { Reader scan = new Reader(); PrintWriter p = new PrintWriter(System.out); StringBuilder sb = new StringBuilder(); int t = scan.nextInt(); while (t-- > 0) { int n = scan.nextInt(); int[] a = new int[n]; int[] pos = new int[n+1]; for(int i=0;i<n;i++) { a[i] = scan.nextInt(); pos[a[i]] = i; } int start = 0; int end = n; for(int i=n;i>0;i--) { start = pos[i]; if(start < end) { for(int j=start;j<end;j++) { sb.append(a[j]).append(" "); } end = start; } } sb.append("\n"); } p.print(sb); p.close(); } 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 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; } 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
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 11
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
590bb9dbbcf8b35c249c28e5d3c26711
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.io.*; public class Solve{ public static void main(String[] args) throws Exception{ Fast sc=new Fast(); StringBuilder sb=new StringBuilder(); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int[] ar=new int[n]; int[] pos=new int[n]; int max=0; int po=0; for(int i=0;i<n;i++){ ar[i]=sc.nextInt(); if(ar[i]>max){ max=ar[i]; pos[i]=i; po=i; } else{ pos[i]=po; } } int p=po; int st=n; while(true){ for(int i=p;i<st;i++){ sb.append(ar[i]+" "); } if(p>0){ st=p; p=pos[p-1]; } else{ break; } } sb.append("\n"); } System.out.println(sb); } static void ReverSort(int[] ar){ ArrayList<Integer> al=new ArrayList<>(); for(int i=0;i<ar.length;i++){ al.add(ar[i]); } Collections.sort(al); int j=0; for(int i=al.size()-1;i>=0;i--){ ar[j]=al.get(i); j++; } } static void Sort(int[] ar){ ArrayList<Integer> al=new ArrayList<>(); for(int i=0;i<ar.length;i++){ al.add(ar[i]); } Collections.sort(al); int j=0; for(int i=0;i<al.size();i++){ ar[j]=al.get(i); j++; } } static int gcd(int a,int b){ if(b==0) return a; else return gcd(b,a%b); } } class Pair{ int x, y; Pair(int x,int y){ this.x=x; this.y=y; } } class SortPair implements Comparator<Pair>{ public int compare(Pair p1, Pair p2){ return p1.y-p2.y; } } class Fast{ BufferedReader br; StringTokenizer st; public Fast(){ br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 11
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
6705ac4e3f0155ea7a8c37f74c09a1ce
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.io.*; public class B_r { public static void main(String[] args) { FastScanner scanner = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); int T = scanner.nextInt(); while (T-- > 0) { int n = scanner.nextInt(); int[] original = new int[n]; int[] map = new int[n + 1]; for (int i = 0; i < n; i++) { int num = scanner.nextInt(); original[i] = num; map[num] = i; } int end = n - 1; boolean[] seen = new boolean[n + 1]; for (int num = n; num >= 1 && end >= 0; num--) { if (seen[num]) { continue; } int startInd = map[num]; while (startInd <= end) { int curNum = original[startInd]; pw.printf("%d ", curNum); seen[curNum] = true; startInd++; } end = Math.min(end, map[num] - 1); } pw.println(); pw.flush(); } } static class FastScanner { public BufferedReader reader; public StringTokenizer tokenizer; public FastScanner() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return reader.readLine(); } catch(IOException e) { throw new RuntimeException(e); } } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 11
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
7f213ce88e0a252c37ce541e469e6870
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.io.*; public class B_r { public static void main(String[] args) { FastScanner scanner = new FastScanner(); int T = scanner.nextInt(); while (T-- > 0) { int n = scanner.nextInt(); int[] original = new int[n]; int[] map = new int[n + 1]; for (int i = 0; i < n; i++) { int num = scanner.nextInt(); original[i] = num; map[num] = i; } int end = n - 1; boolean[] seen = new boolean[n + 1]; for (int num = n; num >= 1 && end >= 0; num--) { if (seen[num]) { continue; } int startInd = map[num]; while (startInd <= end) { int curNum = original[startInd]; System.out.printf("%d ", curNum); seen[curNum] = true; startInd++; } end = Math.min(end, map[num] - 1); } System.out.println(); } } static class FastScanner { public BufferedReader reader; public StringTokenizer tokenizer; public FastScanner() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return reader.readLine(); } catch(IOException e) { throw new RuntimeException(e); } } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 11
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
884a511db7417df34aa38ab8db439624
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.io.*; public class B_r { public static void main(String[] args) { FastScanner scanner = new FastScanner(); int T = scanner.nextInt(); while (T-- > 0) { int n = scanner.nextInt(); int[] original = new int[n]; int[] map = new int[n + 1]; for (int i = 0; i < n; i++) { int num = scanner.nextInt(); original[i] = num; map[num] = i; } int end = n - 1; int[] ans = new int[n]; int ind = 0; boolean[] seen = new boolean[n + 1]; for (int num = n; num >= 1 && end >= 0; num--) { if (seen[num]) { continue; } int startInd = map[num]; while (startInd <= end) { int curNum = original[startInd]; ans[ind] = curNum; seen[curNum] = true; ind++; startInd++; } end = Math.min(end, map[num] - 1); } for (int num : ans) { System.out.printf("%d ", num); } System.out.println(); } } static class FastScanner { public BufferedReader reader; public StringTokenizer tokenizer; public FastScanner() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return reader.readLine(); } catch(IOException e) { throw new RuntimeException(e); } } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 11
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output
PASSED
b3d669c94ee982c710ab4e7a436ec988
train_109.jsonl
1614071100
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k &gt; 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.
512 megabytes
import java.util.*; import java.io.*; public class B_r { public static void main(String[] args) { FastReader scanner = new FastReader(); int T = scanner.nextInt(); while (T-- > 0) { int n = scanner.nextInt(); int[] original = new int[n]; int[] map = new int[n + 1]; for (int i = 0; i < n; i++) { int num = scanner.nextInt(); original[i] = num; map[num] = i; } int end = n - 1; int[] ans = new int[n]; int ind = 0; boolean[] seen = new boolean[n + 1]; for (int num = n; num >= 1 && end >= 0; num--) { if (seen[num]) { continue; } int startInd = map[num]; while (startInd <= end) { int curNum = original[startInd]; ans[ind] = curNum; seen[curNum] = true; ind++; startInd++; } end = Math.min(end, map[num] - 1); } for (int num : ans) { System.out.printf("%d ", num); } System.out.println(); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"]
1 second
["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"]
NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
Java 11
standard input
[ "data structures", "greedy", "math" ]
1637670255f8bd82a01e2ab20cdcc9aa
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
1,100
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.
standard output