結果

問題 No.2771 Personal Space
ユーザー tenten
提出日時 2024-06-04 14:49:01
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,100 ms / 2,000 ms
コード長 3,074 bytes
コンパイル時間 3,024 ms
コンパイル使用メモリ 85,212 KB
実行使用メモリ 96,668 KB
最終ジャッジ日時 2024-12-23 10:53:28
合計ジャッジ時間 26,805 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int t = sc.nextInt();
String result = IntStream.range(0, t).mapToObj(i -> {
int n = sc.nextInt();
int m = sc.nextInt();
PriorityQueue<Space> queue = new PriorityQueue<>();
int[] ans = new int[n];
ans[m - 1] = 1;
if (m > 1) {
queue.add(new Space(1, m - 1));
}
if (m < n) {
queue.add(new Space(n, n - m));
}
TreeSet<Integer> exists = new TreeSet<>();
exists.add(m);
int current = 2;
while (queue.size() > 0) {
Space x = queue.poll();
ans[x.idx - 1] = current++;
if (x.idx > 1) {
int left = exists.lower(x.idx);
if (left + 1 < x.idx) {
queue.add(new Space((left + x.idx) / 2, (left + x.idx) / 2 - left));
}
}
if (x.idx < n) {
int right = exists.higher(x.idx);
if (right - 1 > x.idx) {
queue.add(new Space((right + x.idx) / 2, (right + x.idx) / 2 - x.idx));
}
}
exists.add(x.idx);
}
return Arrays.stream(ans).mapToObj(String::valueOf).collect(Collectors.joining(" "));
}).collect(Collectors.joining("\n"));
System.out.println(result);
}
static class Space implements Comparable<Space> {
int idx;
int value;
public Space(int idx, int value) {
this.idx = idx;
this.value = value;
}
public int compareTo(Space another) {
if (value == another.value) {
return idx - another.idx;
} else {
return another.value - value;
}
}
public String toString() {
return idx + ":" + value;
}
}
}
class Scanner {
BufferedReader br;
StringTokenizer st = new StringTokenizer("");
StringBuilder sb = new StringBuilder();
public Scanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
} catch (Exception e) {
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String next() {
try {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
return st.nextToken();
}
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0