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 | 3456728e281d68f9eb36902585296073 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class aa
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
//int t = 1;
int a[] = {2, 3, -2, -3};
while(t-->0)
{
int n=sc.nextInt();
int out = 0;
if(n%3==0)
{
System.out.println(n/3);
}
else if(n%3==1)
{
if(n==1)
System.out.println(2);
else
{
System.out.println(Math.min(n/3+2, (n-4)/3+2));
}
}
else
{
System.out.println(n/3+1);
}
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 9b7881a2e2da8c692778b17f39e6937c | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static class MyScanner {
// https://codeforces.com/blog/entry/7018
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
MyScanner cin = new MyScanner();
PrintWriter cout = new PrintWriter(new BufferedOutputStream(System.out));
int test = cin.nextInt();
int num;
for (int t=1; t <= test; t++) {
num = cin.nextInt();
if (num < 0) num = -num;
int result=0;
if (num == 1){
num=0;
result =2;
}
if (num == 2){
num =0;
result =1;
}
if (num > 0 && num % 3 == 0){
result = num/3;
num=0;
}
if (num % 3 == 2){
result = (num-2)/3 + 1;
num=0;
}
if (num % 3 == 1){
result = (num-4)/3 +2;
num=0;
}
cout.print(result);
cout.print("\n");
}
cout.close();
}
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | ef3dc7451a19ba46e179d9aad788a74c | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Double.parseDouble;
import static java.lang.Math.PI;
import static java.lang.Math.min;
import static java.lang.System.arraycopy;
import static java.lang.System.exit;
import static java.util.Arrays.copyOf;
import java.util.LinkedList;
import java.util.List;
import java.util.Iterator;
import java.io.FileReader;
import java.io.FileWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Deque;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Comparator;
import java.lang.StringBuilder;
import java.util.Collections;
import java.util.*;
import java.text.DecimalFormat;
public class Solution {
// static class Edge{
// int u, v, w;
// Edge(int u, int v, int w){
// this.u = u;
// this.v = v;
// this.w = w;
// }
// }
static class Pair{
int first, second;
Pair(int first, int second){
this.first = first;
this.second = second;
}
@Override
public String toString(){
return (this.first+" "+this.second);
}
}
static class Tuple{
int first, second, third;
Tuple(int first, int second, int third){
this.first = first;
this.second =second;
this.third = third;
}
@Override
public String toString(){
return first+" "+second+" "+third;
}
}
// static class Point{
// int x, y;
// Point(int x, int y){
// this.x = x;
// this.y = y;
// }
// @Override
// public String toString(){
// return x+" "+y;
// }
// }
static BufferedReader in;
static PrintWriter out;
static StringTokenizer tok;
static int dx[] = {0, -1, 0, 1}; //left up right down
static int dy[] = {-1, 0, 1, 0};
static int MOD = (int)1e9+7, INF = (int)1e9;
static boolean vis[];
static ArrayList<Integer> adj[];
private static void solve() throws IOException{
int n = scanInt();
if(n == 1)
out.println(2);
else
out.println((int)Math.ceil((double)n/3));
}
private static int[] inputArray(int n) throws IOException {
int arr[] = new int[n];
for(int i=0; i<n; ++i)
arr[i] = scanInt();
return arr;
}
private static void displayArray(int arr[]){
for(int i = 0; i<arr.length; ++i)
out.print(arr[i]+" ");
out.println();
}
public static void main(String[] args) {
try {
long startTime = System.currentTimeMillis();
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
//in = new BufferedReader(new FileReader("input.txt"));
//out = new PrintWriter(new FileWriter("output.txt"));
int test = scanInt();
for(int t=1; t<=test; t++){
//out.print("Case #"+t+": ");
solve();
}
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
//out.println(totalTime+"---------- "+System.currentTimeMillis() );
in.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
exit(1);
}
}
static int scanInt() throws IOException {
return parseInt(scanString());
}
static long scanLong() throws IOException {
return parseLong(scanString());
}
static double scanDouble() throws IOException {
return parseDouble(scanString());
}
static String scanString() throws IOException {
if (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
static String scanLine() throws IOException {
return in.readLine();
}
private static void sort(int arr[]){
mergeSort(arr, 0, arr.length-1);
}
private static void sort(int arr[], int start, int end){
mergeSort(arr, start, end-1);
}
private static void mergeSort(int arr[], int start, int end){
if(start >= end)
return;
int mid = (start+end)/2;
mergeSort(arr, start, mid);
mergeSort(arr, mid+1, end);
merge(arr, start, mid, end);
}
private static void merge(int arr[], int start, int mid, int end){
int n1 = mid - start+1;
int n2 = end - mid;
int left[] = new int[n1];
int right[] = new int[n2];
for(int i = 0; i<n1; ++i){
left[i] = arr[i+start];
}
for(int i = 0; i<n2; ++i){
right[i] = arr[mid+1+i];
}
int i = 0, j = 0, curr = start;
while(i <n1 && j <n2){
if(left[i] <= right[j]){
arr[curr] = left[i];
++i;
}
else{
arr[curr] = right[j];
++j;
}
++curr;
}
while(i<n1){
arr[curr] = left[i];
++i; ++curr;
}
while(j<n2){
arr[curr] = right[j];
++j; ++curr;
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 7f68838d502262362eeaa4351f1263fa | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class test {
public static void main(String[]args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
long[] arr = new long[t];
for(int i = 0;i < t;i++) {arr[i]=scan.nextLong();}
for(int i = 0;i < t;i++) {
long steps=(arr[i]/3)-1;
if(arr[i] == 2 ||arr[i] == 3 ) {steps = 1;}
else if(arr[i] == 1) {steps = 2;}
else if (arr[i] == 0) {steps = 0;}
else if((arr[i] %3)+3 == 4 || (arr[i] %3)+3 == 5) {steps +=2;}
else if((arr[i] %3)+3 == 3) {steps +=1;}
System.out.println(steps);
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | fcbcfeb34cdcf2f7301b876caefa3c3d | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class Main
{
public static void main(String[] args) {
// System.out.println("Hello World");
Scanner scn=new Scanner(System.in);
int t=scn.nextInt();
while(t-->0){
int n=scn.nextInt();
int ans=0;
if(n==1){
ans=2;
}
else if(n%3==0){
ans=n/3;
}
else if(n%3==1){
ans=(n)/3;
ans=ans+1;
int ans2=999999999;
if(n%2==0){
ans2=n/2;
}
ans=Math.min(ans,ans2);
}
else{
ans=(n)/3;
ans++;
int ans2=999999999;
if(n%2==0){
ans2=n/2;
}
ans=Math.min(ans,ans2);
}
System.out.println(ans);
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | d99c929ed51c2d088f51fb4d0a6a2fcd | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Public
{
public static void main (String[] args) throws java.lang.Exception
{
try{
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
while(tc-->0){
int n = sc.nextInt();
System.out.println(solve(n));
}
}catch(Exception e){
return;
}
}
public static int solve(int n ){
if(n == 1)return 2;
if(n % 3 == 0)return n / 3;
return n / 3 + 1;
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 4854c7ce3e2abad0295f9beb1701daec | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.Scanner;
public class _2_3_Moves {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int noOfTests = sc.nextInt();
while(noOfTests-->0){
int n = sc.nextInt();
if(n==1) {
System.out.println(2);
continue;
}
if(n<0)
n = -n;
int mod3 = n%3;
int div3 = n/3;
if(mod3==0)
System.out.println(div3);
else
System.out.println(div3+1);
}
}
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | c7eec26cb5ba32b15f24dd5c5bd4c546 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
while(n>0){
int fnl=sc.nextInt();
if(fnl<0){
fnl=-fnl;
}
int curr=0;
int count=0;
int s=fnl-curr;
if(s>=3){
count=s/3;
s=s%3;
if(s==1) {
s=0;
count++;
}
}
if(s>=2){
count=count+s/2;
s=s%2;
}
if(s==1){
count+=2;
s=0;
}
System.out.println(count);
n--;
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 8018796eafda182c8232392f32a1366b | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | // Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int num=sc.nextInt();
if( num == 1 || num==5 || num==4){
System.out.println(2);
continue;
}
if(num == 2 || num == 3){
System.out.println(1);
continue;
}
if(num%3 == 0){
System.out.println(num/3);
}else if(num%3 ==1){
System.out.println(num/3+1);
}else{
System.out.println(num/3+1);
}
}
}
private static void display(int arr[]){
for(int num:arr)System.out.print(num+" ");
}
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 5258eb10dddab715c308d4a0cac8d59c | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.util.*;
public class A_TwoThreeMoves {
public final static MyScanner sc = new MyScanner();
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void solve() {
int n = sc.nextInt();
if (n == 1) {
System.out.println(2);
} else {
System.out.println((n+2)/3);
}
}
public static void main(String[] args) {
int T = sc.nextInt();
for (int t = 0; t < T; t++) {
solve();
}
}
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | b1508fd522dee31a742e9f6238845140 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.Scanner;
public class MyClass {
public static void main(String[] args) {
var in = new Scanner(System.in);
int test = in.nextInt();
for (int i=0; i<test;i++){
int n = in.nextInt();
if (n==1) {
System.out.println(2);
}
else {
System.out.println((n+2)/3);
}
}
}
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 1541cdc9399e350760f8afc2696e7fa1 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | //package com.company;
import java.io.*;
import java.util.*;
public class Main{
static final Random random = new Random();
static boolean[] primecheck;
static ArrayList<Integer>[] adj;
static int[] vis;
static int[] parent;
static int[] rank;
static int[] fact;
static int[] invFact;
static int[] ft;
static int mod = (int)1e9 + 7;
public static void main(String[] args) throws IOException {
OutputStream outputStream = System.out;
FastReader in = new FastReader();
// Reader in = new Reader();
PrintWriter out = new PrintWriter(outputStream);
PROBLEM solver = new PROBLEM();
int t = 1;
t = in.ni();
for (int i = 0; i < t; i++) {
//out.print("Case #" + (i+1) + ": ");
solver.solve(in, out);
}
out.close();
}
static class PROBLEM {
public static void solve(FastReader in, PrintWriter out) throws IOException {
int n = in.ni();
if(n%3 == 0){
out.println(n/3);
}else if(n%3 == 1){
int a = (n+2)/3 + 1;
if(n-4>=0) a = Math.min(a, (n-4)/3 + 2);
out.println(a);
}else out.println(n/3 + 1);
}
}
static long ans = 0;
static long dfs(int i, int p, long[] a){
// System.out.println(i + " " + p);
long min = Long.MAX_VALUE, sum = 0;
for(int j : adj[i]){
if(j == p) continue;
long c = dfs(j, i, a);
sum+=c;
if(c<min) min = c;
}
ans += sum - (min*(adj[i].size()-1));
a[i-1] += (min*(adj[i].size()-1));
// System.out.println("idhar ka = " +(sum - (min*(adj[i].size()-1))));
return a[i-1];
}
static int findLower(int[] a, int x){
int l = 0, r = a.length-1;
while(l<=r){
int mid = l + (r-l)/2;
if(a[mid]>=x) r = mid-1;
else l = mid+1;
}
return l;
}
static int findHigher(int[] a, int x){
int l = 0, r = a.length-1;
while(l<=r){
int mid = l + (r-l)/2;
if(a[mid]<=x) l = mid+1;
else r = mid-1;
}
return r;
}
static void graph(int n, int e, Reader in) throws IOException {
adj = new ArrayList[n+1];
vis = new int[n+1];
for (int i = 0; i < n + 1; i++){
adj[i] = new ArrayList<>();
}
for (int i = 0; i < e; i++) {
int a = in.ni(), b = in.ni();
adj[a].add(b);
adj[b].add(a);
}
}
static int[] dx = {0, 0, 1, -1, 0, 0}, dy = {1, -1, 0, 0, 0, 0}, dz = {0, 0, 0, 0, 1, -1};
static int lower_bound(int array[], int key) {
int index = Arrays.binarySearch(array, key);
// If key is not present in the array
if (index < 0) {
// Index specify the position of the key
// when inserted in the sorted array
// so the element currently present at
// this position will be the lower bound
return Math.abs(index) - 1;
}
// If key is present in the array
// we move leftwards to find its first occurrence
else {
// Decrement the index to find the first
// occurrence of the key
// while (index > 0) {
//
// // If previous value is same
// if (array[index - 1] == key)
// index--;
//
// // Previous value is different which means
// // current index is the first occurrence of
// // the key
// else
// return index;
// }
return index;
}
}
static int upper_bound(int arr[], int key)
{
int index = Arrays.binarySearch(arr, key);
int n = arr.length;
// If key is not present in the array
if (index < 0) {
// Index specify the position of the key
// when inserted in the sorted array
// so the element currently present at
// this position will be the upper bound
return Math.abs(index) - 1;
}
// If key is present in the array
// we move rightwards to find next greater value
else {
// Increment the index until value is equal to
// key
// while (index < n) {
//
// // If current value is same
// if (arr[index] == key)
// index++;
//
// // Current value is different which means
// // it is the greater than the key
// else {
// return index;
// }
// }
return index;
}
}
static int getKthElement(int k){
int l = 0, r = ft.length-1, el = Integer.MAX_VALUE;
while(l <= r){
int mid = l+r>>1;
if(sumFenwick(ft, mid) >= k){
el = Math.min(el, mid);
r = mid-1;
}else l = mid+1;
}
return el;
}
static int rangeFenwick(int[] ft, int i, int j){
return sumFenwick(ft, j) - sumFenwick(ft, i);
}
static int sumFenwick(int[] ft, int i){
int sum = 0;
for(; i>0; i&=~(i&-i)) sum+=ft[i];
return sum;
}
static void addFenwick(int[] ft, int i, int x){
int n = ft.length;
for(; i<n; i+=i&-i) ft[i] += x;
}
static void combi(){
fact = new int[200001]; fact[0] = 1;
invFact = new int[200001]; invFact[0] = 1;
for (int i = 1; i < fact.length; i++) {
fact[i] = (int)(((long)fact[i-1]%mod * i%mod) % mod);
invFact[i] = (int)fast_pow(fact[i], mod-2);
}
}
static int nCk(int n, int k){
return (int)((long)fact[n] * invFact[k]%mod * invFact[n-k]%mod)%mod;
}
static String reverse(String s){
char[] ch = s.toCharArray();
for (int j = 0; j < ch.length / 2; j++) {
char temp = ch[j];
ch[j] = ch[ch.length-j-1];
ch[ch.length-j-1] = temp;
}
return String.valueOf(ch);
}
static int[][] matrixMul(int[][] a, int[][] m){
if(a[0].length == m.length) {
int[][] b = new int[a.length][m.length];
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m.length; j++) {
int sum = 0;
for (int k = 0; k < m.length; k++) {
sum += m[i][k] * m[k][j];
}
b[i][j] = sum;
}
}
return b;
}
return null;
}
// static void dfs(int i, int d){
// vis[i] = 1;
// for(int j : adj[i]){
// if (vis[j] == 0) dfs(j, d+1);
// }
// }
static int find(int u){
if(u == parent[u]) return u;
return parent[u] = find(parent[u]);
}
static void union(int u, int v){
int a = find(u), b = find(v);
if(a == b) return;
if(rank[a] > rank[b]) parent[b] = a;
else if(rank[a] < rank[b]) parent[a] = b;
else{
parent[b] = a;
rank[a]++;
}
}
static void dsu(int n){
parent = new int[n];
rank = new int[n];
for (int i = 0; i < parent.length; i++) {
parent[i] = i;
rank[i] = 1;
}
}
static boolean isPalindrome(char[] s){
boolean b = true;
for (int i = 0; i < s.length / 2; i++) {
if(s[i] != s[s.length-1-i]){
b = false;
break;
}
}
return b;
}
static void yn(boolean b, PrintWriter out){
if(b) out.println("YES");
else out.println("NO");
}
static void pa(int[] a, PrintWriter out){
for (int j : a) out.print(j + " ");
out.println();
}
static void pa(long[] a, PrintWriter out){
for (long j : a) out.print(j + " ");
out.println();
}
public static void sortByColumn(int arr[][], int col)
{
// Using built-in sort function Arrays.sort
Arrays.sort(arr, new Comparator<>() {
@Override
// Compare values according to columns
public int compare(final int[] entry1,
final int[] entry2) {
// To sort in descending order revert
// the '>' Operator
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
}); // End of function call sort().
}
static boolean isPoT(long n){
return ((n&(n-1)) == 0);
}
static long sigmaK(long k){
return (k*(k+1))/2;
}
static void swap(int[] a, int l, int r) {
int temp = a[l];
a[l] = a[r];
a[r] = temp;
}
static int binarySearch(int[] a, int l, int r, int x){
if(r>=l){
int mid = l + (r-l)/2;
if(a[mid] == x) return mid;
if(a[mid] > x) return binarySearch(a, l, mid-1, x);
else return binarySearch(a,mid+1, r, x);
}
return -1;
}
static long gcd(long a, long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
static long lcm(long a, long b){
return (a / gcd(a, b)) * b;
}
static int ceil(int a, int b){
return (a+b-1)/b;
}
static long ceil(long a, long b){
return (a+b-1)/b;
}
static boolean isSquare(double a) {
boolean isSq = false;
double b = Math.sqrt(a);
double c = Math.sqrt(a) - Math.floor(b);
if (c == 0) isSq = true;
return isSq;
}
static long fast_pow(long a, long b) { //Jeel bhai OP
if(b == 0)
return 1L;
long val = fast_pow(a, b / 2);
if(b % 2 == 0)
return val * val % mod;
else
return val * val % mod * a % mod;
}
// 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 o){
//
// int ans = Integer.compare(x, o.x);
// if(o.x == x) ans = Integer.compare(y, o.y);
//
// return ans;
//
//// int ans = Integer.compare(y, o.y);
//// if(o.y == y) ans = Integer.compare(x, o.x);
////
//// return ans;
// }
// }
static class Tuple implements Comparable<Tuple>{
int x, y, id;
Tuple(int x, int y, int id){
this.x = x;
this.y = y;
this.id = id;
}
public int compareTo(Tuple o){
int ans = Integer.compare(x, o.x);
if(o.x == x) ans = Integer.compare(y, o.y);
return ans;
}
}
public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> {
public U x;
public V y;
public Pair(U x, V y) {
this.x = x;
this.y = y;
}
public int hashCode() {
return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode());
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair<U, V> p = (Pair<U, V>) o;
return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y));
}
public int compareTo(Pair<U, V> b) {
int cmpU = x.compareTo(b.x);
return cmpU != 0 ? cmpU : y.compareTo(b.y);
}
public int compareToY(Pair<U, V> b) {
int cmpU = y.compareTo(b.y);
return cmpU != 0 ? cmpU : x.compareTo(b.x);
}
public String toString() {
return String.format("(%s, %s)", x.toString(), y.toString());
}
}
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 nline(int n) throws IOException {
byte[] buf = new byte[n+1]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int ni() 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 nl() 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 nd() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
public int[] ra(int size) throws IOException {
int[] array = new int[size];
for (int i = 0; i < size; i++) array[i] = ni();
return array;
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int ni() {
return Integer.parseInt(next());
}
long nl() {
return Long.parseLong(next());
}
double nd() {
return Double.parseDouble(next());
}
char nc() {
return next().charAt(0);
}
boolean nb() {
return !(ni() == 0);
}
// boolean nextBoolean(){return Boolean.parseBoolean(next());}
String nline() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int[] ra(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) array[i] = ni();
return array;
}
public long[] ral(int size) {
long[] array = new long[size];
for (int i = 0; i < size; i++) array[i] = nl();
return array;
}
}
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
public static void Sieve(int n) {
Arrays.fill(primecheck, true);
primecheck[0] = false;
primecheck[1] = false;
for (int i = 2; i * i < n + 1; i++) {
if (primecheck[i]) {
for (int j = i * 2; j < n + 1; j += i) {
primecheck[j] = false;
}
}
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | b1c78100f7941867f1f3be4cfd476eae | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.net.Inet4Address;
import java.util.*;
public class Main {
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private FastWriter wr;
private Reader rd;
public final int MOD = 1000000007;
/************************************************** FAST INPUT IMPLEMENTATION *********************************************/
class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
public Reader(String path) throws FileNotFoundException {
br = new BufferedReader(
new InputStreamReader(new FileInputStream(path)));
}
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 int ni() throws IOException {
return rd.nextInt();
}
public long nl() throws IOException {
return rd.nextLong();
}
public void yOrn(boolean flag) {
if (flag) {
wr.println("YES");
} else {
wr.println("NO");
}
}
char nc() throws IOException {
return rd.next().charAt(0);
}
public String ns() throws IOException {
return rd.nextLine();
}
public Double nd() throws IOException {
return rd.nextDouble();
}
public ArrayList<Integer> nli(int n, int start) throws IOException {
ArrayList<Integer> list=new ArrayList<>(n+start);
for (int i = 0; i < n+start; i++) {
list.add(rd.nextInt());
}
return list;
}
public ArrayList<Integer> nli(int n) throws IOException {
int start=0;
ArrayList<Integer> list=new ArrayList<>(n+start);
for (int i = 0; i < n+start; i++) {
list.add(rd.nextInt());
}
return list;
}
public ArrayList<String> nls(int n, int start) throws IOException {
ArrayList<String> list=new ArrayList<>(n+start);
for (int i = 0; i < n+start; i++) {
list.add(rd.nextLine());
}
return list;
}
public ArrayList<String> nls(int n) throws IOException {
int start=0;
ArrayList<String> list=new ArrayList<>(n+start);
for (int i = 0; i < n+start; i++) {
list.add(rd.nextLine());
}
return list;
}
public ArrayList<Long> nll(int n, int start) throws IOException {
ArrayList<Long> list=new ArrayList<>(n+start);
for (int i = 0; i < n+start; i++) {
list.add(rd.nextLong());
}
return list;
}
public ArrayList<Long> nll(int n) throws IOException {
int start=0;
ArrayList<Long> list=new ArrayList<>(n+start);
for (int i = 0; i < n+start; i++) {
list.add(rd.nextLong());
}
return list;
}
/************************************************** FAST OUTPUT IMPLEMENTATION *********************************************/
public static class FastWriter {
private static final int BUF_SIZE = 1 << 13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter() {
out = null;
}
public FastWriter(OutputStream os) {
this.out = os;
}
public FastWriter(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE) innerflush();
return this;
}
public FastWriter write(char c) {
return write((byte) c);
}
public FastWriter write(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
}
return this;
}
public FastWriter write(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
});
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000) return 10;
if (l >= 100000000) return 9;
if (l >= 10000000) return 8;
if (l >= 1000000) return 7;
if (l >= 100000) return 6;
if (l >= 10000) return 5;
if (l >= 1000) return 4;
if (l >= 100) return 3;
if (l >= 10) return 2;
return 1;
}
public FastWriter write(int x) {
if (x == Integer.MIN_VALUE) {
return write((long) x);
}
if (ptr + 12 >= BUF_SIZE) innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L) return 19;
if (l >= 100000000000000000L) return 18;
if (l >= 10000000000000000L) return 17;
if (l >= 1000000000000000L) return 16;
if (l >= 100000000000000L) return 15;
if (l >= 10000000000000L) return 14;
if (l >= 1000000000000L) return 13;
if (l >= 100000000000L) return 12;
if (l >= 10000000000L) return 11;
if (l >= 1000000000L) return 10;
if (l >= 100000000L) return 9;
if (l >= 10000000L) return 8;
if (l >= 1000000L) return 7;
if (l >= 100000L) return 6;
if (l >= 10000L) return 5;
if (l >= 1000L) return 4;
if (l >= 100L) return 3;
if (l >= 10L) return 2;
return 1;
}
public FastWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (ptr + 21 >= BUF_SIZE) innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter write(double x, int precision) {
if (x < 0) {
write('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
write((long) x).write(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
write((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastWriter writeln(char c) {
return write(c).writeln();
}
public FastWriter writeln(int x) {
return write(x).writeln();
}
public FastWriter writeln(long x) {
return write(x).writeln();
}
public FastWriter writeln(double x, int precision) {
return write(x, precision).writeln();
}
public FastWriter write(int... xs) {
boolean first = true;
for (int x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs) {
boolean first = true;
for (long x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln() {
return write((byte) '\n');
}
public FastWriter writeln(int... xs) {
return write(xs).writeln();
}
public FastWriter writeln(long... xs) {
return write(xs).writeln();
}
public FastWriter writeln(char[] line) {
return write(line).writeln();
}
public FastWriter writeln(char[]... map) {
for (char[] line : map) write(line).writeln();
return this;
}
public FastWriter writeln(String s) {
return write(s).writeln();
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter print(byte b) {
return write(b);
}
public FastWriter print(char c) {
return write(c);
}
public FastWriter print(char[] s) {
return write(s);
}
public FastWriter print(String s) {
return write(s);
}
public FastWriter print(int x) {
return write(x);
}
public FastWriter print(long x) {
return write(x);
}
public FastWriter print(double x, int precision) {
return write(x, precision);
}
public FastWriter println(char c) {
return writeln(c);
}
public FastWriter println(int x) {
return writeln(x);
}
public FastWriter println(long x) {
return writeln(x);
}
public FastWriter println(double x, int precision) {
return writeln(x, precision);
}
public FastWriter print(int... xs) {
return write(xs);
}
public FastWriter print(long... xs) {
return write(xs);
}
public FastWriter println(int... xs) {
return writeln(xs);
}
public FastWriter println(long... xs) {
return writeln(xs);
}
public FastWriter println(char[] line) {
return writeln(line);
}
public FastWriter println(char[]... map) {
return writeln(map);
}
public FastWriter println(String s) {
return writeln(s);
}
public FastWriter println() {
return writeln();
}
}
/********************************************************* USEFUL CODE **************************************************/
boolean[] SAPrimeGenerator(int n) {
// TC-N*LOG(LOG N)
//Create Prime Marking Array and fill it with true value
boolean[] primeMarker = new boolean[n + 1];
Arrays.fill(primeMarker, true);
primeMarker[0] = false;
primeMarker[1] = false;
for (int i = 2; i <= n; i++) {
if (primeMarker[i]) {
// we start from 2*i because i*1 must be prime
for (int j = 2 * i; j <= n; j += i) {
primeMarker[j] = false;
}
}
}
return primeMarker;
}
private void tr(Object... o) {
if (!oj) System.out.println(Arrays.deepToString(o));
}
class Pair<F, S> {
F first;
S second;
Pair(F first, S second) {
this.first = first;
this.second = second;
}
@Override
public String toString() {
return "Pair{" +
"first=" + first +
", second=" + second +
'}';
}
}
class PairThree<F, S, X> {
private F first;
private S second;
private X third;
PairThree(F first, S second, X third) {
this.first = first;
this.second = second;
this.third = third;
}
}
public static void main(String[] args) throws IOException {
new Main().run();
}
public void run() throws IOException {
if (oj) {
rd = new Reader();
wr = new FastWriter(System.out);
} else {
File input = new File("input.txt");
File output = new File("output.txt");
if (input.exists() && output.exists()) {
rd = new Reader(input.getPath());
wr = new FastWriter(output.getPath());
} else {
rd = new Reader();
wr = new FastWriter(System.out);
oj = true;
}
}
long s = System.currentTimeMillis();
solve();
wr.flush();
tr(System.currentTimeMillis() - s + "ms");
}
/***************************************************************************************************************************
*********************************************************** MAIN CODE ******************************************************
****************************************************************************************************************************/
boolean[] sieve;
public void solve() throws IOException {
int t = 1;
t = ni();
while (t-- > 0) {
go();
}
}
/********************************************************* MAIN LOGIC HERE ****************************************************/
int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
boolean isPowerOfTwo(int n) {
int counter = 0;
while (n != 0) {
if ((n & 1) == 1) {
counter++;
}
n = n >> 1;
}
return counter <= 1;
}
void printList(ArrayList<Integer> al){
for(int i=0;i<al.size();i++){
wr.print(al.get(i)+" ");
}
wr.println("");
}
int lower_than(ArrayList<Long> al,long key){
int start=0,end=al.size()-1;
int ans=end;
while (start<=end){
int mid=(start+end)/2;
if(al.get(mid)<=key){
ans=mid;
start=mid+1;
}else {
end=mid-1;
}
}
return ans;
}
int grater_than(ArrayList<Long> al,long key){
int start=0,end=al.size()-1;
int ans=0;
while (start<=end){
int mid=(start+end)/2;
if(al.get(mid)>=key){
ans=mid;
end=mid-1;
}else {
start=mid+1;
}
}
return ans;
}
boolean isComplete(long[] arr,int index,long q){
boolean flag=true;
for(int i=index;i<arr.length;i++){
if(q==0){
flag=false;
break;
}
if(arr[i]>q){
q--;
}
}
return flag;
}
public static void subSequenceSum(
ArrayList<ArrayList<Integer>> ans,
int a[], ArrayList<Integer> temp,
int k, int start)
{
if(start > a.length || k < 0)
return ;
if(k == 0)
{
ans.add(
new ArrayList<Integer>(temp)
);
return ;
}
else {
for (int i = start;
i < a.length; i++) {
temp.add(a[i]);
subSequenceSum(ans, a,
temp, k - a[i],i+1);
temp.remove(temp.size() - 1);
}
}
}
int lcm(int a,int b){
return (a*b)/gcd(a,b);
}
String mul(String s,int k){
String t="";
while (k-->0){
t+=s;
}
return t;
}
public int uniqueCounter(String st,int k){
int[] counter=new int[26];
for(int i=0;i<st.length();i++){
counter[st.charAt(i)-'a']++;
}
int cnt=0;
for(int i=0;i<26;i++){
if(counter[i]==k){
cnt++;
}
}
return cnt;
}
public int get(String[] arr,int pointer,String st,int k){
if(pointer==arr.length){
return uniqueCounter(st,k);
}
int v1=get(arr,pointer+1,st+arr[pointer],k);
int v2=get(arr,pointer+1,st,k);
return Math.max(v1, v2);
}
public void go() throws IOException {
long n=ni();
if(n==1){
wr.println(2);
return;
}
long time=n/3;
n=n%3;
if(n==2){
time+=1;
}else if(n==1){
time+=1;
}
wr.println(time);
}
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 479976c680fe8a07794a28c86f93f3e3 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class codeforces {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-->0){
int a = sc.nextInt();
a = Math.abs(a);
if(a==1) System.out.println(2);
else {
if (a % 3 == 1) a = a + 2;
else if(a%3==2) a = a + 1;
// System.out.println(a);
System.out.println(a / 3);
}
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 16f7d1bba2e1e2be8547acfa778376b8 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Main {
public static void main(String[]args) throws IOException {
new Main().run();
}
void run() throws IOException{
new solve().setIO(System.in, System.out).run();
}
class solve extends ioTask{
int t,n,s,k,i,j,len,h,m;
public void run() throws IOException {
t=in.in();
while(t-->0) {
n=in.in();
if(n==1)out.println(2);
else if(n%3==0)out.println(n/3);
else out.println(n/3+1);
}
out.close();
}
}
class In{
private StringTokenizer in=new StringTokenizer("");
private InputStream is;
private BufferedReader bf;
public In(File file) throws IOException {
is=new FileInputStream(file);
init();
}
public In(InputStream is) throws IOException
{
this.is=is;
init();
}
private void init() throws IOException {
bf=new BufferedReader(new InputStreamReader(is));
}
boolean hasNext() throws IOException {
return in.hasMoreTokens()||bf.ready();
}
String ins() throws IOException {
while(!in.hasMoreTokens()) {
in=new StringTokenizer(bf.readLine());
}
return in.nextToken();
}
int in() throws IOException {
return Integer.parseInt(ins());
}
long inl() throws IOException {
return Long.parseLong(ins());
}
double ind() throws IOException {
return Double.parseDouble(ins());
}
}
class Out{
PrintWriter out;
private OutputStream os;
private void init() {
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(os)));
}
public Out(File file) throws IOException {
os=new FileOutputStream(file);
init();
}
public Out(OutputStream os) throws IOException
{
this.os=os;
init();
}
}
class graph{
int[]to,nxt,head;
int cnt;
void init(int n) {
cnt=1;
for(int i=1;i<=n;i++)
{
head[i]=0;
}
}
public graph(int n,int m) {
to=new int[m+1];
nxt=new int[m+1];
head=new int[n+1];
cnt=1;
}
void add(int u,int v) {
to[cnt]=v;
nxt[cnt]=head[u];
head[u]=cnt++;
}
}
abstract class ioTask{
In in;
PrintWriter out;
public ioTask setIO(File in,File out) throws IOException{
this.in=new In(in);
this.out=new Out(out).out;
return this;
}
public ioTask setIO(File in,OutputStream out) throws IOException{
this.in=new In(in);
this.out=new Out(out).out;
return this;
}
public ioTask setIO(InputStream in,OutputStream out) throws IOException{
this.in=new In(in);
this.out=new Out(out).out;
return this;
}
public ioTask setIO(InputStream in,File out) throws IOException{
this.in=new In(in);
this.out=new Out(out).out;
return this;
}
void run()throws IOException{
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 21fd42c5bb540951083ab9396e2bf5df | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Main {
public static void main(String[]args) throws IOException {
new Main().run();
}
void run() throws IOException{
new solve().setIO(System.in, System.out).run();
}
class solve extends ioTask{
int t,n,s,k,i,j,len,h,m;
int[]ans= {
0,1,1,1,2,2
};
public void run() throws IOException {
t=in.in();
while(t-->0)
{
n=in.in();
if(n==1)out.println(2);
else out.println(n/6*2+ans[n%6]);
}
out.close();
}
}
class In{
private StringTokenizer in=new StringTokenizer("");
private InputStream is;
private BufferedReader bf;
public In(File file) throws IOException {
is=new FileInputStream(file);
init();
}
public In(InputStream is) throws IOException
{
this.is=is;
init();
}
private void init() throws IOException {
bf=new BufferedReader(new InputStreamReader(is));
}
boolean hasNext() throws IOException {
return in.hasMoreTokens()||bf.ready();
}
String ins() throws IOException {
while(!in.hasMoreTokens()) {
in=new StringTokenizer(bf.readLine());
}
return in.nextToken();
}
int in() throws IOException {
return Integer.parseInt(ins());
}
long inl() throws IOException {
return Long.parseLong(ins());
}
double ind() throws IOException {
return Double.parseDouble(ins());
}
}
class Out{
PrintWriter out;
private OutputStream os;
private void init() {
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(os)));
}
public Out(File file) throws IOException {
os=new FileOutputStream(file);
init();
}
public Out(OutputStream os) throws IOException
{
this.os=os;
init();
}
}
class graph{
int[]to,nxt,head;
int cnt;
void init(int n) {
cnt=1;
for(int i=1;i<=n;i++)
{
head[i]=0;
}
}
public graph(int n,int m) {
to=new int[m+1];
nxt=new int[m+1];
head=new int[n+1];
cnt=1;
}
void add(int u,int v) {
to[cnt]=v;
nxt[cnt]=head[u];
head[u]=cnt++;
}
}
abstract class ioTask{
In in;
PrintWriter out;
public ioTask setIO(File in,File out) throws IOException{
this.in=new In(in);
this.out=new Out(out).out;
return this;
}
public ioTask setIO(File in,OutputStream out) throws IOException{
this.in=new In(in);
this.out=new Out(out).out;
return this;
}
public ioTask setIO(InputStream in,OutputStream out) throws IOException{
this.in=new In(in);
this.out=new Out(out).out;
return this;
}
public ioTask setIO(InputStream in,File out) throws IOException{
this.in=new In(in);
this.out=new Out(out).out;
return this;
}
void run()throws IOException{
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | d8ab713f470be2ee9eb2b6282b10303a | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
String nul = in.nextLine();
for(int i=0;i<t;i++){
long a = in.nextLong();
if(a%3==0){
System.out.println(a/3);
}
else if(a%3==1){
if(a%2==0 && a<4){
System.out.println(a/2);
}
else if(a==1){
System.out.println(2);
}
else
System.out.println((a-1)/3 + 1);
}
else if(a%3==2){
System.out.println((a-2)/3 + 1);
}
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | e3109f929231b1e8f91ca150770895c2 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
public class coding {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
String str = br.readLine();
if(str == null)
throw new InputMismatchException();
st = new StringTokenizer(str);
} 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();
}
if(str == null)
throw new InputMismatchException();
return str;
}
public BigInteger nextBigInteger() {
// TODO Auto-generated method stub
return null;
}
public void close() {
// TODO Auto-generated method stub
}
}
public static <K, V> K getKey(Map<K, V> map, V value)
{
for (Map.Entry<K, V> entry: map.entrySet())
{
if (value.equals(entry.getValue())) {
return entry.getKey();
}
}
return null;
}
public static int median(ArrayList<Integer> x)
{
int p=0;
if(x.size()==1)
{
p=x.get(0);
}
else
{
p=x.get((x.size()-1)/2 );
}
return p;
}
// public static boolean palindrome(long x)
// {
// String s="",s1="";
// while(x!=BigInteger.valueOf(0))
// {
// s=s+x%10;
// x=x/10;
// }
// for(int i=0;i<s.length();i++)
// {
// s1=s1+s.charAt(s.length()-i-1);
// }
// if(s1.contentEquals(s))
// {
// return true;
// }
// else
// {
// return false;
// }
// }
static void reverse(String a[], int n)
{
String k="", t="";
for (int i = 0; i < n / 2; i++) {
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
}
static int fact(int n)
{
int fact=1;
int i=1;
while(i<=n)
{
fact=fact*i;
i++;
}
return fact;
}
static int gcd(int y, int x)
{
while(x!=y)
{
if(x>y)
{
x=x-y;
}
else
{
y=y-x;
}
}
return y;
}
public static String generatePermutation(String str, int start, int end,ArrayList<String> x)
{
if (start == end-1)
System.out.println(x);
else
{
for (int i = start; i < end; i++)
{
str = swapString(str,start,i);
generatePermutation(str,start+1,end,x);
String s1=generatePermutation(str,start+1,end,x);
x.add(s1);
str = swapString(str,start,i);
}
}
return str;
}
public static String swapString(String a, int i, int j) {
char[] b =a.toCharArray();
char ch;
ch = b[i];
b[i] = b[j];
b[j] = ch;
return String.valueOf(b);
}
public static List<String> findPermutations(String str)
{
// base case
if (str == null || str.length() == 0) {
return null;
}
// create an empty ArrayList to store (partial) permutations
List<String> partial = new ArrayList<>();
// initialize the list with the first character of the string
partial.add(String.valueOf(str.charAt(0)));
// do for every character of the specified string
for (int i = 1; i < str.length(); i++)
{
// consider previously constructed partial permutation one by one
// (iterate backward to avoid ConcurrentModificationException)
for (int j = partial.size() - 1; j >= 0 ; j--)
{
// remove current partial permutation from the ArrayList
String s = partial.remove(j);
// Insert the next character of the specified string at all
// possible positions of current partial permutation. Then
// insert each of these newly constructed strings in the list
for (int k = 0; k <= s.length(); k++)
{
// Advice: use StringBuilder for concatenation
if(!partial.contains(s.substring(0, k) + str.charAt(i) + s.substring(k)))
{
partial.add(s.substring(0, k) + str.charAt(i) + s.substring(k));
}
}
}
}
return partial;
}
public static void main(String args[] ) throws Exception {
FastReader sc = new FastReader();
PrintWriter out = new PrintWriter(System.out);
StringBuilder sb = new StringBuilder();
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
if(n%3==0)
{
System.out.println(n/3);
}
else
{
if(n==1)
{
System.out.println(2);
}
else if(n==4)
{
System.out.println(2);
}
else
{
System.out.println(n/3 +1);
}
}
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | d81b7f92711c9758357cef7fe660caec | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.Scanner;
public class CodeForce1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
int ans=0;
for(int i=0;i<t;i++) {
int n=sc.nextInt();
if(n%2==0 && n%3==0) {
System.out.println(ans=n/3);
}else if(n==2 || n==4) {
System.out.println(ans=n/2);
}else if(n==1) {
System.out.println(ans=2);
}else if(n%3==0) {
System.out.println(ans=n/3);
}else if(n%3==1 || n%3==2) {
System.out.println(ans=n/3 +1);
}
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 9889fb041cbeb0604a6528a6c58bd5be | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
// write your code here
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
if(n%3==0) System.out.println(n/3);
else if(n%3==2 ) System.out.println((n/3)+1);
else if(n%3==1){
if(n==1) System.out.println("2");
else if(n%2==0){
System.out.println(Math.min((n/3)+1,n/2));
}
else{
System.out.println(n/3+1);
}
}
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | bff742527db36a57efa36d4c8a478bc8 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.LongStream;
public class Main {
static Scanner in = new Scanner(System.in);
public static void generator() {
}
public static void solve(int n) {
int count;
if (n == 1){
System.out.println(2);
return;
}
count = n / 3;
count--;
System.out.println(count + (n - 3 * count) / 2);
}
public static void main(String[] args) {
int t = in.nextInt();
for (int i = 0; i < t; i++) {
int n = in.nextInt();
solve(n);
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 5fc7d622ae1c5347e9f2afe3ad015698 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import org.w3c.dom.ls.LSOutput;
import java.util.*;
import java.util.function.IntBinaryOperator;
public class Example {
static long[][] dp;
// static int[] ar;
static int mod = 1000000000 + 7;
static long[] ans = new long[100000 + 1];
static List<List<Integer>> adj;
static int[] vis;
static int[] dis;
static int row;
static boolean[] chosen;
static List<Integer> permutation;
static int k;
static int count;
static int[][] arr;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int test = sc.nextInt();
while(test-- != 0){
long n= sc.nextLong();
if(n==0) {
System.out.println(0);
} else {
n = Math.abs(n);
long path=0, x=0;
if(n%2==0) {
x = n/2;
}
if(n==1) {
path = 2;
}
else if(n%3==0) {
path = n/3;
} else if(n%3==1) {
path = n/3 + 1;
} else if(n%3==2) {
path = n/3 + 1;
}
if(x==0) {
System.out.println(path);
} else {
path = Math.min(path, x);
System.out.println(path);
}
}
}
}
public static String reverseString(String str){
StringBuilder input1 = new StringBuilder();
// append a string into StringBuilder input1
input1.append(str);
// reverse StringBuilder input1
input1.reverse();
return input1+"";
}
// private static int requiredPath(int currIndex, int target, HashMap<Integer,Integer> memo){
// if(currIndex > target ){
// return 0;
// }
// if(currIndex == target){
// return 1;
// }
// int currKey = currIndex;
// if(memo.containsKey(currKey)){
// return memo.get(currKey);
// }
// int one = 1 + requiredPath(currIndex+1,target,memo);
// int two = 1 + requiredPath(currIndex+2,target,memo);
// int three = 1 + requiredPath(currIndex+3,target,memo);
// int four = 1 + requiredPath(currIndex+4,target,memo);
// int five = 1 + requiredPath(currIndex+5,target,memo);
// int x = Math.min(one,two);
// int y = Math.min(three,four);
// int z = Math.min(x,y);
// memo.put(currKey, Math.min(five,z));
// return currKey;
// }
private static long getans1(int[] s, int[] c, int i, int cc, int maxValue) {
if (cc == 1 && i >= 0) {
return c[i];
} else if (cc == 0 && i >= 0) {
return 0;
} else if (i < 0) {
return Integer.MAX_VALUE;
}
if (dp[i][cc] != -1) {
return dp[i][cc];
}
if (s[i] < maxValue || maxValue == 0) {
return dp[i][cc] = Math.min(getans1(s, c, i - 1, cc, maxValue), c[i] + getans1(s, c, i - 1, cc - 1, s[i]));
} else {
return dp[i][cc] = getans1(s, c, i - 1, cc, maxValue);
}
}
private static void swap(int i, int j, int[] arrr) {
int temp = arrr[i];
arrr[i] = arrr[j];
arrr[j] = temp;
}
private static long getsum(int[][] h, int i, int p, long[][] dp) {
if (i >= h[0].length) {
return 0;
}
if (dp[i][p] != -1) {
return dp[i][p];
}
if (p == 0) {
long a = getsum(h, i + 1, p, dp);
a = Math.max(a, h[1][i] + getsum(h, i + 1, 1, dp));
a = Math.max(a, h[2][i] + getsum(h, i + 1, 2, dp));
return dp[i][p] = a;
} else if (p == 1) {
long a = getsum(h, i + 1, p, dp);
//a=Math.max(a,h[i][1]+getsum(h,i+1,1));
a = Math.max(a, h[2][i] + getsum(h, i + 1, 2, dp));
return dp[i][p] = a;
} else {
long a = getsum(h, i + 1, p, dp);
a = Math.max(a, h[1][i] + getsum(h, i + 1, 1, dp));
// a= Math.max(a,h[i][2]+getsum(h,i+1,2));
return dp[i][p] = a;
}
}
private static int getans(int[] arr, int i) {
if (i < 0)
return 0;
else {
int min = mod;
for (int j = 0; j < arr.length; j++) {
if (arr[j] == 1) {
arr[j] = 2;
for (int k = 0; k < j; k++) {
if (arr[k] == 0) {
arr[k] = 2;
min = Math.min(min, Math.abs(j - k) + getans(arr, i - 1));
}
}
}
}
return min;
}
}
private static int ncr(int notsure, int i) {
int a = 1;
a = (factorail(notsure)) / (factorail(notsure - 1) * (factorail(i)));
return a;
}
private static int factorail(int notsure) {
int fac = 1;
for (int i = 2; i <= notsure; i++) {
fac = fac + i;
}
return fac;
}
private static void allPerMutation(int n) {
if (permutation.size() == n) {
// System.out.println(permutation);
long sum = arr[1][permutation.get(0)];
for (int i = 0; i < permutation.size() - 1; i++) {
sum += arr[permutation.get(i)][permutation.get(i + 1)];
}
sum = sum + arr[permutation.get(n - 1)][1];
if (sum == k) {
count++;
}
} else {
for (int i = 2; i <= (n + 1); i++) {
if (chosen[i]) {
continue;
}
permutation.add(i);
chosen[i] = true;
allPerMutation(n);
permutation.remove(permutation.size() - 1);
chosen[i] = false;
}
}
}
public static int Check(int[] arr, int k) {
int sum = 0;
int j = 1;
int total = 0;
for (int i = 1; i < arr.length; i++) {
sum += arr[i];
if (sum == k) {
total += (i - j);
j = i + 1;
sum = 0;
}
if (sum > k) {
return arr.length;
}
}
return total;
}
public static char[][] rotateTheBox(char[][] box) {
Stack<Character> st;
int m = box.length;
int n = box[0].length;
char[][] cc = new char[box[0].length][box.length];
int k = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cc[i][j] = '.';
}
}
for (int i = m - 1; i >= 0; i--) {
st = new Stack<>();
for (int j = 0; j < n; j++) {
char c = box[i][j];
if (c == '#') {
st.push(c);
} else if (c == '*') {
cc[j][k] = '*';
for (int p = j - 1; p >= 0; p--) {
if (!st.isEmpty())
cc[p][k] = st.pop();
else
break;
}
st = new Stack<>();
}
}
if (!st.isEmpty()) {
for (int p = n - 1; p >= 0; p--) {
if (!st.isEmpty())
cc[p][k] = st.pop();
else
break;
}
}
k++;
}
return cc;
}
private static long findans(int[] arr, int i, int s) {
if (i < 0 && s == 0)
return 0;
if (i < 0 && s == 1)
return 0;
if (dp[i][s] != -1) {
return dp[i][s];
} else {
if (s == 0)
return dp[i][s] = Math.max(findans(arr, i - 1, s), -1 * arr[i] + findans(arr, i - 1, 1 - s));
else
return dp[i][s] = Math.max(findans(arr, i - 1, s), arr[i] + findans(arr, i - 1, 1 - s));
}
}
public static void dfs(int v, int di) {
dis[v] = di;
vis[v] = 1;
for (int i = 0; i < adj.get(v).size(); i++) {
int x = adj.get(v).get(i);
if (vis[x] == 0) {
dfs(x, di + 1);
}
}
}
private static boolean isPrime(long a) {
if (a == 1)
return true;
for (int i = 2; i * i <= a; i++) {
if (a % i == 0)
return false;
}
return true;
}
private static int gcd(int aa, int bb) {
if (bb == 0)
return aa;
return gcd(bb, aa % bb);
}
private static int lowerBound(int[] ar, int a) {
int res = 0;
int s = 1;
int e = ar.length - 1;
while (s <= e) {
int mid = (s) + (e - s) / 2;
if (ar[mid] <= a) {
res = mid;
s = mid + 1;
} else {
e = mid - 1;
}
}
return res;
}
public static int getXORSum(int[] arr1, int[] arr2) {
long c = 0;
int n = arr1.length;
int m = arr2.length;
if (n == 1 && m == 1) {
return arr1[0] & arr2[0];
}
int aa = n + m;
for (int i = 0; i < 32; i++) {
int z = 0;
int o = 0;
int cc = 0;
for (int a : arr1) {
int x = (a & (1 << i));
if (x == 0)
z++;
else
o++;
}
for (int a : arr2) {
int x = (a & (1 << i));
if (x == 0)
z++;
else
o++;
}
System.out.println(z + " " + o);
if (z != aa && o != aa) {
c = c + (1 << i);
}
}
return (int) c;
}
static boolean[] dpbo;
public static boolean canReach(String s, int minJump, int maxJump, int i) {
if (i == 0) {
return true;
}
if (i < 0) {
return false;
}
boolean tt = false;
for (int j = minJump; j <= maxJump; j++) {
if (i - j >= 0 && s.charAt(i - j) == '0') {
tt = (tt | canReach(s, minJump, maxJump, i - j));
}
}
return tt;
}
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | a9bd438cf6af830aa45c289067e80a95 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Moves {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
if (st.hasMoreTokens()) {
str = st.nextToken("\n");
} else {
str = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader scanner = new FastReader();
int t = scanner.nextInt();
while(t-- > 0){
int n = scanner.nextInt();
if(n > 4) {
if (n % 3 == 0) {
System.out.println(n / 3);
} else if (n % 3 == 1) {
System.out.println((n-4)/3 + 2);
} else if (n % 3 == 2) {
System.out.println(n / 3 + 1);
}
}
else{
if(n == 1){
System.out.println(2);
}
else if(n == 2){
System.out.println(1);
}
else if(n == 3){
System.out.println(1);
}
else if(n == 4){
System.out.println(2);
}
}
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 4074d59b59dc71462d014000bc4438f3 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class code4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
while(t-- > 0) {
int n = Math.abs(input.nextInt());
if(n == 1){
System.out.println(n+1);
}
else if(n%3 == 0){
System.out.println(n/3);
}else{
System.out.println(n/3+1);
}
}
}
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | fc802fce7b4991cc4138ee2601dab39a | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Scanner;
/**
*
* @author xpeng
*/
public class codeforces {
static class obj {
int prev, index, val;
obj(int prev, int index, int val) {
this.prev = prev;
this.index = index;
this.val = val;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
if (n%2==0) {
int ans1=n/2;
int div=(int)(n/3.0);
int rem=n%3;
int ans=div;
if (rem==1) {
ans+=2;
}else if (rem==0) {
} else{
ans++;
}
if (div%2!=0) {
div--;
}
n-=div*3;
int ans2=div;
ans2+=n/2;
ans=Math.min(ans, ans1);
System.out.println(Math.min(ans, ans2));
}else{
int div=(int)(n/3.0);
if (n==1) {
System.out.println(2);continue;
}
int rem=n%3;
int ans=div;
if (rem==1) {
ans+=2;
}else if (rem==0) {
} else{
ans++;
}
if (div%2==0) {
div--;
}
int ans2=div;
ans2+=(n-(div*3))/2;
n-=3;
ans=Math.min(ans, ans2);
int ans1=1;
if (n<0) {
System.out.println(ans);
}else{
ans1+=n/2;
System.out.println(Math.min(ans1, ans));
}
}
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 65268550577e1bf8486929724565817e | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
// For fast input output
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
try {
br = new BufferedReader(
new InputStreamReader(System.in));
} catch (Exception e) {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
// end of fast i/o code
public static void main(String[] args) {
FastReader reader = new FastReader();
int T = reader.nextInt();
while (T-- > 0) {
int n=reader.nextInt();
int m=Math.abs(n-0);
if(m==1) System.out.println(2);
else{if (m%3==0) {
System.out.println(m/3);
} else {
if(m%3==1 && m%2!=0){
int temp=Math.min(((m-1)/3)+2,((m-4)/3)+2);
System.out.println(temp);
}
else{
if(m%3==1 && m%2==0){
int temp=Math.min(((m-1)/3)+2,((m-4)/3)+2);
System.out.println(temp);
}
else{
System.out.println(((m-2)/3)+1);
}
}
}}
}
}
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | daa8238a15c085fb5b011567e710a648 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//Reader s=new Reader();
FastReader s=new FastReader();
//Scanner s=new Scanner(System.in);
int t=s.nextInt();
for(int x=0;x<t;x++) {
int n=s.nextInt();
int op1=n/3;
int op2=n/2;
if(n%3==1) {
op1+=1;
}else if(n%3==2) {
op1+=1;
}
if(n==1) {
op2=2;
op1=2;
}else if(n==2) {
op1=1;
op2=1;
}
System.out.println(Math.min(op1, op2));
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | f78a5e904455dd52c436dd081d6649e4 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.util.stream.Collectors;
public class Main {
InputStream is;
PrintWriter out;
String INPUT = "";
void run() throws Exception {
is = System.in; out = new PrintWriter(System.out);
solve(); out.flush(); out.close();
}
public static void main(String[] args) throws Exception {
new Main().run();
}
public byte[] inbuf = new byte[1 << 16];
public int lenbuf = 0, ptrbuf = 0;
public int readByte() {
if (lenbuf == -1) {
throw new InputMismatchException();
}
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
public boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b))
;
return b;
}
public double nd() {
return Double.parseDouble(ns());
}
public char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private int ni() {
return (int) nl();
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
class Pair {
int first;
int second;
Pair(int a, int b) {
first = a;
second = b;
}
}
long[] nal(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nl();
}
return arr;
}
// Sorting an array (O(nlogn)):-
int partition(long[] array, int begin, int end) {
int pivot = end;
int counter = begin;
for (int i = begin; i < end; i++) {
if (array[i] < array[pivot]) {
long temp = array[counter];
array[counter] = array[i];
array[i] = temp;
counter++;
}
}
long temp = array[pivot];
array[pivot] = array[counter];
array[counter] = temp;
return counter;
}
void sort(long[] array, int begin, int end) {
if (end <= begin) return;
int pivot = partition(array, begin, end);
sort(array, begin, pivot-1);
sort(array, pivot+1, end);
}
void solve() {
int test_case = 1;
test_case = ni();
while (test_case-- > 0) {
go();
}
}
// WRITE CODE FROM HERE :-
void go() {
long n = nl();
if(n%3 == 0) {
long ans = n / 3;
out.println(ans);
return;
}
if(n == 1) {
out.println(2);
return;
}
if(n == 2 || n == 4) {
out.println(n/2);
return;
}
long ans1 = 0;
long p = n / 3;
ans1 += p;
if(n % 3 == 2) {
ans1 += 1;
}
if(n % 3 == 1) {
ans1 += 1;
}
out.println(ans1);
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 76c0049f64954096e4bb3e36a7921656 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Pattern;
public class a
{
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args)
{
FastReader scan=new FastReader();
int t = scan.nextInt();
while(t-- > 0)
{
int n = scan.nextInt();
int x = 0;
int count = 0;
if(n %3 == 0)
count = n/3;
else
count = n/3 + 1;
if(n == 1)
count=2;
int count2 = Integer.MAX_VALUE;
if (n % 2 == 0)
count2 = n / 2;
System.out.println(Math.min(count, count2));
}
}
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | e718b2df382b2b29404efa12789e2c29 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.math.*;
public class New {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t= sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int ans1=n/3;
int ans2=n/2;
if(n==1) {
System.out.println(2);
continue;
}
if(n%3==0) {
System.out.println(ans1);
}else{
System.out.println(ans1+1);
}
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 270d68a0776e84b8ec80bb3f38ca4ded | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class Two
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int t=s.nextInt();
int a[]=new int[t];
for(int i=0;i<t;i++)
a[i]=s.nextInt();
for(int i=0;i<t;i++)
{
int x=a[i];
int d=0;
if(Math.abs(x)%3==0)
d=x/3;
else
d=x/3+1;
if(x==0)
d=1;
else if(x==1)
d=2;
System.out.println(d);
}
}
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | a2dbfa0ad517f8e03bac95f66f0c3a68 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class Solution {
// TODO global variables - must be static
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// testcase
int t = Integer.parseInt(scanner.nextLine());
for (int testcase = 0; testcase < t; testcase++) {
// TODO input
long n = Integer.parseInt(scanner.nextLine());
// TODO //////////////////////////////
n = Math.max(n, n * (-1));
if (n == 1) {
System.out.println(2);
continue;
}
long mod = n % 3;
if (mod == 0)
System.out.println(n / 3);
else if (mod == 1)
System.out.println((n - 4) / 3 + 2);
else
System.out.println(n / 3 + 1);
// TODO ///////////////////////////////
}
}
// TODO function or private class
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 9991f66aa2c84c563aced514917b0370 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 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 z = 0; z < t; z++){
int n = in.nextInt();
if (n == 1) System.out.println(2);
else if (n % 3 == 0) System.out.println(n / 3);
else if(n % 3 == 1) {
System.out.println((n - 4) / 3 + 2);
}
else System.out.println(n / 3 + 1);
}
}
}
/*
* BAN
* */ | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 6eaa867ecfcc87fe2a6c61d26a554987 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testNum = sc.nextInt();
StringBuilder ans = new StringBuilder();
for (int i = 0; i < testNum; i++) {
int num = sc.nextInt();
if (num % 2 == 0) {//为偶数
if (num % 3 == 0) {
ans.append(num / 3 + "\n");
} else if (num % 3 == 1) {
int p1 = num / 3 + 2, p2 = Integer.MAX_VALUE, p3 = Integer.MAX_VALUE;
if ((num - 2) % 3 == 0) {
p2 = (num - 2) / 3 + 1;
}
if ((num - 4) % 3 == 0) {
p3 = (num - 4) / 3 + 2;
}
ans.append(Math.min(p1, Math.min(p2, p3)) + "\n");
} else {
ans.append((num - 2) / 3 + 1 + "\n");
}
} else {
if (num == 1) {
ans.append(2 + "\n");
} else {
int p1 = num / 3;
int p2 = num % 3;
if (p2 == 0) {
ans.append(p1 + "\n");
} else if (p2 == 2) {
ans.append((p1 + 1) + "\n");
} else {
int temp1 = p1 + 2, temp2 = Integer.MAX_VALUE, temp3 = Integer.MAX_VALUE;
if ((num - 2) % 3 == 0) {
temp2 = (num - 2) / 3 + 1;
} else if ((num - 4) % 3 == 0) {
temp3 = (num - 4) / 3 + 2;
}
ans.append(Math.min(temp1, Math.min(temp2, temp3)) + "\n");
}
}
}
}
System.out.println(ans);
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 134410f2e68e4681a5404ae87b547e4c | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class Main {
static final int INF = 0x3f3f3f3f;
static final long LNF = 0x3f3f3f3f3f3f3f3fL;
static int n;
public static void main(String[] args) throws IOException {
initReader();
int t=nextInt();
while (t--!=0){
int n=nextInt();
if(n==1){
pw.println(2);
continue;
}
if(n%3!=0){
pw.println((n+2)/3);
}else pw.println(n/3);
}
pw.close();
}
/***************************************************************************************/
static BufferedReader reader;
static StringTokenizer tokenizer;
static PrintWriter pw;
public static void initReader() throws IOException {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = new StringTokenizer("");
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
// 从文件读写
// reader = new BufferedReader(new FileReader("test.in"));
// tokenizer = new StringTokenizer("");
// pw = new PrintWriter(new BufferedWriter(new FileWriter("test1.out")));
}
public static boolean hasNext() {
try {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (Exception e) {
return false;
}
return true;
}
public static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public static String nextLine() {
try {
return reader.readLine();
} catch (Exception e) {
return null;
}
}
public static int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static long nextLong() throws IOException {
return Long.parseLong(next());
}
public static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static char nextChar() throws IOException {
return next().charAt(0);
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 5ccb039149a23ee406e4fda177fef5c5 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
public class Run {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
int numberSets = Integer.parseInt(br.readLine());
for (int nos = 0; nos < numberSets; nos ++) {
int dist = Integer.parseInt(br.readLine());
if (dist == 1) {System.out.println(2);} else {
if (dist%3 == 0) {
System.out.println(dist/3);
} else {
System.out.println((dist/3+1));}
}
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | e18ae3c0730fe562d578b4054ef8eef4 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.Scanner;
public class Problem1716A {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
solve();
}
}
public static void solve() {
long n = sc.nextLong();
long output = 0;
if (n==1) {
output = 2;
} else if (n==2 || n==3) {
output = 1;
} else {
output = (long)Math.ceil((double)n/3);
}
System.out.println(output);
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | a158fb5a31dbee3f44021f6faaab4e09 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 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();
if(n < 3)
{
if(n==2)
System.out.println(1);
else
System.out.println(2);
}
else if(n%3==0)
{
System.out.println(n/3);
}
else
{
System.out.println(n/3+1);
}
}
}
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | ef7513719985b35f8fb1767828b21e50 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String []args) {
Scanner scan = new Scanner(System.in);
int testCase = scan.nextInt();
List<Integer> answers = new ArrayList<>();
int j = 0;
while(j < testCase) {
int point = scan.nextInt();
int div = point / 3;
if(point == 1)
answers.add(2);
else if(point % 3 != 0)
answers.add(div + 1);
else
answers.add(div);
j += 1;
}
for(Integer item : answers)
System.out.println(item);
} // end of main method
} // end of Main class
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | d31687743a0dac14a8ed19b0bdbd2df2 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.Scanner;
public class TwoThreeMoves {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int tc = scanner.nextInt();
for (int i = 0; i<tc; i++){
int a =scanner.nextInt();
int byThree = Integer.MAX_VALUE;
int remainderBy3 = a%3;
if (a==1){
System.out.println(2);
continue;
}
if (remainderBy3==0){
byThree = a/3;
}
else {
byThree = (a-remainderBy3)/3+1;
}
System.out.println(byThree);
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | e5f8cb6cc1c20ad3cacd9d0fa046a920 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class sol {
public static void main(String[] args) {
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = fs.nextInt();
while (t-- > 0) {
int n = fs.nextInt();
//int arr[]=fs.readArray(n);
System.out.println(check(n));
}
}
public static int check(int n){
if(n == 1)return 2;
if(n%3==0)
return n/3;
else if(n%3==2)
return n/3 +1;
else{
int i=n/3;
return (i-1)+(n-((i-1)*3))/2;
}
}
static final Random random=new Random();
static final int mod=1_000_000_007;
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static long add(long a, long b) {
return (a+b)%mod;
}
static long sub(long a, long b) {
return ((a-b)%mod+mod)%mod;
}
static long mul(long a, long b) {
return (a*b)%mod;
}
static long exp(long base, long exp) {
if (exp==0) return 1;
long half=exp(base, exp/2);
if (exp%2==0) return mul(half, half);
return mul(half, mul(half, base));
}
static long[] factorials=new long[2_000_001];
static long[] invFactorials=new long[2_000_001];
static void precompFacts() {
factorials[0]=invFactorials[0]=1;
for (int i=1; i<factorials.length; i++) factorials[i]=mul(factorials[i-1], i);
invFactorials[factorials.length-1]=exp(factorials[factorials.length-1], mod-2);
for (int i=invFactorials.length-2; i>=0; i--)
invFactorials[i]=mul(invFactorials[i+1], i+1);
}
static long nCk(int n, int k) {
return mul(factorials[n], mul(invFactorials[k], invFactorials[n-k]));
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class FastScanner {
BufferedReader br=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\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 6a39a43b229d93a0a3da567f23a5bd9b | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | //package Java3rdyearPracticals;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int k=0;k<t;k++)
{
// HashMap<Integer,Integer> m=new HashMap<>();
int n=sc.nextInt();
if(n==1)
{
System.out.println("2");
continue;
}
int ans=0;
ans=n/3+(n%3==0?0:1);
//if rem is non zero let'say 16 so 3+3+3+3+2+2
//if rem is non zero let'say 17 so 3+3+3+3+2+3
System.out.println(ans);
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 60c4bf45d3e4b9af960418cddecad8e0 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes |
import java.awt.*;
import java.io.*;
import java.util.*;
public class Main {
static class QuickReader
{
BufferedReader in;
StringTokenizer token;
public QuickReader(InputStream ins)
{
in=new BufferedReader(new InputStreamReader(ins));
token=new StringTokenizer("");
}
public boolean hasNext()
{
while (!token.hasMoreTokens())
{
try
{
String s = in.readLine();
if (s == null) return false;
token = new StringTokenizer(s);
} catch (IOException e)
{
throw new InputMismatchException();
}
}
return true;
}
public String next()
{
hasNext();
return token.nextToken();
}
public int nextInt()
{
return Integer.parseInt(next());
}
public int[] nextInts(int n)
{
int[] res = new int[n];
for (int i = 0; i < n; i++)
res[i] = nextInt();
return res;
}
public long nextLong() {
return Long.parseLong(next());
}
public long[] nextLongs(int n)
{
long[] res = new long[n];
for (int i = 0; i < n; i++)
res[i] = nextLong();
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 ignored) {}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
private QuickReader sc;
private PrintWriter ptk;
public Main(QuickReader sc, PrintWriter ptk) {
this.sc = sc;
this.ptk = ptk;
}
public static void main(String[] args) {
QuickReader in = new QuickReader(System.in);
try(PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));)
{
new Main(in, out).solve();
}
}
public static String sortString(String inputString)
{
// Converting input string to character array
char tempArray[] = inputString.toCharArray();
// Sorting temp array using
Arrays.sort(tempArray);
// Returning new sorted string
return new String(tempArray);
}
static boolean isPalindrome(String str,
int low, int high)
{
while (low < high)
{
if (str.charAt(low) != str.charAt(high))
return false;
low++;
high--;
}
return true;
}
static int countDigit(long n)
{
return (int)Math.floor(Math.log10(n) + 1);
}
static int Digits(long n)
{
int largest = 0;
int smallest = 9;
while(n != 0)
{
long r = (n % 10);
// Find the largest digit
largest = (int) Math.max(r, largest);
// Find the smallest digit
smallest = (int) Math.min(r, smallest);
n = n / 10;
}
return largest;
}
static long printDivisors(long n)
{
long count=0;
for (int i=1; i<=Math.sqrt(n); i++)
{
if (n%i==0)
{
if (n/i == i){
count++;
}
else {
count++;
//count++;
}
}
}
return count;
}
public void reverseArray(int[] arr) {
int len = arr.length;
int left = 0, right = len - 1 - left;
while (left < right) {
swap(arr, left, right);
left++;
right--;
}
}
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; (long) i *i <=(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
void swap(int[] A, int i, int j) {
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
void swap(int i,int j){
int t=i;
i=j;
j=t;
}
static long factorial(int n)
{
if (n == 0)
return 1;
return n*factorial(n-1)%998244353;
}
boolean checkprime(int n){
int i=2;
while(i*i<=n){
if (n%i==0){
return false;
}
i++;
}
return true;
}
public void solve() {
int tt=sc.nextInt();
while (tt-->0){
long n=sc.nextLong();
long k=n;
long k2=n;
int c=0;
if(n==1){
System.out.println(2);
}else if(n==2 || n==3){
System.out.println(1);
}else{
System.out.println((int) Math.ceil((n+2)/3));
}
}
}
static long lcm(long a, long b)
{
return (a / gcd(a, b)) * b;
}
private static long gcd(long a, long b)
{
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | f610586657bca0172033bfcb2ef5cd4b | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.io.*;
public class Moves{
static void solve(Scanner in, PrintWriter out){
int n = in.nextInt();
if(n == 1){
out.println(2);
return;
}
if(n % 3 == 0) out.println(n/3);
// else if(n % 3 == 1) out.println((int)Math.floor(n/3) + 2);
else out.println((int)Math.floor(n/3) + 1);
}
public static void main(String[] args) throws IOException{
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt();
while(t-->0){
solve(in, out);
}
out.flush();
out.close();
}
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | ea779b9a55dd16b4b5acd2d94ab85a4d | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !st.hasMoreTokens()){
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str="";
try {
str=br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
public static void main(String[] args) {
FastReader in = new FastReader();
int t = in.nextInt(), res = 0;
while(t-->0) {
int n = in.nextInt();
res = n/3 + (n%3==0?0:1);
res += (n==1?1:0);
System.out.println(res);
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 01fefcc844cc5e71f20493750369eb2c | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int cycle = input.nextInt();
// int cycle = 4;
// int[] arr = new int[]{1,3,4,12};
for (int i = 0; i < cycle; i++) {
System.out.println(deez(input.nextInt()));
// System.out.println(deez(arr[i]));
}
}
static int deez(int input){
if(input==1)return 2;
if((input%3+3)==4)return input/3+1;
if(input%3==0){
return input/3;
}else if(input%3==1){
return input/3+2;
}else if(input%3==2){
return input/3+1;
}else{
return -1;
}
}
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | d6340df851a4b76b63299a27a221d15b | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | // Created By Jefferson Samuel => JeffIsHere
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import static java.lang.Double.NEGATIVE_INFINITY;
import static java.lang.Math.*;
import static java.lang.Double.parseDouble;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.System.*;
public class A {
static void solve() throws Exception {
int T = 1;
T = sint();
tc:for(int t = 1;t<=T;t++) {
int N = sint();
if(N <= 3){
out.println(1+(N==1 ?1:0));continue tc;
}
out.println((N+2)/3);
}
}
/* -----------------------------------------------------********------------------------------------------------------*/
//Globals
static int[] d4 = {-1,1,0,0};
static int[] d8r = {-1,-1,-1,1,1,1,0,0};
static int[] d8c = {-1,1,0,-1,1,0,-1,1};
static final int MOD = (int) 1e9 + 7;
static final int IMa = Integer.MAX_VALUE;
static final int IMi = Integer.MIN_VALUE;
static final long LMa = Long.MAX_VALUE;
static final long LMi = Long.MIN_VALUE;
static final double EPS = 1e-9;
// Graphs
static ArrayList<Integer>[] Gph;
static boolean[] vis;
static int[][] Grid;
static char[][] Crid;
// Essentials
static int sint() throws IOException {return parseInt(sstr());}
static double sdob() throws IOException{return parseDouble(sstr());}
static char schar() throws IOException{return sstr().charAt(0);}
static long gcd(long a, long b){
return (b == 0) ? a : gcd(b, a % b);
}
static int[] iar(int N) throws IOException {
int[] a = new int[N];
for(int i = 0;i<N;i++)a[i] = sint();
return a;
}
static String sfull() throws IOException {return in.readLine();}
static void revsor(long[] ar){sortAr(ar);revar(ar);}
static void revar(long[] ar){for (int i = 0; i < ar.length / 2; i++) { long temp = ar[i]; ar[i] = ar[ar.length - 1 - i]; ar[ar.length - 1 - i] = temp;}}
static long[] lar(int N) throws IOException {
long[] a = new long[N];
for(int i = 0;i<N;i++)a[i] = slong();
return a;
}
static String[] sar(int N)throws IOException{
String ar[] = new String[N];
for(int i =0;i<N;i++)
ar[i] = sstr();
return ar;
}
static final Random random=new Random();
static void sortAr(long[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);long temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static void sortAr(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);int temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static long slong() throws IOException {return parseLong(sstr());}
static String sstr() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
static BufferedReader in;
public static PrintWriter out;
static StringTokenizer tok;
static class Pair<T,K>{ T F;K S;public Pair(T F, K S) {this.F = F;this.S = S;}}
static class Pii{int F,S; public Pii(int F,int S){this.F = F;this.S = S;}}
static class Pll{long F;long S; public Pll(long F,long S){this.F=F;this.S=S;}}
public static void main(String[] args) {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
long startTime = System.currentTimeMillis();
solve();
in.close();
out.close();
long endTime = System.currentTimeMillis();
long timeElapsed = endTime - startTime;
err.println("Exec time " + timeElapsed+"ms");
} catch (Throwable e) {
e.printStackTrace();
exit(1);
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 338832a60bb5506bfae7b42459ff74bb | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t, n;
t = sc.nextInt();
while(t-- > 0){
n = sc.nextInt();
if(n==1) System.out.println(2);
else if(n%3 ==0) System.out.println(n/3);
else System.out.println(n/3 + 1);
}
sc.close();
}
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 23559b301e860bb072b44c8117d0ba1a | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static int mod = 1000000007;
static void read(int arr[], int start, int end, FastReader in) {
for (int i = start; i < end; i++) {
arr[i] = in.nextInt();
}
}
static int sumArr(int arr[]) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
static final int MAX = 10000000;
static int prefix[] = new int[MAX + 1];
static void buildPrefix() {
// Create a boolean array "prime[0..n]". A
// value in prime[i] will finally be false
// if i is Not a prime, else true.
boolean prime[] = new boolean[MAX + 1];
Arrays.fill(prime, true);
for (int p = 2; p * p <= MAX; p++) {
// If prime[p] is not changed, then
// it is a prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * 2; i <= MAX; i += p) {
prime[i] = false;
}
}
}
// Build prefix array
prefix[0] = prefix[1] = 0;
for (int p = 2; p <= MAX; p++) {
prefix[p] = prefix[p - 1];
if (prime[p]) {
prefix[p]++;
}
}
}
static int query(int L, int R) {
return prefix[R] - prefix[L - 1];
}
static 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 int min(int arr[]) {
int min = Integer.MAX_VALUE;
for (int i = 0; i < arr.length; i++) {
min = Math.min(min, arr[i]);
}
return min;
}
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);
}
}
public static void sortrev(int[] arr) {
ArrayList<Integer> ls = new ArrayList<>();
for (int x : arr) {
ls.add(x);
}
Collections.sort(ls,Collections.reverseOrder());
for (int i = 0; i < arr.length; i++) {
arr[i] = ls.get(i);
}
}
public static void sort(long[] arr) {
ArrayList<Long> ls = new ArrayList<>();
for (long x : arr) {
ls.add(x);
}
Collections.sort(ls);
for (int i = 0; i < arr.length; i++) {
arr[i] = ls.get(i);
}
}
static int max(int arr[]) {
int max = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
max = Math.max(max, arr[i]);
}
return max;
}
static long max(long arr[]) {
long max = Long.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
max = Math.max(max, arr[i]);
}
return max;
}
static int max(int a, int b) {
return Math.max(a, b);
}
static long max(long a, long b){
return (a>b)?a:b;
}
static int min(int a, int b) {
return Math.min(a, b);
}
static long min(long a, long b){
return Math.min(a, b);
}
public static boolean isPrime(long n) {
if (n < 2) {
return false;
}
if (n == 2 || n == 3) {
return true;
}
if (n % 2 == 0 || n % 3 == 0) {
return false;
}
long sqrtN = (long) Math.sqrt(n) + 1;
for (long i = 6L; i <= sqrtN; i += 6) {
if (n % (i - 1) == 0 || n % (i + 1) == 0) {
return false;
}
}
return true;
}
static class Pair {
int first;
int second;
Pair(int f, int s) {
first = f;
second = s;
}
@Override
public String toString() {
return "first: " + first + " second: " + second;
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
void read(int arr[]) {
for (int i = 0; i < arr.length; i++) {
arr[i] = nextInt();
}
}
void read(long arr[]) {
for (int i = 0; i < arr.length; i++) {
arr[i] = nextLong();
}
}
void read(char arr[][]){
for(int i=0;i<arr.length;i++){
String s=next();
for(int j=0;j<s.length();j++)
arr[i][j]=s.charAt(j);
}
}
void read(Pair arr[]) {
for (int i = 0; i < arr.length; i++) {
int x = nextInt();
int y = nextInt();
arr[i] = new Pair(x, y);
}
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void printarr(int arr[]) throws IOException {
println(Arrays.toString(arr));
}
public void printarr(long arr[]) throws IOException {
println(Arrays.toString(arr));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void close() throws IOException {
bw.close();
}
}
public static int count(String s, char c) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == c) {
count++;
}
}
return count;
}
public static int stoi(String s) {
return Integer.parseInt(s);
}
public static String itos(int n) {
return Integer.toString(n);
}
public static int ctoi(char c) {
return (int) c - 48;
}
public static long countSetBits(long n) {
long count = 0;
while (n != 0) {
count += (n & 1); // check last bit
n >>= 1;
}
return count;
}
static long MOD = (long) (1e9 + 7);
static long powerLL(long x, long n) {
long result = 1;
while (n > 0) {
if (n % 2 == 1) {
result = result * x % MOD;
}
n = n / 2;
x = x * x % MOD;
}
return result;
}
static long countBits(long number) {
return (long) (Math.log(number)
/ Math.log(2) + 1);
}
static boolean isPowerOfTwo(long n) {
if (n == 0) {
return false;
}
while (n != 1) {
if (n % 2 != 0) {
return false;
}
n = n / 2;
}
return true;
}
public static int LCS(String s1, String s2){
int l1=s1.length();
int l2=s2.length();
int dp[][]=new int[l1+1][l2+1];
for(int i=0;i<=l1;i++){
for(int j=0;j<=l2;j++){
if(i==0 || j==0)
dp[i][j]=0;
else{
if(s1.charAt(i-1)==s2.charAt(j-1))
dp[i][j]=1+dp[i-1][j-1];
else
dp[i][j]=Math.max(dp[i-1][j], dp[i][j-1]);
}
}
}
// for(var v:dp)
// System.out.println(Arrays.toString(v));
return dp[l1][l2];
}
public static String LCSstring(String s1, String s2){
int l1=s1.length();
int l2=s2.length();
int dp[][]=new int[l1+1][l2+1];
for(int i=0;i<=l1;i++){
for(int j=0;j<=l2;j++){
if(i==0 || j==0)
dp[i][j]=0;
else{
if(s1.charAt(i-1)==s2.charAt(j-1))
dp[i][j]=1+dp[i-1][j-1];
else
dp[i][j]=Math.max(dp[i-1][j], dp[i][j-1]);
}
}
}
String ans="";
int i=l1,j=l2;
while(i>0 && j>0){
if(s1.charAt(i-1)==s2.charAt(j-1)){
ans=s1.charAt(i-1)+ans;
i--;
j--;
}
else if(dp[i-1][j]>dp[i][j-1]){
i--;
}
else
j--;
}
return ans;
}
public static int factorial(int n)
{
return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
public static long factorial(long n)
{
return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
public static int nCr(int n, int r,int a) {
return factorial(n) / (factorial(r) * factorial(n - r));
}
public static boolean[] sieve()
{
int n=10000000;
// 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;
}
static int maxSubArraySum(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 int calc(int arr[],int n){
if(n<0 || n<arr[0])
return 0;
if(n>=arr[2])
return max(max(1+calc(arr,n-arr[2]),1+calc(arr,n-arr[1])),1+calc(arr,n-arr[0]));
else if(n>=arr[1])
return max(1+calc(arr,n-arr[1]),1+calc(arr,n-arr[0]));
else if(n>=arr[0])
return 1+calc(arr,n-arr[0]);
else
return 0;
}
public static int calc(String s1, String s2){
int arr1[]=new int[26];
int arr2[]=new int[26];
for(char c:s1.toCharArray())
arr1[c-'a']++;
for(char c:s2.toCharArray())
arr2[c-'a']++;
int sum=0;
for(int i=0;i<26;i++)
sum+=((arr2[i]-arr1[i])>0)?(arr2[i]-arr1[i]):0;
return sum;
}
public static boolean isSorted(int arr[]){
int sor[]=new int[arr.length];
for(int i=0;i<arr.length;i++)
sor[i]=arr[i];
sort(sor);
boolean check=true;
for(int i=0;i<arr.length;i++){
if(sor[i]!=arr[i]){
check=false;
break;
}
}
return check;
}
public static int find(int arr[], int n){
for(int i=0;i<arr.length;i++)
if(arr[i]==n)
return i;
return -1;
}
public static long calc(long n,long m, long mid,long k){
long ans=0;
for(int i=1;i<=n;i++)
ans+=Math.min(mid/i,m);
return ans;
}
public static boolean check(int arr[],int k){
int count=0;
for(int i=0;i<arr.length;i++)
if(arr[i]%(i+1) == 0)
count++;
return count==k;
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static long lcm(long a, long b){
return (a/gcd(a,b))*b;
}
static int limit=1000000;
public static boolean same(String s){
boolean check=true;
for(int i=1;i<s.length();i++){
if(s.charAt(i)!=s.charAt(i-1)){
check=false;
break;
}
}
return check;
}
static int limit1=1000000000;
public static void printCount(int arr[],FastWriter out){
try{
for(int p:arr)
out.print(p+" ");
out.println("");
}
catch ( Exception E){
}
}
public static class item{
int len;
int cost;
item(int l, int c){
len=l;
cost=c;
}
@Override
public String toString() {
return "len: " + len + " cost: " + cost;
}
}
public static class comparator implements Comparator<item>{
public int compare(item i, item j){
if(i.len*i.cost != j.len*j.cost)
return (-(i.len*i.cost - j.len*j.cost));
else
return (-(i.len-j.len));
}
}
public static void cpy(int arr1[],int arr2[]){
for(int i=0;i<arr1.length;i++)
arr2[i]=arr1[i];
}
static int UpperBound(ArrayList<Integer> a, int x) {// x is the key or target value
int l=-1,r=a.size();
while(l+1<r) {
int m=(l+r)>>>1;
if(a.get(m)<=x) l=m;
else r=m;
}
return l+1;
}
static int LowerBound(ArrayList<Integer> a, int x) { // x is the target value or key
int l=-1,r=a.size();
while(l+1<r) {
int m=(l+r)>>>1;
if(a.get(m)>=x) r=m;
else l=m;
}
return r-1;
}
static int binarySearch(ArrayList<Integer> arr, int x)
{
int left = 0, right = arr.size() - 1;
while (left <= right)
{
int mid = left + (right - left) / 2;
// Check if x is present at mid
if (arr.get(mid) == x)
return mid;
// If x greater, ignore left half
if (arr.get(mid) < x)
left = mid + 1;
// If x is smaller, ignore right half
else
right = mid - 1;
}
// if we reach here, then element was
// not present
return -1;
}
static int upper_bound(ArrayList<Integer> arr, int key)
{
int upperBound = 0;
while (upperBound < arr.size()) {
// If current value is lesser than or equal to
// key
if (arr.get(upperBound) <= key)
upperBound++;
// This value is just greater than key
else{
// System.out.print("The upper bound of " + key + " is " + arr[upperBound] + " at index " + upperBound);
return upperBound;
}
}
return -1;
// System.out.print("The upper bound of " + key + " does not exist.");
}
public static void solve(FastReader in,FastWriter out){
try{
//-----------------------------------------
int n=in.nextInt();
if(n==1){
out.println("2");
return;
}
if(n==2 || n==3){
out.println("1");
}
else{
out.println((int)Math.ceil(n/3.0));
}
//-----------------------------------------
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
FastReader in = new FastReader();
FastWriter out = new FastWriter();
int tc=1;
tc=in.nextInt();
while(tc-- != 0)
solve(in,out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 927a3fb4c56b65620a0ccb20b71046aa | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.util.*;
public class solution
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int ans=0;
int k=n/6;
if(n==1)
{
ans=2;
}
else if(n%6==0)
{
ans=2*k;
}
else if(n%6==1 || n%6==2 || n%6==3)
{
ans=2*k+1;
}
else
{
ans=2*k+2;
}
System.out.println(ans);
}
}
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | f7c5d8113bb559a67d95be4c0f70f392 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.util.*;
public class solution
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int t=1;
t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int p=0;
p=n/6;
int ans=0;
if(n==1)
{
ans=2;
}
else if(n%6==0)
{
ans=2*p;
}
else if(n%6<=2)
{
ans=2*(p-1)+3;
}
else if(n%6==3)
{
ans=2*p+1;
}
else
{
ans=2*p+2;
}
System.out.println(ans);
}
}
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 33e11a9e633634b5c66a9bc0343e7fb8 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class Main
{
public static void main(String args[])
{
int t;
Scanner scn=new Scanner(System.in);
t=scn.nextInt();
for(int i=0;i<t;i++)
{
int n=scn.nextInt();
int a=n/3;
if(n==1)
System.out.println(2);
else if(n%3!=0)
System.out.println(a+1);
else
System.out.println(a);
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 6387837293a43a3fdce4bd19bde76bab | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.lang.*;
// import java.math.*;
import java.io.*;
import java.util.*;
public class Main{
public static int mod=(int)(1e9) + 7;
public static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public static PrintWriter ot=new PrintWriter(System.out);
public static int[] take_arr(int n){
int a[]=new int[n];
try {
String s[]=br.readLine().trim().split(" ");
for(int i=0;i<n;i++)
a[i]=Integer.parseInt(s[i]);
} catch (Exception e) {
e.printStackTrace();
}
return a;
}
public static List<Integer> take_list(int n){
List<Integer> a=new ArrayList<>();
try {
String s[]=br.readLine().trim().split(" ");
for(int i=0;i<n;i++)
a.add(Integer.parseInt(s[i]));
} catch (Exception e) {
e.printStackTrace();
}
return a;
}
public static void main (String[] args) throws java.lang.Exception
{
try{
int t=Integer.parseInt(br.readLine().trim());
// pre();
// br.readLine();
int cno=1;
while(t-->0){
String s[]=br.readLine().trim().split(" ");
int n=Integer.parseInt(s[0]);
// int m=Integer.parseInt(s[1]);
// long n=Long.parseLong(s[0]);
// long m=Long.parseLong(s[1]);
// int a[]=take_arr(n);
// int b[]=take_arr(n);
if(n<3){
if(n==1)
ot.println(2);
else
ot.println(1);
}
else if(n%3==0)
ot.println(n/3);
else if(n%3==1){
ot.println((n-4)/3 + 2);
} else{
ot.println(n/3 + 1);
}
// char ch[]=br.readLine().trim().toCharArray();
// solve(n,m);
}
ot.close();
br.close();
}catch(Exception e){
e.printStackTrace();
return;
}
}
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 97851d028e73797954bb88fa929e5292 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int o=0;o<t;o++){
int n=sc.nextInt();
//n=Math.abs(n);
int a=n%3;
int b=n/3;
//System.out.println(a+" "+b);
if(n==1){
System.out.println("2");
}
else if(a==1){
System.out.println(((n-4)/3)+2);
}
else if(a==2){
System.out.println(((n-2)/3)+1);
}
else if(a==0){
System.out.println(n/3);
}
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | aa8ef1b9179745c9ca3bdcc2c8f2dfa5 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc = new FastReader();
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
if(n == 1)
out.println(2);
else if(n == 2)
out.println(1);
else if(n == 4)
out.println(2);
else if(n % 3 == 0)
out.println(n/3);
else if(n % 3 == 1 && (n - (n / 3 - 1)*3) == 4)
out.println(n/3 + 1);
else if(n % 3 == 1)
out.println(n/3 + 2);
else if(n % 3 == 2)
out.println(n/3 + 1);
}
out.close();
}
static PrintWriter out = new PrintWriter(System.out);
static final int mod = 1_000_000_007;
static final long max = (int) (1e18);
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());
}
int[] readIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static class DSU {
int[] parent;
int[] ranks;
int[] groupSize;
int size;
public DSU(int n) {
size = n;
parent = new int[n];//0 based
ranks = new int[n];
groupSize = new int[n];//Size of each component
for (int i = 0; i < n; i++) {
parent[i] = i;
ranks[i] = 1; groupSize[i] = 1;
}
}
public int find(int x)//Path Compression
{
if (parent[x] == x)
return x;
else
return parent[x] = find(parent[x]);
}
public void union(int x, int y)//Union by rank
{
int x_rep = find(x);
int y_rep = find(y);
if (x_rep == y_rep)
return;
if (ranks[x_rep] < ranks[y_rep])
{
parent[x_rep] = y_rep;
groupSize[y_rep] += groupSize[x_rep];
}
else if (ranks[x_rep] > ranks[y_rep])
{
parent[y_rep] = x_rep;
groupSize[x_rep] += groupSize[y_rep];
}
else {
parent[y_rep] = x_rep;
ranks[x_rep]++;
groupSize[x_rep] += groupSize[y_rep];
}
size--;//Total connected components
}
}
public static int gcd(int x,int y)
{
return y==0?x:gcd(y,x%y);
}
public static long gcd(long x,long y)
{
return y==0L?x:gcd(y,x%y);
}
public static int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static long pow(long a,long b)
{
if(b==0L)
return 1L;
long tmp=1;
while(b>1L)
{
if((b&1L)==1)
tmp*=a;
a*=a;
b>>=1;
}
return (tmp*a);
}
public static long modPow(long a,long b,long mod)
{
if(b==0L)
return 1L;
long tmp=1;
while(b>1L)
{
if((b&1L)==1L)
tmp*=a;
a*=a;
a%=mod;
tmp%=mod;
b>>=1;
}
return (tmp*a)%mod;
}
static long mul(long a, long b) {
return a*b%mod;
}
static long fact(int n) {
long ans=1;
for (int i=2; i<=n; i++) ans=mul(ans, i);
return ans;
}
static 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));
}
static void debug(int ...a)
{
for(int x: a)
out.print(x+" ");
out.println();
}
static void debug(long ...a)
{
for(long x: a)
out.print(x+" ");
out.println();
}
static void debugMatrix(int[][] a)
{
for(int[] x:a)
out.println(Arrays.toString(x));
}
static void debugMatrix(long[][] a)
{
for(long[] x:a)
out.println(Arrays.toString(x));
}
static void reverseArray(int[] a) {
int n = a.length;
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
static void reverseArray(long[] a) {
int n = a.length;
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
static void sort(int[] a)
{
ArrayList<Integer> ls = new ArrayList<>();
for(int x: a) ls.add(x);
Collections.sort(ls);
for(int i=0;i<a.length;i++)
a[i] = ls.get(i);
}
static void sort(long[] a)
{
ArrayList<Long> ls = new ArrayList<>();
for(long x: a) ls.add(x);
Collections.sort(ls);
for(int i=0;i<a.length;i++)
a[i] = ls.get(i);
}
static class Pair{
int x, y;
Pair(int x, int y)
{
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
return x == pair.x && y == pair.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | a9b5caa370e4456574e0e9a5d7847ed3 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes |
import java.util.*;
/**
*
* @author Caio
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int casosPrueba = sc.nextInt();
while (casosPrueba-- > 0) {
int a = sc.nextInt();
if(a == 1){
System.out.println(2);
}
else if(a % 3 == 0){
System.out.println(a/3);
}
else{
System.out.println(a/3+1);
}
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 404958bf73010807bac02abec32016f8 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.ArrayList;
import java.util.*;
public class Sol2 {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
int t=in.nextInt();
while(t-->0) {
int n=in.nextInt();
if(n==1) System.out.println("2");
else if(n==2) System.out.println("1");
else if(n==4) System.out.println("2");
else {
int k=n%3;
if(k==1||k==2)System.out.println(n/3+1);
else if(k==0) System.out.println(n/3);
}
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 41060a42757cdf6ad5bbe80afee59c1a | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
public class Solution
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int x = sc.nextInt();
if(x==1){
System.out.println(2);
}else if(x%3==0){
System.out.println(x/3);
}
else{
System.out.println((x/3)+1);
}
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 845b1f84f999958dbe8b7081221ed78b | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes |
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
for (int i=0;i<t;i++) {
int n = scan.nextInt();
solution(n);
}
}
public static void solution(int n) {
int k = n/6;
if (n ==1) {
System.out.println(2);
return;
}
if (n%6==0) {
System.out.println(2*k);
return;
}
else if (n%6 <= 2) {
System.out.println(2*(k-1)+3);
return;
}
else if (n%6 == 3) {
System.out.println(2*k+1);
return;
}
else {
System.out.println(2*k+2);
return;
}
}
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 735337231527a2c7fbe439854885241a | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static int helper(int n){
if(n==1) return 2;
if(n==2 || n==3) return 1;
return((n-1)/3 + 1);
}
public static void main(String[] args) throws IOException {
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());
bw.write(helper(n)+"\n");
}
bw.flush();
}
} | Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | e863127baceb39478c1a258b382cc931 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main {
static FastReader in=new FastReader();
static PrintWriter out=new PrintWriter(System.out);
static int n,m,zu;
static void solve(){
n=in.nextInt();
int t=n/3;
if(t*3==n){
out.println(t);
return;
}
if(n==1){
out.println(2);
return;
}
if((t-1)*3+4==n||t*3+2==n){
out.println(t+1);
return;
}
}
public static void main(String[] args) {
zu=in.nextInt();
while(zu-->0){
solve();
out.flush();
}
out.flush();
out.close();
}
static class FastReader{
StringTokenizer st;
BufferedReader br;
FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null||!st.hasMoreElements()){
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
int nextInt(){
return Integer.parseInt(next());
}
String nextLine(){
String str="";
try {
str=br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return str;
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | a6b0a84723f446c68ffd6af404486743 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(; t>0; t--) {
int n = sc.nextInt();
if(n%3==0) System.out.println(n/3);
else if(n==1) System.out.println((n+2)/3+1);
else if(n%6==2 || n%6==5) System.out.println((n-2)/3+1);
else System.out.println((n-4)/3+2);
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 498a1f03c37e48a45a1eb73ad57266fd | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.Scanner;
public class moves23 {
public int moves(int n){
if(n==1){
return 2;
}
if(n==2){
return 1;
}
if(n==3){
return 1;
}
if(n==4){
return 2;
}
int count=0;
if(n%3==0){
return n/3;
}
else{
while(count>=0){
if(n%3==0 && count%2==0){
return n/3+(count)/2;
}
count++;
n--;
}
}
return -1;
}
public static void main(String[] args) {
moves23 ab= new moves23();
Scanner in = new Scanner(System.in);
int n=in.nextInt();
while(n>0){
int a=in.nextInt();
System.out.println(ab.moves(a));
n--;
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | f5f053e5baca6d5a74ac3973c32703db | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
public class Moves23{
public static void main(String[] args) throws IOException{
FastScanner sc = new FastScanner(false);
PrintWriter write = new PrintWriter(System.out);
int t = sc.nextInt();
int n;
while(t-- > 0){
n = sc.nextInt();
if(n == 1){
write.println(2);
continue;
}
write.println(n%3==0? n/3 : n/3+1);
}
sc.close();
write.close();
}
private static class FastScanner {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
private FastScanner(boolean usingFile) throws IOException {
if (usingFile) din=new DataInputStream(new FileInputStream("C:UsersskwekIdeaProjectsHelloWorldsrctesting.in"));
else din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
private short nextShort() throws IOException {
short ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = (short)(ret * 10 + c - '0');
while ((c = read()) >= '0' && c <= '9');
if (neg) return (short)-ret;
return ret;
}
private 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;
}
private char nextChar() throws IOException {
byte c = read();
while (c <= ' ') c = read();
return (char)c;
}
private String nextString() throws IOException {
StringBuilder ret = new StringBuilder();
byte c = read();
while (c <=' ') c = read();
do {
ret.append((char)c);
} while ((c = read())>' ');
return ret.toString();
}
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++];
}
private void close() throws IOException{
din.close();
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | 1f68696a1e2194a6abf1ad4d5edb07a2 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
public class Moves23{
public static void main(String[] args) throws IOException{
FastScanner sc = new FastScanner(false);
PrintWriter write = new PrintWriter(System.out);
int t = sc.nextInt();
int n, moves;
while(t-- > 0){
moves = 0;
n = sc.nextInt();
if(n == 1)
moves = 2;
else if(n % 3 == 0){
moves = n/3;
}
else{
moves = n/3+1;
}
write.println(moves);
}
sc.close();
write.close();
}
private static class FastScanner {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
private FastScanner(boolean usingFile) throws IOException {
if (usingFile) din=new DataInputStream(new FileInputStream("C:UsersskwekIdeaProjectsHelloWorldsrctesting.in"));
else din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
private short nextShort() throws IOException {
short ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = (short)(ret * 10 + c - '0');
while ((c = read()) >= '0' && c <= '9');
if (neg) return (short)-ret;
return ret;
}
private 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;
}
private char nextChar() throws IOException {
byte c = read();
while (c <= ' ') c = read();
return (char)c;
}
private String nextString() throws IOException {
StringBuilder ret = new StringBuilder();
byte c = read();
while (c <=' ') c = read();
do {
ret.append((char)c);
} while ((c = read())>' ');
return ret.toString();
}
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++];
}
private void close() throws IOException{
din.close();
}
}
}
| Java | ["4\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | fb484a958d36c10e5ead673ba478c330 | train_109.jsonl | 1659623700 | You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.io.*;
public class a {
public static void main(String args[]) throws IOException {
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt();
for ( ; t > 0; t--) {
long n = in.nextLong();
long y = solve(n);
out.println(y);
}
out.close();
}
public static long solve(long n) {
long cnt = Integer.MAX_VALUE;
if (n % 3 == 0) {
cnt = Math.min(cnt, n / 3);
}
if (n % 3 == 2) {
cnt = Math.min(cnt, n / 3 + 1);
}
if (n % 3 == 1) {
if (n == 1) cnt = Math.min(cnt, 2);
else cnt = Math.min(cnt, n / 3 + 1);
}
if (n == 1) cnt = Math.min(cnt, 2);
else cnt = Math.min(cnt, n / 2);
return cnt;
}
public static int brute(int start, int end) {
ArrayDeque<int[]> q = new ArrayDeque<>();
q.add(new int[] {start, 0});
int[] moves = {-2, -3, 2, 3};
TreeSet<Integer> seen = new TreeSet<>();
while (!q.isEmpty()) {
int[] curr = q.poll();
if (curr[0] == end) return curr[1];
for (int i = 0; i < 4; i++) {
int next = moves[i] + curr[0];
if (!seen.contains(next)) {
seen.add(next);
q.add(new int[] {next, curr[1] + 1});
}
}
}
return -1;
}
public static void checker() {
for (int i = 0; i <= 1000; i++) {
int x = brute(0, i);
long y = solve(i);
if (x != y) System.out.println(i + " " + x + " " + y);
}
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream i) {
br = new BufferedReader(new InputStreamReader(i));
st = new StringTokenizer("");
}
public String next() throws IOException {
if (st.hasMoreTokens())
return st.nextToken();
else
st = new StringTokenizer(br.readLine());
return next();
}
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\n\n1\n\n3\n\n4\n\n12"] | 1 second | ["2\n1\n2\n4"] | null | Java 11 | standard input | [
"greedy",
"math"
] | 208e285502bed3015b30ef10a351fd6d | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the goal of the $$$i$$$-th test case. | 800 | For each test case, print one integer — the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. | standard output | |
PASSED | a8ae36a4d17391782de4a504a1d0a243 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.System.currentTimeMillis;
/*
* @author: Hivilsm
* @createTime: 2022-04-27, 23:29:16
* @description: Platform
*/
public class Ac {
static FastReader in = new FastReader();
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static Random rand = new Random();
static int[][] DIRS = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
static int MAX = (int)1e9 + 7;
public static void main(String[] args) {
// long start = currentTimeMillis();
// int t = 1;
//
int t = i();
// out.println();
// out.println();
// out.println();
while (t-- > 0){
sol();
// out.println(sol());
// int[] ans = sol();
// for (int i : ans){
// out.print(i + " ");
// }
// out.println();
// boolean ans = sol();
// if (ans){
// out.println("YES");
// }else{
// out.println("NO");
// }
}
// long end = currentTimeMillis();
// out.println(end - start);
out.flush();
out.close();
}
static void sol() {
int n = i();
int[] arr = new int[n];
for (int j = 1; j <= n; j++){
arr[j - 1] = j;
}
out.println(n);
for (int i = 0; i < n; i++){
if (i == 0){
for (int num : arr){
out.print(num + " ");
}
out.println();
}else{
swap(arr, i, i - 1);
for (int num : arr){
out.print(num + " ");
}
out.println();
}
}
}
static boolean check(int x, int y, int n, int m){
return x >= 0 && y >= 0 && x < n && y < m;
}
static void shuffle(int[] nums, int n){
for(int i = 0; i < n; i++){
int cur = rand.nextInt(n);
swap(nums, i, cur);
}
}
static void swap(int[] nums, int l, int r){
int tmp = nums[l];
nums[l] = nums[r];
nums[r] = tmp;
}
static void ranArr() {
int n = 3, len = 10, val = 10;
System.out.println(n);
for (int i = 0; i < n; i++) {
int cnt = rand.nextInt(len) + 1;
System.out.println(cnt);
for (int j = 0; j < cnt; j++) {
System.out.print(rand.nextInt(val) + " ");
}
System.out.println();
}
}
static double fastPower(double x, int n) {
if (x == 0) return 0;
long b = n;
double res = 1.0;
if (b < 0) {
x = 1 / x;
b = -b;
}
while (b > 0) {
if ((b & 1) == 1) res *= x;
x *= x;
b >>= 1;
}
return res;
}
static int i() {
return in.nextInt();
}
static long l() {
return in.nextLong();
}
static double d() {
return in.nextDouble();
}
static String s() {
return in.nextLine();
}
static int[] inputI(int n) {
int nums[] = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = in.nextInt();
}
return nums;
}
static long[] inputLong(int n) {
long nums[] = new long[n];
for (int i = 0; i < n; i++) {
nums[i] = in.nextLong();
}
return nums;
}
static void outputI(int[] arr){
for (int num : arr){
out.print(num + " ");
}
out.println();
}
}
class ListNode {
int val;
ListNode next;
public ListNode() {
}
public ListNode(int val) {
this.val = val;
}
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
public TreeNode() {
}
public TreeNode(int val) {
this.val = val;
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
| Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | b036d3bad318946c77e896613cd16563 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.System.currentTimeMillis;
/*
* @author: Hivilsm
* @createTime: 2022-04-27, 23:29:16
* @description: Platform
*/
public class Ac {
static FastReader in = new FastReader();
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static Random rand = new Random();
static int[][] DIRS = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
static int MAX = (int)1e9 + 7;
public static void main(String[] args) {
// long start = currentTimeMillis();
// int t = 1;
//
int t = i();
out.println();
out.println();
out.println();
while (t-- > 0){
sol();
// out.println(sol());
// int[] ans = sol();
// for (int i : ans){
// out.print(i + " ");
// }
// out.println();
// boolean ans = sol();
// if (ans){
// out.println("YES");
// }else{
// out.println("NO");
// }
}
// long end = currentTimeMillis();
// out.println(end - start);
out.flush();
out.close();
}
static void sol() {
int n = i();
int[] arr = new int[n];
for (int j = 1; j <= n; j++){
arr[j - 1] = j;
}
out.println(n);
for (int i = 0; i < n; i++){
if (i == 0){
for (int num : arr){
out.print(num + " ");
}
out.println();
}else{
swap(arr, i, i - 1);
for (int num : arr){
out.print(num + " ");
}
out.println();
}
}
}
static boolean check(int x, int y, int n, int m){
return x >= 0 && y >= 0 && x < n && y < m;
}
static void shuffle(int[] nums, int n){
for(int i = 0; i < n; i++){
int cur = rand.nextInt(n);
swap(nums, i, cur);
}
}
static void swap(int[] nums, int l, int r){
int tmp = nums[l];
nums[l] = nums[r];
nums[r] = tmp;
}
static void ranArr() {
int n = 3, len = 10, val = 10;
System.out.println(n);
for (int i = 0; i < n; i++) {
int cnt = rand.nextInt(len) + 1;
System.out.println(cnt);
for (int j = 0; j < cnt; j++) {
System.out.print(rand.nextInt(val) + " ");
}
System.out.println();
}
}
static double fastPower(double x, int n) {
if (x == 0) return 0;
long b = n;
double res = 1.0;
if (b < 0) {
x = 1 / x;
b = -b;
}
while (b > 0) {
if ((b & 1) == 1) res *= x;
x *= x;
b >>= 1;
}
return res;
}
static int i() {
return in.nextInt();
}
static long l() {
return in.nextLong();
}
static double d() {
return in.nextDouble();
}
static String s() {
return in.nextLine();
}
static int[] inputI(int n) {
int nums[] = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = in.nextInt();
}
return nums;
}
static long[] inputLong(int n) {
long nums[] = new long[n];
for (int i = 0; i < n; i++) {
nums[i] = in.nextLong();
}
return nums;
}
static void outputI(int[] arr){
for (int num : arr){
out.print(num + " ");
}
out.println();
}
}
class ListNode {
int val;
ListNode next;
public ListNode() {
}
public ListNode(int val) {
this.val = val;
}
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
public TreeNode() {
}
public TreeNode(int val) {
this.val = val;
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
| Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | 9d234d920708bd2c60e1c31764e97f34 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.System.currentTimeMillis;
/*
* @author: Hivilsm
* @createTime: 2022-04-27, 23:29:16
* @description: Platform
*/
public class Ac {
static FastReader in = new FastReader();
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static Random rand = new Random();
static int[][] DIRS = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
static int MAX = (int)1e9 + 7;
public static void main(String[] args) {
// long start = currentTimeMillis();
// int t = 1;
//
int t = i();
out.println();
out.println();
out.println();
while (t-- > 0){
sol();
// out.println(sol());
// int[] ans = sol();
// for (int i : ans){
// out.print(i + " ");
// }
// out.println();
// boolean ans = sol();
// if (ans){
// out.println("YES");
// }else{
// out.println("NO");
// }
}
// long end = currentTimeMillis();
// out.println(end - start);
out.flush();
out.close();
}
static void sol() {
int n = i();
int[] arr = new int[n];
for (int j = 1; j <= n; j++){
arr[j - 1] = j;
}
out.println(n);
int cc = 1;
for (int i = 0; i < n; i++){
if (i == 0){
for (int num : arr){
out.print(num + " ");
}
out.println();
}else{
swap(arr, i, i - 1);
for (int num : arr){
out.print(num + " ");
}
out.println();
}
}
}
static boolean check(int x, int y, int n, int m){
return x >= 0 && y >= 0 && x < n && y < m;
}
static void shuffle(int[] nums, int n){
for(int i = 0; i < n; i++){
int cur = rand.nextInt(n);
swap(nums, i, cur);
}
}
static void swap(int[] nums, int l, int r){
int tmp = nums[l];
nums[l] = nums[r];
nums[r] = tmp;
}
static void ranArr() {
int n = 3, len = 10, val = 10;
System.out.println(n);
for (int i = 0; i < n; i++) {
int cnt = rand.nextInt(len) + 1;
System.out.println(cnt);
for (int j = 0; j < cnt; j++) {
System.out.print(rand.nextInt(val) + " ");
}
System.out.println();
}
}
static double fastPower(double x, int n) {
if (x == 0) return 0;
long b = n;
double res = 1.0;
if (b < 0) {
x = 1 / x;
b = -b;
}
while (b > 0) {
if ((b & 1) == 1) res *= x;
x *= x;
b >>= 1;
}
return res;
}
static int i() {
return in.nextInt();
}
static long l() {
return in.nextLong();
}
static double d() {
return in.nextDouble();
}
static String s() {
return in.nextLine();
}
static int[] inputI(int n) {
int nums[] = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = in.nextInt();
}
return nums;
}
static long[] inputLong(int n) {
long nums[] = new long[n];
for (int i = 0; i < n; i++) {
nums[i] = in.nextLong();
}
return nums;
}
static void outputI(int[] arr){
for (int num : arr){
out.print(num + " ");
}
out.println();
}
}
class ListNode {
int val;
ListNode next;
public ListNode() {
}
public ListNode(int val) {
this.val = val;
}
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
public TreeNode() {
}
public TreeNode(int val) {
this.val = val;
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
| Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | b9005d12dc03cfe058a98924fa5c2497 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.System.currentTimeMillis;
/*
* @author: Hivilsm
* @createTime: 2022-04-27, 23:29:16
* @description: Platform
*/
public class Ac {
static FastReader in = new FastReader();
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static Random rand = new Random();
static int[][] DIRS = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
static int MAX = (int)1e9 + 7;
public static void main(String[] args) {
// long start = currentTimeMillis();
// int t = 1;
//
int t = i();
out.println();
out.println();
out.println();
while (t-- > 0){
sol();
// out.println(sol());
// int[] ans = sol();
// for (int i : ans){
// out.print(i + " ");
// }
// out.println();
// boolean ans = sol();
// if (ans){
// out.println("YES");
// }else{
// out.println("NO");
// }
}
// long end = currentTimeMillis();
// out.println(end - start);
out.flush();
out.close();
}
static void sol() {
int n = i();
int[] arr = new int[n];
for (int j = 1; j <= n; j++){
arr[j - 1] = j;
}
out.println(n);
int d = 1;
for (int i = 0; i < n; i++){
if (i == 0){
for (int num : arr){
out.print(num + " ");
}
out.println();
}else{
swap(arr, i, i - 1);
for (int num : arr){
out.print(num + " ");
}
out.println();
}
}
}
static boolean check(int x, int y, int n, int m){
return x >= 0 && y >= 0 && x < n && y < m;
}
static void shuffle(int[] nums, int n){
for(int i = 0; i < n; i++){
int cur = rand.nextInt(n);
swap(nums, i, cur);
}
}
static void swap(int[] nums, int l, int r){
int tmp = nums[l];
nums[l] = nums[r];
nums[r] = tmp;
}
static void ranArr() {
int n = 3, len = 10, val = 10;
System.out.println(n);
for (int i = 0; i < n; i++) {
int cnt = rand.nextInt(len) + 1;
System.out.println(cnt);
for (int j = 0; j < cnt; j++) {
System.out.print(rand.nextInt(val) + " ");
}
System.out.println();
}
}
static double fastPower(double x, int n) {
if (x == 0) return 0;
long b = n;
double res = 1.0;
if (b < 0) {
x = 1 / x;
b = -b;
}
while (b > 0) {
if ((b & 1) == 1) res *= x;
x *= x;
b >>= 1;
}
return res;
}
static int i() {
return in.nextInt();
}
static long l() {
return in.nextLong();
}
static double d() {
return in.nextDouble();
}
static String s() {
return in.nextLine();
}
static int[] inputI(int n) {
int nums[] = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = in.nextInt();
}
return nums;
}
static long[] inputLong(int n) {
long nums[] = new long[n];
for (int i = 0; i < n; i++) {
nums[i] = in.nextLong();
}
return nums;
}
static void outputI(int[] arr){
for (int num : arr){
out.print(num + " ");
}
out.println();
}
}
class ListNode {
int val;
ListNode next;
public ListNode() {
}
public ListNode(int val) {
this.val = val;
}
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
public TreeNode() {
}
public TreeNode(int val) {
this.val = val;
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
| Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | e42bfb81410d6b3cc719f460c462b325 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.System.currentTimeMillis;
/*
* @author: Hivilsm
* @createTime: 2022-04-27, 23:29:16
* @description: Platform
*/
public class Ac {
static FastReader in = new FastReader();
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static Random rand = new Random();
static int[][] DIRS = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
static int MAX = (int)1e9 + 7;
public static void main(String[] args) {
// long start = currentTimeMillis();
// int t = 1;
//
int t = i();
out.println();
out.println();
out.println();
while (t-- > 0){
sol();
// out.println(sol());
// int[] ans = sol();
// for (int i : ans){
// out.print(i + " ");
// }
// out.println();
// boolean ans = sol();
// if (ans){
// out.println("YES");
// }else{
// out.println("NO");
// }
}
// long end = currentTimeMillis();
// out.println(end - start);
out.flush();
out.close();
}
static void sol() {
int n = i();
int[] arr = new int[n];
for (int j = 1; j <= n; j++){
arr[j - 1] = j;
}
out.println(n);
for (int i = 0; i < n; i++){
if (i == 0){
for (int num : arr){
out.print(num + " ");
}
out.println();
}else{
swap(arr, i, i - 1);
for (int num : arr){
out.print(num + " ");
}
out.println();
}
}
}
static boolean check(int x, int y, int n, int m){
return x >= 0 && y >= 0 && x < n && y < m;
}
static void shuffle(int[] nums, int n){
for(int i = 0; i < n; i++){
int cur = rand.nextInt(n);
swap(nums, i, cur);
}
}
static void swap(int[] nums, int l, int r){
int tmp = nums[l];
nums[l] = nums[r];
nums[r] = tmp;
}
static void ranArr() {
int n = 3, len = 10, val = 10;
System.out.println(n);
for (int i = 0; i < n; i++) {
int cnt = rand.nextInt(len) + 1;
System.out.println(cnt);
for (int j = 0; j < cnt; j++) {
System.out.print(rand.nextInt(val) + " ");
}
System.out.println();
}
}
static double fastPower(double x, int n) {
if (x == 0) return 0;
long b = n;
double res = 1.0;
if (b < 0) {
x = 1 / x;
b = -b;
}
while (b > 0) {
if ((b & 1) == 1) res *= x;
x *= x;
b >>= 1;
}
return res;
}
static int i() {
return in.nextInt();
}
static long l() {
return in.nextLong();
}
static double d() {
return in.nextDouble();
}
static String s() {
return in.nextLine();
}
static int[] inputI(int n) {
int nums[] = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = in.nextInt();
}
return nums;
}
static long[] inputLong(int n) {
long nums[] = new long[n];
for (int i = 0; i < n; i++) {
nums[i] = in.nextLong();
}
return nums;
}
static void outputI(int[] arr){
for (int num : arr){
out.print(num + " ");
}
out.println();
}
}
class ListNode {
int val;
ListNode next;
public ListNode() {
}
public ListNode(int val) {
this.val = val;
}
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
public TreeNode() {
}
public TreeNode(int val) {
this.val = val;
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
| Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | 81798f452d6ea31da91d0404c6542148 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
public class PermChain {
public static void main(String[] args) {
String input = "2\r\n"
+ "2\r\n"
+ "3\r\n"
+ "";
Scanner in = new Scanner(System.in);
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(System.out));
PrintWriter out = new PrintWriter(write);
int t = in.nextInt();
for(int i = 0; i < t; i++) {
int x = in.nextInt();
out.println(x);
for(int k = 0; k < x; k++) {
for(int l = 1; l <= k; l++) {
out.print((l+1) + " ");
}
if(k == x - 1) {
out.println("1");
} else {
out.print("1 ");
for(int l = k + 1; l < x-1; l++) {
out.print((l+1) + " ");
}
out.println(x);
}
}
}
out.close();
}
}
| Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | b862a716b2216f7dd757b7a2137a877b | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | // package com.test;
import java.io.*;
import java.util.*;
public class Main {
static int mod = (int) 1e9+7;
private static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
private static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) throws Exception {
in.nextToken();
int t = (int) in.nval;
while (t-- > 0) {
in.nextToken();
int n = (int) in.nval;
int a[] = new int[n];
out.println(n);
for (int i = 0; i < n; i++)
a[i] = i + 1;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n; j++)
out.print(a[j] + " ");
out.println();
int p = a[i + 1];
a[i + 1] = a[0];
a[0] = p;
}
for (int j = 0; j < n; j++)
out.print(a[j] + " ");
out.println();
}
out.close();
}
}
| Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | f60e6b0f019d1687532115045d2e9035 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes |
import java.io.*;
import java.util.*;
public class Main {
static int mod = (int) 1e9+7;
private static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
private static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws Exception{
in.nextToken();
int t = (int)in.nval;
while(t-- >0)
{
in.nextToken();
int n =(int)in.nval;
int a[] = new int[n];
out.println(n);
for(int i=0;i<n;i++)
a[i]=i+1;
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n;j++)
out.print(a[j]+" ");
out.println();
int p=a[i+1];
a[i+1]=a[0];
a[0]=p;
}
for(int j=0;j<n;j++)
out.print(a[j]+" ");
out.println();
}
out.close();
}
}
| Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | 3096bc126330dcacfd05ac3f09824f34 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes |
import java.io.*;
import java.util.*;
public class Main {
static int mod = (int) 1e9+7;
private static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
private static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) throws Exception{
in.nextToken();
int t = (int)in.nval;
while(t-- >0)
{
in.nextToken();
int n =(int)in.nval;
int a[] = new int[n];
out.println(n);
out.flush();
for(int i=0;i<n;i++)
a[i]=i+1;
for(int i=0;i<n-1;i++)
{
// for(int j=0;j<n;j++) {
print(a);
// out.flush();
// }
// out.println();
int p=a[i+1];
a[i+1]=a[0];
a[0]=p;
out.flush();
}
print(a);
// for(int j=0;j<n;j++) {
// out.print(a[j] + " ");
// out.flush();
// }
}
out.close();
}
public static void print(int []arr) {
for (int i = 0; i < arr.length; i++) {
out.print(arr[i]+" ");
}
out.println();
}
}
| Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | ac093c5102dc1a146b05c9af6be34474 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.util.*;
import java.io.*;
import java.util.*;
public class akarshanand2810 {
public static void main(String[] args) throws IOException {
solve();
}
public static void solve() throws IOException {
Reader sc = new Reader();
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt();
out.print(n+"\n");
int[] arr = new int[n];
for(int i = 1; i <= n; i++)
arr[i - 1] = i;
for(int i = 0;i < n;i++)
out.print(arr[i]+" ");
out.print("\n");
int start = 0;
int end = n - 1;
while(start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
for(int i = 0;i < n;i++)
out.print(arr[i]+" ");
out.print("\n");
start++;
}
out.flush();
}
}
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 e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
if(st.hasMoreTokens())
str = st.nextToken("\n");
else
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
// ********************* GCD Function *********************
//int gcd(int a, int b){
// if (b == 0)
// return a;
// return gcd(b, a % b);
//}
// ********************* Prime Number Function *********************
//int isPrime(int n){
// if(n < 2)
// return 0;
// if(n < 4)
// return 1;
// if(n % 2 == 0 or n % 3 == 0)
// return 0;
// for(int i = 5; i*i <= n; i += 6)
// if(n % i == 0 or n % (i+2) == 0)
// return 0;
// return 1;
//}
// ********************* Custom Pair Class *********************
//class Pair implements Comparable<Pair> {
// int a,b;
// public Pair(int a,int b) {
// this.a = a;
// this.b = b;
// }
// @Override
// public int compareTo(Pair other) {
// if(this.b == other.b)
// return Integer.compare(this.a,other.a);
// return Integer.compare(this.b,other.b);
// }
//}
// ****************** Segment Tree ******************
//public class SegmentTreeNode {
// public SegmentTreeNode left;
// public SegmentTreeNode right;
// public int Start;
// public int End;
// public int Sum;
// public SegmentTreeNode(int start, int end) {
// Start = start;
// End = end;
// Sum = 0;
// }
//}
//public SegmentTreeNode buildTree(int start, int end) {
// if(start > end)
// return null;
// SegmentTreeNode node = new SegmentTreeNode(start, end);
// if(start == end)
// return node;
// int mid = start + (end - start) / 2;
// node.left = buildTree(start, mid);
// node.right = buildTree(mid + 1, end);
// return node;
//}
//public void update(SegmentTreeNode node, int index) {
// if(node == null)
// return;
// if(node.Start == index && node.End == index) {
// node.Sum += 1;
// return;
// }
// int mid = node.Start + (node.End - node.Start) / 2;
// if(index <= mid)
// update(node.left, index);
// else
// update(node.right, index);
// node.Sum = node.left.Sum + node.right.Sum;
//}
//public int SumRange(SegmentTreeNode root, int start, int end) {
// if(root == null || start > end)
// return 0;
// if(root.Start == start && root.End == end)
// return root.Sum;
// int mid = root.Start + (root.End - root.Start) / 2;
// if(end <= mid)
// return SumRange(root.left, start, end);
// else if(start > mid)
// return SumRange(root.right, start, end);
// return SumRange(root.left, start, mid) + SumRange(root.right, mid + 1, end);
//} | Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | a9187773d3c4a7c2ddce9a936bd7f7e8 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.util.*;
import java.io.*;
import java.util.*;
public class B {
public static void main(String[] args) throws IOException {
Reader sc = new Reader();
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt();
out.print(n+"\n");
int[] arr = new int[n];
for(int i = 1; i <= n; i++)
arr[i - 1] = i;
for(int i = 0;i < n;i++)
out.print(arr[i]+" ");
out.print("\n");
int start = 0;
int end = n - 1;
while(start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
for(int i = 0;i < n;i++)
out.print(arr[i]+" ");
out.print("\n");
start++;
}
out.flush();
}
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
} | Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | bdb22716eefc69e80dc92ed7b87f95a2 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.util.*;
import java.io.*;
import java.util.*;
public class B {
public static void main(String[] args) throws IOException {
Reader sc = new Reader();
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt();
System.out.println(n);
int[] arr = new int[n];
for(int i = 1; i <= n; i++)
arr[i - 1] = i;
for(int i = 0;i < n;i++)
output.write(arr[i]+" ");
output.newLine();
int start = 0;
int end = n - 1;
while(start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
for(int i = 0;i < n;i++)
output.write(arr[i]+" ");
output.newLine();
start++;
}
output.flush();
}
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
} | Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | 565bfb659d2c04059c46350a0c0323df | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class contest {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int q = Integer.parseInt(br.readLine());
StringBuilder ans=new StringBuilder("");
for(int tt=1;tt<=q;tt++) {
int n=Integer.parseInt(br.readLine());
ans.append(n+"\n");
int arr[]=new int[n];
for(int i=0;i<n;i++) {
arr[i]=i+1;
ans.append(arr[i]+" ");
}
ans.append("\n");
for(int i=0;i<n-1;i++) {
int tmp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=tmp;
for(int j=0;j<n;j++) {
ans.append(arr[j]+" ");
}
ans.append("\n");
}
}
System.out.print(ans);
}
}
| Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | d7d3d365cf7a8b0bd302a4eb342ce2f9 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.security.cert.CollectionCertStoreParameters;
import java.util.*;
import javax.xml.transform.SourceLocator;
public class B_Permutation_Chain {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
// int arr[] = new int[100];
// for (int i = 0; i < 99; i++) {
// arr[i] = i + 1;
// }
while (t-- > 0) {
int n = s.nextInt();
// System.out.println(n);
String str = "";
int arr[] = new int[n + 1];
arr[1] = 1;
for (int i = 1; i <= n - 1; i++) {
if (i / 10 == 0)
arr[i] = str.length() + 1;
else {
arr[i] = str.length() + 2;
}
str += i + " ";
}
// System.out.println("str = " + str);
// str = str.substring(0, str.length() - 1);
System.out.println(n);
print(str, n, arr);
}
}
static void print(String str, int n, int arr[]) {
// System.out.println("n = " + n);
// System.out.println("n " + str.length());
for (int i = n - 1; i > 0; i--) {
String ans = "";
int tmp = (i * 2 - 1);
String s1 = str.substring(0, arr[i]);
// System.out.println("s1= " + tmp);
ans += s1;
ans += " " + n;
ans += str.substring(arr[i], str.length());
System.out.println(ans);
}
System.out.println(n + " " + str);
// int size = n;
// while (n > 0) {
// int i = 0;
// for (i = 0; i < n - 1; i++) {
// System.out.print(arr[i] + " ");
// }
// System.out.print(size + " ");
// for (int j = i; j < size - 1; j++) {
// System.out.print(arr[j] + " ");
// }
// n--;
// System.out.println();
}
} | Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | 35b7bb55cd37c3ea26a4b743be0633de | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class C {
static StringBuffer str=new StringBuffer();
static int n;
static void solve(){
int a[]=new int[n+1];
str.append(n).append("\n");
for(int i=1;i<=n;i++){
a[i]=i;
str.append(a[i]).append(" ");
}
str.append("\n");
int j=2;
a[n]=1;
a[1]=n;
for(int i=1;i<=n;i++) str.append(a[i]).append(" ");
str.append("\n");
int i1=n-1, i2=n;
while(j<n){
a[i1]^=a[i2];
a[i2]^=a[i1];
a[i1]^=a[i2];
for(int i=1;i<=n;i++) str.append(a[i]).append(" ");
str.append("\n");
i1--;
i2--;
j++;
}
}
public static void main(String[] args) throws java.lang.Exception {
BufferedReader bf;
PrintWriter pw;
boolean lenv=false;
if(lenv){
bf = new BufferedReader(
new FileReader("input.txt"));
pw=new PrintWriter(new
BufferedWriter(new FileWriter("output.txt")));
}else{
bf = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new OutputStreamWriter(System.out));
}
int q1 = Integer.parseInt(bf.readLine().trim());
while (q1-- > 0) {
n=Integer.parseInt(bf.readLine().trim());
solve();
}
pw.println(str);
pw.flush();
// System.out.print(str);
}
} | Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | a3262016231d71fa67d22321dc040976 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Rextester
{
public static void main(String args[]) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
int test;
test=sc.nextInt();
for(int t=0;t<test;++t)
{
long res,fill,place;
int n=sc.nextInt();
System.out.println(n);
for(int i=1;i<=n;i++)
{
StringBuilder str=new StringBuilder();
for(int j=2;j<=i;j++)
str.append(j+" ");
str.append(1+" ");
for(int j=i+1;j<=n;j++)
str.append(j+" ");
System.out.println(str);
}
}
}
} | Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | d1366d4fad7b8d8494235076e8d96e8c | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.io.*;
import java.lang.*;
import java.util.*;
public class PermutationChain {
static StringBuilder sb;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-- > 0) {
int n = s.nextInt();
System.out.println(n);
sb = new StringBuilder();
int[] a = new int[n];
for(int i = 0 ; i < n ; i++) {
a[i] = i + 1;
}
print(a , true);
for(int i = n - 2 ; i >= 0 ; i --) {
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
print(a , true);
}
System.out.println(sb);
}
}
public static void print(int[] a , boolean b) {
for(int i : a) {
sb.append(i);
sb.append(" ");
}
sb.append("\n");
}
}
| Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | fb34bfaec9c7d03cca2a82adf52cd95f | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes |
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while (t-- > 0) {
int n = s.nextInt();
int pos = 1;
System.out.println(n);
for (int i = 1; i <= n; i++) {
StringBuilder str = new StringBuilder();
for (int j = 2; j <= i; j++) {
str.append(j + " ");
}
str.append(1 + " ");
for (int j = i + 1; j <= n; j++)
str.append(j+" ");
System.out.println(str);
}
}
}
}
| Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | 8ecea9033a07239ab190f610eaac29e9 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.util.*;
import java.io.*;
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.hasMoreTokens()){
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str="";
try {
str=br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
static int[] string_to_array(String[] arr){
int[] ans=new int[arr.length];
for(int i=0;i<arr.length;i++){
ans[i]=Integer.parseInt(arr[i]);
}
return ans;
}
static int binarySearch(int arr[], int num){
int l = 0, r = arr.length-1;
int mid = 0;
while(l <= r){
mid = l + (r-l)/2;
if(arr[mid] == num){
return mid;
}
//Item presents in the left SubArray
if(num < arr[mid]){
r = mid - 1;
}
//Item presents in the right SubArray
if(num > arr[mid]){
l = mid + 1;
}
}
return l;
}
public static void main(String[] args) {
try
{
FastReader in=new FastReader();
FastWriter out = new FastWriter();
int testCases=Integer.parseInt(in.nextLine());
// List<String>answer=new ArrayList<>();
while(testCases-- > 0)
{
int n=Integer.parseInt(in.nextLine());
//int[] arr=string_to_array(in.nextLine().split(" "));
int[] arr=new int[n+1];
out.println(n);
for(int i=1;i<=n;i++)
{
arr[i]=i;
out.print(i+" ");
}
out.println("");
for(int i=n;i>=2;i--)
{
int k=arr[i];
arr[i]=arr[i-1];
arr[i-1]=k;
for(int j=1;j<=n;j++)
{
out.print(arr[j]+" ");
}
out.println("");
}
}
// for(String s:answer){
// out.println(s);
// }
out.close();
} catch (Exception e) {
return;
}
}
}
| Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | d0f2865ed6ca628153cc85ec0001d3e2 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class PermutationChain {
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[] arsg)
{
FastReader s=new FastReader();
int T=s.nextInt();
while(T-->0)
{
int N=s.nextInt();
int a[]=new int[N];
System.out.println(N);
for(int i=0;i<N;i++)
{
a[i]=i+1;
System.out.print((i+1)+" ");
}
int i=N-1;
/*ArrayList<int[]> ar=new ArrayList<int[]>();*/
LinkedList<String> ar=new LinkedList<String>();
while(i>0)
{
int temp=a[i];
a[i]=a[i-1];
a[i-1]=temp;
//System.out.println(Arrays.toString(a));
ar.add(Arrays.toString(a));
/*System.out.println();
for(int k=0;k<N;k++)
{
System.out.print(a[k]+" ");
}*/
i--;
}
System.out.println();
for(String j:ar)
{
j=j.replace("[","");
j=j.replace("]","");
j=j.replace(" ","");
j=j.replace(","," ");
System.out.println(j.trim());
}
/*for(int j[]: ar)
{
System.out.println();
for(int k:j)
{
System.out.print(k+" ");
}
}*/
}
}
}
| Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | 736027d7e327980bf0485c5880072dc6 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Test {
public static void main(String[] args) throws IOException {
Reader rd = new Reader();
int t = rd.nextInt();
while (t-- > 0) {
int n = rd.nextInt();
int[] arr = new int[n];
System.out.println(n);
StringBuilder str = new StringBuilder();
for (int i = 0; i < n; i++) {
arr[i] = i + 1;
str.append(arr[i] + " ");
}
System.out.println(str);
for (int i = 1; i < n; i++) {
StringBuilder str1 = new StringBuilder();
arr[i - 1] = arr[i];
arr[i] = 1;
int half = n / 2;
for (int j = 0; j < half; j++) {
str1.append(arr[j] + " ");
}
for (int j = half; j < n; j++) {
str1.append(arr[j] + " ");
}
System.out.println(str1);
}
}
rd.close();
}
/**
* method to print int value in console output
**/
private static void debug(int value) {
if (System.getProperty("ONLINE_JUDGE") == null) {
System.out.println("int value = " + value);
}
}
/**
* method to print int value in console output with a text message
**/
private static void debug(int value, String message) {
if (System.getProperty("ONLINE_JUDGE") == null) {
if (message.charAt(message.length() - 1) != ' ') message += " ";
System.out.println(message + "" + value);
}
}
/**
* method to print long value in console output
**/
private static void debug(long value) {
if (System.getProperty("ONLINE_JUDGE") == null) {
System.out.println("long value = " + value);
}
}
/**
* method to print long value in console output with a text message
**/
private static void debug(long value, String message) {
if (System.getProperty("ONLINE_JUDGE") == null) {
if (message.charAt(message.length() - 1) != ' ') message += " ";
System.out.println(message + "" + value);
}
}
/**
* method to print String value in console output
**/
private static void debug(String value) {
if (System.getProperty("ONLINE_JUDGE") == null) {
System.out.println("String value = " + value);
}
}
/**
* method to print String value in console output with a text message
**/
private static void debug(String value, String message) {
if (System.getProperty("ONLINE_JUDGE") == null) {
if (message.charAt(message.length() - 1) != ' ') message += " ";
System.out.println(message + "" + value);
}
}
/**
* method to print character value in console output
**/
private static void debug(char value) {
if (System.getProperty("ONLINE_JUDGE") == null) {
System.out.println("Character value = " + value);
}
}
/**
* method to print character value in console output with a text message
**/
private static void debug(char value, String message) {
if (System.getProperty("ONLINE_JUDGE") == null) {
if (message.charAt(message.length() - 1) != ' ') message += " ";
System.out.println(message + "" + value);
}
}
/**
* method to print double value in console output
**/
private static void debug(double value) {
if (System.getProperty("ONLINE_JUDGE") == null) {
System.out.println("Double value = " + value);
}
}
/**
* method to print double value in console output with a text message
**/
private static void debug(double value, String message) {
if (System.getProperty("ONLINE_JUDGE") == null) {
if (message.charAt(message.length() - 1) != ' ') message += " ";
System.out.println(message + "" + value);
}
}
/**
* method to print integer type array value in console output
**/
private static void debug(int[] arr) {
if (System.getProperty("ONLINE_JUDGE") == null) {
int n = arr.length;
System.out.print("[");
for (int i = 0; i < n; i++) {
if (i < n - 1) System.out.print(arr[i] + ", ");
else System.out.print(arr[i]);
}
System.out.println("]");
}
}
/**
* method to print long type array value in console output
**/
private static void debug(long[] arr) {
if (System.getProperty("ONLINE_JUDGE") == null) {
int n = arr.length;
System.out.print("[");
for (int i = 0; i < n; i++) {
if (i < n - 1) System.out.print(arr[i] + ", ");
else System.out.print(arr[i]);
}
System.out.println("]");
}
}
/**
* method to print long type array value in console output
**/
private static void debug(String[] arr) {
if (System.getProperty("ONLINE_JUDGE") == null) {
int n = arr.length;
System.out.print("[");
for (int i = 0; i < n; i++) {
if (i < n - 1) System.out.print(arr[i] + ", ");
else System.out.print(arr[i]);
}
System.out.println("]");
}
}
/**
* method to print char type array value in console output
**/
private static void debug(char[] arr) {
if (System.getProperty("ONLINE_JUDGE") == null) {
int n = arr.length;
System.out.print("[");
for (int i = 0; i < n; i++) {
if (i < n - 1) System.out.print(arr[i] + ", ");
else System.out.print(arr[i]);
}
System.out.println("]");
}
}
/**
* method to print double type array value in console output
**/
private static void debug(double[] arr) {
if (System.getProperty("ONLINE_JUDGE") == null) {
int n = arr.length;
System.out.print("[");
for (int i = 0; i < n; i++) {
if (i < n - 1) System.out.print(arr[i] + ", ");
else System.out.print(arr[i]);
}
System.out.println("]");
}
}
/**
* please ignore the below code as it's just used for
* taking faster input in java
*/
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
} else {
continue;
}
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg) return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null) return;
din.close();
}
}
} | Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | b477f7fc28b813e98d55f4a5f5c19f6c | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Test {
public static void main(String[] args) throws IOException {
Reader rd = new Reader();
int t = rd.nextInt();
while (t-- > 0) {
int n = rd.nextInt();
int[] arr = new int[n];
System.out.println(n);
for (int i = 1; i <= n; i++) {
StringBuilder strBuilder = new StringBuilder();
for (int j = 2; j <= i; j++) {
strBuilder.append(j + " ");
}
strBuilder.append("1 ");
for (int j = i + 1; j <= n; j++) {
strBuilder.append(j + " ");
}
System.out.println(strBuilder);
}
}
rd.close();
}
/**
* method to print int value in console output
**/
private static void debug(int value) {
if (System.getProperty("ONLINE_JUDGE") == null) {
System.out.println("int value = " + value);
}
}
/**
* method to print int value in console output with a text message
**/
private static void debug(int value, String message) {
if (System.getProperty("ONLINE_JUDGE") == null) {
if (message.charAt(message.length() - 1) != ' ') message += " ";
System.out.println(message + "" + value);
}
}
/**
* method to print long value in console output
**/
private static void debug(long value) {
if (System.getProperty("ONLINE_JUDGE") == null) {
System.out.println("long value = " + value);
}
}
/**
* method to print long value in console output with a text message
**/
private static void debug(long value, String message) {
if (System.getProperty("ONLINE_JUDGE") == null) {
if (message.charAt(message.length() - 1) != ' ') message += " ";
System.out.println(message + "" + value);
}
}
/**
* method to print String value in console output
**/
private static void debug(String value) {
if (System.getProperty("ONLINE_JUDGE") == null) {
System.out.println("String value = " + value);
}
}
/**
* method to print String value in console output with a text message
**/
private static void debug(String value, String message) {
if (System.getProperty("ONLINE_JUDGE") == null) {
if (message.charAt(message.length() - 1) != ' ') message += " ";
System.out.println(message + "" + value);
}
}
/**
* method to print character value in console output
**/
private static void debug(char value) {
if (System.getProperty("ONLINE_JUDGE") == null) {
System.out.println("Character value = " + value);
}
}
/**
* method to print character value in console output with a text message
**/
private static void debug(char value, String message) {
if (System.getProperty("ONLINE_JUDGE") == null) {
if (message.charAt(message.length() - 1) != ' ') message += " ";
System.out.println(message + "" + value);
}
}
/**
* method to print double value in console output
**/
private static void debug(double value) {
if (System.getProperty("ONLINE_JUDGE") == null) {
System.out.println("Double value = " + value);
}
}
/**
* method to print double value in console output with a text message
**/
private static void debug(double value, String message) {
if (System.getProperty("ONLINE_JUDGE") == null) {
if (message.charAt(message.length() - 1) != ' ') message += " ";
System.out.println(message + "" + value);
}
}
/**
* method to print integer type array value in console output
**/
private static void debug(int[] arr) {
if (System.getProperty("ONLINE_JUDGE") == null) {
int n = arr.length;
System.out.print("[");
for (int i = 0; i < n; i++) {
if (i < n - 1) System.out.print(arr[i] + ", ");
else System.out.print(arr[i]);
}
System.out.println("]");
}
}
/**
* method to print long type array value in console output
**/
private static void debug(long[] arr) {
if (System.getProperty("ONLINE_JUDGE") == null) {
int n = arr.length;
System.out.print("[");
for (int i = 0; i < n; i++) {
if (i < n - 1) System.out.print(arr[i] + ", ");
else System.out.print(arr[i]);
}
System.out.println("]");
}
}
/**
* method to print long type array value in console output
**/
private static void debug(String[] arr) {
if (System.getProperty("ONLINE_JUDGE") == null) {
int n = arr.length;
System.out.print("[");
for (int i = 0; i < n; i++) {
if (i < n - 1) System.out.print(arr[i] + ", ");
else System.out.print(arr[i]);
}
System.out.println("]");
}
}
/**
* method to print char type array value in console output
**/
private static void debug(char[] arr) {
if (System.getProperty("ONLINE_JUDGE") == null) {
int n = arr.length;
System.out.print("[");
for (int i = 0; i < n; i++) {
if (i < n - 1) System.out.print(arr[i] + ", ");
else System.out.print(arr[i]);
}
System.out.println("]");
}
}
/**
* method to print double type array value in console output
**/
private static void debug(double[] arr) {
if (System.getProperty("ONLINE_JUDGE") == null) {
int n = arr.length;
System.out.print("[");
for (int i = 0; i < n; i++) {
if (i < n - 1) System.out.print(arr[i] + ", ");
else System.out.print(arr[i]);
}
System.out.println("]");
}
}
/**
* please ignore the below code as it's just used for
* taking faster input in java
*/
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
} else {
continue;
}
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg) return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null) return;
din.close();
}
}
} | Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | 4ed505a3328222902cee708d612621e1 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes |
import java.io.*;
import java.util.*;
public class oop{
static PrintWriter pw;
public static void main(String[] args) throws Exception {
pw = new PrintWriter(System.out);
Reader sc = new Reader();
StringBuffer sb = new StringBuffer();
int t = sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int[] res = new int[n];
for(int i=0 ;i<n ; i++){
res[i]=i+1;
}
pw.println(n);
print(res);
for(int i=0 ; i<n-1 ; i++){
int temp=res[i];
res[i]=res[i+1];
res[i+1]=temp;
print(res);
}
}
pw.close();
}
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 e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
if(st.hasMoreTokens())
str = st.nextToken("\n");
else
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static void print(long i) {
System.out.println(i);
}
static void print(Object o) {
System.out.println(o);
}
static void print(int a[]) {
for(int i:a) {
pw.print(i+" ");
}
pw.println();
}
static void print(long a[]) {
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(ArrayList<Long> a) {
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(Object a[]) {
for(Object i:a) {
System.out.print(i+" ");
}
System.out.println();
}
} | Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | 7c7ac6ebc18595f102f59cc924ec9ab4 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes |
import java.io.*;
import java.util.*;
public class oop{
public static void main(String[] args) throws Exception {
Reader sc = new Reader();
StringBuffer sb = new StringBuffer();
int t = sc.nextInt();
while(t-->0){
int n=sc.nextInt();
sb.append(n + "\n");
int[] res = new int[n];
for(int i=0 ;i<n ; i++){
res[i]=i+1;
sb.append(res[i] + " ");
}
sb.append("\n");
for(int i=0 ; i<n-1 ; i++){
int temp=res[i];
res[i]=res[i+1];
res[i+1]=temp;
for(int j:res){
sb.append(j+" ");
}
sb.append("\n");
}
}
System.out.println(sb);
}
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 e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
if(st.hasMoreTokens())
str = st.nextToken("\n");
else
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | a0729bec8ecb332a8b7a543896b8c9d6 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.util.*;
import java.io.*;
import java.io.PrintWriter;
public class Solution {
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public String next() {
while (!st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
byte nextByte() {
return Byte.parseByte(next());
}
short nextShort() {
return Short.parseShort(next());
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return java.lang.Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static void main(String[] args){
// FastScanner in = new FastScanner();
// System.out.println("hello world");
Scanner scn = new Scanner(System.in);
int t = Integer.parseInt(scn.nextLine());
PrintWriter o = new PrintWriter(System.out);
while(t > 0){
t--;
int n = Integer.parseInt(scn.nextLine());
int[] arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = i + 1;
}
o.println(n);
for(int i = 0; i < arr.length; i++){
o.print(arr[i] + " ");
}
o.println();
for(int i = arr.length - 2; i >= 0; i--){
swap(arr, i, i+1);
for(int j = 0; j < arr.length; j++){
o.print(arr[j] + " ");
}
o.println();
}
}
o.close();
}
public static void swap(int[] arr, int i, int j){
arr[i] = (arr[i] + arr[j]) - (arr[j] = arr[i]);
}
}
| Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | 1527bd8ba9d3a841ba575128ef80bbd1 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.util.*;
import java.io.*;
import java.io.PrintWriter;
public class Solution {
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public String next() {
while (!st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
byte nextByte() {
return Byte.parseByte(next());
}
short nextShort() {
return Short.parseShort(next());
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return java.lang.Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static void main(String[] args){
FastScanner in = new FastScanner();
// System.out.println("hello world");
Scanner scn = new Scanner(System.in);
int t = Integer.parseInt(scn.nextLine());
PrintWriter o = new PrintWriter(System.out);
while(t > 0){
t--;
int n = Integer.parseInt(scn.nextLine());
int[] arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = i + 1;
}
o.println(n);
for(int i = 0; i < arr.length; i++){
o.print(arr[i] + " ");
}
o.println();
for(int i = arr.length - 2; i >= 0; i--){
swap(arr, i, i+1);
for(int j = 0; j < arr.length; j++){
o.print(arr[j] + " ");
}
o.println();
}
}
o.close();
}
public static void swap(int[] arr, int i, int j){
arr[i] = (arr[i] + arr[j]) - (arr[j] = arr[i]);
}
}
| Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | b4f1a8911b277ec994c89c888655cb26 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class B_Permutation_Chain {
static Scanner in = new Scanner();
static PrintWriter out = new PrintWriter(System.out);
static StringBuilder ans = new StringBuilder();
static int testCases, n, m;
static long a[], b[];
static void solve(int t) {
a = new long[n];
for (int i = 0; i < n; ++i) {
a[i] = i + 1;
}
//ans.append(n).append("\n");
int index = 1;
ArrayList1<long[]> list = new ArrayList1<>();
for (int j = 0; j < m; ++j) {
b = new long[n];
for(int i = 0; i < n; ++i) b[i] = a[i];
list.add(b);
//ans.append("\n");
if (index >= n) {
break;
} else {
swap(a, index, index - 1);
//print(a);
++index;
}
//++index;
}
ans.append(list.size()).append("\n");
while (!list.isEmpty()) {
long now_the_array_a[] = list.get(0);
//if(list.size() > 1) print(list.get(1));
for (long i : now_the_array_a) {
ans.append(i).append(" ");
}
ans.append("\n");
list.popFront();
}
}
public static void main(String[] Priya) throws IOException {
testCases = in.nextInt();
for (int t = 0; t < testCases; ++t) {
n = in.nextInt();
m = n;
solve(t + 1);
}
in.close();
out.print(ans.toString());
out.flush();
}
static void print(long a[]) {
for(long i : a) {
out.print(i + " ");
out.flush();
}
out.println();
out.flush();
}
static int search(long a[], long x, int last) {
int i = 0, j = last;
while (i <= j) {
int mid = i + (j - i) / 2;
if (a[mid] == x) {
return mid;
}
if (a[mid] < x) {
i = mid + 1;
} else {
j = mid - 1;
}
}
return -1;
}
static void swap(long a[], int i, int j) {
long temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void reverse(long a[]) {
n = a.length;
for (int i = 0; i < n / 2; ++i) {
swap(a, i, n - i - 1);
}
}
static long max(long a[], int i, int n, long max) {
if (i > n) {
return max;
}
max = Math.max(a[i], max);
return max(a, i + 1, n, max);
}
static long min(long a[], int i, int n, long max) {
if (i > n) {
return max;
}
max = Math.min(a[i], max);
return max(a, i + 1, n, max);
}
static void printArray(long a[]) {
for (long i : a) {
System.out.print(i + " ");
}
System.out.println();
}
static boolean isSmaller(String str1, String str2) {
int n1 = str1.length(), n2 = str2.length();
if (n1 < n2) {
return true;
}
if (n2 < n1) {
return false;
}
for (int i = 0; i < n1; i++) {
if (str1.charAt(i) < str2.charAt(i)) {
return true;
} else if (str1.charAt(i) > str2.charAt(i)) {
return false;
}
}
return false;
}
static String sub(String str1, String str2) {
if (isSmaller(str1, str2)) {
String t = str1;
str1 = str2;
str2 = t;
}
String str = "";
int n1 = str1.length(), n2 = str2.length();
int diff = n1 - n2;
int carry = 0;
for (int i = n2 - 1; i >= 0; i--) {
int sub
= (((int) str1.charAt(i + diff) - (int) '0')
- ((int) str2.charAt(i) - (int) '0')
- carry);
if (sub < 0) {
sub = sub + 10;
carry = 1;
} else {
carry = 0;
}
str += String.valueOf(sub);
}
for (int i = n1 - n2 - 1; i >= 0; i--) {
if (str1.charAt(i) == '0' && carry > 0) {
str += "9";
continue;
}
int sub = (((int) str1.charAt(i) - (int) '0')
- carry);
if (i > 0 || sub > 0) {
str += String.valueOf(sub);
}
carry = 0;
}
return new StringBuilder(str).reverse().toString();
}
static String sum(String str1, String str2) {
if (str1.length() > str2.length()) {
String t = str1;
str1 = str2;
str2 = t;
}
String str = "";
int n1 = str1.length(), n2 = str2.length();
int diff = n2 - n1;
int carry = 0;
for (int i = n1 - 1; i >= 0; i--) {
int sum = ((int) (str1.charAt(i) - '0')
+ (int) (str2.charAt(i + diff) - '0') + carry);
str += (char) (sum % 10 + '0');
carry = sum / 10;
}
for (int i = n2 - n1 - 1; i >= 0; i--) {
int sum = ((int) (str2.charAt(i) - '0') + carry);
str += (char) (sum % 10 + '0');
carry = sum / 10;
}
if (carry > 0) {
str += (char) (carry + '0');
}
return new StringBuilder(str).reverse().toString();
}
static long detect_sum(int i, long a[], long sum) {
if (i >= a.length) {
return sum;
}
return detect_sum(i + 1, a, sum + a[i]);
}
static String mul(String num1, String num2) {
int len1 = num1.length();
int len2 = num2.length();
if (len1 == 0 || len2 == 0) {
return "0";
}
int result[] = new int[len1 + len2];
int i_n1 = 0;
int i_n2 = 0;
for (int i = len1 - 1; i >= 0; i--) {
int carry = 0;
int n1 = num1.charAt(i) - '0';
i_n2 = 0;
for (int j = len2 - 1; j >= 0; j--) {
int n2 = num2.charAt(j) - '0';
int sum = n1 * n2 + result[i_n1 + i_n2] + carry;
carry = sum / 10;
result[i_n1 + i_n2] = sum % 10;
i_n2++;
}
if (carry > 0) {
result[i_n1 + i_n2] += carry;
}
i_n1++;
}
int i = result.length - 1;
while (i >= 0 && result[i] == 0) {
i--;
}
if (i == -1) {
return "0";
}
String s = "";
while (i >= 0) {
s += (result[i--]);
}
return s;
}
static class Node<T> {
T data;
Node<T> next;
public Node() {
this.next = null;
}
public Node(T data) {
this.data = data;
this.next = null;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node<T> getNext() {
return next;
}
public void setNext(Node<T> next) {
this.next = next;
}
@Override
public String toString() {
return this.getData().toString() + " ";
}
}
static class ArrayList1<T> {
Node<T> head, tail;
int len;
public ArrayList1() {
this.head = null;
this.tail = null;
this.len = 0;
}
int size() {
return len;
}
boolean isEmpty() {
return len == 0;
}
int indexOf(T data) {
if (isEmpty()) {
throw new ArrayIndexOutOfBoundsException();
}
Node<T> temp = head;
int index = -1, i = 0;
while (temp != null) {
if (temp.getData() == data) {
index = i;
}
i++;
temp = temp.getNext();
}
return index;
}
void add(T data) {
Node<T> newNode = new Node<>(data);
if (isEmpty()) {
head = newNode;
tail = newNode;
len++;
} else {
tail.setNext(newNode);
tail = newNode;
len++;
}
}
void see() {
if (isEmpty()) {
throw new ArrayIndexOutOfBoundsException();
}
Node<T> temp = head;
while (temp != null) {
System.out.print(temp.getData().toString() + " ");
//out.flush();
temp = temp.getNext();
}
System.out.println();
//out.flush();
}
void inserFirst(T data) {
Node<T> newNode = new Node<>(data);
Node<T> temp = head;
if (isEmpty()) {
head = newNode;
tail = newNode;
len++;
} else {
newNode.setNext(temp);
head = newNode;
len++;
}
}
T get(int index) {
if (isEmpty() || index >= len) {
throw new ArrayIndexOutOfBoundsException();
}
if (index == 0) {
return head.getData();
}
Node<T> temp = head;
int i = 0;
T data = null;
while (temp != null) {
if (i == index) {
data = temp.getData();
}
i++;
temp = temp.getNext();
}
return data;
}
void addAt(T data, int index) {
if (index >= len) {
throw new ArrayIndexOutOfBoundsException();
}
Node<T> newNode = new Node<>(data);
int i = 0;
Node<T> temp = head;
while (temp.next != null) {
if (i == index) {
newNode.setNext(temp.next);
temp.next = newNode;
}
i++;
temp = temp.getNext();
}
// temp.setNext(temp);
len++;
}
void popFront() {
if (isEmpty()) {
//return;
throw new ArrayIndexOutOfBoundsException();
}
if (head == tail) {
head = null;
tail = null;
} else {
head = head.getNext();
}
len--;
}
void removeAt(int index) {
if (index >= len) {
throw new ArrayIndexOutOfBoundsException();
}
if (index == 0) {
this.popFront();
return;
}
Node<T> temp = head;
int i = 0;
Node<T> n = new Node<>();
while (temp != null) {
if (i == index) {
n.next = temp.next;
temp.next = n;
break;
}
i++;
n = temp;
temp = temp.getNext();
}
tail = n;
--len;
}
void clearAll() {
this.head = null;
this.tail = null;
}
}
static void merge(long a[], int left, int right, int mid) {
int n1 = mid - left + 1, n2 = right - mid;
long L[] = new long[n1];
long R[] = new long[n2];
for (int i = 0; i < n1; i++) {
L[i] = a[left + i];
}
for (int i = 0; i < n2; i++) {
R[i] = a[mid + 1 + i];
}
int i = 0, j = 0, k1 = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
a[k1] = L[i];
i++;
} else {
a[k1] = R[j];
j++;
}
k1++;
}
while (i < n1) {
a[k1] = L[i];
i++;
k1++;
}
while (j < n2) {
a[k1] = R[j];
j++;
k1++;
}
}
static void sort(long a[], int left, int right) {
if (left >= right) {
return;
}
int mid = (left + right) / 2;
sort(a, left, mid);
sort(a, mid + 1, right);
merge(a, left, right, mid);
}
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();
}
String nextLine() throws IOException {
return in.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
void close() throws IOException {
in.close();
}
}
}
| Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | 18286a638fed91927d220b7621b8dae1 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class B {
static Scanner in = new Scanner();
static PrintWriter out = new PrintWriter(System.out);
static StringBuilder ans = new StringBuilder();
static int testCases, n, m;
static long a[], b[];
static void solve(int t) {
a = new long[n];
for (int i = 0; i < n; ++i) {
a[i] = i + 1;
}
//ans.append(n).append("\n");
int index = 1;
ArrayList1<long[]> list = new ArrayList1<>();
for (int j = 0; j < m; ++j) {
b = new long[n];
for(int i = 0; i < n; ++i) b[i] = a[i];
list.add(b);
//ans.append("\n");
if (index >= n) {
break;
} else {
swap(a, index, 0);
//print(a);
++index;
}
//++index;
}
ans.append(list.size()).append("\n");
while (!list.isEmpty()) {
long now_the_array_a[] = list.get(0);
//if(list.size() > 1) print(list.get(1));
for (long i : now_the_array_a) {
ans.append(i).append(" ");
}
ans.append("\n");
list.popFront();
}
}
public static void main(String[] Priya) throws IOException {
testCases = in.nextInt();
for (int t = 0; t < testCases; ++t) {
n = in.nextInt();
m = n;
solve(t + 1);
}
in.close();
out.print(ans.toString());
out.flush();
}
static void print(long a[]) {
for(long i : a) {
out.print(i + " ");
out.flush();
}
out.println();
out.flush();
}
static int search(long a[], long x, int last) {
int i = 0, j = last;
while (i <= j) {
int mid = i + (j - i) / 2;
if (a[mid] == x) {
return mid;
}
if (a[mid] < x) {
i = mid + 1;
} else {
j = mid - 1;
}
}
return -1;
}
static void swap(long a[], int i, int j) {
long temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void reverse(long a[]) {
n = a.length;
for (int i = 0; i < n / 2; ++i) {
swap(a, i, n - i - 1);
}
}
static long max(long a[], int i, int n, long max) {
if (i > n) {
return max;
}
max = Math.max(a[i], max);
return max(a, i + 1, n, max);
}
static long min(long a[], int i, int n, long max) {
if (i > n) {
return max;
}
max = Math.min(a[i], max);
return max(a, i + 1, n, max);
}
static void printArray(long a[]) {
for (long i : a) {
System.out.print(i + " ");
}
System.out.println();
}
static boolean isSmaller(String str1, String str2) {
int n1 = str1.length(), n2 = str2.length();
if (n1 < n2) {
return true;
}
if (n2 < n1) {
return false;
}
for (int i = 0; i < n1; i++) {
if (str1.charAt(i) < str2.charAt(i)) {
return true;
} else if (str1.charAt(i) > str2.charAt(i)) {
return false;
}
}
return false;
}
static String sub(String str1, String str2) {
if (isSmaller(str1, str2)) {
String t = str1;
str1 = str2;
str2 = t;
}
String str = "";
int n1 = str1.length(), n2 = str2.length();
int diff = n1 - n2;
int carry = 0;
for (int i = n2 - 1; i >= 0; i--) {
int sub
= (((int) str1.charAt(i + diff) - (int) '0')
- ((int) str2.charAt(i) - (int) '0')
- carry);
if (sub < 0) {
sub = sub + 10;
carry = 1;
} else {
carry = 0;
}
str += String.valueOf(sub);
}
for (int i = n1 - n2 - 1; i >= 0; i--) {
if (str1.charAt(i) == '0' && carry > 0) {
str += "9";
continue;
}
int sub = (((int) str1.charAt(i) - (int) '0')
- carry);
if (i > 0 || sub > 0) {
str += String.valueOf(sub);
}
carry = 0;
}
return new StringBuilder(str).reverse().toString();
}
static String sum(String str1, String str2) {
if (str1.length() > str2.length()) {
String t = str1;
str1 = str2;
str2 = t;
}
String str = "";
int n1 = str1.length(), n2 = str2.length();
int diff = n2 - n1;
int carry = 0;
for (int i = n1 - 1; i >= 0; i--) {
int sum = ((int) (str1.charAt(i) - '0')
+ (int) (str2.charAt(i + diff) - '0') + carry);
str += (char) (sum % 10 + '0');
carry = sum / 10;
}
for (int i = n2 - n1 - 1; i >= 0; i--) {
int sum = ((int) (str2.charAt(i) - '0') + carry);
str += (char) (sum % 10 + '0');
carry = sum / 10;
}
if (carry > 0) {
str += (char) (carry + '0');
}
return new StringBuilder(str).reverse().toString();
}
static long detect_sum(int i, long a[], long sum) {
if (i >= a.length) {
return sum;
}
return detect_sum(i + 1, a, sum + a[i]);
}
static String mul(String num1, String num2) {
int len1 = num1.length();
int len2 = num2.length();
if (len1 == 0 || len2 == 0) {
return "0";
}
int result[] = new int[len1 + len2];
int i_n1 = 0;
int i_n2 = 0;
for (int i = len1 - 1; i >= 0; i--) {
int carry = 0;
int n1 = num1.charAt(i) - '0';
i_n2 = 0;
for (int j = len2 - 1; j >= 0; j--) {
int n2 = num2.charAt(j) - '0';
int sum = n1 * n2 + result[i_n1 + i_n2] + carry;
carry = sum / 10;
result[i_n1 + i_n2] = sum % 10;
i_n2++;
}
if (carry > 0) {
result[i_n1 + i_n2] += carry;
}
i_n1++;
}
int i = result.length - 1;
while (i >= 0 && result[i] == 0) {
i--;
}
if (i == -1) {
return "0";
}
String s = "";
while (i >= 0) {
s += (result[i--]);
}
return s;
}
static class Node<T> {
T data;
Node<T> next;
public Node() {
this.next = null;
}
public Node(T data) {
this.data = data;
this.next = null;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node<T> getNext() {
return next;
}
public void setNext(Node<T> next) {
this.next = next;
}
@Override
public String toString() {
return this.getData().toString() + " ";
}
}
static class ArrayList1<T> {
Node<T> head, tail;
int len;
public ArrayList1() {
this.head = null;
this.tail = null;
this.len = 0;
}
int size() {
return len;
}
boolean isEmpty() {
return len == 0;
}
int indexOf(T data) {
if (isEmpty()) {
throw new ArrayIndexOutOfBoundsException();
}
Node<T> temp = head;
int index = -1, i = 0;
while (temp != null) {
if (temp.getData() == data) {
index = i;
}
i++;
temp = temp.getNext();
}
return index;
}
void add(T data) {
Node<T> newNode = new Node<>(data);
if (isEmpty()) {
head = newNode;
tail = newNode;
len++;
} else {
tail.setNext(newNode);
tail = newNode;
len++;
}
}
void see() {
if (isEmpty()) {
throw new ArrayIndexOutOfBoundsException();
}
Node<T> temp = head;
while (temp != null) {
System.out.print(temp.getData().toString() + " ");
//out.flush();
temp = temp.getNext();
}
System.out.println();
//out.flush();
}
void inserFirst(T data) {
Node<T> newNode = new Node<>(data);
Node<T> temp = head;
if (isEmpty()) {
head = newNode;
tail = newNode;
len++;
} else {
newNode.setNext(temp);
head = newNode;
len++;
}
}
T get(int index) {
if (isEmpty() || index >= len) {
throw new ArrayIndexOutOfBoundsException();
}
if (index == 0) {
return head.getData();
}
Node<T> temp = head;
int i = 0;
T data = null;
while (temp != null) {
if (i == index) {
data = temp.getData();
}
i++;
temp = temp.getNext();
}
return data;
}
void addAt(T data, int index) {
if (index >= len) {
throw new ArrayIndexOutOfBoundsException();
}
Node<T> newNode = new Node<>(data);
int i = 0;
Node<T> temp = head;
while (temp.next != null) {
if (i == index) {
newNode.setNext(temp.next);
temp.next = newNode;
}
i++;
temp = temp.getNext();
}
// temp.setNext(temp);
len++;
}
void popFront() {
if (isEmpty()) {
//return;
throw new ArrayIndexOutOfBoundsException();
}
if (head == tail) {
head = null;
tail = null;
} else {
head = head.getNext();
}
len--;
}
void removeAt(int index) {
if (index >= len) {
throw new ArrayIndexOutOfBoundsException();
}
if (index == 0) {
this.popFront();
return;
}
Node<T> temp = head;
int i = 0;
Node<T> n = new Node<>();
while (temp != null) {
if (i == index) {
n.next = temp.next;
temp.next = n;
break;
}
i++;
n = temp;
temp = temp.getNext();
}
tail = n;
--len;
}
void clearAll() {
this.head = null;
this.tail = null;
}
}
static void merge(long a[], int left, int right, int mid) {
int n1 = mid - left + 1, n2 = right - mid;
long L[] = new long[n1];
long R[] = new long[n2];
for (int i = 0; i < n1; i++) {
L[i] = a[left + i];
}
for (int i = 0; i < n2; i++) {
R[i] = a[mid + 1 + i];
}
int i = 0, j = 0, k1 = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
a[k1] = L[i];
i++;
} else {
a[k1] = R[j];
j++;
}
k1++;
}
while (i < n1) {
a[k1] = L[i];
i++;
k1++;
}
while (j < n2) {
a[k1] = R[j];
j++;
k1++;
}
}
static void sort(long a[], int left, int right) {
if (left >= right) {
return;
}
int mid = (left + right) / 2;
sort(a, left, mid);
sort(a, mid + 1, right);
merge(a, left, right, mid);
}
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();
}
String nextLine() throws IOException {
return in.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
void close() throws IOException {
in.close();
}
}
}
| Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | 832fec87cb6b2159231d87cc4201f6aa | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.io.PrintWriter;
import java.util.Scanner;
public class Moves {
public static void solve(int n) {
PrintWriter out=new PrintWriter(System.out);
int A[]=new int[n];
for(int i=0;i<n;i++) A[i]=i+1;
System.out.println(n);
for(int i=0;i<n-1;i++) out.print(A[i]+" ");
out.print(A[n-1]+"\n");
int k=1;
while(k<n) {
//System.out.println(j);
int t=A[k];
A[k]=A[k-1];
A[k-1]=t;
for(int i=0;i<n-1;i++) out.print(A[i]+" ");
out.print(A[n-1]+"\n");
k++;
}
out.flush();
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t--!=0) {
int n=sc.nextInt();
solve(n);
}
}
}
| Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | a053242b04e2e95ac2bf3119d677d9f6 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
public class Set_Intersect {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int t=s.nextInt();
StringBuffer s1=new StringBuffer();
while(t-->0)
{
int n=s.nextInt();
int[] a=new int[n];
s1.append(n);
s1.append("\n");
for(int i=0;i<n;i++)
{ a[i]=i+1;
s1.append(a[i]+" ");
}
s1.append("\n");
int k=n;
k--;
if(k>0)
{
int temp=a[n-1];
a[n-1]=a[n-2];
a[n-2]=temp;
for(int i=0;i<n;i++)
s1.append(a[i]+" ");
s1.append("\n");
k--;
}
int j=n-2;
while(k-->0)
{
int temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
for(int i=0;i<n;i++)
s1.append(a[i]+" ");
s1.append("\n");
j--;
}
}
System.out.println(s1);
}
}
| Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | 9bad3d50f523be6a9b689575fc412177 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | //package CP.Codeforces.Div2_4_8_22;
import java.util.*;
import java.io.*;
import java.util.stream.Collectors;
public class Permutation {
static FastReader in=new FastReader();
static final Random random=new Random();
static long mod=1000000007L;
static HashMap<String,Integer>map=new HashMap<>();
public static void main(String args[]) throws IOException {
int t=in.nextInt();
StringBuilder res=new StringBuilder();
int cse=1;
while(t-->0) {
int n = in.nextInt();
List<List<Integer>> list = permutation(n);
res.append(list.size() + "\n");
for(List<Integer> l : list) {
for(int i : l) res.append(i + " ");
res.append("\n");
}
}
print(res);
}
private static List<List<Integer>> permutation(int n) {
List<List<Integer>> res = new ArrayList<>();
int[] arr = new int[n];
for(int i=0; i<n; i++) arr[i] = i+1;
res.add(Arrays.stream(arr)
.boxed()
.collect(Collectors.toList()));
for(int i=0; i<n-1; i++){
swap(arr, i, i+1);
res.add(Arrays.stream(arr)
.boxed()
.collect(Collectors.toList()));
}
return res;
}
private static void swap(int[] arr, int i, int j) {
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
// UTIL functions
static int max(int a, int b) {
if(a<b)
return b;
return a;
}
// Sorts in N log N always
static void sort(int[] a) {
ArrayList<Integer> list = new ArrayList<>();
for(int i : a) list.add(i);
Collections.sort(list); // list sort uses merge sort so always (N logN) unlike Quick sort (worst is O(n2)
for(int i=0; i<a.length; i++) a[i] = list.get(i);
}
static < E > void print(E res) {
System.out.println(res);
}
static int gcd(int a,int b) {
if(b==0) return a;
return gcd(b,a%b);
}
static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
static int abs(int a) {
if(a<0) return -1*a;
return a;
}
// Fast IO
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int [] readIntArray(int n) {
int res [] = new int [n];
for(int i = 0; i<n; i++)res[i] = nextInt();
return res;
}
long [] readLongArray(int n) {
long res [] = new long [n];
for(int i = 0; i<n; i++)res[i] = nextLong();
return res;
}
}
} | Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | cb55f1f828a6f055a2dff6ae93862d75 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.util.*;
import java.io.PrintWriter;
public class PermutationChain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
int[] arr = new int[1000];
PrintWriter out = new PrintWriter(System.out);
while(t-->0) {
int n = sc.nextInt();
for(int i = 0; i < n; i++) {
arr[i] = i+1;
}
out.println(n);
int ind = 0;
while(ind < n) {
for(int j = 0; j < n; j++) {
out.print(arr[j] + " ");
}
out.println();
if(ind < n-1) {
int temp = arr[ind];
arr[ind] = arr[ind+1];
arr[ind+1] = temp;
}
ind++;
}
out.flush();
}
}
}
| Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | 72ae130661f4ad8afe8a70ca9a369565 | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Codechef {
/* FastReader class to work with 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());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int [] iArray(int n) {
int res [] = new int [n];
for(int i = 0; i<n; i++)res[i] = nextInt();
return res;
}
long [] lArray(int n) {
long res [] = new long [n];
for(int i = 0; i<n; i++)res[i] = nextLong();
return res;
}
}
/* UnionFind class to work with disjoint set */
static class UnionFind {
int[] p, rank, setSize;
int numSets;
public UnionFind(int N) {
p = new int[numSets = N];
rank = new int[N];
setSize = new int[N];
for (int i = 0; i < N; i++) {
p[i] = i;
setSize[i] = 1;
}
}
public int findSet(int i) {
return p[i] == i ? i : (p[i] = findSet(p[i]));
}
public boolean isSameSet(int i, int j) {
return findSet(i) == findSet(j);
}
public void unionSet(int i, int j) {
if (isSameSet(i, j))
return;
numSets--;
int x = findSet(i), y = findSet(j);
if (rank[x] > rank[y]) {
p[y] = x;
setSize[x] += setSize[y];
}
else {
p[x] = y;
setSize[y] += setSize[x];
if (rank[x] == rank[y]) rank[y]++;
}
}
public int numDisjointSets() {
return numSets;
}
public int sizeOfSet(int i) {
return setSize[findSet(i)];
}
}
static class Pair {
int a,b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
// this.c = c;
}
}
static class Compare {
static void compare(ArrayList<Pair> al, int n) {
Collections.sort(al, new Comparator<Pair>(){
@Override public int compare(Pair p1, Pair p2) {
return (int)(p1.b-p2.b);
}
});
}
}
static class Compare1 {
static void compare(ArrayList<Pair> al, int n) {
Collections.sort(al, new Comparator<Pair>(){
@Override public int compare(Pair p1, Pair p2) {
return (int)(p1.a-p2.a);
}
});
}
}
static FastReader in = new FastReader();
static final Random random = new Random();
// static int mod = (int) 1e9 + 7;
static long[] fact = new long[16];
/* function to calculate factorial */
static void init() {
fact[0] = 1;
for(int i=1; i<16; i++)
fact[i] = (i*fact[i-1]);
}
/* function to calculate modular exponentiation */
public static long pow(long a, long b, long mod) {
long res = 1;
while (b > 0) {
if ((b & 1) > 0)
res = (res * a) % mod;
b = b >> 1;
a = ((a % mod) * (a % mod)) % mod;
}
return (res % mod + mod) % mod;
}
/* function to check if string is palindrome or not */
public static boolean isPal(String s) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != s.charAt(s.length() - 1 - i))
return false;
}
return true;
}
/* sieveOfEratosthenes algorithm will return ArrayList of prime numbers */
public static ArrayList<Integer> primeSieve(int n) {
ArrayList<Integer> arr=new ArrayList<>();
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
for (int i = 2; i <= n; i++) {
if (prime[i] == true)
arr.add(i);
}
return arr;
}
/* sieveOfEratosthenes algorithm*/
public static void sieveOfEratosthenes(int N , boolean[] prime) {
Arrays.fill(prime, true);
for(int p = 2; p*p <=N; p++) {
if(prime[p] == true) {
// Update all multiples of p
for(int i = p*p; i <= N; i += p)
prime[i] = false;
}
}
}
/* Eculidean algorithm to find gcd of two number */
static int gcd(int a,int b) {
if(b==0) {
return a;
}
return gcd(b,a%b);
}
/* function to calculate lcm of two number */
static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
static long cntSetBit(long num) {
long ans = 0;
while(num>0) {
if(num%2==1)
ans++;
num /= 2;
}
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 < E > void print(E res) {
System.out.println(res);
}
/* function to check whether ArrayList a and b are equal or not */
static boolean isEqual(ArrayList<Integer> a, ArrayList<Integer> b, int n) {
HashSet<Integer> hs = new HashSet<>();
if (a.size() < (n / 2) || b.size() < (n / 2))
return false;
int s1 = 0;
for (int ele : a)
hs.add(ele);
for (int ele : b)
hs.add(ele);
if (hs.size() == n)
return true;
return false;
}
/* Binary Search Algorithm */
private static int binSearch(int l, int r, long val, long[] arr) {
while(l<=r) {
int mid = (l+r)/2;
if(arr[mid]==val)
return mid;
if(arr[mid]>val)
r = mid-1;
if(arr[mid]<val)
l = mid+1;
}
return -1;
}
/* main method */
public static void main (String[] args) throws java.lang.Exception {
int t=in.nextInt();
// int t=1;
while(t-->0)
solve();
/*
ArrayList<Pair> al = new ArrayList<>();
for(int i=0; i<5; i++) {
al.add(new Pair(in.nextLong(), in.nextLong()));
}
for(int i=0; i<5; i++) {
System.out.println(al.get(i).a + " " + al.get(i).b);
}
Compare obj = new Compare();
obj.compare(al, 5);
Arrays.sort(al, (p0, p1) -> Integer.compare(p0.a, p1.a));
for(int i=0; i<5; i++) {
System.out.println(al.get(i).a + " " + al.get(i).b);
}
*/
}
static void solve() {
int n = in.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; i++) {
arr[i] = i+1;
}
StringBuilder ans = new StringBuilder();
for(int i=0; i<n; i++) {
ans.append((i+1)+" ");
}
ans.append("\n");
for(int i=0; i<n-1; i++) {
int tmp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = tmp;
for(int j=0; j<n; j++) {
ans.append(arr[j]+" ");
}
ans.append("\n");
}
System.out.println(n);
System.out.print(ans.toString());
}
} | Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output | |
PASSED | 29a062e8639c883749fc2f025bf198ca | train_109.jsonl | 1659623700 | A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. | 256 megabytes | /*
https://fb.watch/2_f8UL2Jev/ watch this if you are facing rejections
***********************************************
Don't Copy template it might lead to palagarism
***********************************************
I only Believe on will of fire.
*******************************
Work so hard, be so good that luck doesn't get a chance to decide the fate of your dreams.
*****************************************************************************************************
If you want to aim high, aim high Don't let that studying and grades consume you Just live life young.
*****************************************************************
If I'm the sun, you're the moon Because when I go up, you go down
*****************************************
I'm working for the day I will surpass you
****************************************
*/
import java.util.*;
import java.lang.Math;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Shivam {
static long systemTime;
static long mod = 1000000007;
static StringBuilder ans = new StringBuilder();
public static void main(String[] args) {
FastScanner in = new FastScanner();
// Scanner sc = new Scanner(System.in);
int T = in.ni();
// setTime();
while (T-- > 0) {
int N = in.ni();
ans.append(N + "\n");
for (int i = 1; i <= N; i++) {
ans.append(i + " ");
}
ans.append("\n");
for (int i = N - 2; i >= 0; i--) {
for (int j = 1; j <= i; j++)
ans.append(j + " ");
ans.append(N + " ");
for (int j = i + 1; j < N; j++)
ans.append(j + " ");
ans.append("\n");
}
}
in.println(ans);
// printTime();
// printMemory();
}
static class Q {
long at;
public Q(long at) {
this.at = at;
}
}
static void setTime() {
systemTime = System.currentTimeMillis();
}
static void printTime() {
System.err.println("Time consumed: " + (System.currentTimeMillis() - systemTime));
}
static void printMemory() {
System.err.println("Memory consumed: "
+ (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000 + "kb");
}
static class Pair {
int val;
int i;
public Pair(int a, int c) {
this.val = a;
this.i = c;
}
}
static class Sort implements Comparator<Pair> {
public int compare(Pair c1, Pair c2) {
return 0;
}
}
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 ni() {
return Integer.parseInt(next());
}
char nc() {
return next().charAt(0);
}
void println(Object object) {
System.out.println(object);
}
void print(Object object) {
System.out.print(object);
}
String n() {
return next();
}
int[] nia(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
long nl() {
return Long.parseLong(next());
}
}
public int factorial(int n) {
int fact = 1;
int i = 1;
while (i <= n) {
fact *= i;
i++;
}
return fact;
}
public static long gcd(long x, long y) {
if (x % y == 0)
return y;
else
return gcd(y, x % y);
}
public static int gcd(int x, int y) {
if (x % y == 0)
return y;
else
return gcd(y, x % y);
}
public static int abs(int a, int b) {
return (int) Math.abs(a - b);
}
public static long abs(long a, long b) {
return (long) Math.abs(a - b);
}
public static int max(int a, int b) {
if (a > b)
return a;
else
return b;
}
public static int min(int a, int b) {
if (a > b)
return b;
else
return a;
}
public static long max(long a, long b) {
if (a > b)
return a;
else
return b;
}
public static long min(long a, long b) {
if (a > b)
return b;
else
return a;
}
public static long pow(long n, long p, long m) {
long result = 1;
if (p == 0)
return 1;
if (p == 1)
return n;
while (p != 0) {
if (p % 2 == 1)
result *= n;
if (result >= m)
result %= m;
p >>= 1;
n *= n;
if (n >= m)
n %= m;
}
return result;
}
public static long pow(long n, long p) {
long result = 1;
if (p == 0)
return 1;
if (p == 1)
return n;
while (p != 0) {
if (p % 2 == 1)
result *= n;
p >>= 1;
n *= n;
}
return result;
}
static long sort(int a[]) {
int n = a.length;
int b[] = new int[n];
return mergeSort(a, b, 0, n - 1);
}
static long mergeSort(int a[], int b[], long left, long right) {
long c = 0;
if (left < right) {
long mid = left + (right - left) / 2;
c = mergeSort(a, b, left, mid);
c += mergeSort(a, b, mid + 1, right);
c += merge(a, b, left, mid + 1, right);
}
return c;
}
static long merge(int a[], int b[], long left, long mid, long right) {
long c = 0;
int i = (int) left;
int j = (int) mid;
int k = (int) left;
while (i <= (int) mid - 1 && j <= (int) right) {
if (a[i] <= a[j]) {
b[k++] = a[i++];
} else {
b[k++] = a[j++];
c += mid - i;
}
}
while (i <= (int) mid - 1)
b[k++] = a[i++];
while (j <= (int) right)
b[k++] = a[j++];
for (i = (int) left; i <= (int) right; i++)
a[i] = b[i];
return c;
}
static class InputReader extends BufferedReader {
public InputReader(InputStream st) {
super(new InputStreamReader(st));
}
public String readLine() {
try {
return super.readLine();
} catch (IOException e) {
return null;
}
}
private int readByte() {
try {
return read();
} catch (IOException e) {
throw new RuntimeException();
}
}
public int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
}
} | Java | ["2\n\n2\n\n3"] | 2 seconds | ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | 02bdd12987af6e666d4283f075f73725 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain. | 800 | For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.