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 | 7bb568e337a6b1211315cf39a206ed0e | train_002.jsonl | 1281970800 | A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? | 256 megabytes | import java.util.*;
public class Practica_1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String ln = in.next();
int a = 0, b = 0;
for (int j = 0; j < ln.length(); j++) {
if (ln.charAt(j) == '(') {
a++;
b++;
}
if (ln.charAt(j) == ')' && a > 0) {
b++;
a--;
}
}
System.out.println(b - a);
}
}
| Java | ["(()))(", "((()())"] | 5 seconds | ["4", "6"] | null | Java 7 | standard input | [
"greedy"
] | 2ce2d0c4ac5e630bafad2127a1b589dd | Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. | 1,400 | Output the maximum possible length of a regular bracket sequence. | standard output | |
PASSED | 68d33958156786cfb0549fe41b504d66 | train_002.jsonl | 1281970800 | A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? | 256 megabytes |
import java.util.*;
public class Practica_1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String ln = in.next();
int a = 0, b = 0;
for (int j = 0; j < ln.length(); j++) {
if (ln.charAt(j) == '(') {
a++;
b++;
}
if (ln.charAt(j) == ')' && a > 0) {
b++;
a--;
}
}
System.out.println(b - a);
}
}
| Java | ["(()))(", "((()())"] | 5 seconds | ["4", "6"] | null | Java 7 | standard input | [
"greedy"
] | 2ce2d0c4ac5e630bafad2127a1b589dd | Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. | 1,400 | Output the maximum possible length of a regular bracket sequence. | standard output | |
PASSED | 0a81799e7e7994ea8d0625c356865723 | train_002.jsonl | 1281970800 | A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? | 256 megabytes | import java.util.*;
import java.io.*;
import java.awt.Point;
import java.math.BigDecimal;
import java.math.BigInteger;
import static java.lang.Math.*;
// Solution is at the bottom of code
public class B implements Runnable{
final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
BufferedReader in;
OutputWriter out;
StringTokenizer tok = new StringTokenizer("");
public static void main(String[] args){
new Thread(null, new B(), "", 128 * (1L << 20)).start();
}
/////////////////////////////////////////////////////////////////////
void init() throws FileNotFoundException{
Locale.setDefault(Locale.US);
if (ONLINE_JUDGE){
in = new BufferedReader(new InputStreamReader(System.in));
out = new OutputWriter(System.out);
}else{
in = new BufferedReader(new FileReader("input.txt"));
out = new OutputWriter("output.txt");
}
}
////////////////////////////////////////////////////////////////
long timeBegin, timeEnd;
void time(){
timeEnd = System.currentTimeMillis();
System.err.println("Time = " + (timeEnd - timeBegin));
}
void debug(Object... objects){
if (ONLINE_JUDGE){
for (Object o: objects){
System.err.println(o.toString());
}
}
}
/////////////////////////////////////////////////////////////////////
public void run(){
try{
timeBegin = System.currentTimeMillis();
Locale.setDefault(Locale.US);
init();
solve();
out.close();
time();
}catch (Exception e){
e.printStackTrace(System.err);
System.exit(-1);
}
}
/////////////////////////////////////////////////////////////////////
String delim = " ";
String readString() throws IOException{
while(!tok.hasMoreTokens()){
try{
tok = new StringTokenizer(in.readLine());
}catch (Exception e){
return null;
}
}
return tok.nextToken(delim);
}
String readLine() throws IOException{
return in.readLine();
}
/////////////////////////////////////////////////////////////////
final char NOT_A_SYMBOL = '\0';
char readChar() throws IOException{
int intValue = in.read();
if (intValue == -1){
return NOT_A_SYMBOL;
}
return (char) intValue;
}
char[] readCharArray() throws IOException{
return readLine().toCharArray();
}
/////////////////////////////////////////////////////////////////
int readInt() throws IOException {
return Integer.parseInt(readString());
}
int[] readIntArray(int size) throws IOException {
int[] array = new int[size];
for (int index = 0; index < size; ++index){
array[index] = readInt();
}
return array;
}
int[] readIntArrayWithDecrease(int size) throws IOException {
int[] array = readIntArray(size);
for (int i = 0; i < size; ++i) {
array[i]--;
}
return array;
}
///////////////////////////////////////////////////////////////////
long readLong() throws IOException{
return Long.parseLong(readString());
}
long[] readLongArray(int size) throws IOException{
long[] array = new long[size];
for (int index = 0; index < size; ++index){
array[index] = readLong();
}
return array;
}
////////////////////////////////////////////////////////////////////
double readDouble() throws IOException{
return Double.parseDouble(readString());
}
double[] readDoubleArray(int size) throws IOException{
double[] array = new double[size];
for (int index = 0; index < size; ++index){
array[index] = readDouble();
}
return array;
}
////////////////////////////////////////////////////////////////////
BigInteger readBigInteger() throws IOException {
return new BigInteger(readString());
}
BigDecimal readBigDecimal() throws IOException {
return new BigDecimal(readString());
}
/////////////////////////////////////////////////////////////////////
Point readPoint() throws IOException{
int x = readInt();
int y = readInt();
return new Point(x, y);
}
Point[] readPointArray(int size) throws IOException{
Point[] array = new Point[size];
for (int index = 0; index < size; ++index){
array[index] = readPoint();
}
return array;
}
/////////////////////////////////////////////////////////////////////
List<Integer>[] readGraph(int vertexNumber, int edgeNumber)
throws IOException{
@SuppressWarnings("unchecked")
List<Integer>[] graph = new List[vertexNumber];
for (int index = 0; index < vertexNumber; ++index){
graph[index] = new ArrayList<Integer>();
}
while (edgeNumber-- > 0){
int from = readInt() - 1;
int to = readInt() - 1;
graph[from].add(to);
graph[to].add(from);
}
return graph;
}
/////////////////////////////////////////////////////////////////////
static class IntIndexPair {
static Comparator<IntIndexPair> increaseComparator = new Comparator<B.IntIndexPair>() {
@Override
public int compare(IntIndexPair indexPair1, IntIndexPair indexPair2) {
int value1 = indexPair1.value;
int value2 = indexPair2.value;
if (value1 != value2) return value1 - value2;
int index1 = indexPair1.index;
int index2 = indexPair2.index;
return index1 - index2;
}
};
static Comparator<IntIndexPair> decreaseComparator = new Comparator<B.IntIndexPair>() {
@Override
public int compare(IntIndexPair indexPair1, IntIndexPair indexPair2) {
int value1 = indexPair1.value;
int value2 = indexPair2.value;
if (value1 != value2) return -(value1 - value2);
int index1 = indexPair1.index;
int index2 = indexPair2.index;
return index1 - index2;
}
};
int value, index;
public IntIndexPair(int value, int index) {
super();
this.value = value;
this.index = index;
}
public int getRealIndex() {
return index + 1;
}
}
IntIndexPair[] readIntIndexArray(int size) throws IOException {
IntIndexPair[] array = new IntIndexPair[size];
for (int index = 0; index < size; ++index) {
array[index] = new IntIndexPair(readInt(), index);
}
return array;
}
/////////////////////////////////////////////////////////////////////
static class OutputWriter extends PrintWriter{
final int DEFAULT_PRECISION = 12;
int precision;
String format, formatWithSpace;
{
precision = DEFAULT_PRECISION;
format = createFormat(precision);
formatWithSpace = format + " ";
}
public OutputWriter(OutputStream out) {
super(out);
}
public OutputWriter(String fileName) throws FileNotFoundException {
super(fileName);
}
public int getPrecision() {
return precision;
}
public void setPrecision(int precision) {
precision = max(0, precision);
this.precision = precision;
format = createFormat(precision);
formatWithSpace = format + " ";
}
private String createFormat(int precision){
return "%." + precision + "f";
}
@Override
public void print(double d){
printf(format, d);
}
public void printWithSpace(double d){
printf(formatWithSpace, d);
}
public void printAll(double...d){
for (int i = 0; i < d.length - 1; ++i){
printWithSpace(d[i]);
}
print(d[d.length - 1]);
}
@Override
public void println(double d){
printlnAll(d);
}
public void printlnAll(double... d){
printAll(d);
println();
}
}
/////////////////////////////////////////////////////////////////////
int[][] steps = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int[][] steps8 = {
{-1, 0}, {1, 0}, {0, -1}, {0, 1},
{-1, -1}, {1, 1}, {1, -1}, {-1, 1}
};
boolean check(int index, int lim){
return (0 <= index && index < lim);
}
/////////////////////////////////////////////////////////////////////
boolean checkBit(int mask, int bit){
return (mask & (1 << bit)) != 0;
}
/////////////////////////////////////////////////////////////////////
void solve() throws IOException {
char[] c = readCharArray();
int n = c.length;
int size = (n * 2 + 1);
int[] lastIndex = new int[size];
Arrays.fill(lastIndex, -1);
int[] dp = new int[n];
int start = n;
lastIndex[start] = 0;
for (int i = 0; i < n; ++i) {
dp[i] = (i == 0? 0: dp[i-1]);
if (c[i] == ')') {
--start;
if (lastIndex[start] != -1) {
dp[i] = max(dp[i], i + 1 - lastIndex[start] + (lastIndex[start] == 0? 0: dp[lastIndex[start]-1]));
}
} else {
++start;
}
lastIndex[start] = i + 1;
}
out.println(dp[n-1]);
}
}
| Java | ["(()))(", "((()())"] | 5 seconds | ["4", "6"] | null | Java 7 | standard input | [
"greedy"
] | 2ce2d0c4ac5e630bafad2127a1b589dd | Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. | 1,400 | Output the maximum possible length of a regular bracket sequence. | standard output | |
PASSED | 4a70a7619e32d858dfe5574a3dafadbc | train_002.jsonl | 1281970800 | A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? | 256 megabytes |
import java.util.Scanner;
import java.util.Stack;
public class B {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
Stack<Character> st = new Stack<Character>();
int ans = 0;
for (int i = 1; i <= s.length(); i++) {
if (s.charAt(i-1) == '('){
st.push('(');
}else{
if (!st.isEmpty()){
st.pop();
ans += 2;
}
}
}
System.out.println(ans);
}
}
| Java | ["(()))(", "((()())"] | 5 seconds | ["4", "6"] | null | Java 7 | standard input | [
"greedy"
] | 2ce2d0c4ac5e630bafad2127a1b589dd | Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. | 1,400 | Output the maximum possible length of a regular bracket sequence. | standard output | |
PASSED | 1063787d424f95bedcf6240f04eacf44 | train_002.jsonl | 1281970800 | A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? | 256 megabytes | import static java.util.Arrays.deepToString;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Archive {
static int search(int[] a, int i, int key){
int left = i - 1;
int right = a.length;
while(right - left > 1){
int mid = (right + left) / 2;
if(a[mid] >= key){
right = mid;
}else{
left = mid;
}
}
if(right < a.length && a[right] == key){
return right;
}else{
return -1;
}
}
static void solve() throws Exception {
String s = next();
int b = 0;
int cnt = 0;
for(int i = 0; i < s.length(); i++){
if(s.charAt(i) == '('){
b++;
cnt++;
}else if(b - 1 >= 0){
b--;
cnt++;
}
}
int ans = cnt - b;
out.println(ans);
}
static BufferedReader br;
static StringTokenizer st;
static PrintWriter out;
static void debug(Object... a) {
System.err.println(deepToString(a));
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
String line = br.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
}
return st.nextToken();
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static void main(String[] args) {
try {
File file = new File("fn.in");
InputStream input = System.in;
OutputStream output = System.out;
if (file.canRead()) {
input = (new FileInputStream(file));
output = (new PrintStream("fn.out"));
}
br = new BufferedReader(new InputStreamReader(input));
out = new PrintWriter(output);
solve();
out.close();
br.close();
} catch (Throwable e) {
e.printStackTrace();
System.exit(1);
}
}
} | Java | ["(()))(", "((()())"] | 5 seconds | ["4", "6"] | null | Java 7 | standard input | [
"greedy"
] | 2ce2d0c4ac5e630bafad2127a1b589dd | Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. | 1,400 | Output the maximum possible length of a regular bracket sequence. | standard output | |
PASSED | a99f10abbd42bfe3ed463c4de1b2f649 | train_002.jsonl | 1281970800 | A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? | 256 megabytes | // package main;
import java.io.*;
import java.math.*;
import java.util.*;;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Togrul Gasimov
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner scan = new FastScanner(inputStream);
FastPrinter out = new FastPrinter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, scan, out);
out.close();
}
}
class TaskD {
public void solve(int testNumber, FastScanner scan, FastPrinter out) {
Stack <Character>S = new Stack();
String s = scan.next();
int k = s.length();
S.push(s.charAt(0));
for(int i = 1; i < k; i++){
if(S.isEmpty()){
S.push(s.charAt(i));
}else if(S.peek() == '(' && s.charAt(i) == ')'){
S.pop();
}else S.push(s.charAt(i));
}
out.println(k - S.size());
}
}
class FastScanner extends BufferedReader {
public FastScanner(InputStream is) {
super(new InputStreamReader(is));
}
public int read() {
try {
int ret = super.read();
// if (isEOF && ret < 0) {
// throw new InputMismatchException();
// }
// isEOF = ret == -1;
return ret;
} catch (IOException e) {
throw new InputMismatchException();
}
}
public String next() {
StringBuilder sb = new StringBuilder();
int c = read();
while (isWhiteSpace(c)) {
c = read();
}
if (c < 0) {
return null;
}
while (c >= 0 && !isWhiteSpace(c)) {
sb.appendCodePoint(c);
c = read();
}
return sb.toString();
}
static boolean isWhiteSpace(int c) {
return c >= 0 && c <= 32;
}
public int nextInt() {
int c = read();
while (isWhiteSpace(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int ret = 0;
while (c >= 0 && !isWhiteSpace(c)) {
if (c < '0' || c > '9') {
throw new NumberFormatException("digit expected " + (char) c
+ " found");
}
ret = ret * 10 + c - '0';
c = read();
}
return ret * sgn;
}
public long nextLong() {
return Long.parseLong(next());
}
public String readLine() {
try {
return super.readLine();
} catch (IOException e) {
return null;
}
}
}
class FastPrinter extends PrintWriter {
public FastPrinter(OutputStream out) {
super(out);
}
public FastPrinter(Writer out) {
super(out);
}
} | Java | ["(()))(", "((()())"] | 5 seconds | ["4", "6"] | null | Java 7 | standard input | [
"greedy"
] | 2ce2d0c4ac5e630bafad2127a1b589dd | Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. | 1,400 | Output the maximum possible length of a regular bracket sequence. | standard output | |
PASSED | 817d87ad1ebf3eed120bccc64e6ad648 | train_002.jsonl | 1281970800 | A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? | 256 megabytes | // package main;
import java.io.*;
import java.math.*;
import java.util.*;;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Togrul Gasimov
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner scan = new FastScanner(inputStream);
FastPrinter out = new FastPrinter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, scan, out);
out.close();
}
}
class TaskD {
public void solve(int testNumber, FastScanner scan, FastPrinter out) {
String s = scan.next();
Stack <Character>S = new Stack();
int k = s.length();
S.push(s.charAt(0));
for(int i = 1; i < k; i++){
if(S.isEmpty()){
S.push(s.charAt(i));
}else if(S.peek() == '(' && s.charAt(i) == ')'){
S.pop();
}else{
S.push(s.charAt(i));
}
}
out.println(k - S.size());
}
}
class FastScanner extends BufferedReader {
public FastScanner(InputStream is) {
super(new InputStreamReader(is));
}
public int read() {
try {
int ret = super.read();
// if (isEOF && ret < 0) {
// throw new InputMismatchException();
// }
// isEOF = ret == -1;
return ret;
} catch (IOException e) {
throw new InputMismatchException();
}
}
public String next() {
StringBuilder sb = new StringBuilder();
int c = read();
while (isWhiteSpace(c)) {
c = read();
}
if (c < 0) {
return null;
}
while (c >= 0 && !isWhiteSpace(c)) {
sb.appendCodePoint(c);
c = read();
}
return sb.toString();
}
static boolean isWhiteSpace(int c) {
return c >= 0 && c <= 32;
}
public int nextInt() {
int c = read();
while (isWhiteSpace(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int ret = 0;
while (c >= 0 && !isWhiteSpace(c)) {
if (c < '0' || c > '9') {
throw new NumberFormatException("digit expected " + (char) c
+ " found");
}
ret = ret * 10 + c - '0';
c = read();
}
return ret * sgn;
}
public long nextLong() {
return Long.parseLong(next());
}
public String readLine() {
try {
return super.readLine();
} catch (IOException e) {
return null;
}
}
}
class FastPrinter extends PrintWriter {
public FastPrinter(OutputStream out) {
super(out);
}
public FastPrinter(Writer out) {
super(out);
}
} | Java | ["(()))(", "((()())"] | 5 seconds | ["4", "6"] | null | Java 7 | standard input | [
"greedy"
] | 2ce2d0c4ac5e630bafad2127a1b589dd | Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. | 1,400 | Output the maximum possible length of a regular bracket sequence. | standard output | |
PASSED | b1ae165e28a3ae17b44c4851ee942c05 | train_002.jsonl | 1281970800 | A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? | 256 megabytes | // package main;
import java.io.*;
import java.math.*;
import java.util.*;;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Togrul Gasimov
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner scan = new FastScanner(inputStream);
FastPrinter out = new FastPrinter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, scan, out);
out.close();
}
}
class TaskD {
public void solve(int testNumber, FastScanner scan, FastPrinter out) {
String s = scan.next();
int y = 0, a = 0, b = 0, ans = 0;
for(int i = 0; i <= s.length() - 1; i++){
if(s.charAt(i) == '(')y++;
else{
if(y > 0){
y--; ans += 2;
}
}
}
out.println(ans);
}
}
class FastScanner extends BufferedReader {
public FastScanner(InputStream is) {
super(new InputStreamReader(is));
}
public int read() {
try {
int ret = super.read();
// if (isEOF && ret < 0) {
// throw new InputMismatchException();
// }
// isEOF = ret == -1;
return ret;
} catch (IOException e) {
throw new InputMismatchException();
}
}
public String next() {
StringBuilder sb = new StringBuilder();
int c = read();
while (isWhiteSpace(c)) {
c = read();
}
if (c < 0) {
return null;
}
while (c >= 0 && !isWhiteSpace(c)) {
sb.appendCodePoint(c);
c = read();
}
return sb.toString();
}
static boolean isWhiteSpace(int c) {
return c >= 0 && c <= 32;
}
public int nextInt() {
int c = read();
while (isWhiteSpace(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int ret = 0;
while (c >= 0 && !isWhiteSpace(c)) {
if (c < '0' || c > '9') {
throw new NumberFormatException("digit expected " + (char) c
+ " found");
}
ret = ret * 10 + c - '0';
c = read();
}
return ret * sgn;
}
public long nextLong() {
return Long.parseLong(next());
}
public String readLine() {
try {
return super.readLine();
} catch (IOException e) {
return null;
}
}
}
class FastPrinter extends PrintWriter {
public FastPrinter(OutputStream out) {
super(out);
}
public FastPrinter(Writer out) {
super(out);
}
} | Java | ["(()))(", "((()())"] | 5 seconds | ["4", "6"] | null | Java 7 | standard input | [
"greedy"
] | 2ce2d0c4ac5e630bafad2127a1b589dd | Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. | 1,400 | Output the maximum possible length of a regular bracket sequence. | standard output | |
PASSED | 28a3684cbbca50621bc5938ccdd02414 | train_002.jsonl | 1281970800 | A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class Bracket {
public static void main(String[] args) {
Stack<String> p = new Stack<String>();
Scanner sc = new Scanner(System.in);
String entry = sc.nextLine();
int ans = entry.length();
for(int i = 0; i < entry.length(); i++) {
if(entry.charAt(i) == ')')
if(p.isEmpty()) ans--;
else p.pop();
else
p.push("(");
}
while(!p.isEmpty()) {
ans--;
p.pop();
}
System.out.println(ans);
}
}
| Java | ["(()))(", "((()())"] | 5 seconds | ["4", "6"] | null | Java 7 | standard input | [
"greedy"
] | 2ce2d0c4ac5e630bafad2127a1b589dd | Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. | 1,400 | Output the maximum possible length of a regular bracket sequence. | standard output | |
PASSED | af6e47663f259d68004f62241df0adc1 | train_002.jsonl | 1281970800 | A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? | 256 megabytes | import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
public class Task26B {
public static void main(String[] args) throws IOException {
InputStreamReader reader = new InputStreamReader(System.in, Charset.forName("UTF-8"));
int c = -1, opened = 0, total = 0, ret = 0;
while((c = reader.read()) != -1) {
char ch = (char) c;
if (ch != '(' && ch != ')') break;
if (ch == '(') opened ++;
else if (opened == 0) ret++;
else opened--;
total++;
}
if (opened == 0) System.out.println(total - ret);
else System.out.println(total - ret - opened);
}
}
| Java | ["(()))(", "((()())"] | 5 seconds | ["4", "6"] | null | Java 7 | standard input | [
"greedy"
] | 2ce2d0c4ac5e630bafad2127a1b589dd | Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. | 1,400 | Output the maximum possible length of a regular bracket sequence. | standard output | |
PASSED | 61c4f94b0abddc77c9159b3a04367fef | train_002.jsonl | 1281970800 | A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? | 256 megabytes | import java.io.InputStream;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Task {
private static final boolean readFromFile = false;
public static void main(String args[]){
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FileOutputStream fileOutputStream;
FileInputStream fileInputStream;
if (readFromFile){
try{
fileInputStream = new FileInputStream(new File("input.txt"));
fileOutputStream = new FileOutputStream(new File("output.txt"));
}catch (FileNotFoundException e){
throw new RuntimeException(e);
}
}
PrintWriter out = new PrintWriter((readFromFile)?fileOutputStream:outputStream);
InputReader in = new InputReader((readFromFile)?fileInputStream:inputStream);
Solver s = new Solver(in,out);
s.solve();
out.close();
}
}
class Solver{
InputReader in;
PrintWriter out;
public void solve(){
char lastChar[] = new char[1000001];
int lastId[] = new int[1000001],
m = 0;
String s =in.nextLine();
lastId[0] = -1;
for (int i=0;i<s.length();i++){
if (s.charAt(i)=='('){
m++;
lastId[m]=i;
lastChar[m]='(';
}else{
if (m>0 && lastChar[m]=='(')
m--;
else{
m++;
lastId[m]=i;
lastChar[m]=')';
}
}
}
out.println(s.length()-m);
}
Solver(InputReader in, PrintWriter out){
this.in=in;
this.out=out;
}
}
class InputReader{
private BufferedReader buf;
private StringTokenizer tok;
InputReader(InputStream in){
tok = null;
buf = new BufferedReader(new InputStreamReader(in));
}
InputReader(FileInputStream in){
tok = null;
buf = new BufferedReader(new InputStreamReader(in));
}
public String next(){
while (tok==null || !tok.hasMoreTokens()){
try{
tok = new StringTokenizer(buf.readLine());
}catch (IOException e){
throw new RuntimeException(e);
}
}
return tok.nextToken();
}
public int nextInt(){
return Integer.parseInt(next());
}
public long nextLong(){
return Long.parseLong(next());
}
public double nextDouble(){
return Double.parseDouble(next());
}
public float nextFloat(){
return Float.parseFloat(next());
}
public String nextLine(){
try{
return buf.readLine();
}catch (IOException e){
return null;
}
}
} | Java | ["(()))(", "((()())"] | 5 seconds | ["4", "6"] | null | Java 7 | standard input | [
"greedy"
] | 2ce2d0c4ac5e630bafad2127a1b589dd | Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. | 1,400 | Output the maximum possible length of a regular bracket sequence. | standard output | |
PASSED | d042e172143be668e4a86b9a8fa28736 | train_002.jsonl | 1281970800 | A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? | 256 megabytes | import java.util.ArrayList;
import java.util.Scanner;
public class Main16 {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
char[] cad = s.toCharArray();
int count = 0;
ArrayList<Character> stack = new ArrayList<Character>();
for (int i = 0; i < s.length(); i++) {
char p = cad[i];
if(stack.isEmpty())
stack.add(p);
else if(p == ')' && stack.get(stack.size()-1) == '(' ){
stack.remove(stack.size()-1);
count+=2;
}else stack.add(p);
}
System.out.println(count);
}
}
| Java | ["(()))(", "((()())"] | 5 seconds | ["4", "6"] | null | Java 7 | standard input | [
"greedy"
] | 2ce2d0c4ac5e630bafad2127a1b589dd | Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. | 1,400 | Output the maximum possible length of a regular bracket sequence. | standard output | |
PASSED | 708fe3213a0da6eaf4c17bd15775065a | train_002.jsonl | 1281970800 | A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? | 256 megabytes |
import java.util.Scanner;
public class ACMD {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
String str=sc.nextLine();
int rTol=0;
int sum=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)=='(')
rTol++;
else if(str.charAt(i)==')'){
if(rTol>0){
rTol--;
sum+=2;
}
}
}
System.out.println(sum);
}
}
| Java | ["(()))(", "((()())"] | 5 seconds | ["4", "6"] | null | Java 7 | standard input | [
"greedy"
] | 2ce2d0c4ac5e630bafad2127a1b589dd | Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. | 1,400 | Output the maximum possible length of a regular bracket sequence. | standard output | |
PASSED | 1019910558c1a370ab20cccd6c435bc9 | train_002.jsonl | 1281970800 | A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Mohammad Jamel
*/
public class PNum4 {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line = in.readLine();
Stack<Character> list = new Stack<>();
int sum = 0;
for (int i = 0; i < line.length(); i++) {
if (list.size() == 0) {
list.push(line.charAt(i));
} else {
if (list.peek() == line.charAt(i)||list.peek()>line.charAt(i)) {
list.push(line.charAt(i));
} else {
list.pop();
sum+=2;
}
}
}
System.out.println(sum);
}
}
| Java | ["(()))(", "((()())"] | 5 seconds | ["4", "6"] | null | Java 7 | standard input | [
"greedy"
] | 2ce2d0c4ac5e630bafad2127a1b589dd | Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. | 1,400 | Output the maximum possible length of a regular bracket sequence. | standard output | |
PASSED | b0f20e02a0464303587b55cea31282ed | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes |
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
static BigInteger c(BigInteger n, int m) {
BigInteger a = BigInteger.ONE, b = BigInteger.ONE;
for(int i = 1; i <= m; i ++) {
a = a.multiply(n.subtract(BigInteger.valueOf(i - 1)));
b = b.multiply(BigInteger.valueOf(i));
}
return a.divide(b);
}
public static void main(String[] args) {
BigInteger n, ans, c;
Scanner cin = new Scanner(System.in);
n = cin.nextBigInteger();
ans = c(n, 7).add(c(n, 6)).add(c(n, 5));
System.out.println(ans.toString());
}
}
| Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | 8e632775357ada12a5835144bd9bb1d3 | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes | import java.io.*;
import java.util.*;
public class Main{
static int mod = (int)(Math.pow(10, 9) + 7);
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
long n = sc.nextLong();
long sol1 = 0;
long sol2 = 0;
long sol3 = 0;
long top = n;
top *= (n-1);
top *= (n-2);
top *= (n-4);
top *= (n-3);
top /= 120;
sol1 = top;
top *= (n-5);
top/=6;
sol2 = top;
top *= (n-6);
top/=7;
sol3 = (top);
out.println(sol1 + sol2 + sol3);
out.close();
}
static void mergeSort(int[] A){ // low to hi sort, single array only
int n = A.length;
if (n < 2) return;
int[] l = new int[n/2];
int[] r = new int[n - n/2];
for (int i = 0; i < n/2; i++){
l[i] = A[i];
}
for (int j = n/2; j < n; j++){
r[j-n/2] = A[j];
}
mergeSort(l);
mergeSort(r);
merge(l, r, A);
}
static void merge(int[] l, int[] r, int[] a){
int i = 0, j = 0, k = 0;
while (i < l.length && j < r.length && k < a.length){
if (l[i] < r[j]){
a[k] = l[i];
i++;
}
else{
a[k] = r[j];
j++;
}
k++;
}
while (i < l.length){
a[k] = l[i];
i++;
k++;
}
while (j < r.length){
a[k] = r[j];
j++;
k++;
}
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//--------------------------------------------------------
}
| Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | 5d2d33e72b7c2ddfcfa0e9be0fd69ce8 | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes | public class f {
public static void main(String[] args) {
long r = new java.util.Scanner(System.in).nextInt()+1;
System.out.println(((((((((((1*(r-1))/1)*(r-2))/2)*(r-3))/3)*(r-4))/4)*(r-5))/5)+((((((((((((1*(r-1))/1)*(r-2))/2)*(r-3))/3)*(r-4))/4)*(r-5))/5)*(r-6))/6)+(((((((((((((1*(r-1))/1)*(r-2))/2)*(r-3))/3)*(r-4))/4)*(r-5))/5)*(r-6))/6)*(r-7))/7);
}
} | Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | 066516ee035d6cd1a59472717e37f9fd | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes | import java.math.BigInteger;
import java.util.Scanner;
public class Personnel {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
System.out.println(BigInteger.ZERO.add(comb(n, 5)).add(comb(n, 6)).add(comb(n, 7)));
}
public static BigInteger comb(long n, long r) {
BigInteger res = BigInteger.ONE;
for (long i = n; i > n - r; i--) {
res = res.multiply(BigInteger.valueOf(i));
}
for (long i = 2; i <= r; i++) {
res = res.divide(BigInteger.valueOf(i));
}
return res;
}
}
| Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | ead020b965aedb4e2ad8372a9d63d880 | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes |
import java.util.*;
public class random_num {
public static void main(String args[]){
Scanner s=new Scanner(System.in);
int n=s.nextInt();
long ans=0;
long temp=1;
for(int i=0;i<7;i++)
{
temp=(temp*(n-i))/(i+1);
if(i>=4)ans+=temp;
}
System.out.print(ans);
}
}
| Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | 87909ae1aae997e9eb119ea468766787 | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes |
import java.math.BigInteger;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
/**
*
* @author Mohammad Hadi
*/
public class F630 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
System.out.println(c(n, 5).add(c(n, 6)).add(c(n, 7)));
}
static BigInteger c(int n, int r) {
BigInteger a = BigInteger.ONE;
int max = Math.max(r, n - r);
int min = Math.min(r, n - r);
for (int i = n; i > max; i--) {
a = a.multiply(new BigInteger(String.valueOf(i)));
}
return a.divide(new BigInteger(String.valueOf(fact(min))));
}
static long fact(int n) {
long ans = 1;
for (int i = 2; i <= n; i++) {
ans *= i;
}
return ans;
}
}
| Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | 3370d9ad26d512e709c3dc66dd1eb8cb | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes |
import java.io.*;
import java.util.*;
public class Main {
void solve(Scanner in, PrintWriter out){
long n = in.nextLong();
long a = n / 1 * (n - 1)/2 * (n - 2)/3 * (n - 3)/4 * (n - 4)/5;
long b = n / 1 * (n - 1)/2 * (n - 2)/3 * (n - 3)/4 * (n - 4)/5 * (n - 5) / 6;
long c = n / 1 * (n - 1)/2 * (n - 2)/3 * (n - 3)/4 * (n - 4)/5 * (n - 5) / 6 * (n - 6) / 7;
out.println(a + b + c);
}
/*������������ ������*/
void run(){
try(
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out) ) {
solve (in, out);
}
}
public static void main(String[]args){
new Main().run();
}
}
| Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | 494e85e1ca8365be883f21562b6b535b | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes |
import java.io.*;
import java.util.*;
/**
*
* @author Sourav Kumar Paul
*/
public class SolveF {
public static void main(String[] args) throws IOException{
Reader in = new Reader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
long ncr[][] = new long[1000][1000];
ncr[0][0] = 1;
for(int i=1; i<1000; i++)
{
ncr[i][0] = 1;
for(int j=1; j<1000; j++)
ncr[i][j] = ncr[i-1][j]+ncr[i-1][j-1];
}
int n = in.nextInt();
out.println(ncr[n][5] + ncr[n][6] + ncr[n][7]);
out.flush();
out.close();
}
public static class Reader {
public BufferedReader reader;
public StringTokenizer st;
public Reader(InputStreamReader stream) {
reader = new BufferedReader(stream);
st = null;
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public String nextLine() throws IOException{
return reader.readLine();
}
public long nextLong(){
return Long.parseLong(next());
}
public double nextDouble(){
return Double.parseDouble(next());
}
}
}
| Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | 42d585b2b2cc19996bd37da6417d25d2 | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long n = in.nextLong();
long k5 = n / 1 * (n - 1) / 2 * (n - 2) / 3 * (n - 3) / 4 * (n - 4) / 5;
long k6 = k5 * (n - 5) / 6;
long k7 = k6 * (n - 6) / 7;
System.out.println(k5 + k6 + k7);
}
}
| Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | 9ffeafc3092ce6392933ec3e45cf7f26 | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes | import java.io.PrintWriter;
import java.util.Scanner;
public class Main {
void solve(Scanner in, PrintWriter out) {
long n = in.nextLong();
long ans1 = n / 1 * (n - 1) / 2 * (n - 2) / 3 * (n - 3) / 4 * (n - 4) / 5;
long ans2 = n / 1 * (n - 1) / 2 * (n - 2) / 3 * (n - 3) / 4 * (n - 4) / 5 * (n - 5) / 6;
long ans3 = n / 1 * (n - 1) / 2 * (n - 2) / 3 * (n - 3) / 4 * (n - 4) / 5 * (n - 5) / 6 * (n - 6) / 7;
out.println(ans1 + ans2 + ans3);
}
void run() {
try (Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out)) {
solve(in, out);
}
}
public static void main(String args[]) {
new Main().run();
}
} | Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | d8d0ad61e872eb72478dd8ed8054c00e | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes | import java.util.*;
import java.io.*;
import java.lang.Math.*;
public class main{
void solve(Scanner in, PrintWriter out){
long n=in.nextInt();
long c_5=n/1*(n-1)/2*(n-2)/3*(n-3)/4*(n-4)/5;
long c_6=c_5*(n-5)/6;
long c_7=c_6*(n-6)/7;
out.println(c_5+c_6+c_7);
}
void run(){
try( Scanner in=new Scanner(System.in);
PrintWriter out= new PrintWriter(System.out)
//Scanner in=new Scanner("sum.in");
//PrintWriter out=new PrintWriter (new FileWriter("sum.out"));
){
solve(in,out);
}
}
public static void main (String args[]){ // Main не отличается
new main().run();
}
} | Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | 0402056a4a65b5dac13d51438a1c87c4 | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes | import java.util.*;
import java.io.*;
import java.lang.Math.*;
public class main{
void solve(Scanner in, PrintWriter out){
long n=in.nextLong();
long c_5=n/1*(n-1)/2*(n-2)/3*(n-3)/4*(n-4)/5;
long c_6=c_5*(n-5)/6;
long c_7=c_6*(n-6)/7;
out.println(c_5+c_6+c_7);
}
void run(){
try( Scanner in=new Scanner(System.in);
PrintWriter out= new PrintWriter(System.out)
//Scanner in=new Scanner("sum.in");
//PrintWriter out=new PrintWriter (new FileWriter("sum.out"));
){
solve(in,out);
}
}
public static void main (String args[]){ // Main не отличается
new main().run();
}
} | Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | 13dff513a04afc0ccbb9659cec30a55a | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes | import java.math.BigInteger;
import java.util.Scanner;
public class CF_630F {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
BigInteger[] factorials = new BigInteger[10];
factorials[0] = BigInteger.ONE;
for (int i = 1; i < factorials.length; i++) {
factorials[i] = BigInteger.valueOf(i).multiply(factorials[i - 1]);
}
long n = scan.nextInt();
BigInteger[] kids = { BigInteger.valueOf(n), BigInteger.valueOf(n-1), BigInteger.valueOf(n-2), BigInteger.valueOf(n-3), BigInteger.valueOf(n-4), BigInteger.valueOf(n-5), BigInteger.valueOf(n-6)};
BigInteger a = kids[0].multiply(kids[1]).multiply(kids[2]).multiply(kids[3]).multiply(kids[4]).divide(factorials[5]);
BigInteger b = a.multiply(kids[5]).divide(BigInteger.valueOf(6));
BigInteger c = b.multiply(kids[6]).divide(BigInteger.valueOf(7));
System.out.println(a.add(b).add(c));
}
}
// 11 choose 5 = 11!/(6!5!) = 11*10*9*8*7
// 8 choose 5 = 8!/(5!3!) = 8*7*6*5*4
// 8 choose 5 = (8 * 7 * 6) / (5!)
// = n! / (n-5!)5!
// = | Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | c42eef0c19cb987bdde718bd6bc192a1 | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.StringTokenizer;
public class Main2 {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
long n = sc.nextLong();
out.println(nCr(n, 5).add(nCr(n, 6).add(nCr(n, 7))));
out.flush();
out.close();
}
static BigInteger nCr(long n, int r)
{
BigInteger a = BigInteger.valueOf(n);
BigInteger b = BigInteger.ONE;
for(int i = 2; i <= r; ++i)
{
a = a.multiply(BigInteger.valueOf(n - i + 1));
b = b.multiply(BigInteger.valueOf(i));
}
return a.divide(b);
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException
{
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-')
{
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.')
{
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else
{
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
public boolean ready() throws IOException {return br.ready();}
}
}
| Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | 3511179de9a6b817021130c60d3dabf6 | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes | import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
long y = 1L*n*(n-1)/2*(n-2)/3*(n-3)/4*(n-4)/5*(n-5)/6*(n-6)/7 + 1L*n*(n-1)/2*(n-2)/3*(n-3)/4*(n-4)/5*(n-5)/6 + 1L*n*(n-1)/2*(n-2)/3*(n-3)/4*(n-4)/5;
System.out.println(y);
}
} | Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | 8ad9d71200d19c51c6e4edfe942f597d | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 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 BufferedReader in;
static PrintWriter out;
static StringTokenizer buffer;
public static void solve() {
long N = nl();
long x5 = N*(N-1)*(N-2)*(N-3)*(N-4)/120;
long x6 = x5*(N-5)/6;
long x7 = x6*(N-6)/7;
out.println((x5 + x6 + x7));
}
public static void main(String[] args) throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
}
static String next() {
while (buffer == null || !buffer.hasMoreElements()) {
try {
buffer = new StringTokenizer(in.readLine());
} catch (IOException e) {
}
}
return buffer.nextToken();
}
static int ni() {
return Integer.parseInt(next());
}
static long nl() {
return Long.parseLong(next());
}
static double nd() {
return Double.parseDouble(next());
}
static String ns() {
return next();
}
static int[] ni(int n) {
int[] res = new int[n];
for (int i = 0; i < n; i++)
res[i] = Integer.parseInt(next());
return res;
}
static long[] nl(int n) {
long[] res = new long[n];
for (int i = 0; i < n; i++)
res[i] = Long.parseLong(next());
return res;
}
static double[] nd(int n) {
double[] res = new double[n];
for (int i = 0; i < n; i++)
res[i] = Double.parseDouble(next());
return res;
}
static String[] ns(int n) {
String[] res = new String[n];
for (int i = 0; i < n; i++)
res[i] = next();
return res;
}
}
| Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | 22210986ff8cf43dea125a1a3184f61a | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes | import java.util.*;
public class CodeForces
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
long n = in.nextLong();
long ans = 0;
ans += get_combinatorics(n,5);
ans += get_combinatorics(n,6);
ans += get_combinatorics(n,7);
System.out.println(ans);
}
static long get_combinatorics(long n , long r){
long result = 1 , x = n , y = n-r ;
int flag = 0 ;
List<Long> divis = new ArrayList<>();
for(long i= r ; i>1 ; i--){
divis.add(i);
}
while(x>y){
result *= x ;
for(int i=0 ; i<divis.size() ; i++){
if(result % divis.get(i) == 0){
result /= divis.get(i);
divis.remove(i);
}
}
x-- ;
}
return result;
}
} | Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | 934cd5d509767c90fa2e6e799fa06aa1 | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes | import java.util.Scanner;
public class codeforces {
public static void main(String[] args) {
TaskF Solver = new TaskF();
Solver.Solve();
}
public static class TaskF {
double tr[][];
public void Solve() {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
PascalTriangle(n + 1);
long ans1 = (long) tr[n][5];
long ans2 = (long) tr[n][6];
long ans3 = (long) tr[n][7];
System.out.println(ans1 + ans2 + ans3);
}
private void PascalTriangle(int n) {
tr = new double [n + 1][];
for (int i = 0; i < n + 1; i++)
tr[i] = new double [i + 1];
tr[0][0] = 1;
tr[1][0] = 1;
tr[1][1] = 1;
for (int i = 2; i < n + 1; i++) {
tr[i][0] = 1;
tr[i][i] = 1;
for (int j = 1; j < i; j++) {
tr[i][j] = tr[i - 1][j - 1] + tr[i - 1][j];
}
}
}
}
} | Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | eba72f835f7750860ec78ead472f9b49 | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class B {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(bf.readLine());
BigInteger n = new BigInteger(st.nextToken());
long l = n.longValue();
out.println(n.multiply(BigInteger.valueOf(l-1)).multiply(BigInteger.valueOf(l-2)).multiply(BigInteger.valueOf(l-3)).multiply(BigInteger.valueOf(l-4)).multiply(BigInteger.valueOf((42+7*(l-5)+(l-5)*(l-6)))).divide(BigInteger.valueOf(5040)).longValue());
out.flush();
out.close();
}
} | Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | cabfbe6ef2f1d5381c8c5bfa3c52845c | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes | /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.math.BigInteger;
import java.util.Scanner;
/**
*
* @author Marwen
*/
public class Main {
static BigInteger fact(int x)
{
BigInteger f = BigInteger.ONE;
//f = 1 ;
for (int i=1;i<=x;i++)
f = f.multiply(BigInteger.valueOf(i));
return f ;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt() ;
BigInteger ret1 = BigInteger.ONE;
ret1 = ret1.multiply((fact(n))).divide((fact(n-5)));
ret1 = ret1.divide((fact(5)));
BigInteger ret2 = BigInteger.ONE;
ret2 = ret2.multiply((fact(n))).divide((fact(n-6)));
ret2 = ret2.divide((fact(6)));
BigInteger ret3 = BigInteger.ONE;
ret3 = ret3.multiply((fact(n))).divide((fact(n-7)));
ret3 = ret3.divide((fact(7)));
BigInteger ans = BigInteger.ZERO ;
ans = ans.add(ret1).add(ret2).add(ret3) ;
System.out.println(ans);
}
} | Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | 8c3502012ab92bcf94634cbe847e6ecd | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes | import java.math.BigInteger;
import java.util.*;
public class Test {
private static final Scanner INPUT = new Scanner(System.in);
public static void main(String[] args) {
int num = INPUT.nextInt();
System.out.printf("%s", C(num, 5).add(C(num, 6).add(C(num, 7))).toString(10));
}
private static BigInteger C(int n, int m) {
BigInteger son = new BigInteger("1");
BigInteger mon = new BigInteger("1");
if(n / 2 > m) {
for(int i = n; i > (n - m); --i) {
son = son.multiply(new BigInteger(Integer.toString(i)));
}
for(int i = 1; i <= m; ++i) {
mon = mon.multiply(new BigInteger(Integer.toString(i)));
}
} else {
for(int i = n; i > m; --i) {
son = son.multiply(new BigInteger(Integer.toString(i)));
}
for(int i = 1; i <= (n - m); ++i) {
mon = mon.multiply(new BigInteger(Integer.toString(i)));
}
}
return son.divide(mon);
}
}
| Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | 4fc735386c37bffdbb1ee61a9d198e04 | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
public class F630 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
String[] line = br.readLine().split(" ");
long n = Long.parseLong(line[0]);
BigInteger nom = BigInteger.ONE,
nom2 =BigInteger.ONE,
nom3 = BigInteger.ONE;
for (int i=8; i<= n; i++) {
nom = nom.multiply(BigInteger.valueOf(i));
}
for (int i = 2; i <= n-7; i++) {
nom = nom.divide(BigInteger.valueOf(i));
}
for (int i=7; i<= n; i++) {
nom2 = nom2.multiply(BigInteger.valueOf(i));
}
for (int i = 2; i <= n-6; i++) {
nom2 = nom2.divide(BigInteger.valueOf(i));;
}
for (int i=6; i<= n; i++) {
nom3 = nom3.multiply(BigInteger.valueOf(i));
}
for (int i = 2; i <= n-5; i++) {
nom3 = nom3.divide(BigInteger.valueOf(i));
}
pw.println(nom.add(nom2).add(nom3));
br.close();
pw.flush();
pw.close();
}
}
| Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | 019eebda871967df97d742fa77951637 | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes | import java.io.*;
import java.util.*;
public class Main {
void solve(Scanner in, PrintWriter out) {
long n = in.nextInt();
long c_5 = (n * (n - 1) / 2 * (n - 2) / 3 * (n - 3) / 4 * (n - 4) / 5);
long c_6 = c_5 * (n - 5) / 6;
long c_7 = c_6 * (n - 6) / 7;
out.print(c_5 + c_6 + c_7);
}
void run() {
try (
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);) {
solve(in, out);
}
}
public static void main(String args[]) {
new Main().run();
}
} | Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | eb23115a44adc14b60a97c7aadaf3e15 | train_002.jsonl | 1455807600 | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | 64 megabytes | import java.io.*;
import java.util.*;
public class ForProblems {
Random rnd = new Random();
double binpow(double a, int b) {
if (b == 0)
return 1;
if (b % 2 == 0) {
double x = binpow(a, b / 2);
return x * x;
} else
return binpow(a, b - 1) * a;
}
long fact(int a, int b, int j) {
long ans = 1;
int[] ar = {2, 3, 4, 5, 6, 7};
int k = 0;
for (int i = a; i < b + 1; ++i) {
ans *= i;
if (k <= j - 2 && ans % ar[k] == 0)
ans /= ar[k++];
}
return ans;
}
void solve() throws IOException {
int n = readInt();
long ans = 0;
for (int i = 5; i < 8; ++i)
//Cn(i) = n! / i! / (n - i)! = (n - i + 1)...(n) / i!
ans += fact(n - i + 1, n, i);
out.print(ans);
}
final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
public static void main(String[] args){
new ForProblems().run();
}
public void run(){
try{
init();
solve();
out.close();
} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(-1);
}
}
String readString() throws IOException{
while(!tok.hasMoreTokens()){
try{
tok = new StringTokenizer(in.readLine());
} catch (Exception e) {
return null;
}
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
void init() throws FileNotFoundException {
if (ONLINE_JUDGE){
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
}
} | Java | ["7"] | 0.5 seconds | ["29"] | null | Java 8 | standard input | [
"combinatorics",
"math"
] | 09276406e16b46fbefd6f8c9650472f0 | The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes. | 1,300 | Output one integer — the number of different variants of group composition. | standard output | |
PASSED | 85b6db39c4bbbc9e03d7f902bd8b3cb5 | train_002.jsonl | 1534602900 | You have $$$n$$$ sticks of the given lengths.Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks.Let $$$S$$$ be the area of the rectangle and $$$P$$$ be the perimeter of the rectangle. The chosen rectangle should have the value $$$\frac{P^2}{S}$$$ minimal possible. The value is taken without any rounding.If there are multiple answers, print any of them.Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. | 256 megabytes | import java.io.*;
import java.util.*;
import java.awt.geom.Line2D;
public class Task5 {
static BufferedReader in;
static PrintWriter out;
static StringTokenizer tok;
public static void main(String[] args) throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
tok = new StringTokenizer("");
new Task5().solve();
out.flush();
out.close();
}
double f(double a, double b) {
return b / a;
}
void solve() throws IOException {
int t = nextInt();
while (t-- > 0) {
int n = nextInt();
HashMap<Integer, Integer> map = new HashMap<>();
double min = 1000000000;
ArrayList<Integer> al = new ArrayList<>();
for (int i = 0; i < n; i++) {
int val = nextInt();
if (map.containsKey(val)) {
al.add(val);
map.remove(val);
} else {
map.put(val, 1);
}
}
Collections.sort(al);
int a = -1;
int b = -1;
for (int i = 0; i < al.size() - 1; i++) {
int ff = al.get(i);
int ss = al.get(i + 1);
double m = f(ff, ss);
if (min > m) {
min = m;
a = ff;
b = ss;
}
}
out.println(a + " " + a + " " + b + " " + b);
}
}
String nextTok() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
String nextLine() throws IOException {
return in.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(nextTok());
}
long nextLong() throws IOException {
return Long.parseLong(nextTok());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextTok());
}
} | Java | ["3\n4\n7 2 2 7\n8\n2 8 1 4 8 2 1 5\n5\n5 5 5 5 5"] | 2 seconds | ["2 7 7 2\n2 2 1 1\n5 5 5 5"] | NoteThere is only one way to choose four sticks in the first list, they form a rectangle with sides $$$2$$$ and $$$7$$$, its area is $$$2 \cdot 7 = 14$$$, perimeter is $$$2(2 + 7) = 18$$$. $$$\frac{18^2}{14} \approx 23.143$$$.The second list contains subsets of four sticks that can form rectangles with sides $$$(1, 2)$$$, $$$(2, 8)$$$ and $$$(1, 8)$$$. Their values are $$$\frac{6^2}{2} = 18$$$, $$$\frac{20^2}{16} = 25$$$ and $$$\frac{18^2}{8} = 40.5$$$, respectively. The minimal one of them is the rectangle $$$(1, 2)$$$.You can choose any four of the $$$5$$$ given sticks from the third list, they will form a square with side $$$5$$$, which is still a rectangle with sides $$$(5, 5)$$$. | Java 8 | standard input | [
"greedy"
] | fc54d6febbf1d91e9f0e6121f248d2aa | The first line contains a single integer $$$T$$$ ($$$T \ge 1$$$) — the number of lists of sticks in the testcase. Then $$$2T$$$ lines follow — lines $$$(2i - 1)$$$ and $$$2i$$$ of them describe the $$$i$$$-th list. The first line of the pair contains a single integer $$$n$$$ ($$$4 \le n \le 10^6$$$) — the number of sticks in the $$$i$$$-th list. The second line of the pair contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_j \le 10^4$$$) — lengths of the sticks in the $$$i$$$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $$$T$$$ lists doesn't exceed $$$10^6$$$ in each testcase. | 1,600 | Print $$$T$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th list of the input. That is the lengths of the four sticks you choose from the $$$i$$$-th list, so that they form a rectangle and the value $$$\frac{P^2}{S}$$$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. | standard output | |
PASSED | fa3b58f20f63f1a163209344e1debbce | train_002.jsonl | 1534602900 | You have $$$n$$$ sticks of the given lengths.Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks.Let $$$S$$$ be the area of the rectangle and $$$P$$$ be the perimeter of the rectangle. The chosen rectangle should have the value $$$\frac{P^2}{S}$$$ minimal possible. The value is taken without any rounding.If there are multiple answers, print any of them.Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. | 256 megabytes | import java.io.*;
import java.util.*;
import java.awt.geom.Line2D;
public class Task5 {
static BufferedReader in;
static PrintWriter out;
static StringTokenizer tok;
public static void main(String[] args) throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
tok = new StringTokenizer("");
new Task5().solve();
out.flush();
out.close();
}
double f(double a, double b) {
return b / a;
}
void solve() throws IOException {
int t = nextInt();
while (t-- > 0) {
int n = nextInt();
TreeSet<Integer> set = new TreeSet<>();
double min = 1000000000;
ArrayList<Integer> al = new ArrayList<>();
for (int i = 0; i < n; i++) {
int val = nextInt();
if (set.contains(val)) {
al.add(val);
set.remove(val);
} else {
set.add(val);
}
}
Collections.sort(al);
int a = -1;
int b = -1;
for (int i = 0; i < al.size() - 1; i++) {
int ff = al.get(i);
int ss = al.get(i + 1);
double m = f(ff, ss);
if (min > m) {
min = m;
a = ff;
b = ss;
}
}
out.println(a + " " + a + " " + b + " " + b);
}
}
String nextTok() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
String nextLine() throws IOException {
return in.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(nextTok());
}
long nextLong() throws IOException {
return Long.parseLong(nextTok());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextTok());
}
} | Java | ["3\n4\n7 2 2 7\n8\n2 8 1 4 8 2 1 5\n5\n5 5 5 5 5"] | 2 seconds | ["2 7 7 2\n2 2 1 1\n5 5 5 5"] | NoteThere is only one way to choose four sticks in the first list, they form a rectangle with sides $$$2$$$ and $$$7$$$, its area is $$$2 \cdot 7 = 14$$$, perimeter is $$$2(2 + 7) = 18$$$. $$$\frac{18^2}{14} \approx 23.143$$$.The second list contains subsets of four sticks that can form rectangles with sides $$$(1, 2)$$$, $$$(2, 8)$$$ and $$$(1, 8)$$$. Their values are $$$\frac{6^2}{2} = 18$$$, $$$\frac{20^2}{16} = 25$$$ and $$$\frac{18^2}{8} = 40.5$$$, respectively. The minimal one of them is the rectangle $$$(1, 2)$$$.You can choose any four of the $$$5$$$ given sticks from the third list, they will form a square with side $$$5$$$, which is still a rectangle with sides $$$(5, 5)$$$. | Java 8 | standard input | [
"greedy"
] | fc54d6febbf1d91e9f0e6121f248d2aa | The first line contains a single integer $$$T$$$ ($$$T \ge 1$$$) — the number of lists of sticks in the testcase. Then $$$2T$$$ lines follow — lines $$$(2i - 1)$$$ and $$$2i$$$ of them describe the $$$i$$$-th list. The first line of the pair contains a single integer $$$n$$$ ($$$4 \le n \le 10^6$$$) — the number of sticks in the $$$i$$$-th list. The second line of the pair contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_j \le 10^4$$$) — lengths of the sticks in the $$$i$$$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $$$T$$$ lists doesn't exceed $$$10^6$$$ in each testcase. | 1,600 | Print $$$T$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th list of the input. That is the lengths of the four sticks you choose from the $$$i$$$-th list, so that they form a rectangle and the value $$$\frac{P^2}{S}$$$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. | standard output | |
PASSED | 1b4d7a73b7d150612f51ff7b286ceef7 | train_002.jsonl | 1534602900 | You have $$$n$$$ sticks of the given lengths.Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks.Let $$$S$$$ be the area of the rectangle and $$$P$$$ be the perimeter of the rectangle. The chosen rectangle should have the value $$$\frac{P^2}{S}$$$ minimal possible. The value is taken without any rounding.If there are multiple answers, print any of them.Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. | 256 megabytes | import java.io.*;
import java.util.*;
import java.awt.geom.Line2D;
public class Task5 {
static BufferedReader in;
static PrintWriter out;
static StringTokenizer tok;
public static void main(String[] args) throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
tok = new StringTokenizer("");
new Task5().solve();
out.flush();
out.close();
}
double f(double a, double b) {
return b / a;
}
void solve() throws IOException {
int t = nextInt();
while (t-- > 0) {
int n = nextInt();
HashSet<Integer> set = new HashSet<>();
double min = 1000000000;
ArrayList<Integer> al = new ArrayList<>();
for (int i = 0; i < n; i++) {
int val = nextInt();
if (set.contains(val)) {
al.add(val);
set.remove(val);
} else {
set.add(val);
}
}
Collections.sort(al);
int a = -1;
int b = -1;
for (int i = 0; i < al.size() - 1; i++) {
int ff = al.get(i);
int ss = al.get(i + 1);
double m = f(ff, ss);
if (min > m) {
min = m;
a = ff;
b = ss;
}
}
out.println(a + " " + a + " " + b + " " + b);
}
}
String nextTok() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
String nextLine() throws IOException {
return in.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(nextTok());
}
long nextLong() throws IOException {
return Long.parseLong(nextTok());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextTok());
}
} | Java | ["3\n4\n7 2 2 7\n8\n2 8 1 4 8 2 1 5\n5\n5 5 5 5 5"] | 2 seconds | ["2 7 7 2\n2 2 1 1\n5 5 5 5"] | NoteThere is only one way to choose four sticks in the first list, they form a rectangle with sides $$$2$$$ and $$$7$$$, its area is $$$2 \cdot 7 = 14$$$, perimeter is $$$2(2 + 7) = 18$$$. $$$\frac{18^2}{14} \approx 23.143$$$.The second list contains subsets of four sticks that can form rectangles with sides $$$(1, 2)$$$, $$$(2, 8)$$$ and $$$(1, 8)$$$. Their values are $$$\frac{6^2}{2} = 18$$$, $$$\frac{20^2}{16} = 25$$$ and $$$\frac{18^2}{8} = 40.5$$$, respectively. The minimal one of them is the rectangle $$$(1, 2)$$$.You can choose any four of the $$$5$$$ given sticks from the third list, they will form a square with side $$$5$$$, which is still a rectangle with sides $$$(5, 5)$$$. | Java 8 | standard input | [
"greedy"
] | fc54d6febbf1d91e9f0e6121f248d2aa | The first line contains a single integer $$$T$$$ ($$$T \ge 1$$$) — the number of lists of sticks in the testcase. Then $$$2T$$$ lines follow — lines $$$(2i - 1)$$$ and $$$2i$$$ of them describe the $$$i$$$-th list. The first line of the pair contains a single integer $$$n$$$ ($$$4 \le n \le 10^6$$$) — the number of sticks in the $$$i$$$-th list. The second line of the pair contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_j \le 10^4$$$) — lengths of the sticks in the $$$i$$$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $$$T$$$ lists doesn't exceed $$$10^6$$$ in each testcase. | 1,600 | Print $$$T$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th list of the input. That is the lengths of the four sticks you choose from the $$$i$$$-th list, so that they form a rectangle and the value $$$\frac{P^2}{S}$$$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. | standard output | |
PASSED | 7a01d6c4d6cfd3818e6893cbd05ebc3b | train_002.jsonl | 1534602900 | You have $$$n$$$ sticks of the given lengths.Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks.Let $$$S$$$ be the area of the rectangle and $$$P$$$ be the perimeter of the rectangle. The chosen rectangle should have the value $$$\frac{P^2}{S}$$$ minimal possible. The value is taken without any rounding.If there are multiple answers, print any of them.Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.stream.Stream;
public class Solution implements Runnable {
static final double time = 1e9;
static final int MOD = (int) 1e9 + 7;
static final long mh = Long.MAX_VALUE;
static final Reader in = new Reader();
static final PrintWriter out = new PrintWriter(System.out);
StringBuilder answer = new StringBuilder();
public static void main(String[] args) {
new Thread(null, new Solution(), "persefone", 1 << 28).start();
}
@Override
public void run() {
long start = System.nanoTime();
solve();
printf();
long elapsed = System.nanoTime() - start;
// out.println("stamp : " + elapsed / time);
// out.println("stamp : " + TimeUnit.SECONDS.convert(elapsed,
// TimeUnit.NANOSECONDS));
close();
}
/*
x ^ 2 + (x + y) ^ 2 / x(x + y) = 2x(x+y) + y^2 / x(x + y)
y ^ 2 / x(x + y) min
*/
void solve() {
int m = 10001;
for (int t = in.nextInt(); t > 0; t--) {
int n = in.nextInt();
int[] f = in.nextIntArray(n);
PriorityQueue<Integer> queue = new PriorityQueue<>();
for (int i = 0; i < n; i++) queue.add(f[i]);
for (int i = 0; i < n; i++) f[i] = queue.poll();
int a = 0, b = 0;
int r = 1, prev = -1;
for (int i = 1; i < n; i++) {
if (f[i] == f[i - 1]) {
r++;
if (r == 4) {
a = b = f[i];
break;
} else if (r == 2) {
if (a == 0) {
a = f[i];
} else if (b == 0) {
b = f[i];
} else {
if (f[i] * a < b * prev) {
a = prev;
b = f[i];
}
}
prev = f[i];
}
} else {
r = 1;
}
}
add(a + " " + b + " " + a + " " + b, '\n');
}
}
public static int[] shuffle(int[] a, Random gen) {
for (int i = 0, n = a.length; i < n; i++) {
int ind = gen.nextInt(n - i) + i;
int d = a[i];
a[i] = a[ind];
a[ind] = d;
}
return a;
}
public static int[] shrinkX(int[] a) {
int n = a.length;
long[] b = new long[n];
for (int i = 0; i < n; i++)
b[i] = (long) a[i] << 32 | i;
Arrays.sort(b);
int[] ret = new int[n];
int p = 0;
ret[0] = (int) (b[0] >> 32);
for (int i = 0; i < n; i++) {
if (i > 0 && (b[i] ^ b[i - 1]) >> 32 != 0) {
p++;
ret[p] = (int) (b[i] >> 32);
}
a[(int) b[i]] = p;
}
return Arrays.copyOf(ret, p + 1);
}
int pow(int a, int b, int p) {
long ans = 1, base = a;
while (b > 0) {
if ((b & 1) > 0) {
ans *= base %= p;
}
base *= base %= p;
b >>= 1;
}
return (int) ans;
}
public static int[] enumLowestPrimeFactors(int n) {
int tot = 0;
int[] lpf = new int[n + 1];
int u = n + 32;
double lu = Math.log(u);
int[] primes = new int[(int) (u / lu + u / lu / lu * 1.5)];
for (int i = 2; i <= n; i++)
lpf[i] = i;
for (int p = 2; p <= n; p++) {
if (lpf[p] == p)
primes[tot++] = p;
int tmp;
for (int i = 0; i < tot && primes[i] <= lpf[p] && (tmp = primes[i] * p) <= n; i++) {
lpf[tmp] = primes[i];
}
}
return lpf;
}
public static int[] enumNumDivisorsFast(int n, int[] lpf) {
int[] nd = new int[n + 1];
nd[1] = 1;
for (int i = 2; i <= n; i++) {
int j = i, e = 0;
while (j % lpf[i] == 0) {
j /= lpf[i];
e++;
}
nd[i] = nd[j] * (e + 1);
}
return nd;
}
boolean isPrime(long n) {
for (int i = 2; 1l * i * i <= n; i++)
if (n % i == 0)
return false;
return true;
}
void printf() {
out.print(answer);
}
void close() {
out.close();
}
void printf(Stream<?> str) {
str.forEach(o -> add(o, " "));
add("\n");
}
void printf(Object... obj) {
printf(false, obj);
}
void printfWithDescription(Object... obj) {
printf(true, obj);
}
private void printf(boolean b, Object... obj) {
if (obj.length > 1) {
for (int i = 0; i < obj.length; i++) {
if (b)
add(obj[i].getClass().getSimpleName(), " - ");
if (obj[i] instanceof Collection<?>) {
printf((Collection<?>) obj[i]);
} else if (obj[i] instanceof int[][]) {
printf((int[][]) obj[i]);
} else if (obj[i] instanceof long[][]) {
printf((long[][]) obj[i]);
} else if (obj[i] instanceof double[][]) {
printf((double[][]) obj[i]);
} else
printf(obj[i]);
}
return;
}
if (b)
add(obj[0].getClass().getSimpleName(), " - ");
printf(obj[0]);
}
void printf(Object o) {
if (o instanceof int[])
printf(Arrays.stream((int[]) o).boxed());
else if (o instanceof char[])
printf(new String((char[]) o));
else if (o instanceof long[])
printf(Arrays.stream((long[]) o).boxed());
else if (o instanceof double[])
printf(Arrays.stream((double[]) o).boxed());
else if (o instanceof boolean[]) {
for (boolean b : (boolean[]) o)
add(b, " ");
add("\n");
} else
add(o, "\n");
}
void printf(int[]... obj) {
for (int i = 0; i < obj.length; i++)
printf(obj[i]);
}
void printf(long[]... obj) {
for (int i = 0; i < obj.length; i++)
printf(obj[i]);
}
void printf(double[]... obj) {
for (int i = 0; i < obj.length; i++)
printf(obj[i]);
}
void printf(boolean[]... obj) {
for (int i = 0; i < obj.length; i++)
printf(obj[i]);
}
void printf(Collection<?> col) {
printf(col.stream());
}
<T, K> void add(T t, K k) {
if (t instanceof Collection<?>) {
((Collection<?>) t).forEach(i -> add(i, " "));
} else if (t instanceof Object[]) {
Arrays.stream((Object[]) t).forEach(i -> add(i, " "));
} else
add(t);
add(k);
}
<T> void add(T t) {
answer.append(t);
}
@SuppressWarnings("unchecked")
<T extends Comparable<? super T>> T min(T... t) {
if (t.length == 0)
return null;
T m = t[0];
for (int i = 1; i < t.length; i++)
if (t[i].compareTo(m) < 0)
m = t[i];
return m;
}
@SuppressWarnings("unchecked")
<T extends Comparable<? super T>> T max(T... t) {
if (t.length == 0)
return null;
T m = t[0];
for (int i = 1; i < t.length; i++)
if (t[i].compareTo(m) > 0)
m = t[i];
return m;
}
int gcd(int a, int b) {
return (b == 0) ? a : gcd(b, a % b);
}
long gcd(long a, long b) {
return (b == 0) ? a : gcd(b, a % b);
}
long lcm(int a, int b) {
return (1l * a * b / gcd(a, b));
}
long lcm(long a, long b) {
if (a > Long.MAX_VALUE / b)
throw new RuntimeException();
return a * b / gcd(a, b);
}
// c = gcd(a, b) -> extends gcd method: ax + by = c <----> (b % a)p + q = c;
int[] ext_gcd(int a, int b) {
if (b == 0)
return new int[] { a, 1, 0 };
int[] vals = ext_gcd(b, a % b);
int d = vals[0]; // gcd
int p = vals[2]; //
int q = vals[1] - (a / b) * vals[2];
return new int[] { d, p, q };
}
// find any solution of the equation: ax + by = c using extends gcd
boolean find_any_solution(int a, int b, int c, int[] root) {
int[] vals = ext_gcd(Math.abs(a), Math.abs(b));
if (c % vals[0] != 0)
return false;
printf(vals);
root[0] = c * vals[1] / vals[0];
root[1] = c * vals[2] / vals[0];
if (a < 0)
root[0] *= -1;
if (b < 0)
root[1] *= -1;
return true;
}
int mod(int x) {
return x % MOD;
}
int mod(int x, int y) {
return mod(mod(x) + mod(y));
}
long mod(long x) {
return x % MOD;
}
long mod(long x, long y) {
return mod(mod(x) + mod(y));
}
int lw(long[] f, int l, int r, long k) {
int R = r, m = 0;
while (l <= r) {
m = l + r >> 1;
if (m == R)
return m;
if (f[m] >= k)
r = m - 1;
else
l = m + 1;
}
return l;
}
int up(long[] f, int l, int r, long k) {
int R = r, m = 0;
while (l <= r) {
m = l + r >> 1;
if (m == R)
return m;
if (f[m] > k)
r = m - 1;
else
l = m + 1;
}
return l;
}
int lw(int[] f, int l, int r, int k) {
int R = r, m = 0;
while (l <= r) {
m = l + r >> 1;
if (m == R)
return m;
if (f[m] >= k)
r = m - 1;
else
l = m + 1;
}
return l;
}
int up(int[] f, int l, int r, int k) {
int R = r, m = 0;
while (l <= r) {
m = l + r >> 1;
if (m == R)
return m;
if (f[m] > k)
r = m - 1;
else
l = m + 1;
}
return l;
}
<K extends Comparable<K>> int lw(List<K> li, int l, int r, K k) {
int R = r, m = 0;
while (l <= r) {
m = l + r >> 1;
if (m == R)
return m;
if (li.get(m).compareTo(k) >= 0)
r = m - 1;
else
l = m + 1;
}
return l;
}
<K extends Comparable<K>> int up(List<K> li, int l, int r, K k) {
int R = r, m = 0;
while (l <= r) {
m = l + r >> 1;
if (m == R)
return m;
if (li.get(m).compareTo(k) > 0)
r = m - 1;
else
l = m + 1;
}
return l;
}
<K extends Comparable<K>> int bs(List<K> li, int l, int r, K k) {
while (l <= r) {
int m = l + r >> 1;
if (li.get(m) == k)
return m;
else if (li.get(m).compareTo(k) < 0)
l = m + 1;
else
r = m - 1;
}
return -1;
}
long calc(int base, int exponent, int p) {
if (exponent == 0)
return 1;
if (exponent == 1)
return base % p;
long m = calc(base, exponent >> 1, p);
if (exponent % 2 == 0)
return (m * m) % p;
return 1l * base * m % p * m % p;
}
long calc(int base, long exponent, int p) {
if (exponent == 0)
return 1;
if (exponent == 1)
return base % p;
long m = calc(base, exponent >> 1, p);
if (exponent % 2 == 0)
return (m * m) % p;
return 1l * base * m % p * m % p;
}
long calc(long base, long exponent, int p) {
if (exponent == 0)
return 1;
if (exponent == 1)
return base % p;
long m = calc(base, exponent >> 1, p);
if (exponent % 2 == 0)
return (m * m) % p;
return base * m % p * m % p;
}
long power(int base, int exponent) {
if (exponent == 0)
return 1;
long m = power(base, exponent / 2);
if (exponent % 2 == 0)
return m * m;
return base * m * m;
}
void swap(int[] a, int i, int j) {
a[i] ^= a[j];
a[j] ^= a[i];
a[i] ^= a[j];
}
void swap(long[] a, int i, int j) {
long tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
static class Pair<K extends Comparable<? super K>, V extends Comparable<? super V>>
implements Comparable<Pair<K, V>> {
private K k;
private V v;
Pair() {
}
Pair(K k, V v) {
this.k = k;
this.v = v;
}
K getK() {
return k;
}
V getV() {
return v;
}
void setK(K k) {
this.k = k;
}
void setV(V v) {
this.v = v;
}
void setKV(K k, V v) {
this.k = k;
this.v = v;
}
@SuppressWarnings("unchecked")
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || !(o instanceof Pair))
return false;
Pair<K, V> p = (Pair<K, V>) o;
return k.compareTo(p.k) == 0 && v.compareTo(p.v) == 0;
}
@Override
public int hashCode() {
int hash = 31;
hash = hash * 89 + k.hashCode();
hash = hash * 89 + v.hashCode();
return hash;
}
@Override
public int compareTo(Pair<K, V> pair) {
return k.compareTo(pair.k) == 0 ? v.compareTo(pair.v) : k.compareTo(pair.k);
}
@Override
public Pair<K, V> clone() {
return new Pair<K, V>(this.k, this.v);
}
@Override
public String toString() {
return String.valueOf(k).concat(" ").concat(String.valueOf(v)).concat("\n");
}
}
static class Reader {
private BufferedReader br;
private StringTokenizer st;
Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
try {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String s = "";
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
}
} | Java | ["3\n4\n7 2 2 7\n8\n2 8 1 4 8 2 1 5\n5\n5 5 5 5 5"] | 2 seconds | ["2 7 7 2\n2 2 1 1\n5 5 5 5"] | NoteThere is only one way to choose four sticks in the first list, they form a rectangle with sides $$$2$$$ and $$$7$$$, its area is $$$2 \cdot 7 = 14$$$, perimeter is $$$2(2 + 7) = 18$$$. $$$\frac{18^2}{14} \approx 23.143$$$.The second list contains subsets of four sticks that can form rectangles with sides $$$(1, 2)$$$, $$$(2, 8)$$$ and $$$(1, 8)$$$. Their values are $$$\frac{6^2}{2} = 18$$$, $$$\frac{20^2}{16} = 25$$$ and $$$\frac{18^2}{8} = 40.5$$$, respectively. The minimal one of them is the rectangle $$$(1, 2)$$$.You can choose any four of the $$$5$$$ given sticks from the third list, they will form a square with side $$$5$$$, which is still a rectangle with sides $$$(5, 5)$$$. | Java 8 | standard input | [
"greedy"
] | fc54d6febbf1d91e9f0e6121f248d2aa | The first line contains a single integer $$$T$$$ ($$$T \ge 1$$$) — the number of lists of sticks in the testcase. Then $$$2T$$$ lines follow — lines $$$(2i - 1)$$$ and $$$2i$$$ of them describe the $$$i$$$-th list. The first line of the pair contains a single integer $$$n$$$ ($$$4 \le n \le 10^6$$$) — the number of sticks in the $$$i$$$-th list. The second line of the pair contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_j \le 10^4$$$) — lengths of the sticks in the $$$i$$$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $$$T$$$ lists doesn't exceed $$$10^6$$$ in each testcase. | 1,600 | Print $$$T$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th list of the input. That is the lengths of the four sticks you choose from the $$$i$$$-th list, so that they form a rectangle and the value $$$\frac{P^2}{S}$$$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. | standard output | |
PASSED | 11e75e1a0e7836b49b8bfe077b424cd8 | train_002.jsonl | 1534602900 | You have $$$n$$$ sticks of the given lengths.Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks.Let $$$S$$$ be the area of the rectangle and $$$P$$$ be the perimeter of the rectangle. The chosen rectangle should have the value $$$\frac{P^2}{S}$$$ minimal possible. The value is taken without any rounding.If there are multiple answers, print any of them.Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Random;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.readInt();
int[] sticks = new int[n];
for (int i = 0; i < n; ++i) {
sticks[i] = in.readInt();
}
ArrayUtils.sort(sticks);
int k = 0;
int cur = 0;
while (cur + 1 < n) {
if (sticks[cur] == sticks[cur + 1]) {
sticks[k++] = sticks[cur];
sticks[k++] = sticks[cur];
cur++;
}
cur++;
}
long curA = 10000;
long curB = 1;
boolean found = false;
for (int i = 3; i < k; ++i) {
int a = sticks[i];
int b = sticks[i - 2];
if (sticks[i - 3] != b || sticks[i - 1] != a) {
continue;
}
if (!found || curA * curB * (a * a + b * b) < (curA * curA + curB * curB) * a * b) {
curA = a;
curB = b;
found = true;
}
}
out.printf("%d %d %d %d\n", curA, curA, curB, curB);
}
}
static class ArrayUtils {
public static void sort(int[] a) {
ArrayShuffler.shuffle(a);
Arrays.sort(a);
}
}
static class ArrayShuffler {
static Random random = new Random(7428429L);
public static void shuffle(int[] p) {
for (int i = 0; i < p.length; ++i) {
int j = i + random.nextInt(p.length - i);
int temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
// InputMismatchException -> UnknownError
if (numChars == -1)
throw new UnknownError();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new UnknownError();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
} else if (c == '+') {
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
}
}
| Java | ["3\n4\n7 2 2 7\n8\n2 8 1 4 8 2 1 5\n5\n5 5 5 5 5"] | 2 seconds | ["2 7 7 2\n2 2 1 1\n5 5 5 5"] | NoteThere is only one way to choose four sticks in the first list, they form a rectangle with sides $$$2$$$ and $$$7$$$, its area is $$$2 \cdot 7 = 14$$$, perimeter is $$$2(2 + 7) = 18$$$. $$$\frac{18^2}{14} \approx 23.143$$$.The second list contains subsets of four sticks that can form rectangles with sides $$$(1, 2)$$$, $$$(2, 8)$$$ and $$$(1, 8)$$$. Their values are $$$\frac{6^2}{2} = 18$$$, $$$\frac{20^2}{16} = 25$$$ and $$$\frac{18^2}{8} = 40.5$$$, respectively. The minimal one of them is the rectangle $$$(1, 2)$$$.You can choose any four of the $$$5$$$ given sticks from the third list, they will form a square with side $$$5$$$, which is still a rectangle with sides $$$(5, 5)$$$. | Java 8 | standard input | [
"greedy"
] | fc54d6febbf1d91e9f0e6121f248d2aa | The first line contains a single integer $$$T$$$ ($$$T \ge 1$$$) — the number of lists of sticks in the testcase. Then $$$2T$$$ lines follow — lines $$$(2i - 1)$$$ and $$$2i$$$ of them describe the $$$i$$$-th list. The first line of the pair contains a single integer $$$n$$$ ($$$4 \le n \le 10^6$$$) — the number of sticks in the $$$i$$$-th list. The second line of the pair contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_j \le 10^4$$$) — lengths of the sticks in the $$$i$$$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $$$T$$$ lists doesn't exceed $$$10^6$$$ in each testcase. | 1,600 | Print $$$T$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th list of the input. That is the lengths of the four sticks you choose from the $$$i$$$-th list, so that they form a rectangle and the value $$$\frac{P^2}{S}$$$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. | standard output | |
PASSED | fe81bbbf6a68ca7b27dcd71309083c7a | train_002.jsonl | 1534602900 | You have $$$n$$$ sticks of the given lengths.Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks.Let $$$S$$$ be the area of the rectangle and $$$P$$$ be the perimeter of the rectangle. The chosen rectangle should have the value $$$\frac{P^2}{S}$$$ minimal possible. The value is taken without any rounding.If there are multiple answers, print any of them.Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Random;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.readInt();
int[] sticks = new int[n];
for (int i = 0; i < n; ++i) {
sticks[i] = in.readInt();
}
ArrayUtils.sort(sticks);
int k = 0;
int cur = 0;
while (cur + 1 < n) {
if (sticks[cur] == sticks[cur + 1]) {
sticks[k++] = sticks[cur];
sticks[k++] = sticks[cur];
cur++;
}
cur++;
}
long curA = 10000;
long curB = 1;
boolean found = false;
for (int i = 3; i < k; ++i) {
int a = sticks[i];
int b = sticks[i - 2];
if (sticks[i - 3] != b || sticks[i - 1] != a) {
continue;
}
if (!found || a * curB < curA * b) {
curA = a;
curB = b;
found = true;
}
}
out.printf("%d %d %d %d\n", curA, curA, curB, curB);
}
}
static class ArrayUtils {
public static void sort(int[] a) {
ArrayShuffler.shuffle(a);
Arrays.sort(a);
}
}
static class ArrayShuffler {
static Random random = new Random(7428429L);
public static void shuffle(int[] p) {
for (int i = 0; i < p.length; ++i) {
int j = i + random.nextInt(p.length - i);
int temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
// InputMismatchException -> UnknownError
if (numChars == -1)
throw new UnknownError();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new UnknownError();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
} else if (c == '+') {
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
}
}
| Java | ["3\n4\n7 2 2 7\n8\n2 8 1 4 8 2 1 5\n5\n5 5 5 5 5"] | 2 seconds | ["2 7 7 2\n2 2 1 1\n5 5 5 5"] | NoteThere is only one way to choose four sticks in the first list, they form a rectangle with sides $$$2$$$ and $$$7$$$, its area is $$$2 \cdot 7 = 14$$$, perimeter is $$$2(2 + 7) = 18$$$. $$$\frac{18^2}{14} \approx 23.143$$$.The second list contains subsets of four sticks that can form rectangles with sides $$$(1, 2)$$$, $$$(2, 8)$$$ and $$$(1, 8)$$$. Their values are $$$\frac{6^2}{2} = 18$$$, $$$\frac{20^2}{16} = 25$$$ and $$$\frac{18^2}{8} = 40.5$$$, respectively. The minimal one of them is the rectangle $$$(1, 2)$$$.You can choose any four of the $$$5$$$ given sticks from the third list, they will form a square with side $$$5$$$, which is still a rectangle with sides $$$(5, 5)$$$. | Java 8 | standard input | [
"greedy"
] | fc54d6febbf1d91e9f0e6121f248d2aa | The first line contains a single integer $$$T$$$ ($$$T \ge 1$$$) — the number of lists of sticks in the testcase. Then $$$2T$$$ lines follow — lines $$$(2i - 1)$$$ and $$$2i$$$ of them describe the $$$i$$$-th list. The first line of the pair contains a single integer $$$n$$$ ($$$4 \le n \le 10^6$$$) — the number of sticks in the $$$i$$$-th list. The second line of the pair contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_j \le 10^4$$$) — lengths of the sticks in the $$$i$$$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $$$T$$$ lists doesn't exceed $$$10^6$$$ in each testcase. | 1,600 | Print $$$T$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th list of the input. That is the lengths of the four sticks you choose from the $$$i$$$-th list, so that they form a rectangle and the value $$$\frac{P^2}{S}$$$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. | standard output | |
PASSED | 802ce724cb1cd72276ecd7535879ea59 | train_002.jsonl | 1534602900 | You have $$$n$$$ sticks of the given lengths.Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks.Let $$$S$$$ be the area of the rectangle and $$$P$$$ be the perimeter of the rectangle. The chosen rectangle should have the value $$$\frac{P^2}{S}$$$ minimal possible. The value is taken without any rounding.If there are multiple answers, print any of them.Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
public class C {
public void run() throws Exception {
FastScanner sc = new FastScanner();
int T = sc.nextInt();
while(T-- >0) {
int n = sc.nextInt();
ArrayList<Integer> sticks = new ArrayList<Integer>();
TreeMap<Integer, Integer> freq = new TreeMap<Integer, Integer>();
for(int i = 0; i < n; i++) {
int k = sc.nextInt();
if(freq.containsKey(k)) {
freq.put(k, freq.get(k)+1);
} else {
freq.put(k, 1);
}
}
for(int key : freq.keySet()) {
int f = freq.get(key);
while(f > 1) {
f -= 2;
sticks.add(key);
}
}
Collections.sort(sticks);
//System.out.println(sticks);
double mindif = Integer.MAX_VALUE;
int loc = -1;
for(int i = 0; i < sticks.size()-1; i++) {
if((double) sticks.get(i)/sticks.get(i+1) + (double) sticks.get(i+1)/sticks.get(i) < mindif) {
mindif = (double) sticks.get(i)/sticks.get(i+1) + (double) sticks.get(i+1)/sticks.get(i);
loc = i;
}
}
System.out.println(sticks.get(loc) + " " + sticks.get(loc) + " " + sticks.get(loc+1) + " " + sticks.get(loc+1));
}
}
static class FastScanner {
public BufferedReader reader;
public StringTokenizer tokenizer;
public FastScanner() {
reader = new BufferedReader(new InputStreamReader(System.in), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public static void main(String[] args) throws Exception {
new C().run();
}
} | Java | ["3\n4\n7 2 2 7\n8\n2 8 1 4 8 2 1 5\n5\n5 5 5 5 5"] | 2 seconds | ["2 7 7 2\n2 2 1 1\n5 5 5 5"] | NoteThere is only one way to choose four sticks in the first list, they form a rectangle with sides $$$2$$$ and $$$7$$$, its area is $$$2 \cdot 7 = 14$$$, perimeter is $$$2(2 + 7) = 18$$$. $$$\frac{18^2}{14} \approx 23.143$$$.The second list contains subsets of four sticks that can form rectangles with sides $$$(1, 2)$$$, $$$(2, 8)$$$ and $$$(1, 8)$$$. Their values are $$$\frac{6^2}{2} = 18$$$, $$$\frac{20^2}{16} = 25$$$ and $$$\frac{18^2}{8} = 40.5$$$, respectively. The minimal one of them is the rectangle $$$(1, 2)$$$.You can choose any four of the $$$5$$$ given sticks from the third list, they will form a square with side $$$5$$$, which is still a rectangle with sides $$$(5, 5)$$$. | Java 8 | standard input | [
"greedy"
] | fc54d6febbf1d91e9f0e6121f248d2aa | The first line contains a single integer $$$T$$$ ($$$T \ge 1$$$) — the number of lists of sticks in the testcase. Then $$$2T$$$ lines follow — lines $$$(2i - 1)$$$ and $$$2i$$$ of them describe the $$$i$$$-th list. The first line of the pair contains a single integer $$$n$$$ ($$$4 \le n \le 10^6$$$) — the number of sticks in the $$$i$$$-th list. The second line of the pair contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_j \le 10^4$$$) — lengths of the sticks in the $$$i$$$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $$$T$$$ lists doesn't exceed $$$10^6$$$ in each testcase. | 1,600 | Print $$$T$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th list of the input. That is the lengths of the four sticks you choose from the $$$i$$$-th list, so that they form a rectangle and the value $$$\frac{P^2}{S}$$$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. | standard output | |
PASSED | b061642fd1498125c068154b8c6f9989 | train_002.jsonl | 1534602900 | You have $$$n$$$ sticks of the given lengths.Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks.Let $$$S$$$ be the area of the rectangle and $$$P$$$ be the perimeter of the rectangle. The chosen rectangle should have the value $$$\frac{P^2}{S}$$$ minimal possible. The value is taken without any rounding.If there are multiple answers, print any of them.Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
/*
* 数学题
*/
public class Main{
public static void main(String[] args) throws IOException {
// TODO 自动生成的方法存根
StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
in.nextToken();
int n=(int)in.nval;
for(int i=0;i<n;i++)
{
in.nextToken();int num=(int)in.nval;
int a[]=new int[num];
Map<Integer,Integer>map=new HashMap();
for(int j=0;j<num;j++)
{
in.nextToken();
int q=(int)in.nval;
if(map.containsKey(q)) {map.put(q, map.get(q)+1);}
else map.put(q, 1);
}
int index=0;int a2=0;int b2=0;
boolean b=false;
for(int q:map.keySet())
{
if(map.get(q)>=4) {a2=q;b2=q;b=true;break;}
else
if(map.get(q)>=2) {a[index++]=q;}
}
if(!b) {
Arrays.sort(a,0, index);//0-到index-1排序
//
double min=Double.MAX_VALUE;;
for(int q=0;q<index-1;q++)
{
double d=(double)a[q]/(double)a[q+1]+(double)a[q+1]/(double)a[q];
if(d<min) {min=d;a2=a[q];b2=a[q+1];}
//out.println(min+" "+d+" "+a[q]+" "+a[q+1]);
}
}
out.println(a2+" "+a2+" "+b2+" "+b2);
out.flush();
}
}
} | Java | ["3\n4\n7 2 2 7\n8\n2 8 1 4 8 2 1 5\n5\n5 5 5 5 5"] | 2 seconds | ["2 7 7 2\n2 2 1 1\n5 5 5 5"] | NoteThere is only one way to choose four sticks in the first list, they form a rectangle with sides $$$2$$$ and $$$7$$$, its area is $$$2 \cdot 7 = 14$$$, perimeter is $$$2(2 + 7) = 18$$$. $$$\frac{18^2}{14} \approx 23.143$$$.The second list contains subsets of four sticks that can form rectangles with sides $$$(1, 2)$$$, $$$(2, 8)$$$ and $$$(1, 8)$$$. Their values are $$$\frac{6^2}{2} = 18$$$, $$$\frac{20^2}{16} = 25$$$ and $$$\frac{18^2}{8} = 40.5$$$, respectively. The minimal one of them is the rectangle $$$(1, 2)$$$.You can choose any four of the $$$5$$$ given sticks from the third list, they will form a square with side $$$5$$$, which is still a rectangle with sides $$$(5, 5)$$$. | Java 8 | standard input | [
"greedy"
] | fc54d6febbf1d91e9f0e6121f248d2aa | The first line contains a single integer $$$T$$$ ($$$T \ge 1$$$) — the number of lists of sticks in the testcase. Then $$$2T$$$ lines follow — lines $$$(2i - 1)$$$ and $$$2i$$$ of them describe the $$$i$$$-th list. The first line of the pair contains a single integer $$$n$$$ ($$$4 \le n \le 10^6$$$) — the number of sticks in the $$$i$$$-th list. The second line of the pair contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_j \le 10^4$$$) — lengths of the sticks in the $$$i$$$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $$$T$$$ lists doesn't exceed $$$10^6$$$ in each testcase. | 1,600 | Print $$$T$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th list of the input. That is the lengths of the four sticks you choose from the $$$i$$$-th list, so that they form a rectangle and the value $$$\frac{P^2}{S}$$$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. | standard output | |
PASSED | 4313a18d612d0536f69c42e8915fdc23 | train_002.jsonl | 1534602900 | You have $$$n$$$ sticks of the given lengths.Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks.Let $$$S$$$ be the area of the rectangle and $$$P$$$ be the perimeter of the rectangle. The chosen rectangle should have the value $$$\frac{P^2}{S}$$$ minimal possible. The value is taken without any rounding.If there are multiple answers, print any of them.Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. | 256 megabytes |
import java.io.*;
import java.util.*;
public class C
{
static StringBuilder st = new StringBuilder() ;
public static void main(String[] args) throws Exception
{
Scanner sc = new Scanner();
PrintWriter out = new PrintWriter(System.out);
int TC = sc.nextInt() ;
while(TC -->0)
{
int n = sc.nextInt();
TreeMap<Integer,Integer> map = new TreeMap<>();
boolean [] vis = new boolean[(int)1e4+1];
boolean print = false ;
for(int i = 0 ; i < n ;i++)
{
int x = sc.nextInt() ;
if(print)continue ;
if(vis[x])
{
map.put(x, map.getOrDefault(x, 1)+1) ;
if(!print && (map.get(x) == 4))
{
out.println(x+" "+x+" "+x+" "+x);
while(i + 1 < n)
{
sc.next() ; i++;
}
print = true ;
}
}
else
vis[x] = true;
}
if(!print)
{
n = map.size() ;
int [] a = new int [n] ;
int idx = 0 ;
for(int x : map.keySet())
a[idx++] = x ;
double EPS = 1e-9 ;
double min = Double.MAX_VALUE;
int x = 0 ;
int y = 0 ;
for(int i = 0 ; i < n - 1 ; i ++)
{
int j = i + 1;
double ratio = (a[j]*1.0) / (a[i]*1.0) + (a[i]*1.0) / (a[j]*1.0);
if(min > ratio )
{
min = ratio ;
x = a[i] ;
y = a[j] ;
}
}
out.println(x+" "+x+" "+y+" "+y);
}
}
out.flush();
out.close();
}
static class Scanner
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st ;
Scanner(){}
Scanner(String path) throws Exception{br = new BufferedReader(new FileReader(path));}
String next() throws Exception {while(st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()) ; return st.nextToken();}
int nextInt() throws Exception { return Integer.parseInt(next()) ; }
long nextLong () throws Exception {return Long.parseLong(next());}
double nextDouble () throws Exception{return Double.parseDouble(next());}
}
static void shuffle(long[] a)
{
int n = a.length;
for (int i = 0; i < n; i++)
{
int r = i + (int) (Math.random() * (n - i));
long tmp = a[i];
a[i] = a[r];
a[r] = tmp;
}
}
private static boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private static void tr(Object... o) {if (!oj)System.out.println(Arrays.deepToString(o));
}
} | Java | ["3\n4\n7 2 2 7\n8\n2 8 1 4 8 2 1 5\n5\n5 5 5 5 5"] | 2 seconds | ["2 7 7 2\n2 2 1 1\n5 5 5 5"] | NoteThere is only one way to choose four sticks in the first list, they form a rectangle with sides $$$2$$$ and $$$7$$$, its area is $$$2 \cdot 7 = 14$$$, perimeter is $$$2(2 + 7) = 18$$$. $$$\frac{18^2}{14} \approx 23.143$$$.The second list contains subsets of four sticks that can form rectangles with sides $$$(1, 2)$$$, $$$(2, 8)$$$ and $$$(1, 8)$$$. Their values are $$$\frac{6^2}{2} = 18$$$, $$$\frac{20^2}{16} = 25$$$ and $$$\frac{18^2}{8} = 40.5$$$, respectively. The minimal one of them is the rectangle $$$(1, 2)$$$.You can choose any four of the $$$5$$$ given sticks from the third list, they will form a square with side $$$5$$$, which is still a rectangle with sides $$$(5, 5)$$$. | Java 8 | standard input | [
"greedy"
] | fc54d6febbf1d91e9f0e6121f248d2aa | The first line contains a single integer $$$T$$$ ($$$T \ge 1$$$) — the number of lists of sticks in the testcase. Then $$$2T$$$ lines follow — lines $$$(2i - 1)$$$ and $$$2i$$$ of them describe the $$$i$$$-th list. The first line of the pair contains a single integer $$$n$$$ ($$$4 \le n \le 10^6$$$) — the number of sticks in the $$$i$$$-th list. The second line of the pair contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_j \le 10^4$$$) — lengths of the sticks in the $$$i$$$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $$$T$$$ lists doesn't exceed $$$10^6$$$ in each testcase. | 1,600 | Print $$$T$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th list of the input. That is the lengths of the four sticks you choose from the $$$i$$$-th list, so that they form a rectangle and the value $$$\frac{P^2}{S}$$$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. | standard output | |
PASSED | 7f382f8b60aa878f72a002ea5e343401 | train_002.jsonl | 1534602900 | You have $$$n$$$ sticks of the given lengths.Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks.Let $$$S$$$ be the area of the rectangle and $$$P$$$ be the perimeter of the rectangle. The chosen rectangle should have the value $$$\frac{P^2}{S}$$$ minimal possible. The value is taken without any rounding.If there are multiple answers, print any of them.Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Collections;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
CMinimumValueRectangle solver = new CMinimumValueRectangle();
solver.solve(1, in, out);
out.close();
}
static class CMinimumValueRectangle {
public void solve(int testNumber, Scanner sc, PrintWriter pw) {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[] arr = new int[n];
int[] cnt = new int[10001];
ArrayList<Integer> arrl = new ArrayList<>();
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
cnt[arr[i]]++;
if (cnt[arr[i]] == 2) {
arrl.add(arr[i]);
cnt[arr[i]] = 0;
}
}
Collections.sort(arrl);
int a1, a2, b1 = 0, b2 = 0;
double sum1, min;
min = Double.MAX_VALUE;
a1 = arrl.get(0);
a2 = arrl.get(1);
sum1 = (4.0 * (a1 * a1 + 2 * a1 * a2 + a2 * a2)) / (1.0 * a1 * a2);
for (int i = 0; i < arrl.size() - 1; i++) {
a1 = arrl.get(i);
a2 = arrl.get(i + 1);
sum1 = (4.0 * (a1 * a1 + 2 * a1 * a2 + a2 * a2)) / (1.0 * a1 * a2);
if (min > sum1) {
min = sum1;
b1 = a1;
b2 = a2;
}
}
pw.println(b1 + " " + b1 + " " + b2 + " " + b2);
}
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["3\n4\n7 2 2 7\n8\n2 8 1 4 8 2 1 5\n5\n5 5 5 5 5"] | 2 seconds | ["2 7 7 2\n2 2 1 1\n5 5 5 5"] | NoteThere is only one way to choose four sticks in the first list, they form a rectangle with sides $$$2$$$ and $$$7$$$, its area is $$$2 \cdot 7 = 14$$$, perimeter is $$$2(2 + 7) = 18$$$. $$$\frac{18^2}{14} \approx 23.143$$$.The second list contains subsets of four sticks that can form rectangles with sides $$$(1, 2)$$$, $$$(2, 8)$$$ and $$$(1, 8)$$$. Their values are $$$\frac{6^2}{2} = 18$$$, $$$\frac{20^2}{16} = 25$$$ and $$$\frac{18^2}{8} = 40.5$$$, respectively. The minimal one of them is the rectangle $$$(1, 2)$$$.You can choose any four of the $$$5$$$ given sticks from the third list, they will form a square with side $$$5$$$, which is still a rectangle with sides $$$(5, 5)$$$. | Java 8 | standard input | [
"greedy"
] | fc54d6febbf1d91e9f0e6121f248d2aa | The first line contains a single integer $$$T$$$ ($$$T \ge 1$$$) — the number of lists of sticks in the testcase. Then $$$2T$$$ lines follow — lines $$$(2i - 1)$$$ and $$$2i$$$ of them describe the $$$i$$$-th list. The first line of the pair contains a single integer $$$n$$$ ($$$4 \le n \le 10^6$$$) — the number of sticks in the $$$i$$$-th list. The second line of the pair contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_j \le 10^4$$$) — lengths of the sticks in the $$$i$$$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $$$T$$$ lists doesn't exceed $$$10^6$$$ in each testcase. | 1,600 | Print $$$T$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th list of the input. That is the lengths of the four sticks you choose from the $$$i$$$-th list, so that they form a rectangle and the value $$$\frac{P^2}{S}$$$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. | standard output | |
PASSED | 60cf88601c4e4b68835e4a5bc55357d2 | train_002.jsonl | 1534602900 | You have $$$n$$$ sticks of the given lengths.Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks.Let $$$S$$$ be the area of the rectangle and $$$P$$$ be the perimeter of the rectangle. The chosen rectangle should have the value $$$\frac{P^2}{S}$$$ minimal possible. The value is taken without any rounding.If there are multiple answers, print any of them.Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. | 256 megabytes | import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Map;
public class Fast {
InputStream is;
PrintWriter out;
String INPUT = "";
public static void main(String[] args) throws Exception {
new Fast().run();
}
void run() throws Exception {
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis() - s + "ms");
}
private void solve() {
int T = nextInt();
long a;
long b;
double temp;
long answerA = 0;
long answerB = 0;
while (T-- > 0) {
int N = nextInt();
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < N; i++) {
int val = nextInt();
Integer res = map.get(val);
if (res == null) {
map.put(val, 1);
} else {
map.put(val, res + 1);
}
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() >= 4) {
list.add(entry.getKey());
list.add(entry.getKey());
} else if (entry.getValue() >= 2) {
list.add(entry.getKey());
}
}
Collections.sort(list);
Double result = null;
for (int i = 0; i < list.size() - 1; i++) {
a = list.get(i);
b = list.get(i + 1);
temp = ((4 * (a + b) * (a + b))) * 1.0 / (a * b);
if (result == null) {
result = temp;
answerA = a;
answerB = b;
} else {
if (result > temp) {
result = temp;
answerA = a;
answerB = b;
}
}
}
System.out.println(answerA + " " + answerA + " " + answerB + " "
+ answerB);
}
}
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte() {
if (lenbuf == -1)
throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b))
;
return b;
}
private double nextDouble() {
return Double.parseDouble(nextString());
}
private char nextCharactor() {
return (char) skip();
}
private String nextString() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b !=
// ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] nextString(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nextMap(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++)
map[i] = nextString(m);
return map;
}
private int[] nextIntArr(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
private int nextInt() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private long nextLong() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) {
if (!oj)
System.out.println(Arrays.deepToString(o));
}
}
| Java | ["3\n4\n7 2 2 7\n8\n2 8 1 4 8 2 1 5\n5\n5 5 5 5 5"] | 2 seconds | ["2 7 7 2\n2 2 1 1\n5 5 5 5"] | NoteThere is only one way to choose four sticks in the first list, they form a rectangle with sides $$$2$$$ and $$$7$$$, its area is $$$2 \cdot 7 = 14$$$, perimeter is $$$2(2 + 7) = 18$$$. $$$\frac{18^2}{14} \approx 23.143$$$.The second list contains subsets of four sticks that can form rectangles with sides $$$(1, 2)$$$, $$$(2, 8)$$$ and $$$(1, 8)$$$. Their values are $$$\frac{6^2}{2} = 18$$$, $$$\frac{20^2}{16} = 25$$$ and $$$\frac{18^2}{8} = 40.5$$$, respectively. The minimal one of them is the rectangle $$$(1, 2)$$$.You can choose any four of the $$$5$$$ given sticks from the third list, they will form a square with side $$$5$$$, which is still a rectangle with sides $$$(5, 5)$$$. | Java 8 | standard input | [
"greedy"
] | fc54d6febbf1d91e9f0e6121f248d2aa | The first line contains a single integer $$$T$$$ ($$$T \ge 1$$$) — the number of lists of sticks in the testcase. Then $$$2T$$$ lines follow — lines $$$(2i - 1)$$$ and $$$2i$$$ of them describe the $$$i$$$-th list. The first line of the pair contains a single integer $$$n$$$ ($$$4 \le n \le 10^6$$$) — the number of sticks in the $$$i$$$-th list. The second line of the pair contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_j \le 10^4$$$) — lengths of the sticks in the $$$i$$$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $$$T$$$ lists doesn't exceed $$$10^6$$$ in each testcase. | 1,600 | Print $$$T$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th list of the input. That is the lengths of the four sticks you choose from the $$$i$$$-th list, so that they form a rectangle and the value $$$\frac{P^2}{S}$$$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. | standard output | |
PASSED | 46644c517dd12b8e87365663788355d3 | train_002.jsonl | 1534602900 | You have $$$n$$$ sticks of the given lengths.Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks.Let $$$S$$$ be the area of the rectangle and $$$P$$$ be the perimeter of the rectangle. The chosen rectangle should have the value $$$\frac{P^2}{S}$$$ minimal possible. The value is taken without any rounding.If there are multiple answers, print any of them.Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. | 256 megabytes | import java.util.*;
import java.io.*;
public class A
{
public static void main(String ar[]) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
StringBuffer sb=new StringBuffer();
for(int r=0;r<t;r++)
{
int n=Integer.parseInt(br.readLine());
String s1[]=br.readLine().split(" ");
Integer b[]=new Integer[n];
for(int i=0;i<n;i++)
b[i]=Integer.parseInt(s1[i]);
Arrays.sort(b);
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=(int)b[i];
int xx=0,yy=0;
double min=20001.0;
int mm=Integer.MAX_VALUE;
//System.out.println(Arrays.toString(a));
int j=0,k=2;
while(j<n-3 && k<n-1)
{
if(k<=j+1)
k++;
else if(a[j]!=a[j+1])
j++;
else if(a[k]!=a[k+1])
k++;
else if((a[j]==a[j+1]) && (a[k]==a[k+1]))
{
int x=a[j];
int y=a[k];
int dd=y/x;
double d=(((double)(y%x))/((double)x));
//System.out.println(dd+" "+d);
if(dd<mm)
{ mm=dd; min=d; xx=x; yy=y; }
else if(dd==mm && d<min)
{ mm=dd; min=d; xx=x; yy=y; }
j++;
}
}
sb.append(xx).append(" ").append(xx).append(" ");
sb.append(yy).append(" ").append(yy).append("\n");
}
System.out.println(sb);
}
} | Java | ["3\n4\n7 2 2 7\n8\n2 8 1 4 8 2 1 5\n5\n5 5 5 5 5"] | 2 seconds | ["2 7 7 2\n2 2 1 1\n5 5 5 5"] | NoteThere is only one way to choose four sticks in the first list, they form a rectangle with sides $$$2$$$ and $$$7$$$, its area is $$$2 \cdot 7 = 14$$$, perimeter is $$$2(2 + 7) = 18$$$. $$$\frac{18^2}{14} \approx 23.143$$$.The second list contains subsets of four sticks that can form rectangles with sides $$$(1, 2)$$$, $$$(2, 8)$$$ and $$$(1, 8)$$$. Their values are $$$\frac{6^2}{2} = 18$$$, $$$\frac{20^2}{16} = 25$$$ and $$$\frac{18^2}{8} = 40.5$$$, respectively. The minimal one of them is the rectangle $$$(1, 2)$$$.You can choose any four of the $$$5$$$ given sticks from the third list, they will form a square with side $$$5$$$, which is still a rectangle with sides $$$(5, 5)$$$. | Java 8 | standard input | [
"greedy"
] | fc54d6febbf1d91e9f0e6121f248d2aa | The first line contains a single integer $$$T$$$ ($$$T \ge 1$$$) — the number of lists of sticks in the testcase. Then $$$2T$$$ lines follow — lines $$$(2i - 1)$$$ and $$$2i$$$ of them describe the $$$i$$$-th list. The first line of the pair contains a single integer $$$n$$$ ($$$4 \le n \le 10^6$$$) — the number of sticks in the $$$i$$$-th list. The second line of the pair contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_j \le 10^4$$$) — lengths of the sticks in the $$$i$$$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $$$T$$$ lists doesn't exceed $$$10^6$$$ in each testcase. | 1,600 | Print $$$T$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th list of the input. That is the lengths of the four sticks you choose from the $$$i$$$-th list, so that they form a rectangle and the value $$$\frac{P^2}{S}$$$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. | standard output | |
PASSED | 7cf36f6fc2419519d89c36247b6e3507 | train_002.jsonl | 1534602900 | You have $$$n$$$ sticks of the given lengths.Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks.Let $$$S$$$ be the area of the rectangle and $$$P$$$ be the perimeter of the rectangle. The chosen rectangle should have the value $$$\frac{P^2}{S}$$$ minimal possible. The value is taken without any rounding.If there are multiple answers, print any of them.Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Codechef
{ static PrintWriter out=new PrintWriter(System.out);static FastScanner in = new FastScanner(System.in);static class FastScanner {BufferedReader br;StringTokenizer stok;FastScanner(InputStream is) {br = new BufferedReader(new InputStreamReader(is));}
String next() throws IOException {while (stok == null || !stok.hasMoreTokens()) {String s = br.readLine();if (s == null) {return null;}
stok = new StringTokenizer(s);}return stok.nextToken();}
int ni() throws IOException {return Integer.parseInt(next());}long nl() throws IOException {return Long.parseLong(next());}double nd() throws IOException {return Double.parseDouble(next());}char nc() throws IOException {return (char) (br.read());}String ns() throws IOException {return br.readLine();}
int[] nia(int n) throws IOException{int a[] = new int[n];for (int i = 0; i < n; i++)a[i] = ni();return a;}long[] nla(int n) throws IOException {long a[] = new long[n];for (int i = 0; i < n; i++)a[i] = nl();return a;}
double[] nda(int n)throws IOException {double a[] = new double[n];for (int i = 0; i < n; i++)a[i] = nd();return a;}int [][] imat(int n,int m) throws IOException{int mat[][]=new int[n][m];for(int i=0;i<n;i++){for(int j=0;j<m;j++)mat[i][j]=ni();}return mat;}
}
static long mod=Long.MAX_VALUE;
public static void main (String[] args) throws java.lang.Exception
{ int i,j;
HashSet<Integer> set=new HashSet<Integer>();
PriorityQueue<Integer> pq=new PriorityQueue<Integer>();
int t=in.ni();
while(t--!=0)
{
int n=in.ni();
int a[]=in.nia(n);
HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>();
for(i=0;i<n;i++)
{ int z=a[i];
if(hm.containsKey(z))
hm.put(z,hm.get(z)+1);
else
hm.put(z,1);
}
ArrayList<Integer> arr=new ArrayList<>();
for(int c:hm.keySet())
if(hm.get(c)>=2)
arr.add(c);
Collections.sort(arr);
//out.println(hm);
double min=1000000000000000.0;int ans1=0,ans2=0;
for(i=0;i<arr.size();i++)
{ int p=arr.get(i);
if(hm.get(p)>=4)
{ int x=4*p;
int y=p*p;
double temp=(double)(x*x)/(double)y;
//out.println("p="+p+" q="+p+"temp="+temp);
if(temp<=min)
{ ans1=p;
ans2=p;
min=temp;
}
}
if(hm.get(p)>=2 && i+1<arr.size())
{ int q=arr.get(i+1);
if(hm.get(q)>=2)
{
int x=2*(p+q);
int y=p*q;
double temp=(double)(x*x)/(double)y;
//out.println("p="+p+" q="+q+"temp="+temp);
if(temp<=min)
{ ans1=p;
ans2=q;
min=temp;
}
}
}
}
out.println(ans1+" "+ans1+" "+ans2+" "+ans2);
}
out.close();
}
static class pair implements Comparable<pair>{
int x, y;
public pair(int x, int y){this.x = x; this.y = y;}
@Override
public int compareTo(pair arg0)
{ if(x<arg0.x) return -1;
else if(x==arg0.x)
{ if(y<arg0.y) return -1;
else if(y>arg0.y) return 1;
else return 0;
}
else return 1;
}
}
static long gcd(long a,long b)
{ if(b==0)
return a;
return gcd(b,a%b);
}
static long exponent(long a,long n)
{ long ans=1;
while(n!=0)
{ if(n%2==1)
ans=(ans*a)%mod;
a=(a*a)%mod;
n=n>>1;
}
return ans;
}
static int binarySearch(int a[], int item, int low, int high)
{ if (high <= low)
return (item > a[low])? (low + 1): low;
int mid = (low + high)/2;
if(item == a[mid])
return mid+1;
if(item > a[mid])
return binarySearch(a, item, mid+1, high);
return binarySearch(a, item, low, mid-1);
}
static void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1; int n2 = r - m; int L[] = new int [n1]; int R[] = new int [n2];
for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else{ arr[k] = R[j]; j++; } k++; } while (i < n1){ arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; }
}
static void Sort(int arr[], int l, int r) {if (l < r) { int m = (l+r)/2; Sort(arr, l, m); Sort(arr , m+1, r); merge(arr, l, m, r); } }
static void sort(int a[])
{Sort(a,0,a.length-1);}
} | Java | ["3\n4\n7 2 2 7\n8\n2 8 1 4 8 2 1 5\n5\n5 5 5 5 5"] | 2 seconds | ["2 7 7 2\n2 2 1 1\n5 5 5 5"] | NoteThere is only one way to choose four sticks in the first list, they form a rectangle with sides $$$2$$$ and $$$7$$$, its area is $$$2 \cdot 7 = 14$$$, perimeter is $$$2(2 + 7) = 18$$$. $$$\frac{18^2}{14} \approx 23.143$$$.The second list contains subsets of four sticks that can form rectangles with sides $$$(1, 2)$$$, $$$(2, 8)$$$ and $$$(1, 8)$$$. Their values are $$$\frac{6^2}{2} = 18$$$, $$$\frac{20^2}{16} = 25$$$ and $$$\frac{18^2}{8} = 40.5$$$, respectively. The minimal one of them is the rectangle $$$(1, 2)$$$.You can choose any four of the $$$5$$$ given sticks from the third list, they will form a square with side $$$5$$$, which is still a rectangle with sides $$$(5, 5)$$$. | Java 8 | standard input | [
"greedy"
] | fc54d6febbf1d91e9f0e6121f248d2aa | The first line contains a single integer $$$T$$$ ($$$T \ge 1$$$) — the number of lists of sticks in the testcase. Then $$$2T$$$ lines follow — lines $$$(2i - 1)$$$ and $$$2i$$$ of them describe the $$$i$$$-th list. The first line of the pair contains a single integer $$$n$$$ ($$$4 \le n \le 10^6$$$) — the number of sticks in the $$$i$$$-th list. The second line of the pair contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_j \le 10^4$$$) — lengths of the sticks in the $$$i$$$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $$$T$$$ lists doesn't exceed $$$10^6$$$ in each testcase. | 1,600 | Print $$$T$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th list of the input. That is the lengths of the four sticks you choose from the $$$i$$$-th list, so that they form a rectangle and the value $$$\frac{P^2}{S}$$$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. | standard output | |
PASSED | 478862df9837271a07f745302cc14be8 | train_002.jsonl | 1534602900 | You have $$$n$$$ sticks of the given lengths.Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks.Let $$$S$$$ be the area of the rectangle and $$$P$$$ be the perimeter of the rectangle. The chosen rectangle should have the value $$$\frac{P^2}{S}$$$ minimal possible. The value is taken without any rounding.If there are multiple answers, print any of them.Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. | 256 megabytes | import java.io.*;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class ExC {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
/*Solution starts here*/
int T = sc.nextInt();
for (int i = 0; i < T; i++) {
boolean[] buckets = new boolean[10001];
PriorityQueue<Integer> twins = new PriorityQueue<>();
int minA = Integer.MAX_VALUE;
int minB = Integer.MAX_VALUE;
int n = sc.nextInt();
int pred_min = 0;
int cur_min = 0;
double val_min = Integer.MAX_VALUE;
for (int j = 0; j < n; j++) {
int a = sc.nextInt();
if (buckets[a]) {
twins.add(a);
buckets[a] = false;
} else buckets[a] = true;
}
int pred = twins.poll();
while (twins.size() > 0) {
int cur = twins.poll();
int p = 2 * (pred+cur);
double s = (pred*cur);
double val = p*p/s;
if (val < val_min) {
val_min = val;
pred_min = pred;
cur_min = cur;
}
//System.out.println(String.format("(%d, %s) => %d", pred, cur, val));
pred = cur;
}
out.print(pred_min); out.print(' ');
out.print(pred_min); out.print(' ');
out.print(cur_min); out.print(' ');
out.print(cur_min); out.print(' ');
out.println();
}
/*Solution ends here*/
out.close();
}
/**
* CodeForces Competition
* IO Functionality by Flatfoot <= thx (http://codeforces.com/blog/entry/7018)
*/
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["3\n4\n7 2 2 7\n8\n2 8 1 4 8 2 1 5\n5\n5 5 5 5 5"] | 2 seconds | ["2 7 7 2\n2 2 1 1\n5 5 5 5"] | NoteThere is only one way to choose four sticks in the first list, they form a rectangle with sides $$$2$$$ and $$$7$$$, its area is $$$2 \cdot 7 = 14$$$, perimeter is $$$2(2 + 7) = 18$$$. $$$\frac{18^2}{14} \approx 23.143$$$.The second list contains subsets of four sticks that can form rectangles with sides $$$(1, 2)$$$, $$$(2, 8)$$$ and $$$(1, 8)$$$. Their values are $$$\frac{6^2}{2} = 18$$$, $$$\frac{20^2}{16} = 25$$$ and $$$\frac{18^2}{8} = 40.5$$$, respectively. The minimal one of them is the rectangle $$$(1, 2)$$$.You can choose any four of the $$$5$$$ given sticks from the third list, they will form a square with side $$$5$$$, which is still a rectangle with sides $$$(5, 5)$$$. | Java 8 | standard input | [
"greedy"
] | fc54d6febbf1d91e9f0e6121f248d2aa | The first line contains a single integer $$$T$$$ ($$$T \ge 1$$$) — the number of lists of sticks in the testcase. Then $$$2T$$$ lines follow — lines $$$(2i - 1)$$$ and $$$2i$$$ of them describe the $$$i$$$-th list. The first line of the pair contains a single integer $$$n$$$ ($$$4 \le n \le 10^6$$$) — the number of sticks in the $$$i$$$-th list. The second line of the pair contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_j \le 10^4$$$) — lengths of the sticks in the $$$i$$$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $$$T$$$ lists doesn't exceed $$$10^6$$$ in each testcase. | 1,600 | Print $$$T$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th list of the input. That is the lengths of the four sticks you choose from the $$$i$$$-th list, so that they form a rectangle and the value $$$\frac{P^2}{S}$$$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. | standard output | |
PASSED | 03b0df82ca7c9a5a8937066c4d770d8c | train_002.jsonl | 1534602900 | You have $$$n$$$ sticks of the given lengths.Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks.Let $$$S$$$ be the area of the rectangle and $$$P$$$ be the perimeter of the rectangle. The chosen rectangle should have the value $$$\frac{P^2}{S}$$$ minimal possible. The value is taken without any rounding.If there are multiple answers, print any of them.Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.util.Collections;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author kessido
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
CMinimumValueRectangle solver = new CMinimumValueRectangle();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class CMinimumValueRectangle {
int[] op = new int[10001];
int[] op2 = new int[10001];
ArrayList<Integer> options = new ArrayList<>();
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.NextInt();
options.clear();
while (n-- != 0) {
int a = in.NextInt();
if (op[a] == testNumber) {
op[a] = 0;
options.add(a);
if (op2[a] == testNumber) {
out.println(a + " " + a + " " + a + " " + a);
while (n-- != 0) {
int aa = in.NextInt();
}
return;
} else {
op2[a] = testNumber;
}
} else {
op[a] = testNumber;
}
}
int bestOptionX = 0, bestOptionY = 0;
double best = Double.POSITIVE_INFINITY;
Collections.sort(options);
for (int i1 = 1; i1 < options.size(); i1++) {
int i = options.get(i1 - 1);
int j = options.get(i1);
if (best > ((double) j) / i) {
best = ((double) j) / i;
bestOptionX = i;
bestOptionY = j;
}
}
out.println(bestOptionX + " " + bestOptionX + " " + bestOptionY + " " + bestOptionY);
}
}
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine(), " \t\n\r\f,");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int NextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["3\n4\n7 2 2 7\n8\n2 8 1 4 8 2 1 5\n5\n5 5 5 5 5"] | 2 seconds | ["2 7 7 2\n2 2 1 1\n5 5 5 5"] | NoteThere is only one way to choose four sticks in the first list, they form a rectangle with sides $$$2$$$ and $$$7$$$, its area is $$$2 \cdot 7 = 14$$$, perimeter is $$$2(2 + 7) = 18$$$. $$$\frac{18^2}{14} \approx 23.143$$$.The second list contains subsets of four sticks that can form rectangles with sides $$$(1, 2)$$$, $$$(2, 8)$$$ and $$$(1, 8)$$$. Their values are $$$\frac{6^2}{2} = 18$$$, $$$\frac{20^2}{16} = 25$$$ and $$$\frac{18^2}{8} = 40.5$$$, respectively. The minimal one of them is the rectangle $$$(1, 2)$$$.You can choose any four of the $$$5$$$ given sticks from the third list, they will form a square with side $$$5$$$, which is still a rectangle with sides $$$(5, 5)$$$. | Java 8 | standard input | [
"greedy"
] | fc54d6febbf1d91e9f0e6121f248d2aa | The first line contains a single integer $$$T$$$ ($$$T \ge 1$$$) — the number of lists of sticks in the testcase. Then $$$2T$$$ lines follow — lines $$$(2i - 1)$$$ and $$$2i$$$ of them describe the $$$i$$$-th list. The first line of the pair contains a single integer $$$n$$$ ($$$4 \le n \le 10^6$$$) — the number of sticks in the $$$i$$$-th list. The second line of the pair contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_j \le 10^4$$$) — lengths of the sticks in the $$$i$$$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $$$T$$$ lists doesn't exceed $$$10^6$$$ in each testcase. | 1,600 | Print $$$T$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th list of the input. That is the lengths of the four sticks you choose from the $$$i$$$-th list, so that they form a rectangle and the value $$$\frac{P^2}{S}$$$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. | standard output | |
PASSED | e7e70acd1af2d05e0ef8c425b379076d | train_002.jsonl | 1401809400 | Polycarpus adores TV series. Right now he is ready to finish watching a season of a popular sitcom "Graph Theory". In total, the season has n episodes, numbered with integers from 1 to n.Polycarpus watches episodes not one by one but in a random order. He has already watched all the episodes except for one. Which episode has Polycaprus forgotten to watch? | 256 megabytes | import java.util.ArrayList;
import java.util.Scanner;
public class Forgotten_Episode {
public static void main(String args[]) {
Scanner reader = new Scanner(System.in);
int t = reader.nextInt();
boolean[] episode = new boolean[t];
while (t-- > 1)
episode[reader.nextInt() - 1] = true;
reader.close();
for (t = 0; t < episode.length && episode[t] == true; t++) {
}
System.out.print(t + 1);
}
}
| Java | ["10\n3 8 10 1 7 9 6 5 2"] | 1 second | ["4"] | null | Java 11 | standard input | [
"implementation"
] | 0e4ff955c1e653fbeb003987fa701729 | The first line of the input contains integer n (2 ≤ n ≤ 100000) — the number of episodes in a season. Assume that the episodes are numbered by integers from 1 to n. The second line contains n - 1 integer a1, a2, ..., an (1 ≤ ai ≤ n) — the numbers of episodes that Polycarpus has watched. All values of ai are distinct. | 800 | Print the number of the episode that Polycarpus hasn't watched. | standard output | |
PASSED | 9dc9539dbae1d24693dea7263e6ec3c4 | train_002.jsonl | 1401809400 | Polycarpus adores TV series. Right now he is ready to finish watching a season of a popular sitcom "Graph Theory". In total, the season has n episodes, numbered with integers from 1 to n.Polycarpus watches episodes not one by one but in a random order. He has already watched all the episodes except for one. Which episode has Polycaprus forgotten to watch? | 256 megabytes | import java.io.*;
import java.util.*;
public class ForgottenEpisode {
static final Scanner in = new Scanner(System.in);
public static void main(String[] args) {
//int t=in.nextInt();
//for (int i=1; i<=t; ++i) {
new Solver();
//}
in.close();
}
static class Solver {
Solver() {
int length = in.nextInt();
int sum = 0;
int baseSum = 0;
for (int i = 1; i < length; i++) {
sum+=in.nextInt();
baseSum+=i;
}
System.out.println((baseSum+length)-sum);
}
}
} | Java | ["10\n3 8 10 1 7 9 6 5 2"] | 1 second | ["4"] | null | Java 11 | standard input | [
"implementation"
] | 0e4ff955c1e653fbeb003987fa701729 | The first line of the input contains integer n (2 ≤ n ≤ 100000) — the number of episodes in a season. Assume that the episodes are numbered by integers from 1 to n. The second line contains n - 1 integer a1, a2, ..., an (1 ≤ ai ≤ n) — the numbers of episodes that Polycarpus has watched. All values of ai are distinct. | 800 | Print the number of the episode that Polycarpus hasn't watched. | standard output | |
PASSED | 52803559bf7bb2744f561a5b5752bb31 | train_002.jsonl | 1401809400 | Polycarpus adores TV series. Right now he is ready to finish watching a season of a popular sitcom "Graph Theory". In total, the season has n episodes, numbered with integers from 1 to n.Polycarpus watches episodes not one by one but in a random order. He has already watched all the episodes except for one. Which episode has Polycaprus forgotten to watch? | 256 megabytes | import java.util.Scanner;
import java.util.*;
public class ayush {
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int[]b=new int[n-1];
int []a=new int [n];
for(int i=1;i<n+1;i++)
{
a[i-1]=i;
}
for(int i=0;i<n-1;i++)
{
b[i]=scan.nextInt();
}
int flag=0;
Arrays.sort(b);
for(int i=0;i<n-1;i++)
{
if(a[i]!=b[i])
{
System.out.println(a[i]);
flag=1;
break;
}
}
if(flag==0)
{
System.out.println(n);
}
}
} | Java | ["10\n3 8 10 1 7 9 6 5 2"] | 1 second | ["4"] | null | Java 11 | standard input | [
"implementation"
] | 0e4ff955c1e653fbeb003987fa701729 | The first line of the input contains integer n (2 ≤ n ≤ 100000) — the number of episodes in a season. Assume that the episodes are numbered by integers from 1 to n. The second line contains n - 1 integer a1, a2, ..., an (1 ≤ ai ≤ n) — the numbers of episodes that Polycarpus has watched. All values of ai are distinct. | 800 | Print the number of the episode that Polycarpus hasn't watched. | standard output | |
PASSED | 3b7ad155f16dd3a44d82c91c61cf5039 | train_002.jsonl | 1401809400 | Polycarpus adores TV series. Right now he is ready to finish watching a season of a popular sitcom "Graph Theory". In total, the season has n episodes, numbered with integers from 1 to n.Polycarpus watches episodes not one by one but in a random order. He has already watched all the episodes except for one. Which episode has Polycaprus forgotten to watch? | 256 megabytes |
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class episodes {
public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();
long n = Long.parseLong(s);
long sum = (n*(n+1))/2;
String s1 = reader.readLine();
String s2[] = s1.split(" ");
for(int i = 0 ; i<s2.length ; i++) {
sum = sum - Long.parseLong(s2[i]);
}
System.out.println(sum);
}
}
| Java | ["10\n3 8 10 1 7 9 6 5 2"] | 1 second | ["4"] | null | Java 11 | standard input | [
"implementation"
] | 0e4ff955c1e653fbeb003987fa701729 | The first line of the input contains integer n (2 ≤ n ≤ 100000) — the number of episodes in a season. Assume that the episodes are numbered by integers from 1 to n. The second line contains n - 1 integer a1, a2, ..., an (1 ≤ ai ≤ n) — the numbers of episodes that Polycarpus has watched. All values of ai are distinct. | 800 | Print the number of the episode that Polycarpus hasn't watched. | standard output | |
PASSED | 1bed9c5ebe5bde4aee92e824b2fb02bd | train_002.jsonl | 1401809400 | Polycarpus adores TV series. Right now he is ready to finish watching a season of a popular sitcom "Graph Theory". In total, the season has n episodes, numbered with integers from 1 to n.Polycarpus watches episodes not one by one but in a random order. He has already watched all the episodes except for one. Which episode has Polycaprus forgotten to watch? | 256 megabytes | import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Codeforces {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
boolean[] series = new boolean[n];
Arrays.fill(series, false);
for (int i = 0; i < n - 1; i++) {
int x = scanner.nextInt();
series[x - 1] = true;
}
int ans = -1;
for (int i = 0; i < n; i++) {
if (!series[i])
ans = i + 1;
}
System.out.println(ans);
}
}
| Java | ["10\n3 8 10 1 7 9 6 5 2"] | 1 second | ["4"] | null | Java 11 | standard input | [
"implementation"
] | 0e4ff955c1e653fbeb003987fa701729 | The first line of the input contains integer n (2 ≤ n ≤ 100000) — the number of episodes in a season. Assume that the episodes are numbered by integers from 1 to n. The second line contains n - 1 integer a1, a2, ..., an (1 ≤ ai ≤ n) — the numbers of episodes that Polycarpus has watched. All values of ai are distinct. | 800 | Print the number of the episode that Polycarpus hasn't watched. | standard output | |
PASSED | 82547cbc946e9f4ca8e92627ff528a8b | train_002.jsonl | 1401809400 | Polycarpus adores TV series. Right now he is ready to finish watching a season of a popular sitcom "Graph Theory". In total, the season has n episodes, numbered with integers from 1 to n.Polycarpus watches episodes not one by one but in a random order. He has already watched all the episodes except for one. Which episode has Polycaprus forgotten to watch? | 256 megabytes |
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class main {
public static void main(String[] args) {
int l,r,k,x=1,b=0,c,d,t,e=0,y,z;
Scanner in=new Scanner(System.in);
t=in.nextInt();
ArrayList<Integer>a =new ArrayList<>();
for(int i=0;i<t-1;i++)
{
a.add(in.nextInt());
}
Collections.sort(a);
//System.out.println(a);
for(int i=0;i<t-1;i++)
{
if(a.get(i)==x)
{
x++;
}
else
{
System.out.println(x);
System.exit(0);
}
}
System.out.println(x);
}
}
| Java | ["10\n3 8 10 1 7 9 6 5 2"] | 1 second | ["4"] | null | Java 11 | standard input | [
"implementation"
] | 0e4ff955c1e653fbeb003987fa701729 | The first line of the input contains integer n (2 ≤ n ≤ 100000) — the number of episodes in a season. Assume that the episodes are numbered by integers from 1 to n. The second line contains n - 1 integer a1, a2, ..., an (1 ≤ ai ≤ n) — the numbers of episodes that Polycarpus has watched. All values of ai are distinct. | 800 | Print the number of the episode that Polycarpus hasn't watched. | standard output | |
PASSED | 67fdc6549fe9f367e2986330ee7d3e7c | train_002.jsonl | 1401809400 | Polycarpus adores TV series. Right now he is ready to finish watching a season of a popular sitcom "Graph Theory". In total, the season has n episodes, numbered with integers from 1 to n.Polycarpus watches episodes not one by one but in a random order. He has already watched all the episodes except for one. Which episode has Polycaprus forgotten to watch? | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class solution {
public static void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[] = new int[n1];
int R[] = new int[n2];
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
public static void sort(int arr[], int l, int r) {
if (l < r) {
int m = (l + r) / 2;
sort(arr, l, m);
sort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
public static int Prec(char ch)
{
switch (ch)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
// The main method that converts given infix expression
// to postfix expression.
public static String infixToPostfix(String exp)
{
// initializing empty String for result
String result = new String("");
// initializing empty stack
Stack<Character> stack = new Stack<>();
for (int i = 0; i<exp.length(); ++i)
{
char c = exp.charAt(i);
// If the scanned character is an operand, add it to output.
if (Character.isLetterOrDigit(c))
result += c;
// If the scanned character is an '(', push it to the stack.
else if (c == '(')
stack.push(c);
// If the scanned character is an ')', pop and output from the stack
// until an '(' is encountered.
else if (c == ')')
{
while (!stack.isEmpty() && stack.peek() != '(')
result += stack.pop();
if (!stack.isEmpty() && stack.peek() != '(')
return "Invalid Expression"; // invalid expression
else
stack.pop();
}
else // an operator is encountered
{
while (!stack.isEmpty() && Prec(c) <= Prec(stack.peek())){
if(stack.peek() == '(')
return "Invalid Expression";
result += stack.pop();
}
stack.push(c);
}
}
// pop all the operators from the stack
while (!stack.isEmpty()){
if(stack.peek() == '(')
return "Invalid Expression";
result += stack.pop();
}
return result;
}
static class comp implements Comparator<ArrayList<Integer>> {
public int compare(ArrayList<Integer> a, ArrayList<Integer> b) {
return (a.get(0) - b.get(0));
}
}
public static String findSum(String str1, String str2)
{
// Before proceeding further, make sure length
// of str2 is larger.
if (str1.length() > str2.length()){
String t = str1;
str1 = str2;
str2 = t;
}
// Take an empty String for storing result
String str = "";
// Calculate length of both String
int n1 = str1.length(), n2 = str2.length();
// Reverse both of Strings
str1=new StringBuilder(str1).reverse().toString();
str2=new StringBuilder(str2).reverse().toString();
int carry = 0;
for (int i = 0; i < n1; i++)
{
// Do school mathematics, compute sum of
// current digits and carry
int sum = ((int)(str1.charAt(i) - '0') +
(int)(str2.charAt(i) - '0') + carry);
str += (char)(sum % 10 + '0');
// Calculate carry for next step
carry = sum / 10;
}
// Add remaining digits of larger number
for (int i = n1; i < n2; i++)
{
int sum = ((int)(str2.charAt(i) - '0') + carry);
str += (char)(sum % 10 + '0');
carry = sum / 10;
}
// Add remaining carry
if (carry > 0)
str += (char)(carry + '0');
// reverse resultant String
str = new StringBuilder(str).reverse().toString();
return str;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
boolean a[] = new boolean[n+1];
Arrays.fill(a,false);
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i=0;i<n-1;i++){
int b= Integer.parseInt(st.nextToken());
a[b] = true;
}
for(int i=1;i<=n;i++){
if(a[i]==false){
System.out.print(i+" ");
}
}
System.out.println();
}
} | Java | ["10\n3 8 10 1 7 9 6 5 2"] | 1 second | ["4"] | null | Java 11 | standard input | [
"implementation"
] | 0e4ff955c1e653fbeb003987fa701729 | The first line of the input contains integer n (2 ≤ n ≤ 100000) — the number of episodes in a season. Assume that the episodes are numbered by integers from 1 to n. The second line contains n - 1 integer a1, a2, ..., an (1 ≤ ai ≤ n) — the numbers of episodes that Polycarpus has watched. All values of ai are distinct. | 800 | Print the number of the episode that Polycarpus hasn't watched. | standard output | |
PASSED | 8144f7e9d2b225c60b84821e998e8e2e | train_002.jsonl | 1401809400 | Polycarpus adores TV series. Right now he is ready to finish watching a season of a popular sitcom "Graph Theory". In total, the season has n episodes, numbered with integers from 1 to n.Polycarpus watches episodes not one by one but in a random order. He has already watched all the episodes except for one. Which episode has Polycaprus forgotten to watch? | 256 megabytes | import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
long n = input.nextLong();
long sum = 0;
for (int i = 0; i < n - 1; i++) {
long x = input.nextLong();
sum += x;
}
long sum2 = n * (n + 1) / 2;
System.out.println(sum2 - sum);
}
}
| Java | ["10\n3 8 10 1 7 9 6 5 2"] | 1 second | ["4"] | null | Java 11 | standard input | [
"implementation"
] | 0e4ff955c1e653fbeb003987fa701729 | The first line of the input contains integer n (2 ≤ n ≤ 100000) — the number of episodes in a season. Assume that the episodes are numbered by integers from 1 to n. The second line contains n - 1 integer a1, a2, ..., an (1 ≤ ai ≤ n) — the numbers of episodes that Polycarpus has watched. All values of ai are distinct. | 800 | Print the number of the episode that Polycarpus hasn't watched. | standard output | |
PASSED | 621b89188b3c0c294e9d96fc1b0b0a1c | train_002.jsonl | 1401809400 | Polycarpus adores TV series. Right now he is ready to finish watching a season of a popular sitcom "Graph Theory". In total, the season has n episodes, numbered with integers from 1 to n.Polycarpus watches episodes not one by one but in a random order. He has already watched all the episodes except for one. Which episode has Polycaprus forgotten to watch? | 256 megabytes | import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class ForgottenEpisode_Mohamed {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < n-1; i++) {
list.add(scanner.nextInt());
}
Collections.sort(list);
for (int i = 1; i <= n; i++){
if(Collections.binarySearch(list,i) < 0){
System.out.println(i);
return;
}
}
}
} | Java | ["10\n3 8 10 1 7 9 6 5 2"] | 1 second | ["4"] | null | Java 11 | standard input | [
"implementation"
] | 0e4ff955c1e653fbeb003987fa701729 | The first line of the input contains integer n (2 ≤ n ≤ 100000) — the number of episodes in a season. Assume that the episodes are numbered by integers from 1 to n. The second line contains n - 1 integer a1, a2, ..., an (1 ≤ ai ≤ n) — the numbers of episodes that Polycarpus has watched. All values of ai are distinct. | 800 | Print the number of the episode that Polycarpus hasn't watched. | standard output | |
PASSED | 83ddc37056a6fd3fa6101f608483bf72 | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static class Scan {
private byte[] buf=new byte[1024];
private int index;
private InputStream in;
private int total;
public Scan()
{
in=System.in;
}
public int scan()throws IOException
{
if(total<0)
throw new InputMismatchException();
if(index>=total)
{
index=0;
total=in.read(buf);
if(total<=0)
return -1;
}
return buf[index++];
}
public int scanInt()throws IOException
{
int integer=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
integer*=10;
integer+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
return neg*integer;
}
public double scanDouble()throws IOException
{
double doub=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n)&&n!='.')
{
if(n>='0'&&n<='9')
{
doub*=10;
doub+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
if(n=='.')
{
n=scan();
double temp=1;
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
temp/=10;
doub+=(n-'0')*temp;
n=scan();
}
else throw new InputMismatchException();
}
}
return doub*neg;
}
public String scanString()throws IOException
{
StringBuilder sb=new StringBuilder();
int n=scan();
while(isWhiteSpace(n))
n=scan();
while(!isWhiteSpace(n))
{
sb.append((char)n);
n=scan();
}
return sb.toString();
}
private boolean isWhiteSpace(int n)
{
if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
return true;
return false;
}
}
public static void main(String args[]) throws IOException {
Scan input=new Scan();
int test=input.scanInt();
StringBuilder ans=new StringBuilder("");
for(int t=1;t<=test;t++) {
int n=input.scanInt();
ans.append(solve(n)+"\n");
}
System.out.println(ans);
}
public static StringBuilder solve(int n) {
ArrayList<Integer> arrli[]=new ArrayList[n+1];
for(int i=0;i<n+1;i++) {
arrli[i]=new ArrayList<>();
}
arrli[n].add(0);
int cnt=n;
int arr[]=new int[n];
int i=1;
for(;i<=n;i++) {
boolean in=false;
while(arrli[cnt].size()==0) {
cnt--;
in=true;
}
if(in) {
arrli[cnt].sort(null);
}
if(cnt==2) {
break;
}
int indx=arrli[cnt].get(0);
if(cnt%2==0) {
int l=indx;
int r=indx+cnt-1;
int pivot=(r+l-1)/2;
arr[pivot]=i;
int lft=pivot-1-l+1;
arrli[lft].add(l);
int rgt=r-(pivot+1)+1;
arrli[rgt].add(pivot+1);
}
else {
int l=indx;
int r=indx+cnt-1;
int pivot=(r+l)/2;
arr[pivot]=i;
int lft=pivot-1-l+1;
arrli[lft].add(l);
int rgt=r-(pivot+1)+1;
arrli[rgt].add(pivot+1);
}
arrli[cnt].remove(0);
}
for(int j=0;j<n-1;j++) {
if(arr[j]==0 && arr[j+1]==0) {
arr[j]=i;
i++;
}
}
for(int j=0;j<n;j++) {
if(arr[j]==0) {
arr[j]=i;
i++;
}
}
StringBuilder ans=new StringBuilder("");
for(i=0;i<n;i++) {
ans.append(arr[i]+" ");
}
return ans;
}
}
| Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | d2c26829f4b88717ef2eaa2cb667f69c | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static class Scan {
private byte[] buf=new byte[1024];
private int index;
private InputStream in;
private int total;
public Scan()
{
in=System.in;
}
public int scan()throws IOException
{
if(total<0)
throw new InputMismatchException();
if(index>=total)
{
index=0;
total=in.read(buf);
if(total<=0)
return -1;
}
return buf[index++];
}
public int scanInt()throws IOException
{
int integer=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
integer*=10;
integer+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
return neg*integer;
}
public double scanDouble()throws IOException
{
double doub=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n)&&n!='.')
{
if(n>='0'&&n<='9')
{
doub*=10;
doub+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
if(n=='.')
{
n=scan();
double temp=1;
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
temp/=10;
doub+=(n-'0')*temp;
n=scan();
}
else throw new InputMismatchException();
}
}
return doub*neg;
}
public String scanString()throws IOException
{
StringBuilder sb=new StringBuilder();
int n=scan();
while(isWhiteSpace(n))
n=scan();
while(!isWhiteSpace(n))
{
sb.append((char)n);
n=scan();
}
return sb.toString();
}
private boolean isWhiteSpace(int n)
{
if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
return true;
return false;
}
}
public static void main(String args[]) throws IOException {
Scan input=new Scan();
int test=input.scanInt();
StringBuilder ans=new StringBuilder("");
for(int t=1;t<=test;t++) {
int n=input.scanInt();
ans.append(solve(n)+"\n");
}
System.out.println(ans);
}
public static StringBuilder solve(int n) {
ArrayList<Integer> arrli[]=new ArrayList[n+1];
for(int i=0;i<n+1;i++) {
arrli[i]=new ArrayList<>();
}
arrli[n].add(0);
int cnt=n;
int arr[]=new int[n];
int i=1;
for(;i<=n;i++) {
boolean in=false;
while(arrli[cnt].size()==0) {
cnt--;
in=true;
}
if(in) {
arrli[cnt].sort(null);
}
if(cnt==2) {
break;
}
int indx=arrli[cnt].get(0);
if(cnt%2==0) {
int l=indx;
int r=indx+cnt-1;
int pivot=(r+l-1)/2;
arr[pivot]=i;
int lft=pivot-1-l+1;
arrli[lft].add(l);
int rgt=r-(pivot+1)+1;
arrli[rgt].add(pivot+1);
}
else {
int l=indx;
int r=indx+cnt-1;
int pivot=(r+l)/2;
arr[pivot]=i;
int lft=pivot-1-l+1;
arrli[lft].add(l);
int rgt=r-(pivot+1)+1;
arrli[rgt].add(pivot+1);
}
arrli[cnt].remove(0);
}
for(int j=0;j<n-1;j++) {
if(arr[j]==0 && arr[j+1]==0) {
arr[j]=i;
i++;
}
}
for(int j=0;j<n;j++) {
if(arr[j]==0) {
arr[j]=i;
i++;
}
}
StringBuilder ans=new StringBuilder("");
for(i=0;i<n;i++) {
ans.append(arr[i]+" ");
}
return ans;
}
}
| Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 6483303053cc56b44b86698ec827b35f | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class Main {
static class Pair {
int l;
int r;
public Pair(int l, int r) {
this.l = l;
this.r = r;
}
}
static void solve6() {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
long[][] a = new long[n + 1][m + 1];
long[][] steps = new long[n + 1][m + 1];
for(int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
a[i][j] = sc.nextLong();
}
}
long diff = 0;
for(int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (i == 1) {
steps[i][j] = Math.abs(a[i][j - 1] - a[i][j]) - 1;
a[i][j] -= steps[i][j];
} else if (j == 1) {
steps[i][j] = Math.abs(a[i - 1][j] - a[i][j]) - 1;
a[i][j] -= steps[i][j];
} else {
long val = 0;
if (steps[i - 1][j] <= steps[i][j - 1]) {
val = a[i - 1][j];
} else {
val = a[i][j - 1];
}
steps[i][j] = Math.min(steps[i - 1][j], steps[i][j - 1]) + Math.abs(val - a[i][j]) - 1;
a[i][j] -= steps[i][j];
}
}
}
System.out.println(steps[n][m]);
}
sc.close();
}
static void solve4() {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt();
int[] a = new int[n];
Queue<Pair> q = new PriorityQueue<>(n, (Pair p1, Pair p2) -> {
int d1 = p1.r - p1.l;
int d2 = p2.r - p2.l;
if (d1 == d2) {
return Integer.compare(p1.l, p2.l);
}
return Integer.compare(d2, d1);
});
q.offer(new Pair(0, n-1));
int step = 1;
while(n-- > 0) {
Pair p = q.poll();
int idx = p.l + p.r;
idx /= 2;
a[idx] = step++;
if (p.l < idx) {
q.offer(new Pair(p.l, idx - 1));
}
if (p.r > idx) {
q.offer(new Pair(idx + 1, p.r));
}
}
StringBuilder sb = new StringBuilder(a.length);
for(int aa : a) {
sb.append(aa);
sb.append(" ");
}
System.out.println(sb.toString());
}
sc.close();
}
public static void main(String[] args) {
solve4();
}
static int GCD(int a, int b) {
while(a > 0 && b > 0) {
if (a > b)
a %= b;
else
b %= a;
}
return a + b;
}
static int LCM(int a, int b) {
return a / GCD(a, b) * b;
}
}
| Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 5a392dda27a9767c91ad4eaeddded51a | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
public class ConstructingTheArray {
InputStream is;
PrintWriter pw;
String INPUT = "";
long L_INF = (1L << 60L);
void solve() {
int n = ni(), m, idx = 1, l, r;
PriorityQueue<List<Integer>> pq =
new PriorityQueue<>(new Comparator<List<Integer>>() {
@Override
public int compare(List<Integer> o1, List<Integer> o2) {
int diff1 = o1.get(1) - o1.get(0);
int diff2 = o2.get(1) - o2.get(0);
if (diff1 > diff2)
return -11;
else if (diff2 > diff1)
return 1;
else {
return o1.get(0) < o2.get(0) ? -1 : 1;
}
}
});
List<Integer> a = new ArrayList<>();
a.add(1);
a.add(n);
int arr[] = new int[n + 1];
pq.add(a);
while (!pq.isEmpty()) {
// pw.println(pq);
List<Integer> f = pq.poll();
l = f.get(0);
r = f.get(1);
int mid;
if ((r - l + 1) % 2 == 1) {
mid = (l + r) / 2;
arr[mid] = idx;
} else {
mid = (l + r - 1) / 2;
arr[mid] = idx;
}
idx++;
if (mid > l)
pq.add(Arrays.asList(l, mid - 1));
if (r > mid)
pq.add(Arrays.asList(mid + 1, r));
}
for (int i = 1; i <= n; i++) {
pw.print(arr[i] + " ");
}
pw.println();
}
static class IntIntPair implements Comparable<IntIntPair> {
public final int first;
public final int second;
public static IntIntPair makePair(int first, int second) {
return new IntIntPair(first, second);
}
public IntIntPair(int first, int second) {
this.first = first;
this.second = second;
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
IntIntPair pair = (IntIntPair) o;
return first == pair.first && second == pair.second;
}
public int hashCode() {
int result = first;
result = 31 * result + second;
return result;
}
public String toString() {
return "(" + first + "," + second + ")";
}
public int compareTo(IntIntPair o) {
int value = Integer.compare(first, o.first);
if (value != 0) {
return value;
}
return Integer.compare(second, o.second);
}
}
void run() throws Exception {
// is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
is = System.in;
pw = new PrintWriter(System.out);
long s = System.currentTimeMillis();
int t = ni();
while (t-- > 0)
solve();
pw.flush();
tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception {
new ConstructingTheArray().run();
}
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0;
private int readByte() {
if (lenbuf == -1) throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0) return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b)) ;
return b;
}
private double nd() {
return Double.parseDouble(ns());
}
private char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++) map[i] = ns(m);
return map;
}
private int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = ni();
return a;
}
private 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();
}
}
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();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) {
if (!oj) System.out.println(Arrays.deepToString(o));
}
} | Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 046490a8a116a0817215bda684dde70d | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
// Compiler version JDK 11.0.2
public class Dcoder
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
Solution sol=new Solution();
sol.solve(n);
sol.getAns();
System.out.println();
}
}
static class Solution{
static int arr[];
static int itr=1;
int dp[];
static Map<Integer,SortedSet<Integer>> m=new TreeMap<>();
public void solve(int n){
arr=new int[n];
itr=1;
dp=new int[n+1];
createArray(0,n-1);
}
public void createArray(int left,int right){
if(right-left+1==0)
return;
SortedSet<Integer> set=new TreeSet<>();
set.add(left);
dp[left]=right;
m.put(right-left+1,set);
while(!m.isEmpty()){
Set<Integer> s=m.keySet();
int k=0;
for(int r:s){
k=r;
}
SortedSet<Integer> u=m.get(k);
int kk=0;
for(int r:u){
kk=r;
break;
}
u.remove(kk);
if(u.isEmpty())
m.remove(k);
int x=kk;
int y=dp[x];
int len=y-x+1;
int mid=(x+y)/2;
if(len%2==0)
mid=(x+y-1)/2;
arr[mid]=itr++;
if(x<=mid-1)
add(x,mid-1);
if(y>=mid+1)
add(mid+1,y);
}
}
public void add(int x,int y)
{
int ind=y-x+1;
if(m.get(ind)==null){
SortedSet<Integer> temp=new TreeSet<>();
temp.add(x);
dp[x]=y;
m.put(ind,temp);
}
else{
SortedSet<Integer> temp=m.get(ind);
temp.add(x);
dp[x]=y;
m.put(ind,temp);
}
}
public void getAns(){
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
return ;
}
}
}
| Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 27380bb4559d35fcbb279982eb6cbc89 | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.Vector;
import java.util.function.Function;
import java.util.stream.Collectors;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.pow;
import static java.lang.Math.abs;
import static java.lang.Math.ceil;
import static java.lang.Math.floor;
import static java.util.Arrays.fill;
import static java.util.Arrays.sort;
import static java.util.Arrays.parallelSort;
import static java.util.Arrays.binarySearch;
public class cfa {
static final Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
static final int MOD = (int) (1e9 + 7);
static final int MOD_FFT = 998244353;
static final Reader r = new Reader();
static final PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static Vector<Integer> adj[], v;
static{
}
public static void main(final String[] args) throws Exception {
new Thread(null, new Runnable() {
@Override
public void run() {
try {
final int testcases = HAS_TEST_CASES ? ni() : 1;
for (int i = 1; i <= testcases; i++) {
// out.print("Case #" + (i + 1) + ": ");
try {
solve();
} catch (final Exception e) {
pn(e.getMessage());
pni("idk Exception");
e.printStackTrace(System.err);
}
}
out.flush();
} catch (Throwable t) {
t.printStackTrace(System.err);
System.exit(-1);
}
}
}, "", (1L << 1)).start();
// final int testcases = HAS_TEST_CASES ? ni() : 1;
// for (int i = 1; i <= testcases; i++) {
// // out.print("Case #" + (i + 1) + ": ");
// try {
// solve();
// } catch (final ArrayIndexOutOfBoundsException e) {
// e.printStackTrace();
// } catch (final Exception e) {
// pni("idk Exception in solve");
// e.printStackTrace();
// }
// }
// out.flush();
}
static final boolean HAS_TEST_CASES = true, FASTIO = true;
static final int SCAN_LINE_LENGTH = 1000002;
static int n, a[], e[][];
static void solve() throws IOException {
n = ni();
a = new int[n];
final PriorityQueue<Pair<Integer, pi>> ts = new PriorityQueue<>();
add(ts, -n, 0, n - 1);
int num = 1;
while (!ts.isEmpty() && num <= n) {
final Pair<Integer, pi> p = ts.poll();
final int l = p.snd.fir, r = p.snd.snd, m = (l + r) / 2;
a[m] = num++;
if (n > 1) {
add(ts, m - l, l, m - 1);
}
add(ts, r - m, m + 1, r);
}
for (int i = 0; i < a.length; i++) {
p(a[i] + " ");
}
pn();
}
private static void add(final PriorityQueue<Pair<Integer, pi>> ts, final int s, final int l, final int r) {
final pi pp = new pi(l, r);
final Pair<Integer, pi> p = new Pair<>(-s, pp);
ts.add(p);
}
private static void swap(final int i, final int j) {
final int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static boolean isMulOverflow(final long A, final long B) {
if (A == 0 || B == 0)
return false;
final long result = A * B;
if (A == result / B)
return true;
return false;
}
private static int gcd(final int a, final int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static class pi implements Comparable<pi> {
int fir, snd;
pi() {
}
pi(final int x, final int y) {
fir = x;
snd = y;
}
public int compareTo(final pi o) {
return fir == o.fir ? snd - o.snd : fir - o.fir;
}
}
static class Pair<T, E> implements Comparable<Pair<T, E>> {
T fir;
E snd;
Pair() {
}
Pair(final T x, final E y) {
this.fir = x;
this.snd = y;
}
@Override
@SuppressWarnings("unchecked")
public int compareTo(final Pair<T, E> o) {
final int c = ((Comparable<T>) fir).compareTo(o.fir);
return c != 0 ? c : ((Comparable<E>) snd).compareTo(o.snd);
}
}
private static <T> void checkV(final Vector<T> adj[], final int i) {
adj[i] = adj[i] == null ? new Vector<>() : adj[i];
}
private static int[] na(final int n) throws Exception {
final int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
private static int[] na1(final int n) throws Exception {
final int[] a = new int[n + 1];
for (int i = 1; i < a.length; i++) {
a[i] = ni();
}
return a;
}
private static String n() throws Exception {
return FASTIO ? r.readToken() : in.next();
}
private static String nln() throws Exception {
return FASTIO ? r.readLine() : in.nextLine();
}
private static int ni() throws IOException {
return FASTIO ? r.nextInt() : in.nextInt();
}
private static long nl() throws Exception {
return FASTIO ? r.nextLong() : in.nextLong();
}
private static double nd() throws Exception {
return FASTIO ? r.nextDouble() : in.nextDouble();
}
private static void p(final Object o) {
out.print(o);
}
private static void pn(final Object o) {
out.println(o);
}
private static void pn() {
out.println("");
}
private static void pni(final Object o) {
out.println(o);
out.flush();
}
private static class Reader {
private final int BUFFER_SIZE = 1 << 19;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
private StringTokenizer st;
private final BufferedReader br;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
br = new BufferedReader(new InputStreamReader(System.in));
}
public Reader(final String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
br = new BufferedReader(new FileReader(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String next() throws Exception {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (final IOException e) {
throw new Exception(e.toString());
}
}
return st.nextToken();
}
public String nextLine() throws Exception {
String str = "";
try {
str = br.readLine();
} catch (final IOException e) {
throw new Exception(e.toString());
}
return str;
}
final byte[] buf = new byte[SCAN_LINE_LENGTH];
public String readLine() throws IOException {
int cnt = 0;
int c;
while ((c = read()) != -1) {
if (c == '\n')
if (cnt == 0)
continue;
else
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public String readToken() throws IOException {
int cnt = 0;
int c;
while ((c = read()) != -1) {
if (c == '\n' || c == ' ')
if (cnt == 0)
continue;
else
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
final boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
final boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
final boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
@SuppressWarnings({ "unchecked" })
private static <T> T deepCopy(final T old) {
try {
return (T) deepCopyObject(old);
} catch (final Exception e) {
e.printStackTrace();
}
return null;
}
private static Object deepCopyObject(final Object oldObj) throws Exception {
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
final ByteArrayOutputStream bos = new ByteArrayOutputStream(); // A
oos = new ObjectOutputStream(bos); // B
// serialize and pass the object
oos.writeObject(oldObj); // C
oos.flush(); // D
final ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray()); // E
ois = new ObjectInputStream(bin); // F
// return the new object
return ois.readObject(); // G
} catch (final Exception e) {
pni("Exception in ObjectCloner = " + e);
throw (e);
} finally {
oos.close();
ois.close();
}
}
} | Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 5b011eb09c774da94ea4326bd0a3a5f2 | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.Vector;
import java.util.function.Function;
import java.util.stream.Collectors;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.pow;
import static java.lang.Math.abs;
import static java.lang.Math.ceil;
import static java.lang.Math.floor;
import static java.util.Arrays.fill;
import static java.util.Arrays.sort;
import static java.util.Arrays.parallelSort;
import static java.util.Arrays.binarySearch;
public class cfa {
static final Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
static final int MOD = (int) (1e9 + 7);
static final int MOD_FFT = 998244353;
static final Reader r = new Reader();
static final PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static Vector<Integer> adj[], v;
public static void main(final String[] args) throws Exception {
final int testcases = HAS_TEST_CASES ? ni() : 1;
for (int i = 1; i <= testcases; i++) {
// out.print("Case #" + (i + 1) + ": ");
try {
solve();
} catch (final ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
} catch (final Exception e) {
pni("idk Exception in solve");
e.printStackTrace();
}
}
out.flush();
}
static final boolean HAS_TEST_CASES = true, FASTIO = true;
static final int SCAN_LINE_LENGTH = 1000002;
static int n, a[], e[][];
static void solve() throws IOException {
n = ni();
a = new int[n];
PriorityQueue<Pair<Integer, pi>> ts = new PriorityQueue<>();
add(ts, -n, 0, n - 1);
int num = 1;
while (!ts.isEmpty() && num <= n) {
Pair<Integer, pi> p = ts.poll();
int l = p.snd.fir, r = p.snd.snd, m = (l + r) / 2;
a[m] = num++;
if (n > 1) {
add(ts, m - l, l, m - 1);
}
add(ts, r - m, m + 1, r);
}
for (int i = 0; i < a.length; i++) {
p(a[i] + " ");
}
pn();
}
private static void add(PriorityQueue<Pair<Integer, pi>> ts, int s, int l, int r) {
pi pp = new pi(l, r);
Pair<Integer, pi> p = new Pair<>(-s, pp);
ts.add(p);
}
private static void swap(final int i, final int j) {
final int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static boolean isMulOverflow(final long A, final long B) {
if (A == 0 || B == 0)
return false;
final long result = A * B;
if (A == result / B)
return true;
return false;
}
private static int gcd(final int a, final int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static class pi implements Comparable<pi> {
int fir, snd;
pi() {
}
pi(int x, int y) {
fir = x;
snd = y;
}
public int compareTo(final pi o) {
return fir == o.fir ? snd - o.snd : fir - o.fir;
}
}
static class Pair<T, E> implements Comparable<Pair<T, E>> {
T fir;
E snd;
Pair() {
}
Pair(T x, E y) {
this.fir = x;
this.snd = y;
}
@Override
@SuppressWarnings("unchecked")
public int compareTo(Pair<T, E> o) {
int c = ((Comparable<T>) fir).compareTo(o.fir);
return c != 0 ? c : ((Comparable<E>) snd).compareTo(o.snd);
}
}
private static <T> void checkV(final Vector<T> adj[], final int i) {
adj[i] = adj[i] == null ? new Vector<>() : adj[i];
}
private static int[] na(final int n) throws Exception {
final int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
private static int[] na1(final int n) throws Exception {
final int[] a = new int[n + 1];
for (int i = 1; i < a.length; i++) {
a[i] = ni();
}
return a;
}
private static String n() throws Exception {
return FASTIO ? r.readToken() : in.next();
}
private static String nln() throws Exception {
return FASTIO ? r.readLine() : in.nextLine();
}
private static int ni() throws IOException {
return FASTIO ? r.nextInt() : in.nextInt();
}
private static long nl() throws Exception {
return FASTIO ? r.nextLong() : in.nextLong();
}
private static double nd() throws Exception {
return FASTIO ? r.nextDouble() : in.nextDouble();
}
private static void p(final Object o) {
out.print(o);
}
private static void pn(final Object o) {
out.println(o);
}
private static void pn() {
out.println("");
}
private static void pni(final Object o) {
out.println(o);
out.flush();
}
private static class Reader {
private final int BUFFER_SIZE = 1 << 19;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
private StringTokenizer st;
private final BufferedReader br;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
br = new BufferedReader(new InputStreamReader(System.in));
}
public Reader(final String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
br = new BufferedReader(new FileReader(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String next() throws Exception {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (final IOException e) {
throw new Exception(e.toString());
}
}
return st.nextToken();
}
public String nextLine() throws Exception {
String str = "";
try {
str = br.readLine();
} catch (final IOException e) {
throw new Exception(e.toString());
}
return str;
}
final byte[] buf = new byte[SCAN_LINE_LENGTH];
public String readLine() throws IOException {
int cnt = 0;
int c;
while ((c = read()) != -1) {
if (c == '\n')
if (cnt == 0)
continue;
else
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public String readToken() throws IOException {
int cnt = 0;
int c;
while ((c = read()) != -1) {
if (c == '\n' || c == ' ')
if (cnt == 0)
continue;
else
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
final boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
final boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
final boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
@SuppressWarnings({ "unchecked" })
private static <T> T deepCopy(final T old) {
try {
return (T) deepCopyObject(old);
} catch (final Exception e) {
e.printStackTrace();
}
return null;
}
private static Object deepCopyObject(final Object oldObj) throws Exception {
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
final ByteArrayOutputStream bos = new ByteArrayOutputStream(); // A
oos = new ObjectOutputStream(bos); // B
// serialize and pass the object
oos.writeObject(oldObj); // C
oos.flush(); // D
final ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray()); // E
ois = new ObjectInputStream(bin); // F
// return the new object
return ois.readObject(); // G
} catch (final Exception e) {
pni("Exception in ObjectCloner = " + e);
throw (e);
} finally {
oos.close();
ois.close();
}
}
} | Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 94f4e3c08494e34e77c8422bc5cb1b10 | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.Vector;
import java.util.function.Function;
import java.util.stream.Collectors;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.pow;
import static java.lang.Math.abs;
import static java.lang.Math.ceil;
import static java.lang.Math.floor;
import static java.util.Arrays.fill;
import static java.util.Arrays.sort;
import static java.util.Arrays.parallelSort;
import static java.util.Arrays.binarySearch;
public class cfa {
static final Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
static final int MOD = (int) (1e9 + 7);
static final int MOD_FFT = 998244353;
static final Reader r = new Reader();
static final PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static Vector<Integer> adj[], v;
static{
}
public static void main(final String[] args) throws Exception {
new Thread(null, new Runnable() {
@Override
public void run() {
try {
final int testcases = HAS_TEST_CASES ? ni() : 1;
for (int i = 1; i <= testcases; i++) {
// out.print("Case #" + (i + 1) + ": ");
try {
solve();
} catch (final Exception e) {
pn(e.getMessage());
pni("idk Exception");
e.printStackTrace(System.err);
}
}
out.flush();
} catch (Throwable t) {
t.printStackTrace(System.err);
System.exit(-1);
}
}
}, "", (1L << 8)).start();
// final int testcases = HAS_TEST_CASES ? ni() : 1;
// for (int i = 1; i <= testcases; i++) {
// // out.print("Case #" + (i + 1) + ": ");
// try {
// solve();
// } catch (final ArrayIndexOutOfBoundsException e) {
// e.printStackTrace();
// } catch (final Exception e) {
// pni("idk Exception in solve");
// e.printStackTrace();
// }
// }
// out.flush();
}
static final boolean HAS_TEST_CASES = true, FASTIO = true;
static final int SCAN_LINE_LENGTH = 1000002;
static int n, a[], e[][];
static void solve() throws IOException {
n = ni();
a = new int[n];
final PriorityQueue<Pair<Integer, pi>> ts = new PriorityQueue<>();
add(ts, -n, 0, n - 1);
int num = 1;
while (!ts.isEmpty() && num <= n) {
final Pair<Integer, pi> p = ts.poll();
final int l = p.snd.fir, r = p.snd.snd, m = (l + r) / 2;
a[m] = num++;
if (n > 1) {
add(ts, m - l, l, m - 1);
}
add(ts, r - m, m + 1, r);
}
for (int i = 0; i < a.length; i++) {
p(a[i] + " ");
}
pn();
}
private static void add(final PriorityQueue<Pair<Integer, pi>> ts, final int s, final int l, final int r) {
final pi pp = new pi(l, r);
final Pair<Integer, pi> p = new Pair<>(-s, pp);
ts.add(p);
}
private static void swap(final int i, final int j) {
final int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static boolean isMulOverflow(final long A, final long B) {
if (A == 0 || B == 0)
return false;
final long result = A * B;
if (A == result / B)
return true;
return false;
}
private static int gcd(final int a, final int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static class pi implements Comparable<pi> {
int fir, snd;
pi() {
}
pi(final int x, final int y) {
fir = x;
snd = y;
}
public int compareTo(final pi o) {
return fir == o.fir ? snd - o.snd : fir - o.fir;
}
}
static class Pair<T, E> implements Comparable<Pair<T, E>> {
T fir;
E snd;
Pair() {
}
Pair(final T x, final E y) {
this.fir = x;
this.snd = y;
}
@Override
@SuppressWarnings("unchecked")
public int compareTo(final Pair<T, E> o) {
final int c = ((Comparable<T>) fir).compareTo(o.fir);
return c != 0 ? c : ((Comparable<E>) snd).compareTo(o.snd);
}
}
private static <T> void checkV(final Vector<T> adj[], final int i) {
adj[i] = adj[i] == null ? new Vector<>() : adj[i];
}
private static int[] na(final int n) throws Exception {
final int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
private static int[] na1(final int n) throws Exception {
final int[] a = new int[n + 1];
for (int i = 1; i < a.length; i++) {
a[i] = ni();
}
return a;
}
private static String n() throws Exception {
return FASTIO ? r.readToken() : in.next();
}
private static String nln() throws Exception {
return FASTIO ? r.readLine() : in.nextLine();
}
private static int ni() throws IOException {
return FASTIO ? r.nextInt() : in.nextInt();
}
private static long nl() throws Exception {
return FASTIO ? r.nextLong() : in.nextLong();
}
private static double nd() throws Exception {
return FASTIO ? r.nextDouble() : in.nextDouble();
}
private static void p(final Object o) {
out.print(o);
}
private static void pn(final Object o) {
out.println(o);
}
private static void pn() {
out.println("");
}
private static void pni(final Object o) {
out.println(o);
out.flush();
}
private static class Reader {
private final int BUFFER_SIZE = 1 << 19;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
private StringTokenizer st;
private final BufferedReader br;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
br = new BufferedReader(new InputStreamReader(System.in));
}
public Reader(final String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
br = new BufferedReader(new FileReader(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String next() throws Exception {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (final IOException e) {
throw new Exception(e.toString());
}
}
return st.nextToken();
}
public String nextLine() throws Exception {
String str = "";
try {
str = br.readLine();
} catch (final IOException e) {
throw new Exception(e.toString());
}
return str;
}
final byte[] buf = new byte[SCAN_LINE_LENGTH];
public String readLine() throws IOException {
int cnt = 0;
int c;
while ((c = read()) != -1) {
if (c == '\n')
if (cnt == 0)
continue;
else
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public String readToken() throws IOException {
int cnt = 0;
int c;
while ((c = read()) != -1) {
if (c == '\n' || c == ' ')
if (cnt == 0)
continue;
else
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
final boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
final boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
final boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
@SuppressWarnings({ "unchecked" })
private static <T> T deepCopy(final T old) {
try {
return (T) deepCopyObject(old);
} catch (final Exception e) {
e.printStackTrace();
}
return null;
}
private static Object deepCopyObject(final Object oldObj) throws Exception {
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
final ByteArrayOutputStream bos = new ByteArrayOutputStream(); // A
oos = new ObjectOutputStream(bos); // B
// serialize and pass the object
oos.writeObject(oldObj); // C
oos.flush(); // D
final ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray()); // E
ois = new ObjectInputStream(bin); // F
// return the new object
return ois.readObject(); // G
} catch (final Exception e) {
pni("Exception in ObjectCloner = " + e);
throw (e);
} finally {
oos.close();
ois.close();
}
}
} | Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 00eae2a201657773ff30f9c231de2c45 | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.Vector;
import java.util.function.Function;
import java.util.stream.Collectors;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.pow;
import static java.lang.Math.abs;
import static java.lang.Math.ceil;
import static java.lang.Math.floor;
import static java.util.Arrays.fill;
import static java.util.Arrays.sort;
import static java.util.Arrays.parallelSort;
import static java.util.Arrays.binarySearch;
public class cfa {
static final Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
static final int MOD = (int) (1e9 + 7);
static final int MOD_FFT = 998244353;
static final Reader r = new Reader();
static final PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static Vector<Integer> adj[], v;
static{
new Thread(null, new Runnable() {
@Override
public void run() {
try {
final int testcases = HAS_TEST_CASES ? ni() : 1;
for (int i = 1; i <= testcases; i++) {
// out.print("Case #" + (i + 1) + ": ");
try {
solve();
} catch (final Exception e) {
pn(e.getMessage());
pni("idk Exception");
e.printStackTrace(System.err);
}
}
out.flush();
} catch (Throwable t) {
t.printStackTrace(System.err);
System.exit(-1);
}
}
}, "", (1L << 8)).start();
}
public static void main(final String[] args) throws Exception {
// final int testcases = HAS_TEST_CASES ? ni() : 1;
// for (int i = 1; i <= testcases; i++) {
// // out.print("Case #" + (i + 1) + ": ");
// try {
// solve();
// } catch (final ArrayIndexOutOfBoundsException e) {
// e.printStackTrace();
// } catch (final Exception e) {
// pni("idk Exception in solve");
// e.printStackTrace();
// }
// }
// out.flush();
}
static final boolean HAS_TEST_CASES = true, FASTIO = true;
static final int SCAN_LINE_LENGTH = 1000002;
static int n, a[], e[][];
static void solve() throws IOException {
n = ni();
a = new int[n];
final PriorityQueue<Pair<Integer, pi>> ts = new PriorityQueue<>();
add(ts, -n, 0, n - 1);
int num = 1;
while (!ts.isEmpty() && num <= n) {
final Pair<Integer, pi> p = ts.poll();
final int l = p.snd.fir, r = p.snd.snd, m = (l + r) / 2;
a[m] = num++;
if (n > 1) {
add(ts, m - l, l, m - 1);
}
add(ts, r - m, m + 1, r);
}
for (int i = 0; i < a.length; i++) {
p(a[i] + " ");
}
pn();
}
private static void add(final PriorityQueue<Pair<Integer, pi>> ts, final int s, final int l, final int r) {
final pi pp = new pi(l, r);
final Pair<Integer, pi> p = new Pair<>(-s, pp);
ts.add(p);
}
private static void swap(final int i, final int j) {
final int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static boolean isMulOverflow(final long A, final long B) {
if (A == 0 || B == 0)
return false;
final long result = A * B;
if (A == result / B)
return true;
return false;
}
private static int gcd(final int a, final int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static class pi implements Comparable<pi> {
int fir, snd;
pi() {
}
pi(final int x, final int y) {
fir = x;
snd = y;
}
public int compareTo(final pi o) {
return fir == o.fir ? snd - o.snd : fir - o.fir;
}
}
static class Pair<T, E> implements Comparable<Pair<T, E>> {
T fir;
E snd;
Pair() {
}
Pair(final T x, final E y) {
this.fir = x;
this.snd = y;
}
@Override
@SuppressWarnings("unchecked")
public int compareTo(final Pair<T, E> o) {
final int c = ((Comparable<T>) fir).compareTo(o.fir);
return c != 0 ? c : ((Comparable<E>) snd).compareTo(o.snd);
}
}
private static <T> void checkV(final Vector<T> adj[], final int i) {
adj[i] = adj[i] == null ? new Vector<>() : adj[i];
}
private static int[] na(final int n) throws Exception {
final int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
private static int[] na1(final int n) throws Exception {
final int[] a = new int[n + 1];
for (int i = 1; i < a.length; i++) {
a[i] = ni();
}
return a;
}
private static String n() throws Exception {
return FASTIO ? r.readToken() : in.next();
}
private static String nln() throws Exception {
return FASTIO ? r.readLine() : in.nextLine();
}
private static int ni() throws IOException {
return FASTIO ? r.nextInt() : in.nextInt();
}
private static long nl() throws Exception {
return FASTIO ? r.nextLong() : in.nextLong();
}
private static double nd() throws Exception {
return FASTIO ? r.nextDouble() : in.nextDouble();
}
private static void p(final Object o) {
out.print(o);
}
private static void pn(final Object o) {
out.println(o);
}
private static void pn() {
out.println("");
}
private static void pni(final Object o) {
out.println(o);
out.flush();
}
private static class Reader {
private final int BUFFER_SIZE = 1 << 19;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
private StringTokenizer st;
private final BufferedReader br;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
br = new BufferedReader(new InputStreamReader(System.in));
}
public Reader(final String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
br = new BufferedReader(new FileReader(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String next() throws Exception {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (final IOException e) {
throw new Exception(e.toString());
}
}
return st.nextToken();
}
public String nextLine() throws Exception {
String str = "";
try {
str = br.readLine();
} catch (final IOException e) {
throw new Exception(e.toString());
}
return str;
}
final byte[] buf = new byte[SCAN_LINE_LENGTH];
public String readLine() throws IOException {
int cnt = 0;
int c;
while ((c = read()) != -1) {
if (c == '\n')
if (cnt == 0)
continue;
else
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public String readToken() throws IOException {
int cnt = 0;
int c;
while ((c = read()) != -1) {
if (c == '\n' || c == ' ')
if (cnt == 0)
continue;
else
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
final boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
final boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
final boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
@SuppressWarnings({ "unchecked" })
private static <T> T deepCopy(final T old) {
try {
return (T) deepCopyObject(old);
} catch (final Exception e) {
e.printStackTrace();
}
return null;
}
private static Object deepCopyObject(final Object oldObj) throws Exception {
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
final ByteArrayOutputStream bos = new ByteArrayOutputStream(); // A
oos = new ObjectOutputStream(bos); // B
// serialize and pass the object
oos.writeObject(oldObj); // C
oos.flush(); // D
final ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray()); // E
ois = new ObjectInputStream(bin); // F
// return the new object
return ois.readObject(); // G
} catch (final Exception e) {
pni("Exception in ObjectCloner = " + e);
throw (e);
} finally {
oos.close();
ois.close();
}
}
} | Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | f30c8dab4cc039cab1eb3934d454304a | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.Vector;
import java.util.function.Function;
import java.util.stream.Collectors;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.pow;
import static java.lang.Math.abs;
import static java.lang.Math.ceil;
import static java.lang.Math.floor;
import static java.util.Arrays.fill;
import static java.util.Arrays.sort;
import static java.util.Arrays.parallelSort;
import static java.util.Arrays.binarySearch;
public class cfa {
static final Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
static final int MOD = (int) (1e9 + 7);
static final int MOD_FFT = 998244353;
static final Reader r = new Reader();
static final PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static Vector<Integer> adj[], v;
static{
new Thread(null, new Runnable() {
@Override
public void run() {
try {
final int testcases = HAS_TEST_CASES ? ni() : 1;
for (int i = 1; i <= testcases; i++) {
// out.print("Case #" + (i + 1) + ": ");
try {
solve();
} catch (final Exception e) {
pn(e.getMessage());
pni("idk Exception");
e.printStackTrace(System.err);
}
}
out.flush();
} catch (Throwable t) {
t.printStackTrace(System.err);
System.exit(-1);
}
}
}, "", (1L << 28)).start();
}
public static void main(final String[] args) throws Exception {
// final int testcases = HAS_TEST_CASES ? ni() : 1;
// for (int i = 1; i <= testcases; i++) {
// // out.print("Case #" + (i + 1) + ": ");
// try {
// solve();
// } catch (final ArrayIndexOutOfBoundsException e) {
// e.printStackTrace();
// } catch (final Exception e) {
// pni("idk Exception in solve");
// e.printStackTrace();
// }
// }
// out.flush();
}
static final boolean HAS_TEST_CASES = true, FASTIO = true;
static final int SCAN_LINE_LENGTH = 1000002;
static int n, a[], e[][];
static void solve() throws IOException {
n = ni();
a = new int[n];
final PriorityQueue<Pair<Integer, pi>> ts = new PriorityQueue<>();
add(ts, -n, 0, n - 1);
int num = 1;
while (!ts.isEmpty() && num <= n) {
final Pair<Integer, pi> p = ts.poll();
final int l = p.snd.fir, r = p.snd.snd, m = (l + r) / 2;
a[m] = num++;
if (n > 1) {
add(ts, m - l, l, m - 1);
}
add(ts, r - m, m + 1, r);
}
for (int i = 0; i < a.length; i++) {
p(a[i] + " ");
}
pn();
}
private static void add(final PriorityQueue<Pair<Integer, pi>> ts, final int s, final int l, final int r) {
final pi pp = new pi(l, r);
final Pair<Integer, pi> p = new Pair<>(-s, pp);
ts.add(p);
}
private static void swap(final int i, final int j) {
final int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static boolean isMulOverflow(final long A, final long B) {
if (A == 0 || B == 0)
return false;
final long result = A * B;
if (A == result / B)
return true;
return false;
}
private static int gcd(final int a, final int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static class pi implements Comparable<pi> {
int fir, snd;
pi() {
}
pi(final int x, final int y) {
fir = x;
snd = y;
}
public int compareTo(final pi o) {
return fir == o.fir ? snd - o.snd : fir - o.fir;
}
}
static class Pair<T, E> implements Comparable<Pair<T, E>> {
T fir;
E snd;
Pair() {
}
Pair(final T x, final E y) {
this.fir = x;
this.snd = y;
}
@Override
@SuppressWarnings("unchecked")
public int compareTo(final Pair<T, E> o) {
final int c = ((Comparable<T>) fir).compareTo(o.fir);
return c != 0 ? c : ((Comparable<E>) snd).compareTo(o.snd);
}
}
private static <T> void checkV(final Vector<T> adj[], final int i) {
adj[i] = adj[i] == null ? new Vector<>() : adj[i];
}
private static int[] na(final int n) throws Exception {
final int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
private static int[] na1(final int n) throws Exception {
final int[] a = new int[n + 1];
for (int i = 1; i < a.length; i++) {
a[i] = ni();
}
return a;
}
private static String n() throws Exception {
return FASTIO ? r.readToken() : in.next();
}
private static String nln() throws Exception {
return FASTIO ? r.readLine() : in.nextLine();
}
private static int ni() throws IOException {
return FASTIO ? r.nextInt() : in.nextInt();
}
private static long nl() throws Exception {
return FASTIO ? r.nextLong() : in.nextLong();
}
private static double nd() throws Exception {
return FASTIO ? r.nextDouble() : in.nextDouble();
}
private static void p(final Object o) {
out.print(o);
}
private static void pn(final Object o) {
out.println(o);
}
private static void pn() {
out.println("");
}
private static void pni(final Object o) {
out.println(o);
out.flush();
}
private static class Reader {
private final int BUFFER_SIZE = 1 << 19;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
private StringTokenizer st;
private final BufferedReader br;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
br = new BufferedReader(new InputStreamReader(System.in));
}
public Reader(final String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
br = new BufferedReader(new FileReader(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String next() throws Exception {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (final IOException e) {
throw new Exception(e.toString());
}
}
return st.nextToken();
}
public String nextLine() throws Exception {
String str = "";
try {
str = br.readLine();
} catch (final IOException e) {
throw new Exception(e.toString());
}
return str;
}
final byte[] buf = new byte[SCAN_LINE_LENGTH];
public String readLine() throws IOException {
int cnt = 0;
int c;
while ((c = read()) != -1) {
if (c == '\n')
if (cnt == 0)
continue;
else
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public String readToken() throws IOException {
int cnt = 0;
int c;
while ((c = read()) != -1) {
if (c == '\n' || c == ' ')
if (cnt == 0)
continue;
else
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
final boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
final boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
final boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
@SuppressWarnings({ "unchecked" })
private static <T> T deepCopy(final T old) {
try {
return (T) deepCopyObject(old);
} catch (final Exception e) {
e.printStackTrace();
}
return null;
}
private static Object deepCopyObject(final Object oldObj) throws Exception {
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
final ByteArrayOutputStream bos = new ByteArrayOutputStream(); // A
oos = new ObjectOutputStream(bos); // B
// serialize and pass the object
oos.writeObject(oldObj); // C
oos.flush(); // D
final ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray()); // E
ois = new ObjectInputStream(bin); // F
// return the new object
return ois.readObject(); // G
} catch (final Exception e) {
pni("Exception in ObjectCloner = " + e);
throw (e);
} finally {
oos.close();
ois.close();
}
}
} | Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 19a6f26f35ca454a6f2ff273ef3e30b6 | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | //package round642;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.PriorityQueue;
public class D {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
for(int T = ni();T > 0;T--)go();
}
void go()
{
int n = ni();
PriorityQueue<int[]> pq = new PriorityQueue<>((x, y) -> {
if(x[0] != y[0])return -(x[0] - y[0]);
return x[1] - y[1];
});
pq.add(new int[]{n, 0});
int[] ans = new int[n];
int step = 1;
while(step <= n){
int[] cur = pq.poll();
ans[cur[1]+(cur[0]-1)/2] = step++;
pq.add(new int[]{(cur[0]-1)/2, cur[1]});
pq.add(new int[]{cur[0]-(cur[0]-1)/2-1, cur[1]+(cur[0]-1)/2+1});
}
for(int v : ans){
out.print(v + " ");
}
out.println();
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new D().run(); }
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private 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();
}
}
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();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
| Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 2a717ce595e7507674ab04608bdac7fa | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class ConstructArray {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-- != 0) {
int n;
n = in.nextInt();
int arr[][] = new int[n + 1][2];
int nd = n;
int l = 0, r = n-1;
Func(arr, l, r);
Arrays.sort(arr, Comparator.comparingInt(o -> o[0]));
int ans[] = new int[n+1];
for(int i=1;i<=n;i++)
{
ans[arr[i-1][1]] = i;
}
for(int i=0;i<n;i++)
{
System.out.print(ans[i]+" ");
}
System.out.println();
}
}
public static void Func(int arr[][],int l, int r)
{
if(l<=r)
{
int m = (l+r)/2;
arr[m][0] = l-r;
arr[m][1] = m;
Func(arr, l, m-1);
Func(arr, m+1,r);
}
}
}
//
// public static void MySort(int arr[], int l, int r)
// {
// if(l<r)
// {
// int m = (l+r)/2;
// MySort(arr, l, m);
// MySort(arr, m+1, r);
// PutFunc(arr, l,m,r);
// }
//
//
// }
// public static void PutFunc(int arr[], int l, int m, int r)
// {
// arr[m] = cou++;
//
// }
| Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 773569a04998650bd94cd43e9e87c694 | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.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.PrintWriter;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
/**
* Problem CF1353D
*/
@SuppressWarnings("unchecked")
public class CF1353D {
static class Task extends IOHandler {
public void run() {
int t = in.nextInt();
while(t--!=0){
int n = in.nextInt();
int[] a = new int[n];
var pq = new PriorityQueue<Pair<Integer,Integer>>(10, (x,y) -> {
int lenx = x.b-x.a;
int leny = y.b-y.a;
if(lenx==leny){
if (x.a < y.a) return -1;
else return 1;
}
if(lenx>leny) return -1;
return 1;
});
pq.add(new Pair<>(0,n));
int i = 1;
while(!pq.isEmpty()){
var v = pq.poll();
var s = v.a+v.b;
var m = s%2==0?(s-1)/2:s/2;
a[m]=i++;
if(v.a!=m) pq.add(new Pair<>(v.a,m));
if(m+1!=v.b) pq.add(new Pair<>(m+1, v.b));
}
for(int j = 0; j < n; ++j)
out.print(a[j] + " ");
out.println();
}
}
}
/***********************************************************
* COMMONS *
***********************************************************/
static class Pair<A, B> implements Comparable<Pair<A, B>> {
public A a;
public B b;
public Pair(Pair<A, B> p) {
this(p.a, p.b);
}
public Pair(A a, B b) {
this.a = a;
this.b = b;
}
public String toString() {
return a+" "+b;
}
public int hashCode() {
return Objects.hash(a, b);
}
public boolean equals(Object o) {
if(o instanceof Pair) {
Pair<A,B> p = (Pair<A,B>) o;
return a.equals(p.a)&&b.equals(p.b);
}
return false;
}
@Override
public int compareTo(Pair<A, B> p) {
int cmp = ((Comparable<A>) a).compareTo(p.a);
if(cmp==0) {
return ((Comparable<B>) b).compareTo(p.b);
}
return cmp;
}
}
/***********************************************************
* BOILERPLATE *
***********************************************************/
public static void main(String[] args) {
Task task = new Task();
task.run();
task.cleanup();
}
static class IOHandler {
public InputReader in = new InputReader(System.in);
public PrintWriter out = new PrintWriter(System.out);
public void cleanup() {
out.close();
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 2536d91634960fe85270cab7d17d76e4 | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | /*
*created by Kraken on 14-05-2020 at 19:50
*/
//package com.kraken.cf.cf642;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.StringTokenizer;
public class D {
private static class Range {
int l, r;
public Range(int l, int r) {
this.l = l;
this.r = r;
}
@Override
public String toString() {
return "Range{" +
"l=" + l +
", r=" + r +
'}';
}
}
public static void main(String[] args) {
FastReader sc = new FastReader();
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
PriorityQueue<Range> q = new PriorityQueue<>(new Comparator<Range>() {
@Override
public int compare(Range o1, Range o2) {
int d1 = o1.r - o1.l + 1;
int d2 = o2.r - o2.l + 1;
if (d1 != d2) {
return d2 - d1;
} else {
return o1.l - o2.l;
}
}
});
int[] res = new int[n + 1];
int cnt = 1;
q.add(new Range(1, n));
while (!q.isEmpty()) {
Range r = q.poll();
int v = r.r - r.l + 1;
int mid;
if (v % 2 != 0) mid = (r.l + r.r) / 2;
else mid = (r.l + r.r - 1) / 2;
res[mid] = cnt++;
if (mid > r.l) q.add(new Range(r.l, mid - 1));
if (r.r > mid) q.add(new Range(mid + 1, r.r));
}
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= n; i++) sb.append(res[i]).append(" ");
System.out.println(sb);
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | c17fa53dfb6b6023f74c86b9d299d84f | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.TreeSet;
public class cf2 {
private static int n;
static class Pair {
public int l;
public int r;
Pair(int x,int y)
{
this.l=x;
this.r=y;
}
}
static class My_Comparator implements Comparator<Pair> {
@Override
public int compare(Pair o1, Pair o2) {
int l1;
int r1;
int l2;
int r2;
l1=o1.l;
l2=o2.l;
r1=o1.r;
r2=o2.r;
if(r2-l2>r1-l1)
return 1;
else if(r2-l2<r1-l1)
return -1;
else
return l1-l2;
}
}
public static void main(String[] args)
{
int t;
Scanner sc = new Scanner(System.in);
t=sc.nextInt();
while(t-- > 0)
{
n=sc.nextInt();
TreeSet<Pair> treeset = new TreeSet(new My_Comparator());
treeset.add(new Pair(1,n));
int a[]=new int[n+1];
int index=1;
while(!treeset.isEmpty())
{
Pair pp = treeset.pollFirst();
int len = pp.r - pp.l +1;
int l,r;
l=pp.l;
r=pp.r;
if(len%2==0)
{
a[l+(len/2)-1]=index++;
if(l+(len/2)-2>=l)
treeset.add(new Pair(l,l+(len/2)-2));
if(l+len/2<=n)
treeset.add(new Pair(l+len/2,r));
}
else
{
a[l+len/2]=index++;
if(l+len/2-1>0 && len/2>0)
{
treeset.add(new Pair(l,l+len/2-1));
}
if(l+len/2+1<=n && len/2>0)
{
treeset.add(new Pair(l+len/2+1,r));
}
}
}
for(int i=1;i<=n;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
}
}
} | Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | a466c5fd51f81283c1437034d4a1e858 | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.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.*;
import java.util.stream.Collectors;
public class Solution extends Thread {
private static final FastReader scanner = new FastReader();
private static final PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
new Thread(null, new Solution(), "Main", 1 << 26).start();
}
public void run() {
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
solve();
}
out.close();
}
private static void solve() {
int n = scanner.nextInt();
int[] arr = new int[n];
Segment segment = new Segment(1, n);
PriorityQueue<Segment> queue = new PriorityQueue<>();
queue.add(segment);
int currentNumber = 1;
while (!queue.isEmpty()) {
Segment currentSegment = queue.poll();
if (currentSegment.l > currentSegment.r) {
continue;
}
int index = (currentSegment.r - currentSegment.l + 1) % 2 == 1 ? (currentSegment.r + currentSegment.l) / 2
: (currentSegment.r + currentSegment.l - 1) / 2;
arr[index-1] = currentNumber++;
queue.add(new Segment(currentSegment.l, index-1));
queue.add(new Segment(index+1, currentSegment.r));
}
out.println(Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.joining(" ")));
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
int nextInt() {
return Integer.parseInt(next());
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
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 Segment implements Comparable<Segment> {
int l;
int r;
public Segment(int l, int r) {
this.l = l;
this.r = r;
}
@Override
public int compareTo(Segment o) {
if (r - l == o.r - o.l) {
return l - o.l;
}
return o.r - o.l - (r - l);
}
}
} | Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 2d901e6a4a1831ae77bebd9084bc9feb | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes |
// Imports
import java.util.*;
import java.io.*;
public class D642 {
static class Segment {
public int start;
public int end;
public int size;
public Segment(int s, int e) {
start = s;
end = e;
size = e - s;
}
@Override
public String toString() {
return start + "/" + end;
}
}
/**
* @param args the command line arguments
* @throws IOException, FileNotFoundException
*/
public static void main(String[] args) throws IOException, FileNotFoundException {
// TODO UNCOMMENT WHEN ALGORITHM CORRECT
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
/* BufferedReader f = new BufferedReader(new StringReader("6\n" +
"1\n" +
"2\n" +
"3\n" +
"4\n" +
"5\n" +
"6")); */
// TODO code application logic here
int T = Integer.parseInt(f.readLine());
for(int i = 0; i < T; i++) {
int N = Integer.parseInt(f.readLine());
PriorityQueue<Segment> pq = new PriorityQueue<>((Segment o1, Segment o2) -> {
if(o2.size == o1.size) {
return o1.start - o2.start;
}
else {
return o2.size - o1.size;
}
});
pq.add(new Segment(0, N - 1));
int[] arr = new int[N];
int val = 1;
while(!pq.isEmpty()) {
Segment s = pq.remove();
// System.out.println(s);
arr[(s.start + s.end)/2] = val;
val++;
if(s.size > 1) {
pq.add(new Segment(s.start, (s.start + s.end)/2 - 1));
pq.add(new Segment((s.start + s.end)/2 + 1, s.end));
}
else if(s.size == 1) {
pq.add(new Segment(s.end, s.end));
}
}
StringBuilder sb = new StringBuilder();
for(int j = 0; j < arr.length; j++) {
sb.append(arr[j]);
sb.append(" ");
}
System.out.println(sb.substring(0, sb.length() - 1));
}
}
}
| Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 5f041ac0a017fb248effdae441c67fee | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.io.*;
public class ConstructingTheArray{
StringTokenizer st;
BufferedReader br;
class Trsup{
Integer l, ind;
Trsup(Integer l, Integer ind){
this.l = l;
this.ind = ind;
}
}
public void construct(Integer l, Integer r, Trsup[] tr){
int m = (l+r)/2;
if(l > r) return;
tr[m] = new Trsup(l-r, m);
construct(l, m-1, tr); construct(m+1, r, tr);
}
public void run() throws Exception{
br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(nextToken());
while(t-- > 0){
int n = Integer.parseInt(nextToken());
Trsup[] tr = new Trsup[n+1];
construct(1, n, tr);
tr[0] = new Trsup(Integer.MAX_VALUE, 0);
Arrays.sort(tr, (a, b) -> a.l.compareTo(b.l));
int[] ans = new int[n+1];
for(int i = 0; i < n; i++)
ans[tr[i].ind] = i+1;
for(int i = 1; i <= n; i++)
System.out.print(ans[i] + " ");
System.out.print("\n");
}
}
String nextToken() throws Exception{
if(st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public static void main(String[] args) throws Exception{
new ConstructingTheArray().run();
}
} | Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 7cf665239fd27b3e7946e88320dfe5e2 | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.io.*;
public class ConstructingTheArray{
StringTokenizer st;
BufferedReader br;
Trsup[] tr;
class Trsup{
Integer l, ind;
Trsup(Integer l, Integer ind){
this.l = l;
this.ind = ind;
}
}
public void construct(Integer l, Integer r){
int m = (l+r)/2;
if(l > r) return;
tr[m] = new Trsup(l-r, m);
construct(l, m-1); construct(m+1, r);
}
public void run() throws Exception{
br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(nextToken());
while(t-- > 0){
int n = Integer.parseInt(nextToken());
tr = new Trsup[n+1];
construct(1, n);
tr[0] = new Trsup(Integer.MAX_VALUE, 0);
Arrays.sort(tr, (a, b) -> a.l.compareTo(b.l));
int[] ans = new int[n+1];
for(int i = 0; i < n; i++)
ans[tr[i].ind] = i+1;
for(int i = 1; i <= n; i++)
System.out.print(ans[i] + " ");
System.out.print("\n");
}
}
String nextToken() throws Exception{
if(st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public static void main(String[] args) throws Exception{
new ConstructingTheArray().run();
}
} | Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 3d7229cbc5151c3be464f828a967a1d2 | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.io.*;
public class ConstructingTheArray{
StringTokenizer st;
BufferedReader br;
class Trsup{
Integer l, ind;
Trsup(Integer l, Integer ind){
this.l = l;
this.ind = ind;
}
}
public void construct(Integer l, Integer r, Trsup[] tr){
int m = (l+r)/2;
if(l > r) return;
tr[m] = new Trsup(l-r, m);
construct(l, m-1, tr); construct(m+1, r, tr);
}
public void run() throws Exception{
br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while(t-- > 0){
int n = Integer.parseInt(br.readLine().trim());
Trsup[] tr = new Trsup[n+1];
construct(1, n, tr);
tr[0] = new Trsup(Integer.MAX_VALUE, 0);
Arrays.sort(tr, (a, b) -> a.l.compareTo(b.l));
int[] ans = new int[n+1];
for(int i = 0; i < n; i++)
ans[tr[i].ind] = i+1;
for(int i = 1; i <= n; i++)
System.out.print(ans[i] + " ");
System.out.print("\n");
}
}
public static void main(String[] args) throws Exception{
new ConstructingTheArray().run();
}
} | Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 8214ca96a50a0830f43bf3868b04e892 | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.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.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Comparator;
*/
import java.io.*;
import java.util.*;
public class D {
public static void main(String args[]) {
PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>() {
public int compare(int[] e1, int[] e2) {
if (e1[1] == e2[1]) {
return e1[0] - e2[0];
} else return e2[1] - e1[1];
}
});
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
FastReader in = new FastReader();
int t = in.nextInt();
while(t-->0) {
int n = in.nextInt();
int[] arr = new int[n];
int l = 0, r=n-1;
int d =1;
int k=0;
while(l<=r) {
if((r-l+1)%2!=0) {
k =(l+r)/2;
}else {
k = (l+r-1)/2;
}
arr[k] = d;
d++;
if(r>l){
if(k-l>0) pq.add(new int[] {l, k-l});
if(r-k>0) pq.add(new int[] {k+1, r-k});
}
if(pq.size()>0) {
int[] s = pq.remove();
l = s[0];
r= l+s[1]-1;
}else break;
}
StringBuffer s = new StringBuffer();
for(int num : arr) {
s.append(Integer.toString(num));
s.append(" ");
}
out.println(s.toString());
pq.clear();
d=0;
}
out.close();
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 5a1359451f8e48d0147f93637af712bb | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.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.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Comparator;
public class D {
public static void main(String args[]) {
PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>() {
public int compare(int[] e1, int[] e2) {
if (e1[1] == e2[1]) {
return e1[0] - e2[0];
} else return e2[1] - e1[1];
}
});
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
FastReader in = new FastReader();
int t = in.nextInt();
while(t-->0) {
int n = in.nextInt();
int[] arr = new int[n];
int l = 0, r=n-1;
int d =1;
int k=0;
while(l<=r) {
if((r-l+1)%2!=0) {
k =(l+r)/2;
}else {
k = (l+r-1)/2;
}
arr[k] = d;
d++;
if(r>l){
if(k-l>0) pq.add(new int[] {l, k-l});
if(r-k>0) pq.add(new int[] {k+1, r-k});
}
if(pq.size()>0) {
int[] s = pq.remove();
l = s[0];
r= l+s[1]-1;
}else break;
}
StringBuffer s = new StringBuffer();
for(int num : arr) {
s.append(Integer.toString(num));
s.append(" ");
}
out.println(s.toString());
pq.clear();
d=0;
}
out.close();
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 24f1d64c951934a633eeea7c3a0131ea | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.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;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Comparator;
public class D {
public static void main(String args[]) {
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
class Segment{
public int length;
public int left;
public Segment(int left, int length) {
this.left = left;
this.length = length;
}
}
Comparator<Segment> segmentComp = new Comparator<Segment>() {
@Override
public int compare(Segment s1, Segment s2) {
if(s1.length!=s2.length) return s2.length-s1.length;
else {
return s1.left-s2.left;
}
}
};
PriorityQueue<Segment> pq = new PriorityQueue<Segment>(segmentComp);
FastReader in = new FastReader();
int t = in.nextInt();
while(t-->0) {
int n = in.nextInt();
int[] arr = new int[n];
int l = 0, r=n-1;
int d =1;
int k=0;
while(l<=r) {
if((r-l+1)%2!=0) {
k =(l+r)/2;
}else {
k = (l+r-1)/2;
}
arr[k] = d;
d++;
if(k-l>0) pq.add(new Segment(l, k-l));
if(r-k>0) pq.add(new Segment(k+1, r-k));
if(pq.size()>0) {
Segment s = pq.remove();
l = s.left;
r= l+s.length-1;
}else break;
}
for(int num : arr) System.out.print(num+" ");
System.out.println("");
pq.clear();
d=0;
}
}
}
| Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 52a382482b25bb5aba845f2c92bdd58d | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.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.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Comparator;
public class D {
public static void main(String args[]) {
PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>() {
public int compare(int[] e1, int[] e2) {
if (e1[1] == e2[1]) {
return e1[0] - e2[0];
} else return e2[1] - e1[1];
}
});
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
FastReader in = new FastReader();
int t = in.nextInt();
while(t-->0) {
int n = in.nextInt();
int[] arr = new int[n];
int l = 0, r=n-1;
int d =1;
int k=0;
while(l<=r) {
if((r-l+1)%2!=0) {
k =(l+r)/2;
}else {
k = (l+r-1)/2;
}
arr[k] = d;
d++;
if(r>l){
if(k-l>0) pq.add(new int[] {l, k-l});
if(r-k>0) pq.add(new int[] {k+1, r-k});
}
if(pq.size()>0) {
int[] s = pq.remove();
l = s[0];
r= l+s[1]-1;
}else break;
}
for(int num : arr) out.print(num+" ");
out.println("");
pq.clear();
d=0;
}
out.close();
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | a73f758f0c0ced446ec472e42a4d7e37 | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.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;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Comparator;
public class D {
public static void main(String args[]) {
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
class Segment{
public int length;
public int left;
public Segment(int left, int length) {
this.left = left;
this.length = length;
}
}
Comparator<Segment> segmentComp = new Comparator<Segment>() {
@Override
public int compare(Segment s1, Segment s2) {
if(s1.length!=s2.length) return s2.length-s1.length;
else {
return s1.left-s2.left;
}
}
};
PriorityQueue<Segment> pq = new PriorityQueue<Segment>(segmentComp);
FastReader in = new FastReader();
int t = in.nextInt();
while(t-->0) {
int n = in.nextInt();
int[] arr = new int[n];
int l = 0, r=n-1;
int d =1;
int k=0;
while(l<=r) {
if((r-l+1)%2!=0) {
k =(l+r)/2;
}else {
k = (l+r-1)/2;
}
arr[k] = d;
d++;
pq.add(new Segment(l, k-l));
pq.add(new Segment(k+1, r-k));
Segment s = pq.remove();
l = s.left;
r= l+s.length-1;
}
for(int num : arr) System.out.print(num+" ");
System.out.println("");
pq.clear();
d=0;
}
}
}
| Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | f2ddeff0f66470b00795b1b22f5ba2e0 | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.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;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Comparator;
public class D {
public static void main(String args[]) {
PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>() {
public int compare(int[] e1, int[] e2) {
if (e1[1] == e2[1]) {
return e1[0] - e2[0];
} else return e2[1] - e1[1];
}
});
FastReader in = new FastReader();
int t = in.nextInt();
while(t-->0) {
int n = in.nextInt();
int[] arr = new int[n];
int l = 0, r=n-1;
int d =1;
int k=0;
while(l<=r) {
if((r-l+1)%2!=0) {
k =(l+r)/2;
}else {
k = (l+r-1)/2;
}
arr[k] = d;
d++;
if(r>l){
if(k-l>0) pq.add(new int[] {l, k-l});
if(r-k>0) pq.add(new int[] {k+1, r-k});
}
if(pq.size()>0) {
int[] s = pq.remove();
l = s[0];
r= l+s[1]-1;
}else break;
}
for(int num : arr) System.out.print(num+" ");
System.out.println("");
pq.clear();
d=0;
}
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 7c34ed5278bc1a6ce253552da8aefc3a | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.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;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Comparator;
public class D {
public static void main(String args[]) {
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;
}
}
PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>() {
public int compare(int[] e1, int[] e2) {
if (e1[1] == e2[1]) {
return e1[0] - e2[0];
} else return e2[1] - e1[1];
}
});
FastReader in = new FastReader();
int t = in.nextInt();
while(t-->0) {
int n = in.nextInt();
int[] arr = new int[n];
int l = 0, r=n-1;
int d =1;
int k=0;
while(l<=r) {
if((r-l+1)%2!=0) {
k =(l+r)/2;
}else {
k = (l+r-1)/2;
}
arr[k] = d;
d++;
if(k-l>0) pq.add(new int[] {l, k-l});
if(r-k>0) pq.add(new int[] {k+1, r-k});
if(pq.size()>0) {
int[] s = pq.remove();
l = s[0];
r= l+s[1]-1;
}else break;
}
for(int num : arr) System.out.print(num+" ");
System.out.println("");
pq.clear();
d=0;
}
}
}
| Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 5dbe47dd1fca560d97bef7a5c7dcb604 | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.util.*;
public class Codechef{
public static void main(String[] args){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
int testCases = Integer.parseInt(br.readLine());
for(int i=0; i<testCases; i++){
int n = Integer.parseInt(br.readLine());
PriorityQueue<Node> q = new PriorityQueue<>();
q.add(new Node(0, n-1));
int[] ans = new int[n];
int operation = 1;
while(!q.isEmpty()) {
Node curr = q.remove();
if((curr.right-curr.left+1 % 2) == 0) {
int index = (curr.left+curr.right-1)/2;
ans[index] = operation;
operation++;
if(index != curr.left) q.add(new Node(curr.left, index-1));
if(index != curr.right) q.add(new Node(index+1, curr.right));
}
else {
int index = (curr.left+curr.right)/2;
ans[index] = operation;
operation++;
if(index != curr.left) q.add(new Node(curr.left, index-1));
if(index != curr.right) q.add(new Node(index+1, curr.right));
}
}
StringBuilder sb = new StringBuilder("");
for(int j=0;j<n;j++) {
sb.append(ans[j] + " ");
}
sb.substring(0, sb.length()-1);
System.out.println(sb);
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
class Node implements Comparable<Node>{
int left;
int right;
public Node(int left, int right) {
this.left = left;
this.right = right;
}
public int compareTo(Node other) {
if(other.right - other.left < this.right - this.left) return -1;
if(other.right - other.left > this.right - this.left) return 1;
else return Integer.compare(this.left, other.left);
}
} | Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | ad632c34416f28ba47e377f4aad0aaec | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.io.*;
public class C1353D1{
static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream stream) {
try {
br = new BufferedReader(new
InputStreamReader(stream));
} catch (Exception e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() {
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
static class Pair{
int l,r;
Pair(int l,int r){
this.l=l;
this.r=r;
}
}
static class My_Comparator implements Comparator<Pair>{
public int compare(Pair p1,Pair p2){
int l1 = p1.l;
int r1 = p1.r;
int l2 = p2.l;
int r2 = p2.r;
int len1 = (r1-l1+1);
int len2 = (r2-l2+1);
if(len1>len2){
return -1;
}
else if(len1<len2){
return 1;
}
else{
if(l1<l2){
return -1;
}
else{
return 1;
}
}
}
}
public static void main(String args[])throws java.lang.Exception{
try{
FastScanner sc=new FastScanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int a[]=new int[n];
TreeSet<Pair> set = new TreeSet<Pair>(new My_Comparator());
int i=1;
set.add(new Pair(0,n-1));
// System.out.println(set.first().l+" "+set.first().r);
while(set.size()>0){
Pair pair = set.pollFirst();
// System.out.println(set);
// set.remove(pair);
// System.out.println(set);
int l = pair.l;
int r = pair.r;
// System.out.println(l+","+r);
if(l>r || l<0 || r<0 || l>n-1 || r>n-1){
}
else{
if((r-l+1)%2==1){
a[(l+r)/2] = i++;
// System.out.println("A");
if(r-l+1>=2){
// System.out.println("B");
set.add(new Pair(l,(l+r)/2-1));
set.add(new Pair((l+r)/2+1,r));
}
}
else{
a[(l+r-1)/2] = i++;
if(r-l+1>=2){
set.add(new Pair((l+r-1)/2+1,r));
set.add(new Pair(l,(l+r-1)/2-1));
}
}
}
}
for(int j=0; j<n; j++){
System.out.print(a[j]+" ");
}
System.out.println();
}
}catch(Exception e){}
}
}
| Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | ca19b7567e8f527d9d71aae294adb698 | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class p implements Comparable<p>{
int l;
int r;
public int compareTo(p o) {
int s1 = this.r - this.l + 1;
int s2 = o.r - o.l + 1;
if(s2 > s1) {
return 1;
}else if(s1 == s2) {
if(this.l > o.l) {
return 1;
}
return -1;
}
return -1;
}
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-- > 0) {
int n = s.nextInt();
int arr[] = new int[n];
PriorityQueue<p> a = new PriorityQueue<p>();
p o1 = new p();
o1.l = 0;
o1.r = n - 1;
a.add(o1);
int x = 1;
while(a.size() != 0) {
p temp = a.remove();
int si = temp.r - temp.l + 1;
if(si % 2 != 0) {
int i = (temp.l + temp.r) / 2;
arr[i] = x;
x++;
int s1 = temp.r - i;
int s2 = i - temp.l;
if(s1 > 0) {
p t1 = new p();
t1.l = i + 1;
t1.r = temp.r;
a.add(t1);
}
if(s2 > 0) {
p t1 = new p();
t1.l = temp.l;
t1.r = i - 1;
a.add(t1);
}
}else {
int i = (temp.l + temp.r) / 2;
arr[i] = x;
x++;
int s1 = temp.r - i;
int s2 = i - temp.l;
if(s1 > 0) {
p t1 = new p();
t1.l = i + 1;
t1.r = temp.r;
a.add(t1);
}
if(s2 > 0) {
p t1 = new p();
t1.l = temp.l;
t1.r = i - 1;
a.add(t1);
}
}
}
for(int i = 0 ; i < n ; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
} | Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 24c6d59af45c3379eb6ac1bd2b7cb34f | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | // package com.company;
import java.util.*;
import java.io.*;
import java.lang.*;
/*
** @author jigar_nainuji
*/
public class Main{
public static void main(String args[]){
PrintWriter out=new PrintWriter(System.out);
InputReader in=new InputReader(System.in);
TASK solver = new TASK();
int t = in.nextInt();
// int t=1;
for(int i=0;i<t;i++)
{
solver.solve(in,out,i);
}
out.close();
}
static class TASK {
static int mod = 1000000007;
void solve(InputReader in, PrintWriter out, int testNumber) {
int n =in.nextInt();
PriorityQueue<pair> pq = new PriorityQueue<>(new Comparator<pair>() {
@Override
public int compare(pair o1, pair o2) {
int x = o2.y-o2.x+1;
int y = o1.y-o1.x+1;
if(x==y)
return o1.x-o2.x;
else
return x-y;
}
});
int a[] = new int[n+1];
pq.add(new pair(1,n));
for(int i=1;i<=n;i++)
{
pair p = pq.poll();
int l = p.y-p.x+1;
if(l%2==0)
{
int m = (p.x+p.y-1)/2;
a[m]=i;
pq.add(new pair(p.x,m-1));
pq.add(new pair(m+1,p.y));
}
else
{
int m = (p.x+p.y)/2;
a[m]=i;
if(l!=1) {
pq.add(new pair(p.x, m - 1));
pq.add(new pair(m + 1, p.y));
}
}
}
for(int i=1;i<=n;i++)
System.out.print(a[i]+" ");
System.out.println();
}
}
static class pair{
int x;
int y;
pair(int x,int y)
{
this.x=x;
this.y=y;
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
} | Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 4d579f5950bcc7ccf1e1157f0856599d | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.util.*;
public class SolutionB {
public static void main(String[] args) throws Exception {
int tc = scan.nextInt();
for (int i = 0; i < tc; i++) {
solve();
}
out.close();
}
static class Range {
int start;
int end;
public Range(int start, int end) {
this.start = start;
this.end = end;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Range range = (Range) o;
return start == range.start &&
end == range.end;
}
@Override
public int hashCode() {
return Objects.hash(start, end);
}
}
private static void solve() {
int n = scan.nextInt();
int[] output = new int[n];
TreeMap<Integer, TreeSet<Range>> treeMap = new TreeMap<>();
treeMap.putIfAbsent(n, new TreeSet<>(Comparator.comparingInt(o -> o.start)));
treeMap.get(n).add(new Range(0, n - 1));
for (int i = 1; i <= n; i++) {
Map.Entry<Integer, TreeSet<Range>> entry = treeMap.lastEntry();
Range currRange = entry.getValue().first();
Range r1 = null;
Range r2 = null;
if ((currRange.end - currRange.start + 1) % 2 != 0) {
output[(currRange.end + currRange.start) / 2] = i;
int s1 = currRange.start;
int e1 = (currRange.end + currRange.start) / 2 - 1;
if (s1 <= e1) {
r1 = new Range(s1, e1);
}
int s2 = (currRange.end + currRange.start) / 2 + 1;
int e2 = currRange.end;
if (s2 <= e2) {
r2 = new Range(s2, e2);
}
} else {
output[(currRange.end + currRange.start - 1) / 2] = i;
int s1 = currRange.start;
int e1 = (currRange.end + currRange.start - 1) / 2 - 1;
if (s1 <= e1) {
r1 = new Range(s1, e1);
}
int s2 = (currRange.end + currRange.start - 1) / 2 + 1;
int e2 = currRange.end;
if (s2 <= e2) {
r2 = new Range(s2, e2);
}
}
if (r1 != null) {
treeMap.putIfAbsent(r1.end - r1.start + 1, new TreeSet<>(Comparator.comparingInt(o -> o.start)));
treeMap.get(r1.end - r1.start + 1).add(r1);
}
if (r2 != null) {
treeMap.putIfAbsent(r2.end - r2.start + 1, new TreeSet<>(Comparator.comparingInt(o -> o.start)));
treeMap.get(r2.end - r2.start + 1).add(r2);
}
entry.getValue().remove(currRange);
if (entry.getValue().size() == 0) {
treeMap.remove(entry.getKey());
}
}
for (int a : output) System.out.print(a + " ");
System.out.println();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static MyScanner scan = new MyScanner();
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//--------------------------------------------------------
} | Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 845bbdb410d105fbc64fb0bf41f44e30 | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.util.*;
public class sol {
InputReader in;
PrintWriter out;
helper_class h;
final long mod=1000000007;
public static void main(String[] args) throws java.lang.Exception{
new sol().run();
}
void run() throws Exception{
in=new InputReader(System.in);
out = new PrintWriter(System.out);
h = new helper_class();
int t=h.ni();
while(t-->0){
solve();
}
out.flush();
out.close();
}
int[] a;
void solve(){
int n=h.ni();
int i;
a=new int[n+1];
PriorityQueue<Pair> pq = new PriorityQueue<Pair>(n, Collections.reverseOrder());
pq.add(new Pair(1,n));
for(i=1;i<=n;i++) {
Pair p=pq.poll();
int k=(p.x+p.y)/2;
a[k]=i;
if (p.x < k)
pq.add(new Pair(p.x,k-1));
if (k < p.y)
pq.add(new Pair(k+1,p.y));
}
for(i=1;i<=n;i++)
out.print(a[i]+" ");
out.println();
}
class Pair implements Comparable<Pair> {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair a) {
if((this.y - this.x) == (a.y - a.x))
return Integer.compare(a.x, this.x);
else
return Integer.compare((this.y - this.x),(a.y - a.x));
}
}
class helper_class{
long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);}
void p(Object o){out.print(o);}
void pn(Object o){out.println(o);}
String n(){return in.next();}
int ni(){return in.nextInt();}
long nl(){return in.nextLong();}
long power(long a, long b){
if(b == 0)
return 1L;
long val = power(a, b / 2);
if(b % 2 == 0) return val * val%mod;
else return val * val%mod * a%mod;
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new UnknownError();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new UnknownError();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int peek() {
if (numChars == -1)
return -1;
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0)
return -1;
}
return buf[curChar];
}
public void skip(int x) {
while (x-->0)
read();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public String nextString() {
return next();
}
public String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
StringBuffer buf = new StringBuffer();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r')
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean hasNext() {
int value;
while (isSpaceChar(value = peek()) && value != -1)
read();
return value != -1;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
} | Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 493037ffda41bacc5871abb1cb5b8cd5 | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.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.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.StringTokenizer;
public class Solution{
public static void main(String[] args) throws Exception{
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int tt = fs.nextInt();
while(tt-->0) {
int n = fs.nextInt();
int[] ans = new int[n+1];
PriorityQueue<Segment> priorityQueue = new PriorityQueue<Segment>();
priorityQueue.add(new Segment((1+n)/2, n));
int i = 1;
while(!priorityQueue.isEmpty()) {
Segment s = priorityQueue.poll();
int c = s.center;
int l = s.length;
ans[c] = i++;
if(l>1) {
int l1 = l/2;
int l2 = (l-1)/2;
if(l1>0) {
priorityQueue.add(new Segment((2*c+l1+1)/2,l1));
}
if(l2>0) {
priorityQueue.add(new Segment((2*c-l2-1)/2, l2));
}
}
}
for(int j=1;j<=n;j++) out.print(ans[j]+" ");
out.println("");
}
out.close();
}
static class Segment implements Comparable<Segment>{
int center;
int length;
Segment(int center, int length){
this.center = center;
this.length = length;
}
public int compareTo(Segment s) {
if(this.length == s.length) return Integer.compare(this.center, s.center);
return Integer.compare(s.length, this.length);
}
}
static final Random random=new Random();
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static class FastScanner{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public String next(){
while(!st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
} catch(IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt(){
return Integer.parseInt(next());
}
public int[] readArray(int n){
int[] a = new int[n];
for(int i=0;i<n;i++)
a[i] = nextInt();
return a;
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | e75ad9fef5905c86d6765faf34c31126 | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
import java.awt.geom.*;
import static java.lang.Math.*;
public class Solution implements Runnable {
long mod1 = (long) 1e9 + 7;
int mod2 = 998244353;
public void solve() throws Exception {
int t = sc.nextInt();
while (t-- > 0) {
int n=sc.nextInt();
int result[]=new int[n];
int start=1;
int l=1;
int r=n;
Pair p=new Pair(l,r);
PriorityQueue<Pair> pq=new PriorityQueue<>((a,b)->((b.y-b.x)-(a.y-a.x)!=0)?(b.y-b.x)-(a.y-a.x):(a.x-b.x));
pq.add(p);
while(!pq.isEmpty())
{
Pair temp=pq.poll();
int mid=(temp.x+temp.y)/2;
result[mid-1]=start;
start++;
if(temp.x<=mid-1)
{
pq.add(new Pair(temp.x,mid-1));
}
if(temp.y>=mid+1)
{
pq.add(new Pair(mid+1,temp.y));
}
}
for(int i:result)
{
out.print(i+" ");
}
out.println();
}
}
class Pair{
int x;
int y;
Pair(int x,int y)
{
this.x=x;
this.y=y;
}
}
static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static long ncr(int n, int r, long p) {
if (r > n)
return 0l;
if (r > n - r)
r = n - r;
long C[] = new long[r + 1];
C[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = Math.min(i, r); j > 0; j--)
C[j] = (C[j] + C[j - 1]) % p;
}
return C[r] % p;
}
void sieveOfEratosthenes(boolean prime[], int size) {
for (int i = 0; i < size; i++)
prime[i] = true;
for (int p = 2; p * p < size; p++) {
if (prime[p] == true) {
for (int i = p * p; i < size; i += p)
prime[i] = false;
}
}
}
public long power(long x, long y, long p) {
long res = 1;
// out.println(x+" "+y);
x = x % p;
if (x == 0)
return 0;
while (y > 0) {
if ((y & 1) == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static Throwable uncaught;
BufferedReader in;
FastScanner sc;
PrintWriter out;
@Override
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
sc = new FastScanner(in);
solve();
} catch (Throwable uncaught) {
Solution.uncaught = uncaught;
} finally {
out.close();
}
}
public static void main(String[] args) throws Throwable {
Thread thread = new Thread(null, new Solution(), "", (1 << 26));
thread.start();
thread.join();
if (Solution.uncaught != null) {
throw Solution.uncaught;
}
}
}
class FastScanner {
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() throws Exception {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
public int[] readArray(int n) throws Exception {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
public double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
} | Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | fa95be68e7011fc9838ebdbdffb2505a | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.PriorityQueue;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.AbstractCollection;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author unknown
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
DConstructingTheArray solver = new DConstructingTheArray();
solver.solve(1, in, out);
out.close();
}
static class DConstructingTheArray {
PriorityQueue<_C.Pair<Integer, Integer>> pq = new PriorityQueue<>(200005, (o1, o2) -> {
int dist = (o2.s - o2.f) - (o1.s - o1.f);
if (dist != 0) {
return dist;
}
return (Integer) o1.f - (Integer) o2.f;
});
public void solve(int testNumber, InputReader in, OutputWriter out) {
int ntc = in.nextInt();
while ((ntc--) > 0) {
int n = in.nextInt();
if (n == 1) {
out.println(1);
continue;
}
int[] arr = new int[n];
partition(0, n - 1);
int x = 1;
while (!pq.isEmpty()) {
_C.Pair<Integer, Integer> p = pq.poll();
arr[p.f + (p.s - p.f) / 2] = x;
x++;
}
StringBuilder ans = new StringBuilder();
for (int i = 0; i < n; i++) {
if (arr[i] == 0) {
ans.append(x++).append(" ");
} else {
ans.append(arr[i]).append(" ");
}
}
pq.clear();
out.println(ans);
}
}
public void partition(int l, int r) {
if (l >= r) {
return;
}
pq.add(new _C.Pair<Integer, Integer>(l, r));
int mid = l + (r - l) / 2;
partition(l, mid - 1);
partition(mid + 1, r);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; ++i) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void println(long i) {
writer.println(i);
}
}
static class _C {
static public class Pair<F extends Comparable<F>, S extends Comparable<S>> implements Comparable<_C.Pair<F, S>> {
public F f;
public S s;
public Pair(F f, S s) {
this.f = f;
this.s = s;
}
public int compareTo(_C.Pair<F, S> o) {
int t = f.compareTo(o.f);
if (t == 0) return s.compareTo(o.s);
return t;
}
public int hashCode() {
return (31 + f.hashCode()) * 31 + s.hashCode();
}
public boolean equals(Object o) {
if (!(o instanceof _C.Pair)) return false;
if (o == this) return true;
_C.Pair p = (_C.Pair) o;
return f.equals(p.f) && s.equals(p.s);
}
public String toString() {
return "{" + f + ", " + s + "}";
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 77b2dc9ea496ba9d805831683c34d8a1 | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.PriorityQueue;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.AbstractCollection;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author unknown
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
DConstructingTheArray solver = new DConstructingTheArray();
solver.solve(1, in, out);
out.close();
}
static class DConstructingTheArray {
PriorityQueue<_C.Pair<Integer, Integer>> pq = new PriorityQueue<>(200005, (o1, o2) -> {
int dist = (o2.s - o2.f) - (o1.s - o1.f);
if (dist != 0) {
return dist;
}
return (Integer) o1.f - (Integer) o2.f;
});
public void solve(int testNumber, InputReader in, OutputWriter out) {
int ntc = in.nextInt();
while ((ntc--) > 0) {
int n = in.nextInt();
if (n == 1) {
out.println(1);
continue;
}
int[] arr = new int[n];
partition(0, n - 1);
int x = 1;
while (!pq.isEmpty()) {
_C.Pair<Integer, Integer> p = pq.poll();
arr[p.f + (p.s - p.f) / 2] = x;
x++;
}
for (int i = 0; i < n; i++) {
if (arr[i] == 0) {
arr[i] = x;
x++;
}
}
out.println(arr);
}
}
public void partition(int l, int r) {
if (l >= r) {
return;
}
pq.add(new _C.Pair<Integer, Integer>(l, r));
int mid = l + (r - l) / 2;
partition(l, mid - 1);
partition(mid + 1, r);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(int[] array) {
for (int i = 0; i < array.length; ++i) {
if (i != 0) {
writer.print(' ');
}
writer.print(array[i]);
}
}
public void println(int[] array) {
print(array);
writer.println();
}
public void close() {
writer.close();
}
public void println(long i) {
writer.println(i);
}
}
static class _C {
static public class Pair<F extends Comparable<F>, S extends Comparable<S>> implements Comparable<_C.Pair<F, S>> {
public F f;
public S s;
public Pair(F f, S s) {
this.f = f;
this.s = s;
}
public int compareTo(_C.Pair<F, S> o) {
int t = f.compareTo(o.f);
if (t == 0) return s.compareTo(o.s);
return t;
}
public int hashCode() {
return (31 + f.hashCode()) * 31 + s.hashCode();
}
public boolean equals(Object o) {
if (!(o instanceof _C.Pair)) return false;
if (o == this) return true;
_C.Pair p = (_C.Pair) o;
return f.equals(p.f) && s.equals(p.s);
}
public String toString() {
return "{" + f + ", " + s + "}";
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output | |
PASSED | 25b390f8f94a51b48607a997979066dd | train_002.jsonl | 1589466900 | You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.You have to answer $$$t$$$ independent test cases. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
public class HelloWorld {
static FastReader f = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
public static void solve() {
int n = f.nextInt();
PriorityQueue<Pair> pq = new PriorityQueue<>(new cmp());
int ans[] = new int[n + 1];
pq.add(new Pair(1, n));
int cnt = 1;
while (!pq.isEmpty()) {
Pair curr = pq.poll();
int r = curr.r;
int l = curr.l;
int mid;
if (curr.diff % 2 == 0) {
mid = (l + r - 1) / 2;
} else {
mid = (l + r) / 2;
}
ans[mid] = cnt++;
if (mid > l) {
pq.add(new Pair(l, mid - 1));
}
if (mid < r) {
pq.add(new Pair(mid + 1, r));
}
}
for (int i = 1; i <= n; i++) {
out.print(ans[i] + " ");
}
out.println();
}
public static void main(String[] args) {
int t = 1;
t = f.nextInt();
while (t-- > 0) {
solve();
}
out.close();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
class Pair {
int l, r, diff;
public Pair(int a, int b) {
l = a;
r = b;
diff = r - l + 1;
}
}
class cmp implements Comparator<Pair> {
public int compare(Pair a, Pair b) {
if (a.diff == b.diff) {
return a.l - b.l;
}
return -(a.diff - b.diff);
}
} | Java | ["6\n1\n2\n3\n4\n5\n6"] | 1 second | ["1 \n1 2 \n2 1 3 \n3 1 2 4 \n2 4 1 3 5 \n3 4 1 5 2 6"] | null | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"sortings"
] | 25fbe21a449c1ab3eb4bdd95a171d093 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$). | 1,600 | For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.